This repository contains an implementation of a bonding contract for the Alkanes metaprotocol. The bonding contract allows users to swap an alkane for diesel (the genesis alkane) following a smooth price curve. As more alkane is swapped for diesel, the price becomes more expensive until the curve is filled.
boiler/
├── Cargo.toml # Workspace manifest
├── contracts/ # Directory for contract crates
│ └── bonding-contract/ # Bonding contract implementation
│ ├── Cargo.toml # Contract manifest
│ └── src/ # Contract source code
│ └── lib.rs # Contract implementation
└── crates/ # Supporting crates
├── alkanes-runtime/ # Runtime environment
├── alkanes-support/ # Support utilities
├── alkanes-macros/ # Macros for contract development
└── ... # Other supporting crates
The bonding contract implements a quadratic bonding curve where the price of tokens increases as more tokens are minted. The contract provides the following functionality:
- Buy tokens with diesel: Users can send diesel to the contract and receive tokens based on the bonding curve.
- Sell tokens for diesel: Users can send tokens back to the contract and receive diesel based on the bonding curve.
- Query contract information: Users can query the current price, total supply, reserve, and other contract information.
The contract uses a quadratic bonding curve where the price is determined by the formula:
price = reserve / (supply^2)
This creates a smooth curve that becomes more expensive as the supply increases.
- Rust and Cargo
- wasm32-unknown-unknown target
To build the bonding contract, use the provided build script:
./scripts/build.shThis will:
- Check if the wasm32-unknown-unknown target is installed and install it if needed
- Build the bonding contract
- Copy the WASM file to the
distdirectory
You can also run tests or examples with the build script:
./scripts/build.sh --test # Run tests
./scripts/build.sh --example # Run exampleThe project includes several types of tests:
To run all tests and generate a report:
./scripts/run_tests.shThis script will run all test suites and generate a report with the results. The test logs are saved in the test-reports directory.
You can also run individual test suites:
Basic unit tests for the bonding contract:
cargo test -p bonding-contractIntegration tests that simulate real-world usage of the contract:
cargo test --test integration_testTests that simulate market behavior with multiple participants:
cargo test --test market_simulation_testPerformance benchmarks for different operations:
cargo test --test benchmark_testThese tests help ensure that the bonding contract works correctly and efficiently in various scenarios.
The bonding contract can be configured for different environments using the provided configuration script:
# Generate deployment scripts and configuration summaries for the default environment
./scripts/configure.js
# Generate for a specific environment (testnet or mainnet)
./scripts/configure.js testnet
./scripts/configure.js mainnetThis will generate:
- Environment-specific deployment scripts in the
scriptsdirectory - Configuration summaries in the
docsdirectory
To deploy the bonding contract, use the provided deploy scripts:
# Deploy with default configuration
./scripts/deploy.sh
# Deploy with environment-specific configuration
./scripts/deploy-testnet.sh
./scripts/deploy-mainnet.shThese scripts provide guidance on how to deploy the contract using the Alkanes CLI or SDK.
In a real deployment, you would use code like this:
let cellpack = Cellpack {
target: AlkaneId { block: 1, tx: 0 },
inputs: vec![
0, // Initialize opcode
0x424f4e44, // "BOND" as u128 (name)
0x424e44, // "BND" as u128 (symbol)
1000000, // Initial supply
1000000, // Initial reserve
],
};The deployment parameters can be customized in the contracts/bonding-contract/config.json file.
To buy tokens with diesel:
// First, send diesel to the contract
let diesel_transfer = AlkaneTransfer {
id: AlkaneId { block: 2, tx: 0 }, // Diesel is [2, 0]
value: 1000,
};
// Then call the buy function
let buy_cellpack = Cellpack {
target: contract_id,
inputs: vec![
1, // Buy opcode
],
};To sell tokens for diesel:
// First, send tokens to the contract
let token_transfer = AlkaneTransfer {
id: contract_id,
value: 1000,
};
// Then call the sell function
let sell_cellpack = Cellpack {
target: contract_id,
inputs: vec![
2, // Sell opcode
1000, // Amount
],
};To get information about the contract:
// Get the current price
let get_current_price_cellpack = Cellpack {
target: contract_id,
inputs: vec![
3, // GetCurrentPrice opcode
],
};
// Get the buy price for a specific amount
let get_buy_price_cellpack = Cellpack {
target: contract_id,
inputs: vec![
4, // GetBuyPrice opcode
1000, // Amount
],
};MIT