A robust Clarity smart contract designed for managing institutional asset custody with multi-signature approval workflows, role-based access control, and comprehensive asset tracking.
This smart contract provides institutional-grade custody services on the Stacks blockchain, enabling secure management of digital assets with approval mechanisms, manager hierarchies, and detailed audit trails.
- Custody Account Management: Create and manage institutional custody accounts with three tiers (standard, premium, enterprise)
- Asset Deposit/Withdrawal: Secure deposit and withdrawal mechanisms with balance validation
- Multi-Approval Workflow: Pending withdrawal system requiring administrative approval
- Manager Role Management: Support for up to 5 approved managers per institution
- Asset Ledger Tracking: Complete transaction history with timestamps
- Owner Authorization: Contract owner controls critical operations
- Real-time Balance Tracking: Total custodied assets monitoring
``` CONTRACT-OWNER: Initial deployer of the contract ERR-UNAUTHORIZED (u401): Access denied ERR-INVALID-AMOUNT (u400): Amount must be > 0 ERR-INSUFFICIENT-BALANCE (u402): Insufficient funds ERR-ACCOUNT-EXISTS (u403): Account already initialized ERR-ACCOUNT-NOT-FOUND (u404): Institution account not found ERR-INVALID-CUSTODY-TYPE (u405): Invalid custody tier ```
total-custodied-assets: Sum of all assets under custodyactive-institutions: Count of active custody accountswithdrawal-counter: Incremental withdrawal ID tracker
custody-accounts ``` Key: { institution: principal } Value: { balance: uint, approved-managers: list of up to 5 principals, created-at: block height, custody-type: "standard" | "premium" | "enterprise", is-active: bool } ```
asset-ledger ``` Key: { institution: principal, asset-id: uint } Value: { amount: uint, timestamp: uint } ```
pending-withdrawals ``` Key: { id: uint } Value: { institution: principal, amount: uint, requester: principal, created-at: block height, approved-count: uint, is-executed: bool } ```
Initializes a new custody account for an institution.
Parameters:
institution: Principal address of the institutioncustody-type: Type of custody ("standard", "premium", or "enterprise")
Authorization: Contract owner only
Returns: ok true or error code
Deposits STX or assets into an institutional custody account.
Parameters:
institution: Target institution principalamount: Amount to deposit (must be > 0)
Authorization: Public (any caller)
Returns: ok true or error code
Requests withdrawal of assets from custody with multi-approval workflow.
Parameters:
institution: Institution to withdraw fromamount: Amount to withdraw (must be > 0 and ≤ balance)
Authorization: Institution or contract owner
Returns: ok withdrawal-id or error code
Approves a pending withdrawal request.
Parameters:
withdrawal-id: ID of the withdrawal to approve
Authorization: Contract owner only
Returns: ok true or error code
Adds an approved manager to an institution's custody account.
Parameters:
institution: Target institutionmanager: Principal of new manager
Authorization: Institution or contract owner
Returns: ok true or error code
Retrieves complete custody account details.
Returns: Optional custody account data structure
Retrieves pending withdrawal status.
Returns: Optional withdrawal data structure
Retrieves total assets under custody across all institutions.
Returns: uint (total balance)
Retrieves number of active institutional accounts.
Returns: uint (count of institutions)
;; Deploy contract
;; ...
;; Initialize custody for institution
(contract-call? .institutional-custody open-custody-account 'SP2X1Y8RFEJB6KPV0DY2CX3M5N4K6J9L1A2B3C4D5 "premium")
;; Deposit assets
(contract-call? .institutional-custody deposit-assets 'SP2X1Y8RFEJB6KPV0DY2CX3M5N4K6J9L1A2B3C4D5 u1000000)
;; Request withdrawal
(contract-call? .institutional-custody request-withdrawal 'SP2X1Y8RFEJB6KPV0DY2CX3M5N4K6J9L1A2B3C4D5 u500000)
;; Approve withdrawal
(contract-call? .institutional-custody approve-withdrawal u0)
;; Add manager
(contract-call? .institutional-custody add-manager 'SP2X1Y8RFEJB6KPV0DY2CX3M5N4K6J9L1A2B3C4D5 'SP3A1B2C3D4E5F6G7H8I9J0K1L2M3N4O5P6Q7R8S9)
;; Query balance
(contract-call? .institutional-custody get-custody-account 'SP2X1Y8RFEJB6KPV0DY2CX3M5N4K6J9L1A2B3C4D5)