This project provides a decentralized exchange (DEX) built on Substrate, featuring an Automated Market Maker (AMM) and Cross-Chain Bridge functionality implemented as substrate pallets.
The project consists of two main components:
- AMM Pallet - Provides liquidity pools and token swapping functionality
- Bridge Pallet - Handles cross-chain asset transfers using XCM
Both pallets are included in the same runtime, but can be prioritized differently based on a feature flag during compilation.
- Rust and Cargo (latest stable)
chain-spec-builder
tool:cargo install chain-spec-builder --git https://github.com/paritytech/cumulus
- Zombienet: Follow installation instructions from Zombienet documentation
We provide a script to build the parachain with different feature priorities:
# Build both AMM and Bridge specialized binaries
./build-parachains.sh both
# OR build only AMM primary
./build-parachains.sh amm
# OR build only Bridge primary
./build-parachains.sh bridge
This will create specialized binaries in the builds/
directory.
# Make the script executable
chmod +x generate-chainspecs.sh
# Generate chain specs for both parachains
./generate-chainspecs.sh
This script will:
- Generate chain specifications using
chain-spec-builder
- Convert them to raw format
- Generate genesis state and WASM files
- Place all files in the
chainspecs/
directory
# Make sure Zombienet is installed and in your PATH
zombienet spawn zombienet-config.toml
This will:
- Start a local relay chain with two validator nodes
- Start two parachains (AMM and Bridge)
- Establish HRMP channels between the parachains
- Configure XCM for cross-chain messaging
Run the unit tests for both pallets to verify core functionality:
# Run all tests for both pallets
cargo test -p pallet-amm -p pallet-bridge
# Run specific test
cargo test -p pallet-amm create_pool_works
Test the prioritization mechanism with feature flags:
# Test AMM as primary
cargo test -p pallet-amm --features primary-amm
# Test Bridge as primary
cargo test -p pallet-bridge --features primary-bridge
Generate weight data for optimal gas usage:
# Generate weight data for AMM pallet
cargo run --release -- benchmark pallet \
--chain=dev \
--pallet=pallet_amm \
--extrinsic=* \
--steps=50 \
--repeat=20 \
--output=pallets/amm/src/weights.rs
# Generate weight data for Bridge pallet
cargo run --release -- benchmark pallet \
--chain=dev \
--pallet=pallet_bridge \
--extrinsic=* \
--steps=50 \
--repeat=20 \
--output=pallets/bridge/src/weights.rs
You can interact with the DEX using the polkadot.js UI:
- Relay Chain: http://localhost:9900
- AMM Parachain: http://localhost:9920
- Bridge Parachain: http://localhost:9930
- Register a token on Bridge parachain
- Initiate a transfer from Bridge to AMM
- Verify token appears on AMM parachain
- Create a liquidity pool for the token on AMM
- Perform swaps
- Verify all transactions succeed on primary nodes
- Verify write operations fail on non-primary nodes
- Add supported asset
- Create liquidity pool
- Add liquidity
- Remove liquidity
- Swap token for native
- Swap native for token
- Process incoming XCM transfer
- Register bridge
- Register token
- Initiate transfer
- Complete transfer
- Process incoming transfer
- Transfer from Bridge to AMM works
- Tokens can be used in AMM after transfer
- Transfer from AMM back to Bridge works
- AMM write operations succeed when AMM is primary
- AMM write operations fail when Bridge is primary
- Bridge write operations succeed when Bridge is primary
- Bridge write operations fail when AMM is primary
The DEX supports the following wrapped tokens:
- wBTC (Wrapped Bitcoin)
- wETH (Wrapped Ethereum)
- wUSDC (Wrapped USD Coin)
- wUSDT (Wrapped Tether)
- wBNB (Wrapped BNB)
- wSOL (Wrapped Solana)
- wADA (Wrapped Cardano)
- XCM Message Failures: Check relay chain logs for XCM errors
- Token Balance Issues: Verify token minting/transfer worked correctly
- Prioritization Logic Issues: Verify feature flags are set correctly at compile time
substrate-dex/
├── pallets/
│ ├── amm/ # AMM pallet code
│ └── bridge/ # Bridge pallet code
├── runtime/ # Runtime with both pallets
├── chainspecs/ # Generated chain specifications
├── builds/ # Compiled binaries with different feature priorities
├── generate-chainspecs.sh # Script to generate chain specs
├── build-parachains.sh # Script to build specialized parachains
└── zombienet-config.toml # Zombienet configuration
This project is licensed under the MIT License.