Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Blocksmith-forge Contracts

Soroban Smart Contracts for Blocksmith-forge AMM Protocol

A decentralized Automated Market Maker (AMM) and token swap protocol built on the Stellar network using Soroban smart contracts.

Overview

This repository contains the core smart contracts for the Blocksmith-forge DEX, including:

  • AMM Pool Contract: Manages liquidity pools, token swaps, and liquidity provision
  • Constant Product Formula: Implements the standard x*y=k AMM formula
  • LP Token System: Tracks liquidity provider positions and rewards

Architecture

AMM Pool Contract

The amm_pool contract provides the following functionality:

  • Pool Initialization: Create new trading pairs
  • Add Liquidity: Deposit tokens to earn LP tokens
  • Remove Liquidity: Withdraw tokens by burning LP tokens
  • Token Swaps: Exchange tokens with automatic price discovery
  • Fee Mechanism: 0.3% trading fee on all swaps

Project Structure

blocksmith-forge-Contracts/
├── Cargo.toml              # Workspace configuration
├── README.md               # This file
└── contracts/
    └── amm_pool/
        ├── Cargo.toml      # Contract dependencies
        └── src/
            └── lib.rs      # Main contract implementation

Prerequisites

  • Rust 1.75.0 or higher
  • Soroban CLI (cargo install soroban-cli)
  • Stellar Testnet account (for deployment)

Installation

# Clone the repository
git clone https://github.com/blocksmith-forge/blocksmith-forge-Contracts.git
cd blocksmith-forge-Contracts

# Install Soroban CLI
cargo install soroban-cli

Build

# Build the contract
cargo build --target wasm32-unknown-unknown --release

# Or use Soroban CLI
soroban contract build

Testing

# Run unit tests
cargo test

# Run with Soroban testutils
cargo test --features testutils

Local Testnet Deployment

1. Start Local Soroban Network

# Start a local standalone network
soroban network standalone

# In a separate terminal, check network status
soroban network status

2. Deploy Contract

# Set network to local
soroban network use standalone

# Deploy the AMM pool contract
soroban contract deploy \
  --wasm target/wasm32-unknown-unknown/release/amm_pool.wasm \
  --source alice \
  --network standalone

3. Initialize Pool

# Initialize the pool with token addresses
soroban contract invoke \
  --id <CONTRACT_ID> \
  --source alice \
  --network standalone \
  initialize \
  --token_a <TOKEN_A_ADDRESS> \
  --token_b <TOKEN_B_ADDRESS>

4. Add Liquidity

soroban contract invoke \
  --id <CONTRACT_ID> \
  --source alice \
  --network standalone \
  add_liquidity \
  --provider <PROVIDER_ADDRESS> \
  --amount_a 1000000 \
  --amount_b 1000000

5. Swap Tokens

soroban contract invoke \
  --id <CONTRACT_ID> \
  --source alice \
  --network standalone \
  swap_exact_tokens_for_tokens \
  --trader <TRADER_ADDRESS> \
  --token_in <TOKEN_IN_ADDRESS> \
  --amount_in 100000 \
  --amount_out_min 0

Contract Functions

initialize(token_a, token_b)

Initialize a new liquidity pool with two token addresses.

add_liquidity(provider, amount_a, amount_b)

Add liquidity to the pool and receive LP tokens. Returns the amount of LP tokens minted.

remove_liquidity(provider, lp_token_amount)

Remove liquidity by burning LP tokens. Returns the amounts of token_a and token_b withdrawn.

swap_exact_tokens_for_tokens(trader, token_in, amount_in, amount_out_min)

Swap exact input tokens for output tokens using constant product formula. Returns the amount of output tokens received.

get_pool_info()

Get current pool information including reserves and LP token supply.

get_liquidity_position(provider)

Get the liquidity position for a specific provider.

Security Considerations

  • All functions require proper authentication
  • Slippage protection via amount_out_min parameter
  • 0.3% trading fee to prevent sandwich attacks
  • LP token burn mechanism ensures proper liquidity tracking

License

MIT License - see LICENSE file for details

Contributing

Contributions are welcome! Please read our contributing guidelines before submitting PRs.

Contact

Releases

Packages

Contributors

Languages