skills/chain/deploy-contract/SKILL.md
# Deploy Contract to 0G Chain ## Metadata - **Category**: chain - **SDK**: `ethers` ^6.13.0, Hardhat or Foundry - **Activation Triggers**: "deploy contract", "Solidity", "0G Chain", "deploy smart contract" ## Purpose Deploy Solidity smart contracts to 0G Chain using Hardhat, Foundry, or ethers v6 directly. All contracts must be compiled with `evmVersion: "cancun"`. ## Prerequisites - Node.js >= 18 - Hardhat or Foundry installed - Funded wallet with 0G tokens - `.env` with `PRIVATE_KEY`, `R
npx skillsauth add 0gfoundation/0g-agent-skills skills/chain/deploy-contractInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
4 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
ethers ^6.13.0, Hardhat or FoundryDeploy Solidity smart contracts to 0G Chain using Hardhat, Foundry, or ethers v6 directly. All
contracts must be compiled with evmVersion: "cancun".
.env with PRIVATE_KEY, RPC_URLevmVersion: "cancun"evmVersion: "cancun" in compiler configurationwaitForDeployment())"cancun" for 0G Chain.deployed(), contract.address, etc.)// contracts/MyContract.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract MyContract {
uint256 public value;
address public owner;
event ValueChanged(uint256 newValue);
constructor(uint256 _initialValue) {
value = _initialValue;
owner = msg.sender;
}
function setValue(uint256 _value) external {
value = _value;
emit ValueChanged(_value);
}
}
// scripts/deploy.ts
import { ethers } from 'hardhat';
async function main() {
const [deployer] = await ethers.getSigners();
console.log('Deploying with:', deployer.address);
const balance = await deployer.provider.getBalance(deployer.address);
console.log('Balance:', ethers.formatEther(balance), '0G');
const Contract = await ethers.getContractFactory('MyContract');
const contract = await Contract.deploy(42);
await contract.waitForDeployment();
const address = await contract.getAddress();
console.log('Deployed to:', address);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
# Compile
npx hardhat compile
# Deploy to testnet
npx hardhat run scripts/deploy.ts --network 0g-testnet
# Verify
npx hardhat verify --network 0g-testnet <CONTRACT_ADDRESS> 42
# Compile
forge build
# Deploy
forge create src/MyContract.sol:MyContract \
--rpc-url https://evmrpc-testnet.0g.ai \
--private-key $PRIVATE_KEY \
--constructor-args 42
# Verify
forge verify-contract <CONTRACT_ADDRESS> src/MyContract.sol:MyContract \
--chain-id 16602 \
--verifier-url https://chainscan-galileo.0g.ai/api
import { ethers, ContractFactory } from 'ethers';
import * as fs from 'fs';
import 'dotenv/config';
async function deploy() {
const provider = new ethers.JsonRpcProvider(process.env.RPC_URL);
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY!, provider);
// Load compiled artifact
const artifact = JSON.parse(
fs.readFileSync('./artifacts/contracts/MyContract.sol/MyContract.json', 'utf8'),
);
const factory = new ContractFactory(artifact.abi, artifact.bytecode, wallet);
const contract = await factory.deploy(42);
await contract.waitForDeployment();
const address = await contract.getAddress();
console.log('Deployed to:', address);
return { address, abi: artifact.abi };
}
// Complex constructor
const contract = await Contract.deploy(
'My Token', // string name
'MTK', // string symbol
ethers.parseEther('1000000'), // uint256 initialSupply
wallet.address, // address owner
);
await contract.waitForDeployment();
// BAD: Missing evmVersion
// hardhat.config.ts
solidity: '0.8.24'; // WRONG — needs cancun setting
// BAD: ethers v5 deployment check
await contract.deployed(); // v5! Use waitForDeployment()
// BAD: Getting address v5 style
console.log(contract.address); // v5! Use await contract.getAddress()
// BAD: Deploying without balance check
await Contract.deploy(42); // May fail with insufficient funds
| Error | Cause | Fix |
| ------------------------------------------- | ----------------------- | -------------------------- |
| invalid opcode | Wrong evmVersion | Set evmVersion: "cancun" |
| insufficient funds | Wallet empty | Fund from faucet |
| nonce too low | Pending transaction | Wait or increment nonce |
| contract creation code storage out of gas | Complex contract | Increase gas limit |
| cannot estimate gas | Constructor will revert | Check constructor args |
development
# Upload File to 0G Storage ## Metadata - **Category**: storage - **SDK**: `@0glabs/0g-ts-sdk` ^0.3.3, `ethers` ^6.13.0 - **Activation Triggers**: "upload file", "store on 0G", "ZgFile", "save to storage" ## Purpose Upload files to 0G decentralized storage using the ZgFile API and Indexer. Files are split into chunks, organized as a Merkle tree, and distributed across storage nodes. Returns a root hash for later retrieval. ## Prerequisites - Node.js >= 18 - `@0glabs/0g-ts-sdk` and `ethers`
development
# Merkle Verification ## Metadata - **Category**: storage - **SDK**: `@0glabs/0g-ts-sdk` ^0.3.3 - **Activation Triggers**: "verify file", "merkle proof", "data integrity", "root hash", "check file" ## Purpose Compute root hashes and verify data integrity for files stored on 0G Storage. Uses Merkle tree proofs to cryptographically verify that downloaded data matches what was originally uploaded. ## Prerequisites - Node.js >= 18 - `@0glabs/0g-ts-sdk` installed ## Quick Workflow 1. Create
development
# Download File from 0G Storage ## Metadata - **Category**: storage - **SDK**: `@0glabs/0g-ts-sdk` ^0.3.3, `ethers` ^6.13.0 - **Activation Triggers**: "download file", "retrieve from 0G", "get file", "fetch from storage" ## Purpose Download and verify files from 0G decentralized storage using a root hash. Supports verified downloads with Merkle proof validation to ensure data integrity. ## Prerequisites - Node.js >= 18 - `@0glabs/0g-ts-sdk` installed - Root hash of the file to download - `
development
# Storage + Chain Integration ## Metadata - **Category**: cross-layer - **SDK**: `@0glabs/0g-ts-sdk` ^0.3.3, `ethers` ^6.13.0 - **Activation Triggers**: "on-chain reference", "NFT metadata on 0G", "store hash on-chain", "registry contract", "chain and storage" ## Purpose Combine 0G Storage with 0G Chain smart contracts to create on-chain references to off-chain data. Common patterns include NFT metadata storage, content registries, and verifiable document systems. ## Prerequisites - Node