agentleFS
0gfoundation0gfoundation/0g-agent-skills/skills/chain/deploy-contract/SKILL.md

deploy-contract

Skill15 starsChanged 57 days agoMITView on GitHub

Deploy Solidity smart contracts to 0G Chain using Hardhat, Foundry, or ethers v6 directly. Compile with evmVersion: "cancun" — 0G Chain supports it and it produces the smallest bytecode.

  • Reads credentials

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 3 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Verify on block explorer (optional)

    line 27
  2. Distinctive:

    Use ethers v6 syntax (NOT v5)

    line 34
  3. Distinctive:

    Test on testnet before deploying to mainnet

    line 35
  4. Very specific:

    Wait for deployment confirmation (waitForDeployment())

    line 36
  5. Very specific:

    Use ethers v5 patterns (.deployed(), contract.address, etc.)

    line 41

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Deploy Contract to 0G Chain

Metadata

  • Category: chain
  • SDK: ethers 6.13.1, 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. Compile with evmVersion: "cancun" — 0G Chain supports it and it produces the smallest bytecode.

Prerequisites

  • Node.js >= 18
  • Hardhat or Foundry installed
  • Funded wallet with 0G tokens
  • .env with PRIVATE_KEY, RPC_URL

Quick Workflow

  1. Write Solidity contract
  2. Configure compiler with evmVersion: "cancun" (recommended — see below)
  3. Compile contract
  4. Deploy to 0G testnet
  5. Verify on block explorer (optional)

Core Rules

ALWAYS

  • Set evmVersion: "cancun" in compiler configuration — smallest bytecode on 0G Chain
  • Use ethers v6 syntax (NOT v5)
  • Test on testnet before deploying to mainnet
  • Wait for deployment confirmation (waitForDeployment())
  • Store deployed contract addresses

NEVER

  • Use ethers v5 patterns (.deployed(), contract.address, etc.)
  • Deploy to mainnet without testnet testing
  • Hardcode private keys

Code Examples

Hardhat Deployment

// 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

Foundry Deployment

# 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

Direct ethers v6 Deployment

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 };
}

Deploy with Constructor Arguments

// 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();

Anti-Patterns

// SUBOPTIMAL: no evmVersion — solc may target an older EVM than 0G supports,
// producing larger, more expensive bytecode. It still deploys and runs.
// hardhat.config.ts
solidity: '0.8.24';

// 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

Choosing an EVM Target

0G Chain is Cancun-capable, so cancun is the right default: solc can emit MCOPY for memory copies, which shrinks the deployed bytecode and lowers deploy gas.

It is not a compatibility requirement. The same contract was compiled at four targets and deployed to both 0G testnet (16602) and mainnet (16661); every one deployed successfully and returned correct values from both a pure function and a memory-copy function:

evmVersionDeploys & runs on 0GBytecode
cancunyes1437 B
shanghaiyes1479 B
parisyes1527 B
londonyes1527 B

So prefer cancun to save gas, but an older target inherited from a dependency or an existing toolchain will not break deployment. If you see invalid opcode, look for a cause other than evmVersion — that symptom is not produced by targeting an older EVM here.

Common Errors & Fixes

ErrorCauseFix
insufficient fundsWallet emptyFund from faucet
nonce too lowPending transactionWait or increment nonce
contract creation code storage out of gasComplex contractIncrease gas limit
cannot estimate gasConstructor will revertCheck constructor args

Related Skills

  • Interact Contract — interact with deployed contracts
  • Scaffold Project — project setup
  • Storage + Chain — on-chain references

References

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.