- Replace manual modulo checks with .is_multiple_of() - Use enumerate() instead of manual loop counters - Use iterator .take() instead of index-based loops - Use slice literals instead of unnecessary vec![] - Allow too_many_arguments in IBC and bridge crates (protocol requirements) - Allow assertions on constants in integration tests |
||
|---|---|---|
| .. | ||
| aggregator | ||
| confidential-token | ||
| dex | ||
| ibc-bridge | ||
| multi-sig | ||
| nft | ||
| oracle | ||
| perps | ||
| staking | ||
| template | ||
| token | ||
| HOST_FUNCTIONS.md | ||
| README.md | ||
Synor Smart Contracts
This directory contains example smart contracts for the Synor blockchain.
Building Contracts
Contracts are compiled to WebAssembly using the wasm32-unknown-unknown target:
# Install the wasm target
rustup target add wasm32-unknown-unknown
# Build the token contract
cargo build --manifest-path contracts/token/Cargo.toml --target wasm32-unknown-unknown --release
# Build the NFT contract
cargo build --manifest-path contracts/nft/Cargo.toml --target wasm32-unknown-unknown --release
The compiled WASM files will be in target/wasm32-unknown-unknown/release/.
Optimizing Contract Size
For production, use wasm-opt to further reduce contract size:
# Install wasm-opt (part of binaryen)
brew install binaryen # macOS
apt install binaryen # Ubuntu
# Optimize the contract
wasm-opt -Oz -o optimized.wasm target/wasm32-unknown-unknown/release/synor_token.wasm
Example Contracts
SYN-20 Token (token/)
A fungible token contract similar to ERC-20:
- Minting (owner only)
- Burning
- Transfers
- Allowances (approve/transferFrom)
- Ownership management
SYN-721 NFT (nft/)
A non-fungible token contract similar to ERC-721:
- Minting NFTs with metadata URI
- Burning
- Transfers
- Approvals (per-token and operator)
- Ownership management
AMM DEX (dex/)
A Uniswap V2-style automated market maker:
- Liquidity pools for token pairs
- Constant product formula (x * y = k)
- LP tokens for liquidity providers
- Swap with 0.3% fee
- Quote/preview swaps
Staking (staking/)
A token staking contract with rewards:
- Stake tokens for rewards
- Lock periods with bonus multipliers (1x to 3x)
- Claim rewards anytime
- Emergency unstake with 10% penalty
- Pausable by owner
Creating Your Own Contract
Using the Template Generator
The fastest way to create a new contract:
./scripts/new-contract.sh my-contract --description "My custom contract"
This creates a new contract in contracts/my-contract/ with boilerplate code.
Manual Setup
-
Create a new contract directory:
mkdir -p contracts/my_contract/src -
Create
Cargo.toml:[package] name = "my-contract" version = "0.1.0" edition = "2021" [lib] crate-type = ["cdylib"] [dependencies] synor-sdk = { path = "../../crates/synor-sdk", default-features = false } borsh = { version = "1.3", default-features = false, features = ["derive"] } [profile.release] opt-level = "z" lto = true panic = "abort" strip = true -
Create
src/lib.rs:#![no_std] extern crate alloc; use synor_sdk::prelude::*; synor_sdk::entry_point!(init, call); fn init(_params: &[u8]) -> Result<()> { Ok(()) } fn call(selector: &[u8], params: &[u8]) -> Result<Vec<u8>> { // Handle method calls Err(Error::InvalidMethod) } -
Build:
cargo build --manifest-path contracts/my_contract/Cargo.toml \ --target wasm32-unknown-unknown --release
Host Function ABI
See HOST_FUNCTIONS.md for the complete list of host functions available to contracts.