This repository contains Soroban smart contracts for SwapTrade, an educational trading simulator built on the Stellar ecosystem.
The contracts replicate key features of real-world cryptocurrency trading in a risk-free, simulated environment:
- Virtual Assets: Mint and manage simulated XLM and Stellar-issued tokens.
- Trading Simulation: Execute token swaps and practice liquidity provision using Stellar’s native AMM model.
- Portfolio Tracking: Track balances, trades, and performance through contract state.
- Gamification: Unlock badges, achievements, and rewards as users progress.
- Extensible Design: Contracts are modular, allowing new features like staking or yield farming to be added.
- Soroban (Rust) for smart contracts
- Stellar SDK for frontend/backend integration
- Soroban CLI for contract deployment and testing
emergency_pause(admin)emergency_unpause(admin)freeze_user(admin, user)unfreeze_user(admin, user)snapshot_state()
The contract auto-pauses when swap volume exceeds configured threshold.
- Investigate issue
- Pause contract
- Freeze affected accounts
- Snapshot state
- Fix & restore
swaptrade-contracts/ │── Cargo.toml # Rust dependencies │── src/ │ ├── lib.rs # main contract logic │ ├── trading.rs # swap & AMM simulation │ ├── portfolio.rs # portfolio state │ ├── rewards.rs # gamification logic │── tests/ │ ├── trading_test.rs │ ├── rewards_test.rs │── soroban.toml # Soroban configuration │── README.md
- Install Soroban CLI.
- Clone this repo:
git clone https://github.com/your-org/swaptrade-contracts.git cd swaptrade-contracts
SwapTrade contracts support versioning and data migration to ensure historical data is preserved during upgrades.
CONTRACT_VERSIONis defined inlib.rs.- Current version is stored in contract storage.
get_contract_version(env)returns the stored version.
- Deploy New Code: Install and deploy the new WASM code.
- Initialize/Migrate:
- For new deployments, call
initialize()to set the initial version. - For upgrades, call
migrate()to transition data from the previous version to the current one.
- For new deployments, call
- Verify: Check
get_contract_version()matches the expected version.
- Bump
CONTRACT_VERSIONinlib.rs. - Implement migration logic in
migration.rs(e.g.,migrate_from_vX_to_vY). - Add tests in
migration_tests.rssimulating the upgrade. - Verify backward compatibility of data structures.
- Run
migrate()after upgrading the contract code.
- Change: Added
migration_timefield toPortfolio. - Migration Logic:
migrate_from_v1_to_v2checks ifmigration_timeis missing and initializes it. - Verification: Version bumps to 2.