# 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:

```bash
# 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:

```bash
# 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:

```bash
./scripts/new-contract.sh my-contract --description "My custom contract"
```

This creates a new contract in `contracts/my-contract/` with boilerplate code.

### Manual Setup

1. Create a new contract directory:
   ```bash
   mkdir -p contracts/my_contract/src
   ```

2. Create `Cargo.toml`:
   ```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
   ```

3. Create `src/lib.rs`:
   ```rust
   #![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)
   }
   ```

4. Build:
   ```bash
   cargo build --manifest-path contracts/my_contract/Cargo.toml \
       --target wasm32-unknown-unknown --release
   ```

## Host Function ABI

See [HOST_FUNCTIONS.md](./HOST_FUNCTIONS.md) for the complete list of host functions
available to contracts.
