Deployment Guide
This guide covers the complete workflow for building and deploying TAKO smart contracts on TOS Network.
Prerequisites
Install Platform Tools
# Install TOS Platform Tools
curl -sSf https://tos.network/install.sh | sh
# Verify installation
cargo-tako --version
# cargo-tako 0.3.1
# Tools are installed to:
# ~/.cache/tos/v1.54/Platform Tools Contents
~/.cache/tos/v1.54/
├── llvm/ # Clang 20.1.7-rust-dev (TBPFv3 backend)
├── rust/ # Rustc 1.89.0-dev with TBPFv3 target
└── version.mdProject Setup
Create New Project
# Option 1: Use cargo-tako
cargo-tako new my_contract
cd my_contract
# Option 2: Manual setup
mkdir my_contract && cd my_contract
cargo init --libConfigure Cargo.toml
[package]
name = "my_contract"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
tako-sdk = { git = "https://github.com/tos-network/tako", branch = "main" }
[profile.release]
opt-level = "z" # Optimize for size
lto = true # Link-time optimization
panic = "abort" # Smaller binary
codegen-units = 1 # Better optimizationConfigure Build Target
Create .cargo/config.toml:
[build]
target = "tbpfv3-tos-tos"
[target.tbpfv3-tos-tos]
rustflags = [
"-C", "link-arg=-z", "-C", "link-arg=notext",
]Configure Toolchain
Create rust-toolchain.toml:
[toolchain]
channel = "1.89.0-dev"Building
Simple Build
cargo build --releaseBuild Script
Create build.sh:
#!/bin/bash
set -e
echo "Building contract..."
cargo build --release
# Copy to project root
cp target/tbpfv3-tos-tos/release/*.so ./
echo "Build complete: $(ls *.so)"chmod +x build.sh
./build.shVerify Build
# Check file size (should be small)
ls -lh my_contract.so
# Typical size: 5-50 KB for simple contractsTesting
Local Testing
# Start local devnet
tos daemon --network devnet
# In another terminal, deploy and test
tos_wallet deploy_contract ./my_contract.soUnit Tests
Create tests/integration.rs:
use tos_test_sdk::*;
#[test]
fn test_contract_deploy() {
let env = TestEnvironment::new();
let contract = env.deploy("my_contract.so");
assert!(contract.address().len() > 0);
}
#[test]
fn test_contract_call() {
let env = TestEnvironment::new();
let contract = env.deploy("my_contract.so");
let result = contract.call(&[0x01]); // Call with opcode 0x01
assert_eq!(result.status, 0); // SUCCESS
}Deployment
Networks
| Network | Explorer | RPC |
|---|---|---|
| Mainnet | explorer.tos.network | rpc.tos.network |
| Testnet | testnet-explorer.tos.network | testnet-rpc.tos.network |
| Devnet | localhost | localhost:8080 |
Deploy to Testnet
# Configure wallet for testnet
tos_wallet config --network testnet
# Check balance (need TOS for gas)
tos_wallet balance
# Deploy contract
tos_wallet deploy_contract ./my_contract.so
# Output:
# Transaction: 0xabc123...
# Contract address: tst1xyz789...Deploy to Mainnet
# Configure for mainnet
tos_wallet config --network mainnet
# Deploy (requires TOS for gas)
tos_wallet deploy_contract ./my_contract.so
# Output:
# Transaction: 0xdef456...
# Contract address: tos1abc123...Deployment Options
# Deploy with specific gas limit
tos_wallet deploy_contract ./my_contract.so --gas-limit 500000
# Deploy with initialization data
tos_wallet deploy_contract ./my_contract.so --init-data "00070048656c6c6f"Contract Interaction
Call Contract
# Call with hex input
tos_wallet call_contract <address> <hex_input>
# Example: Call opcode 0x01 with parameter
tos_wallet call_contract tos1abc123... 01deadbeef
# Send TOS with call
tos_wallet call_contract tos1abc123... 01 --value 1000000Query Contract
# Query (read-only, no gas)
tos_wallet query_contract <address> <hex_input>
# Example: Query balance
tos_wallet query_contract tos1abc123... 10<account_address>Verification
Verify Source Code
# Upload source for verification
tos_wallet verify_contract <address> \
--source ./src/lib.rs \
--cargo-toml ./Cargo.tomlView Contract on Explorer
Visit https://explorer.tos.network/contract/<address> to see:
- Contract code (if verified)
- Transaction history
- Storage state
Upgrading Contracts
TAKO contracts are immutable by default. For upgradeable contracts, use the proxy pattern:
Proxy Pattern
// Proxy contract
const KEY_IMPLEMENTATION: &[u8] = b"impl";
fn get_implementation() -> [u8; 32] {
let mut addr = [0u8; 32];
storage_read(KEY_IMPLEMENTATION, &mut addr);
addr
}
#[no_mangle]
pub extern "C" fn entrypoint() -> u64 {
let mut input = [0u8; 10240];
let len = get_input_data(&mut input);
// Delegate to implementation
let impl_addr = get_implementation();
let mut return_buf = [0u8; 10240];
match cpi_call(&impl_addr, &input[..len as usize], &mut return_buf) {
Ok(ret_len) => {
let _ = set_return_data(&return_buf[..ret_len as usize]);
SUCCESS
}
Err(e) => e,
}
}Upgrade Process
- Deploy new implementation contract
- Call proxy’s
upgradefunction with new address - Verify new implementation works correctly
Best Practices
Security
- Audit before mainnet - Get professional security review
- Test extensively - Cover all edge cases
- Use checked math - Prevent overflows
- Validate all inputs - Never trust user data
Gas Optimization
- Minimize storage writes - 20,000 CU each
- Batch operations - Single transaction when possible
- Use efficient data structures - Pack storage keys
- Optimize code size - Use
opt-level = "z"
Maintenance
- Document thoroughly - Comment all public functions
- Version your contracts - Track deployments
- Monitor transactions - Watch for unusual activity
- Plan for upgrades - Use proxy pattern if needed
Troubleshooting
Build Errors
# Error: toolchain not found
# Solution: Install platform tools
curl -sSf https://tos.network/install.sh | sh
# Error: target not found
# Solution: Check .cargo/config.tomlDeployment Errors
# Error: insufficient balance
# Solution: Get testnet TOS from faucet
tos_wallet faucet
# Error: contract too large
# Solution: Enable LTO and size optimizationRuntime Errors
# Error: compute exceeded
# Solution: Increase gas limit or optimize code
# Error: storage write failed
# Solution: Check contract has permissionResources
- TOS Contract Kit (TCK) - Example contracts
- TAKO SDK Docs - SDK reference
- Discord - Developer support
Last updated on