Skip to main content
This page covers how to deploy, configure, and integrate with the StockCustody contract. It assumes you have deployed the Stock Token suite and have a functioning identity registry and at least one StockToken instance.

Prerequisites

Before deploying StockCustody, the following must be in place:
  1. IdentityRegistryUpgradeable deployed and configured with KYC providers.
  2. At least one StockToken deployed with the identity registry address.
  3. USDC (or chosen settlement stablecoin) available on the target chain.
  4. A LiquidationRouter deployed, or a placeholder address if liquidation is not yet needed.

Compiler Configuration

The batchSettleTrades function takes 9 calldata array parameters plus batchId, which exceeds the EVM’s 16-slot stack limit in the legacy compilation pipeline. The Solidity compiler must use viaIR: true.
Without this setting, compilation will fail with a stack-too-deep error.

Deployment Sequence

Deposit and Withdrawal

Users must deposit tokens into custody before they can place orders on the orderbook.
For compliance-checked tokens, pre-check transferability before submitting the deposit transaction:
Withdrawals work similarly. The user can only withdraw their available (unlocked) balance:

Trade Settlement Flow

The matching engine (operator) follows a lock-then-settle pattern.
For high-throughput scenarios, use batchSettleTrades to settle multiple trades in one transaction. Size batches based on your target chain’s block gas limit. The gas guard stops processing if remaining gas drops below 80,000, so always check the BatchSettled event’s settledCount against totalSubmitted and retry any remaining settlements.

Liquidation Flows

Two paths exist for settling liquidated collateral received from a lending market.

Path A: Internal Buyer

The buyer already has USDC in custody. Tokens move between internal balances and USDC is transferred on-chain to the router.
Partial fills are supported. The liquidation remains open until the full token amount is matched. Check getLiquidation(liquidationId) to see the remaining amount.

Path B: External Sale

The operator sells tokens through an external venue, then provides USDC proceeds.
Both paths notify the lending market via ILendingMarket.receiveLiquidationProceeds. If the callback reverts, settlement still completes. Monitor LiquidationSettled and LiquidationSoldToBuyer events for reconciliation.

Priority Trade Flow

For liquidating tokens already held in a user’s custody balance (not received via the router):
On L1 Ethereum, front-running between these steps is possible. Deploy on an L2 or use a private mempool for liquidation transactions.

Stock Split Reconciliation

After a StockToken.stockSplit() executes, internal custody balances must be updated to match the new token supply.
Monitor the StockSplit event on StockToken to trigger reconciliation. The user list must be complete - any omitted user will have stale balances.

Listening to Events

Index these events for your backend, matching engine, and reporting systems:

API Integration

If you are using Trusset’s API layer, custody balances and settlements are managed through the Stock Trading and Stock Trading Settlements API groups. The API handles deposit orchestration, settlement submission, and balance queries without direct contract interaction. For direct contract integration (self-hosted or custom deployment), use standard ethers.js or viem patterns with the contract ABIs included in the license package.