Skip to content

L3pereira/substrate-dex

Repository files navigation

Substrate DEX Project

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.

Architecture

The project consists of two main components:

  1. AMM Pallet - Provides liquidity pools and token swapping functionality
  2. 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.

Prerequisites

  • 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

Building

Build Specialized Parachains

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.

Generate Chain Specifications

# Make the script executable
chmod +x generate-chainspecs.sh

# Generate chain specs for both parachains
./generate-chainspecs.sh

This script will:

  1. Generate chain specifications using chain-spec-builder
  2. Convert them to raw format
  3. Generate genesis state and WASM files
  4. Place all files in the chainspecs/ directory

Running with Zombienet

# Make sure Zombienet is installed and in your PATH
zombienet spawn zombienet-config.toml

This will:

  1. Start a local relay chain with two validator nodes
  2. Start two parachains (AMM and Bridge)
  3. Establish HRMP channels between the parachains
  4. Configure XCM for cross-chain messaging

Testing

Unit Tests

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

Prioritization Feature Testing

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

Benchmarking

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

Integration Testing with Polkadot.js UI

You can interact with the DEX using the polkadot.js UI:

Testing Workflow

  1. Register a token on Bridge parachain
  2. Initiate a transfer from Bridge to AMM
  3. Verify token appears on AMM parachain
  4. Create a liquidity pool for the token on AMM
  5. Perform swaps
  6. Verify all transactions succeed on primary nodes
  7. Verify write operations fail on non-primary nodes

DEX Testing Checklist

AMM Functionality

  • Add supported asset
  • Create liquidity pool
  • Add liquidity
  • Remove liquidity
  • Swap token for native
  • Swap native for token
  • Process incoming XCM transfer

Bridge Functionality

  • Register bridge
  • Register token
  • Initiate transfer
  • Complete transfer
  • Process incoming transfer

Cross-Chain Tests

  • Transfer from Bridge to AMM works
  • Tokens can be used in AMM after transfer
  • Transfer from AMM back to Bridge works

Prioritization Tests

  • 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

Available Tokens

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)

Debugging Tips

Common Issues

  1. XCM Message Failures: Check relay chain logs for XCM errors
  2. Token Balance Issues: Verify token minting/transfer worked correctly
  3. Prioritization Logic Issues: Verify feature flags are set correctly at compile time

Project Structure

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

License

This project is licensed under the MIT License.

About

A decentralized exchange (DEX) built on Substrate

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published