# /cook:contracts - Move Smart Contract Generation

**Agent:** `smart-contract`

Generate Move smart contracts for the Movement blockchain.

**IMPORTANT**: Delegate to `smart-contract` agent.

## Prerequisites

### Install Movement CLI

Before generating contracts, ensure the Movement CLI is installed. See [Movement CLI docs](https://docs.movementnetwork.xyz/devs/movementcli) for details.

#### Quick Install (Testnet - with Move 2 support)

**macOS (Apple Silicon/M-series):**
```bash
curl -LO https://github.com/movementlabsxyz/homebrew-movement-cli/releases/download/bypass-homebrew/movement-move2-testnet-macos-arm64.tar.gz && mkdir -p temp_extract && tar -xzf movement-move2-testnet-macos-arm64.tar.gz -C temp_extract && chmod +x temp_extract/movement && sudo mv temp_extract/movement /usr/local/bin/movement && rm -rf temp_extract movement-move2-testnet-macos-arm64.tar.gz
```

**macOS (Intel):**
```bash
curl -LO https://github.com/movementlabsxyz/homebrew-movement-cli/releases/download/bypass-homebrew/movement-move2-testnet-macos-x86_64.tar.gz && mkdir -p temp_extract && tar -xzf movement-move2-testnet-macos-x86_64.tar.gz -C temp_extract && chmod +x temp_extract/movement && sudo mv temp_extract/movement /usr/local/bin/movement && rm -rf temp_extract movement-move2-testnet-macos-x86_64.tar.gz
```

**Linux (x86_64):**
```bash
curl -LO https://github.com/movementlabsxyz/homebrew-movement-cli/releases/download/bypass-homebrew/movement-move2-testnet-linux-x86_64.tar.gz && mkdir -p temp_extract && tar -xzf movement-move2-testnet-linux-x86_64.tar.gz -C temp_extract && chmod +x temp_extract/movement && sudo mv temp_extract/movement /usr/local/bin/movement && rm -rf temp_extract movement-move2-testnet-linux-x86_64.tar.gz
```

**Verify installation:**
```bash
movement --version
```

### Initialize Account (if not done)
```bash
movement init --network testnet
```

This creates a `.movement/config.yaml` with your account credentials.

### Get Testnet Tokens
Visit https://faucet.movementnetwork.xyz/ to get testnet MOVE tokens for deployment.

## Workflow

### Step 1: Read Move Language Reference
**CRITICAL:** Always read the Move language reference first:
```bash
cat docs/MOVE_LANGUAGE_REFERENCE.md
```

### Step 2: Analyze Requirements
From the plan or user request, identify:
- Module structure
- Resource definitions
- Entry functions
- View functions
- Events
- Error codes

### Step 3: Generate Move.toml
```toml
[package]
name = "{project_name}"
version = "1.0.0"
authors = []

[addresses]
module_addr = "_"

[dependencies]
AptosFramework = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-framework", rev = "mainnet" }
AptosStdlib = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-stdlib", rev = "mainnet" }
MoveStdlib = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/move-stdlib", rev = "mainnet" }
```

### Step 4: Generate Move Modules
Create modules in `contracts/sources/`:

```move
module module_addr::module_name {
    use std::signer;
    use std::string::String;
    use aptos_framework::event;
    use aptos_framework::account;

    // Error codes
    const E_NOT_AUTHORIZED: u64 = 1;
    const E_ALREADY_EXISTS: u64 = 2;

    // Resources
    struct ResourceName has key, store {
        field1: u64,
        field2: String,
    }

    // Events
    #[event]
    struct EventName has drop, store {
        field1: u64,
        timestamp: u64,
    }

    // Entry functions
    public entry fun function_name(
        account: &signer,
        param1: u64,
    ) acquires ResourceName {
        let addr = signer::address_of(account);
        // Implementation
        event::emit(EventName { field1: param1, timestamp: 0 });
    }

    // View functions
    #[view]
    public fun get_value(addr: address): u64 acquires ResourceName {
        borrow_global<ResourceName>(addr).field1
    }
}
```

### Step 5: Generate Unit Tests
Create tests in `contracts/tests/`:

```move
#[test_only]
module module_addr::module_name_tests {
    use std::signer;
    use module_addr::module_name;

    #[test(account = @0x1)]
    fun test_function_name(account: &signer) {
        // Test implementation
    }

    #[test]
    #[expected_failure(abort_code = module_name::E_NOT_AUTHORIZED)]
    fun test_unauthorized_access() {
        // Test error case
    }
}
```

### Step 6: Compile and Test
```bash
cd contracts
movement move compile
movement move test
```

## Best Practices

1. **Resource Safety**: Always use proper resource management
2. **Access Control**: Validate signers on privileged functions
3. **Events**: Emit events for all state changes
4. **Error Codes**: Use descriptive error constants
5. **Documentation**: Add inline comments for complex logic

## Output Summary
```markdown
# 📜 Contracts Generated

## Modules
| Module | Functions | Events | Tests |
|--------|-----------|--------|-------|
| module_name | 5 | 3 | 10 |

## Compilation: ✅ Success
## Tests: ✅ 10/10 passed

## Next Steps
- Run `/cook:frontend` to generate frontend
- Run `/test:contracts` for comprehensive testing
```

