Skip to main content

Deploy Smart Contracts on 0G Chain

Deploy smart contracts on 0G Chain - an EVM-compatible blockchain with built-in AI capabilities.

Why Deploy on 0G Chain?

⚡ Performance Benefits

  • 2,500 TPS: Higher throughput than Ethereum
  • Low Fees: Fraction of mainnet costs
  • Fast Finality: 1-2 second block times

🔧 Latest EVM Compatibility

  • Pectra & Cancun-Deneb Support: Leverage newest Ethereum capabilities
  • Future-Ready: Architecture designed for quick integration of upcoming EVM upgrades
  • Familiar Tools: Use Hardhat, Foundry, Remix
  • No Learning Curve: Deploy like any EVM chain

Prerequisites

Before deploying contracts on 0G Chain, ensure you have:

  • Node.js 16+ installed (for Hardhat/Truffle)
  • Rust installed (for Foundry)
  • A wallet with testnet OG tokens (get from faucet)
  • Basic Solidity knowledge

Steps to Deploy Your Contract

Step 1: Prepare Your Smart Contract Code

Write your contract code as you would for any Ethereum-compatible blockchain, ensuring that it meets the requirements for your specific use case.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

contract MyToken {
mapping(address => uint256) public balances;
uint256 public totalSupply;

constructor(uint256 _initialSupply) {
totalSupply = _initialSupply;
balances[msg.sender] = _initialSupply;
}

function transfer(address to, uint256 amount) public returns (bool) {
require(balances[msg.sender] >= amount, "Insufficient balance");
balances[msg.sender] -= amount;
balances[to] += amount;
return true;
}
}

Step 2: Compile Your Smart Contract

Use solc or another compatible Solidity compiler to compile your smart contract.

Important: When compiling, specify --evm-version cancun to ensure compatibility with the latest EVM upgrades supported by 0G Chain.

Using solc directly:

solc --evm-version cancun --bin --abi MyToken.sol

Using Hardhat:

// hardhat.config.js
module.exports = {
solidity: {
version: "0.8.19",
settings: {
evmVersion: "cancun",
optimizer: {
enabled: true,
runs: 200
}
}
}
};

Using Foundry:

# foundry.toml
[profile.default]
evm_version = "cancun"

This step will generate the binary and ABI (Application Binary Interface) for your contract.

Step 3: Deploy the Contract on 0G Chain

Once compiled, you can use your preferred Ethereum-compatible deployment tools, such as web3.js, ethers.js, or hardhat, to deploy the contract on 0G Chain.

Configure Network Connection:

// For Hardhat
networks: {
"0g-testnet": {
url: "https://evmrpc-testnet.0g.ai",
chainId: 16601,
accounts: [process.env.PRIVATE_KEY]
}
}

// For Foundry
[rpc_endpoints]
0g_testnet = "https://evmrpc-testnet.0g.ai"

Deploy Using Your Preferred Tool:

Hardhat Deployment
// scripts/deploy.js
async function main() {
const MyToken = await ethers.getContractFactory("MyToken");
const token = await MyToken.deploy(1000000); // 1M initial supply
await token.deployed();

console.log("Token deployed to:", token.address);
}

main().catch((error) => {
console.error(error);
process.exitCode = 1;
});

Run: npx hardhat run scripts/deploy.js --network 0g-testnet

Foundry Deployment
forge create --rpc-url https://evmrpc-testnet.0g.ai \
--private-key $PRIVATE_KEY \
--evm-version cancun \
src/MyToken.sol:MyToken \
--constructor-args 1000000
Truffle Deployment
// migrations/2_deploy_token.js
module.exports = function(deployer) {
deployer.deploy(MyToken, 1000000);
};

Run: truffle migrate --network 0g-testnet

Follow the same deployment steps as you would on Ethereum, using your 0G Chain node or RPC endpoint.

For complete working examples using different frameworks, check out the official deployment scripts repository: 🔗 0G Deployment Scripts

Step 4: Verify Deployment Results on 0G Chain Scan

After deployment, you can view the transaction results and verify the contract on 0G Chain Scan, 0G Chain's block explorer.

You can also verify the contract's code and ABI for public transparency.

Using 0G Precompiles

0G Chain includes special precompiled contracts for advanced features:

Available Precompiles

PrecompileAddressPurpose
DASigners0x...1000Data availability signatures
WrappedOGBase0x...1002Wrapped OG token operations

Troubleshooting

Transaction failing with "invalid opcode"?

If you're using newer experimental opcodes from unreleased Ethereum upgrades and see "invalid opcode" errors, consider:

  • Use --evm-version cancun in your compiler settings
  • Downgrade to an earlier Solidity compiler version (e.g., from 0.8.26 to 0.8.19)
Can't connect to RPC?

Try alternative endpoints:

What's Next?


Need help? Join our Discord for developer support.