Production-ready Soroban smart contract for peer-to-peer marketplace escrow on Stellar blockchain.
StellarEscrow is a decentralized escrow system that enables secure peer-to-peer trades using USDC stablecoin. The platform connects buyers and sellers with optional arbitrators who can resolve disputes, with the smart contract managing escrow, fee collection, and settlement.
- Escrow-Based Trading: Secure USDC deposits held in contract until trade completion
- Arbitrator Network: Registered arbitrators handle dispute resolution
- Automated Fee Collection: Platform fees calculated and accumulated automatically
- Multi-Status Tracking: Trades tracked through Created, Funded, Completed, Disputed, and Cancelled states
- Authorization Security: Role-based access control for all operations
- Event Emission: Comprehensive event logging for off-chain monitoring
- Dispute Resolution: Arbitrators can release funds to buyer or seller
- Cancellation Support: Sellers can cancel unfunded trades
- Admin Controls: Platform fee management and fee withdrawal capabilities
- lib.rs: Main contract implementation with all public functions
- types.rs: Data structures (Trade, TradeStatus, DisputeResolution)
- storage.rs: Persistent and instance storage management
- errors.rs: Custom error types for contract operations
- events.rs: Event emission functions for monitoring
- test.rs: Comprehensive test suite with 20+ test cases
Instance Storage: Admin, USDC token, fee configuration, counters, accumulated fees
Persistent Storage: Individual trades, arbitrator registrations
Fees are calculated in basis points (bps):
- 100 bps = 1.0%
- 250 bps = 2.5%
- 500 bps = 5.0%
Formula: fee = amount * fee_bps / 10000
initialize(admin, usdc_token, fee_bps)- One-time contract initializationregister_arbitrator(arbitrator)- Add arbitrator to approved list (admin only)remove_arbitrator_fn(arbitrator)- Remove arbitrator from approved list (admin only)update_fee(fee_bps)- Update platform fee percentage (admin only)withdraw_fees(to)- Withdraw accumulated fees (admin only)
create_trade(seller, buyer, amount, arbitrator)- Create new trade (seller auth required)fund_trade(trade_id)- Buyer funds the escrow (buyer auth required)complete_trade(trade_id)- Seller confirms delivery (seller auth required)confirm_receipt(trade_id)- Buyer confirms receipt and releases funds (buyer auth required)raise_dispute(trade_id)- Either party raises dispute (buyer/seller auth required)resolve_dispute(trade_id, resolution)- Arbitrator resolves dispute (arbitrator auth required)cancel_trade(trade_id)- Cancel unfunded trade (seller auth required)
get_trade(trade_id)- Retrieve trade detailsget_accumulated_fees()- Check total platform fees collectedis_arbitrator_registered(arbitrator)- Verify arbitrator registration statusget_platform_fee_bps()- Get current fee percentage
- Authorization Checks: All state-changing operations require proper authorization
- Status Validation: Prevents invalid state transitions
- Overflow Protection: Safe math operations with overflow checks
- Arbitrator Verification: Only registered arbitrators can resolve disputes
- Ownership Validation: Only trade parties can perform specific actions
The contract includes comprehensive tests covering:
✅ Initialization and configuration
✅ Arbitrator registration and removal
✅ Fee updates and validation
✅ Trade creation and funding
✅ Trade completion flow
✅ Dispute raising and resolution
✅ Cancellation logic
✅ Fee withdrawal by admin
✅ Authorization enforcement
✅ Error conditions
✅ Event emission verification
✅ Multiple trades handling
✅ Fee calculation accuracy
Run tests with:
cargo testcd StellarEscrow
cargo build --target wasm32-unknown-unknown --release
soroban contract optimize --wasm target/wasm32-unknown-unknown/release/stellar_escrow.wasmsoroban contract deploy \
--wasm target/wasm32-unknown-unknown/release/stellar_escrow.optimized.wasm \
--source deployer \
--network testnetsoroban contract invoke \
--id <CONTRACT_ID> \
--source deployer \
--network testnet \
-- \
initialize \
--admin <ADMIN_ADDRESS> \
--usdc_token <USDC_TOKEN_ADDRESS> \
--fee_bps 100See DEPLOYMENT.md for complete deployment instructions.
- Deploy contract
- Initialize with admin address, USDC token, and fee percentage
- Register trusted arbitrators
- Seller creates trade with buyer address, amount, and optional arbitrator
- Trade ID returned for tracking
- Buyer approves USDC transfer to contract
- Buyer calls
fund_tradewith trade ID - Contract transfers USDC from buyer to escrow
- Seller delivers goods/services off-chain
- Seller calls
complete_trade - Buyer calls
confirm_receipt - Contract transfers USDC minus fee to seller
- Fee added to accumulated platform fees
- Either party calls
raise_dispute - Arbitrator investigates off-chain
- Arbitrator calls
resolve_disputewith resolution (ReleaseToBuyer or ReleaseToSeller) - Contract transfers funds accordingly minus arbitration fee
- Admin monitors accumulated fees
- Admin calls
withdraw_feesto collect platform revenue
| Code | Error | Description |
|---|---|---|
| 1 | AlreadyInitialized | Contract already initialized |
| 2 | NotInitialized | Contract not initialized |
| 3 | InvalidAmount | Amount must be greater than 0 |
| 4 | InvalidFeeBps | Fee must be between 0-10000 bps |
| 5 | ArbitratorNotRegistered | Arbitrator not in approved list |
| 6 | TradeNotFound | Trade ID does not exist |
| 7 | InvalidStatus | Operation not allowed in current status |
| 8 | Overflow | Arithmetic overflow detected |
| 9 | NoFeesToWithdraw | No accumulated fees available |
| 10 | Unauthorized | Caller not authorized for this operation |
The contract emits events for monitoring:
created- New trade createdfunded- Buyer funded escrowcomplete- Seller marked as deliveredconfirm- Buyer confirmed receipt and settleddispute- Dispute initiatedresolved- Arbitrator resolved disputecancel- Trade cancelledarb_reg- Arbitrator registeredarb_rem- Arbitrator removedfee_upd- Platform fee updatedfees_out- Fees withdrawn by admin
soroban-sdk = "21.7.0"- Latest Soroban SDK
- ARCHITECTURE.md - Technical architecture and design decisions
- DEPLOYMENT.md - Complete deployment guide
- CONTRIBUTING.md - Contribution guidelines
MIT
For issues and questions:
- GitHub Issues: Create an issue
- Stellar Discord: https://discord.gg/stellar
- Soroban Docs: https://soroban.stellar.org
Contributions welcome! Please ensure:
- All tests pass:
cargo test - Code follows Rust best practices
- New features include tests
- Documentation is updated
See CONTRIBUTING.md for detailed guidelines.
- Multi-currency support
- Batch trade processing
- Arbitrator reputation system
- Time-locked escrow options
- Partial release mechanism
- Multi-signature arbitration
- Integration with decentralized identity
StellarEscrow is a Soroban smart contract built in Rust that enables secure, escrow-based peer-to-peer trading on the Stellar network.
This project demonstrates production-ready patterns for escrow systems, dispute resolution, and fee management on Stellar blockchain.