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

interact-contract

Skill15 starsChanged 57 days agoMITView on GitHub

- Category: chain - SDK: ethers 6.13.1 - Activation Triggers: "call contract", "read contract", "interact", "write contract", "contract function" Read from and write to deployed smart contracts on 0G Chain using ethers v6. Covers view functions, state-changing transactions, event listening, and gas estimation.

  • Reads credentials

The file

We split this skill into the rules it gives an agent

11 rules. About 0 are common, 8 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈8

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Read from and write to deployed smart contracts on 0G Chain using ethers v6.

    line 12
  2. Distinctive:

    Create provider (no wallet needed for reads)

    line 26
  3. Distinctive:

    Create contract instance with ABI and address

    line 27
  4. Distinctive:

    Create provider and wallet

    line 32
  5. Distinctive:

    Create contract instance with wallet as signer

    line 33
  6. Distinctive:

    Wait for transaction confirmation

    line 35
  7. Distinctive:

    Use ethers v6 syntax throughout

    line 41
  8. Very specific:

    Use await contract.getAddress() (not contract.address)

    line 42
  9. Very specific:

    Wait for transaction confirmation with tx.wait()

    line 44
  10. Very specific:

    Use native BigInt (not BigNumber)

    line 45
  11. Distinctive:

    Use ethers v5 patterns

    line 49

≈ “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

Interact with 0G Chain Contracts

Metadata

  • Category: chain
  • SDK: ethers 6.13.1
  • Activation Triggers: "call contract", "read contract", "interact", "write contract", "contract function"

Purpose

Read from and write to deployed smart contracts on 0G Chain using ethers v6. Covers view functions, state-changing transactions, event listening, and gas estimation.

Prerequisites

  • Node.js >= 18
  • ethers 6.13.1 installed
  • Contract ABI and address
  • .env with PRIVATE_KEY, RPC_URL

Quick Workflow

Read (View Functions)

  1. Create provider (no wallet needed for reads)
  2. Create contract instance with ABI and address
  3. Call view function

Write (State Changes)

  1. Create provider and wallet
  2. Create contract instance with wallet as signer
  3. Call state-changing function
  4. Wait for transaction confirmation

Core Rules

ALWAYS

  • Use ethers v6 syntax throughout
  • Use await contract.getAddress() (not contract.address)
  • Use await contract.waitForDeployment() (not .deployed())
  • Wait for transaction confirmation with tx.wait()
  • Use native BigInt (not BigNumber)

NEVER

  • Use ethers v5 patterns
  • Skip transaction confirmation for important operations
  • Hardcode contract addresses in source (use .env)

Code Examples

Read Contract State

import { ethers } from 'ethers';
import 'dotenv/config';

const ABI = [
  'function getValue() view returns (uint256)',
  'function balanceOf(address) view returns (uint256)',
  'function owner() view returns (address)',
];

async function readContract() {
  const provider = new ethers.JsonRpcProvider(process.env.RPC_URL);
  const contract = new ethers.Contract(process.env.CONTRACT_ADDRESS!, ABI, provider);

  const value = await contract.getValue();
  console.log('Value:', value.toString());

  const balance = await contract.balanceOf(process.env.WALLET_ADDRESS!);
  console.log('Balance:', ethers.formatEther(balance));

  const owner = await contract.owner();
  console.log('Owner:', owner);
}

Write to Contract

async function writeContract() {
  const provider = new ethers.JsonRpcProvider(process.env.RPC_URL);
  const wallet = new ethers.Wallet(process.env.PRIVATE_KEY!, provider);

  const ABI = [
    'function setValue(uint256) external',
    'function transfer(address, uint256) external returns (bool)',
  ];
  const contract = new ethers.Contract(process.env.CONTRACT_ADDRESS!, ABI, wallet);

  // Simple write
  const tx = await contract.setValue(42);
  const receipt = await tx.wait();
  console.log('Tx hash:', receipt.hash);
  console.log('Gas used:', receipt.gasUsed.toString());

  // Write with value
  const tx2 = await contract.transfer('0xRecipient...', ethers.parseEther('1.0'));
  await tx2.wait();
}

Listen for Events

async function listenForEvents() {
  const provider = new ethers.JsonRpcProvider(process.env.RPC_URL);
  const ABI = [
    'event Transfer(address indexed from, address indexed to, uint256 amount)',
    'event ValueChanged(uint256 newValue)',
  ];
  const contract = new ethers.Contract(process.env.CONTRACT_ADDRESS!, ABI, provider);

  // Real-time listener
  contract.on('ValueChanged', (newValue) => {
    console.log('Value changed to:', newValue.toString());
  });

  contract.on('Transfer', (from, to, amount) => {
    console.log(`Transfer: ${from} → ${to}: ${ethers.formatEther(amount)}`);
  });

  // Query past events
  const filter = contract.filters.Transfer();
  const events = await contract.queryFilter(filter, -1000); // Last 1000 blocks
  console.log(`Found ${events.length} transfer events`);
}

Gas Estimation

async function estimateGas() {
  const provider = new ethers.JsonRpcProvider(process.env.RPC_URL);
  const wallet = new ethers.Wallet(process.env.PRIVATE_KEY!, provider);

  const ABI = ['function setValue(uint256) external'];
  const contract = new ethers.Contract(process.env.CONTRACT_ADDRESS!, ABI, wallet);

  const gasEstimate = await contract.setValue.estimateGas(42);
  console.log('Estimated gas:', gasEstimate.toString());

  // Execute with gas buffer
  const tx = await contract.setValue(42, {
    gasLimit: (gasEstimate * 120n) / 100n, // 20% buffer
  });
  await tx.wait();
}

Using Full ABI (from compilation)

import * as fs from 'fs';

async function interactWithArtifact() {
  const artifact = JSON.parse(
    fs.readFileSync('./artifacts/contracts/MyContract.sol/MyContract.json', 'utf8'),
  );

  const provider = new ethers.JsonRpcProvider(process.env.RPC_URL);
  const wallet = new ethers.Wallet(process.env.PRIVATE_KEY!, provider);
  const contract = new ethers.Contract(process.env.CONTRACT_ADDRESS!, artifact.abi, wallet);

  // Now use any function from the contract
  const value = await contract.getValue();
  console.log('Value:', value.toString());
}

ethers v6 Quick Reference

Operationv5 (DO NOT USE)v6 (CORRECT)
Parse etherethers.utils.parseEther("1")ethers.parseEther("1")
Format etherethers.utils.formatEther(x)ethers.formatEther(x)
Big numbersBigNumber.from(42)42n (native BigInt)
Get addresscontract.addressawait contract.getAddress()
Wait deployawait contract.deployed()await contract.waitForDeployment()
Providernew ethers.providers.JsonRpcProvidernew ethers.JsonRpcProvider

Anti-Patterns

// BAD: ethers v5 patterns
const value = ethers.utils.parseEther('1.0'); // v5!
const provider = new ethers.providers.JsonRpcProvider(url); // v5!
const big = BigNumber.from(42); // v5!

// BAD: Not waiting for confirmation
await contract.setValue(42); // Fire and forget — bad!

// BAD: Hardcoded addresses
const contract = new ethers.Contract('0x123...', abi, wallet);

Common Errors & Fixes

ErrorCauseFix
execution revertedRequire/revert conditionCheck function parameters
insufficient fundsWallet emptyFund wallet
nonce too lowPending txWait or set nonce manually
cannot estimate gasFunction will revertCheck args and contract state
call revert exceptionView function revertedCheck function exists and args

Related Skills

  • Deploy Contract — deploy contracts first
  • Scaffold Project — project setup
  • Storage + Chain — on-chain + storage

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.