Mediums of Exchange Zome (mediums_of_exchange_integrity, mediums_of_exchange_coordinator)
1. Overview
- Purpose: Manages the lifecycle of
MediumOfExchangeentries, which define various payment methods and value exchange mechanisms within the requests and offers ecosystem. Supports both traditional currencies (USD, EUR) and alternative exchange systems (Pay It Forward, Local Exchange Trading Systems, Time Banking, etc.). - Status: Complete implementation with approval workflow, entity linking, and cross-zome integration operational
- Approval Workflow: Implements a moderation process for
MediumOfExchangeentries, involving user suggestions, admin review, and approval/rejection states (pending,approved,rejected). Onlyapprovedmediums can be actively used in requests and offers. - Exchange Type Classification: Supports two types of mediums - "base" categories (foundational exchange systems) and "currency" types (specific monetary units)
- Cross-Zome Integration: Full integration with
requests_coordinatorandoffers_coordinatorfor bidirectional linking - Zome Structure:
mediums_of_exchange_integrity: Handles data validation, entry definitions, and validation rules.mediums_of_exchange_coordinator: Provides externally callable functions for business logic, interacting with the integrity zome and managing data flows.
2. Integrity Zome (mediums_of_exchange_integrity)
2.1. Entry Types
MediumOfExchange
The MediumOfExchange entry defines a specific payment method or value exchange mechanism.
#![allow(unused)] fn main() { #[hdk_entry_helper] #[derive(Clone, PartialEq)] pub struct MediumOfExchange { /// Unique identifier (e.g., 'EUR', 'USD', 'TIME', 'LOCAL') pub code: String, /// Human-readable name (e.g., 'Euro', 'US Dollar', 'Time Banking', 'Local Currency') pub name: String, /// Detailed description of the medium of exchange pub description: Option<String>, /// Exchange type: "base" (foundational categories) or "currency" (specific monetary units) pub exchange_type: String, /// ID of corresponding hREA ResourceSpecification (only for approved) pub resource_spec_hrea_id: Option<String>, } }
2.2. Validation Rules
MediumOfExchangeEntry:code: Must not be empty. Serves as unique identifier.name: Must not be empty. Human-readable display name.description: Optional field for detailed explanation.exchange_type: Must be either "base" or "currency". Validated strictly.resource_spec_hrea_id: Optional initially (for suggested state), set during approval.
- Updates/Deletes: Only original author can update/delete entries (author validation enforced).
2.3. Link Types
-
AllMediumsOfExchange- Base:
Pathanchor (e.g.,mediums_of_exchange). - Target:
ActionHashof aMediumOfExchangeentry. - Purpose: Links all medium of exchange entries for global access.
- Base:
-
MediumOfExchangeUpdates- Base:
ActionHashof originalMediumOfExchangeentry. - Target:
ActionHashof updatedMediumOfExchangeentry. - Purpose: Links original entries to their updates for version tracking.
- Base:
-
MediumOfExchangeToRequest- Base:
ActionHashof aMediumOfExchangeentry. - Target:
ActionHashof aRequestentry. - Purpose: Links approved mediums to requests that accept them.
- Base:
-
RequestToMediumOfExchange- Base:
ActionHashof aRequestentry. - Target:
ActionHashof aMediumOfExchangeentry. - Purpose: Bidirectional link from requests to their accepted mediums.
- Base:
-
MediumOfExchangeToOffer- Base:
ActionHashof aMediumOfExchangeentry. - Target:
ActionHashof anOfferentry. - Purpose: Links approved mediums to offers that provide them.
- Base:
-
OfferToMediumOfExchange- Base:
ActionHashof anOfferentry. - Target:
ActionHashof aMediumOfExchangeentry. - Purpose: Bidirectional link from offers to their provided mediums.
- Base:
3. Coordinator Zome (mediums_of_exchange_coordinator)
3.1. Path Anchors Used
- Global Anchor (Static Path):
mediums_of_exchange: For linking all medium of exchange entries.
- Status Anchors (Static Paths):
mediums_of_exchange.status.pending: ForMediumOfExchangeentries awaiting admin review.mediums_of_exchange.status.approved: ForMediumOfExchangeentries approved by an admin and usable in requests/offers.mediums_of_exchange.status.rejected: ForMediumOfExchangeentries rejected by an admin.
3.2. Key Functions
Input/Output structs are defined in Rust for type safety and clarity.
User-Facing Functions
suggest_medium_of_exchange(input: MediumOfExchangeInput) -> ExternResult<Record>- Description: Allows accepted users to suggest new
MediumOfExchangeentries. Non-admin users can only suggest "currency" types. - Actions:
- Validates user is accepted or administrator.
- Ensures non-admin users can only suggest "currency" exchange types.
- Creates a new
MediumOfExchangeentry withresource_spec_hrea_idset to None. - Links the entry to the global
mediums_of_exchangeanchor. - Links the entry to the
mediums_of_exchange.status.pendinganchor.
- Access Control: Accepted users and administrators.
- Returns: The
Recordof the newly createdMediumOfExchangeentry.
- Description: Allows accepted users to suggest new
Administrative Functions
-
create_medium_of_exchange(input: MediumOfExchangeInput) -> ExternResult<Record>- Description: Allows administrators to directly create and approve
MediumOfExchangeentries of any type. - Actions:
- Creates a new
MediumOfExchangeentry. - Links the entry to the global
mediums_of_exchangeanchor. - Links the entry directly to the
mediums_of_exchange.status.approvedanchor.
- Creates a new
- Access Control: Admin only.
- Returns: The
Recordof the newly created and approvedMediumOfExchangeentry.
- Description: Allows administrators to directly create and approve
-
approve_medium_of_exchange(medium_of_exchange_hash: ActionHash) -> ExternResult<()>- Description: Allows an administrator to approve a
MediumOfExchangethat is currentlypending. - Actions:
- Validates that
medium_of_exchange_hashpoints to an existingMediumOfExchangeentry. - Creates placeholder hREA ResourceSpecification ID (format:
hrea_resource_spec_{code}). - Updates the entry with the hREA ResourceSpecification ID.
- Removes from all status paths and links to
mediums_of_exchange.status.approved.
- Validates that
- Access Control: Admin only.
- Returns: Success confirmation.
- Description: Allows an administrator to approve a
-
reject_medium_of_exchange(medium_of_exchange_hash: ActionHash) -> ExternResult<()>- Description: Allows an administrator to reject a
MediumOfExchange. - Actions:
- Validates
medium_of_exchange_hash. - Removes from all status paths.
- Links to
mediums_of_exchange.status.rejected.
- Validates
- Access Control: Admin only.
- Returns: Success confirmation.
- Description: Allows an administrator to reject a
-
update_medium_of_exchange(input: UpdateMediumOfExchangeInput) -> ExternResult<Record>- Description: Allows an administrator to update an existing
MediumOfExchange. - Actions:
- Validates admin permissions and entry existence.
- Creates update entry.
- Creates update link for version tracking.
- Access Control: Admin only.
- Returns: The
Recordof the updatedMediumOfExchangeentry.
- Description: Allows an administrator to update an existing
-
delete_medium_of_exchange(medium_of_exchange_hash: ActionHash) -> ExternResult<()>- Description: Allows an administrator to delete a
MediumOfExchangeentry. - Actions:
- Validates admin permissions and entry existence.
- Removes from all status paths.
- Deletes the entry.
- Access Control: Admin only.
- Returns: Success confirmation.
- Description: Allows an administrator to delete a
Getter Functions
-
get_medium_of_exchange(medium_of_exchange_hash: ActionHash) -> ExternResult<Option<Record>>- Description: Retrieves a specific
MediumOfExchangeentry by itsActionHash. - Access Control: Public.
- Returns:
Some(Record)if found,Noneotherwise.
- Description: Retrieves a specific
-
get_latest_medium_of_exchange_record(original_action_hash: ActionHash) -> ExternResult<Option<Record>>- Description: Gets the latest version of a medium of exchange given the original action hash.
- Access Control: Public.
- Returns: Latest version
Recordor original if no updates.
-
get_all_mediums_of_exchange() -> ExternResult<Vec<Record>>- Description: Retrieves all
MediumOfExchangeentries regardless of status. - Access Control: Public.
- Returns: A vector of
Records.
- Description: Retrieves all
-
get_pending_mediums_of_exchange() -> ExternResult<Vec<Record>>- Description: Retrieves all
MediumOfExchangeentries currently linked to themediums_of_exchange.status.pendinganchor. - Access Control: Admin only.
- Returns: A vector of
Records.
- Description: Retrieves all
-
get_approved_mediums_of_exchange() -> ExternResult<Vec<Record>>- Description: Retrieves all
MediumOfExchangeentries currently linked to themediums_of_exchange.status.approvedanchor. - Access Control: Public (essential for UI selectors).
- Returns: A vector of
Records.
- Description: Retrieves all
-
get_rejected_mediums_of_exchange() -> ExternResult<Vec<Record>>- Description: Retrieves all
MediumOfExchangeentries currently linked to themediums_of_exchange.status.rejectedanchor. - Access Control: Admin only.
- Returns: A vector of
Records.
- Description: Retrieves all
Entity Linking Functions
-
link_to_medium_of_exchange(input: MediumOfExchangeLinkInput) -> ExternResult<()>- Description: Creates bidirectional links between an approved medium of exchange and a request or offer.
- Validation: Verifies the medium of exchange is approved before linking.
- Access Control: Public (for linking approved mediums).
- Returns: Success confirmation.
-
unlink_from_medium_of_exchange(input: MediumOfExchangeLinkInput) -> ExternResult<()>- Description: Removes bidirectional links between a medium of exchange and a request or offer.
- Access Control: Public.
- Returns: Success confirmation.
-
update_medium_of_exchange_links(input: UpdateMediumOfExchangeLinksInput) -> ExternResult<()>- Description: Updates all medium of exchange links for a request or offer, efficiently adding new links and removing outdated ones.
- Access Control: Public.
- Returns: Success confirmation.
-
get_mediums_of_exchange_for_entity(input: GetMediumOfExchangeForEntityInput) -> ExternResult<Vec<ActionHash>>- Description: Gets all medium of exchange hashes linked to a request or offer.
- Access Control: Public.
- Returns: Vector of
ActionHashes.
-
delete_all_medium_of_exchange_links_for_entity(input: GetMediumOfExchangeForEntityInput) -> ExternResult<()>- Description: Deletes all medium of exchange links for a request or offer (used when deleting the entity).
- Access Control: Public.
- Returns: Success confirmation.
Cross-Entity Discovery Functions
-
get_requests_for_medium_of_exchange(medium_of_exchange_hash: ActionHash) -> ExternResult<Vec<Record>>- Description: Retrieves all requests linked to a specific medium of exchange.
- Access Control: Public.
- Returns: A vector of Request
Records.
-
get_offers_for_medium_of_exchange(medium_of_exchange_hash: ActionHash) -> ExternResult<Vec<Record>>- Description: Retrieves all offers linked to a specific medium of exchange.
- Access Control: Public.
- Returns: A vector of Offer
Records.
Utility Functions
is_medium_of_exchange_approved(medium_of_exchange_hash: ActionHash) -> ExternResult<bool>- Description: Checks if a medium of exchange is approved (for internal use and validation).
- Access Control: Public.
- Returns: Boolean indicating approval status.
3.3. Cross-Zome Interactions
-
Called by
requests_coordinatorandoffers_coordinator:- To validate that
MediumOfExchangeActionHashes provided duringRequest/Offercreation/update correspond to approvedMediumOfExchanges. - Entity linking operations to associate requests/offers with their accepted/provided mediums.
- To validate that
-
Integration with
administrationzome:- Uses admin validation functions to check permissions for approval/rejection operations.
- Leverages user acceptance status for suggestion permissions.
4. Testing Status
Backend Tests (Sweettest)
- ✅ Medium of exchange CRUD operations and status management
- ✅ Exchange type validation ("base" vs "currency")
- ✅ Approval workflow (suggest, approve, reject)
- ✅ Entity linking with requests and offers
- ✅ Bidirectional link management and cleanup
- ✅ Admin permission validation and access control
- ✅ Cross-entity discovery functionality
- ✅ Update and version tracking
Frontend Integration
- ✅ Effect-TS Service Layer: Complete with all zome functions exposed
- ✅ Reactive Svelte Store: Full state management with caching and event bus
- ✅ UI Components: Enhanced form and selector components with categorization
- ✅ Cross-Store Integration: Requests and offers stores enhanced with medium linking
- ✅ Test Coverage: All unit tests passing (part of 343 total test suite across 20 files)
5. Implementation Notes
Performance Considerations
- Path anchor indexing provides efficient status-based queries
- Bidirectional linking enables fast cross-entity discovery
- Caching implemented at service and store levels
- Status transition operations optimized to remove from all paths before setting new status
Data Integrity
- Only approved medium of exchange entries can be linked to requests/offers
- Exchange type validation ensures consistency ("base" vs "currency")
- Author validation prevents unauthorized updates/deletes
- Cross-zome validation ensures link consistency
User Experience
- Exchange Type Classification: Clear distinction between foundational categories and specific currencies
- Suggestion Workflow: Users can contribute new mediums for community approval
- Permission-Based Access: Different capabilities for users vs administrators
- Visual Categorization: Enhanced UI with base categories (📂) and currencies (💰)
hREA Integration
MediumOfExchangeentries map to hREA ResourceSpecification (notEconomicResource). A ResourceSpecification defines the type of value exchange — it is a template referenced by the reciprocal Intent inside a Proposal. Concrete resource instances (EconomicResource) are never created from Mediums of Exchange directly; they arise only from Economic Events in a completed exchange lifecycle.- The
resource_spec_hrea_idfield stores the ID of the corresponding hREA ResourceSpecification, set during approval by the hREA store.
Exchange Type System
- "base": Foundational exchange categories (Pay It Forward, LETS, Time Banking)
- "currency": Specific monetary units (USD, EUR, Bitcoin, Local Currency)
- User Restrictions: Non-admin users can only suggest "currency" types
- Admin Capabilities: Full access to create both "base" and "currency" types
6. Security Considerations
Access Control Matrix
| Operation | User | Accepted User | Administrator |
|---|---|---|---|
| Suggest Currency | ❌ | ✅ | ✅ |
| Suggest Base Type | ❌ | ❌ | ✅ |
| Create Direct | ❌ | ❌ | ✅ |
| View Approved | ✅ | ✅ | ✅ |
| View Pending | ❌ | ❌ | ✅ |
| Approve/Reject | ❌ | ❌ | ✅ |
| Update/Delete | ❌ | ❌ | ✅ |
| Link to Requests/Offers | ❌ | ✅ | ✅ |
Validation Layers
- Entry Validation: Field presence and format validation
- Permission Validation: User role and acceptance status checks
- Business Logic Validation: Exchange type restrictions and approval status verification
- Link Validation: Only approved mediums can be linked to entities
This comprehensive documentation provides complete technical coverage of the Mediums of Exchange zome, from backend Rust implementation through validation rules and cross-zome integration patterns.