CEX custody and deposits
Problem: Credit user balances exactly once for unique on-chain deposit IDs.
Model: Every public transaction changes a deterministic state machine. Distinguish token balances from native gas coin, transaction submission from finality, and contract permissions from off-chain operator policy.
BSC vs Ethereum: Both execute EVM-compatible contracts, but chain ID, native gas asset, RPC and network finality assumptions differ. ERC-20/BEP-20 interface compatibility does not move balances across networks.
Safety: Training implementations are intentionally minimal. For deployed tokens/NFTs prefer audited OpenZeppelin contracts, access control, testing and independent review.
CEX custody and deposits — executable lab
Bài toán → Vấn đề → Giải pháp
Credit user balances exactly once for unique on-chain deposit IDs. A centralized implementation cannot assume on-chain transfers and application accounting are automatically synchronized. Implement the contract with explicit validation, accounting and revert behavior.
Setup
- Install Foundry (
forge --version) and Solidity compiler 0.8.24 (Foundry can download it when online). cd solution && forge build && forge test -vvv.- Read
src/CustodyLedger.solandtest/Lab.t.sol, then implement your own version instarter/.
Test plan
- Successful state transition, correct getter values and events where relevant.
- Revert on invalid amount, unauthorized call or duplicate identifier as applicable.
- Add a fuzz test and one failure injection case.
Expected output
forge test exits 0 with [PASS] for each included test. This is an expected result, not a verified runtime log.
Exercises
- Add tests for two distinct actors and zero addresses.
- Explain the trust boundary and identify missing production controls.
- Extend the contract using OpenZeppelin in a separate production-style branch.
Interview
Q: Why is an event not enough to prove final payment? A: Logs can be removed by chain reorg; verify receipt, confirmations/finality, chain ID, token address, and log identity. Q: Is this contract production ready? A: No; the examples omit standards compliance, audits, upgrade and operational controls.
Interview — CEX custody and deposits
- What does on-chain state guarantee? Deterministic execution under consensus, not truthful off-chain inputs.
- Can a transaction be considered final immediately? No; account for inclusion, reorg and chain-specific finality.
- Why avoid homegrown token contracts in production? Missing standard methods, approvals, security review and integration edge cases.