Prerequisites
Before deploying StockCustody, the following must be in place:IdentityRegistryUpgradeabledeployed and configured with KYC providers.- At least one
StockTokendeployed with the identity registry address. - USDC (or chosen settlement stablecoin) available on the target chain.
- A
LiquidationRouterdeployed, or a placeholder address if liquidation is not yet needed.
Compiler Configuration
ThebatchSettleTrades 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.
Deployment Sequence
Deposit and Withdrawal
Users must deposit tokens into custody before they can place orders on the orderbook.Trade Settlement Flow
The matching engine (operator) follows a lock-then-settle pattern. For high-throughput scenarios, usebatchSettleTrades 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.getLiquidation(liquidationId) to see the remaining amount.
Path B: External Sale
The operator sells tokens through an external venue, then provides USDC proceeds.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):Stock Split Reconciliation
After aStockToken.stockSplit() executes, internal custody balances must be updated to match the new token supply.
StockSplit event on StockToken to trigger reconciliation. The user list must be complete - any omitted user will have stale balances.
