Skip to main content

Web3 Libraries

Overview​

Circle Layer supports various Web3 libraries for interacting with the testnet. All standard Ethereum Web3 libraries work with Circle Layer using the same patterns.

Circle Layer Blockchain is compatible with Ethereum's ecosystem,support all Ethereum's RPC API and SDK.

RPC Compatibility​

RPC Method List

Example:

curl -s -H 'content-type:application/json' -d '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":67}' https://testnet-rpc.circlelayer.com

Network Configuration​

Circle Layer Testnet​

Supported Libraries​

1. ethers.js​

import { ethers } from 'ethers';

// HTTP Provider
const provider = new ethers.providers.JsonRpcProvider('https://testnet-rpc.circlelayer.com');

// WebSocket Provider
const wsProvider = new ethers.providers.WebSocketProvider('wss://testnet-rpc.circlelayer.com');

// Wallet setup
const wallet = new ethers.Wallet(privateKey, provider);

// Check CLAYER balance
const balance = await wallet.getBalance();
console.log('Balance:', ethers.utils.formatEther(balance), 'CLAYER');

2. web3.js​

const Web3 = require('web3')

// HTTP Provider
const web3 = new Web3('https://testnet-rpc.circlelayer.com');

// WebSocket Provider
const webSocketWeb3 = new Web3('wss://testnet-rpc.circlelayer.com');

// Get chain info
async function getChainId() {
const web3 = new Web3('https://testnet-rpc.circlelayer.com')
let chainId = await web3.eth.getChainId()
console.log(`chain id: ${chainId}`)
return chainId
}

// Generate account
function generateAccount() {
const Web3Accounts = require('web3-eth-accounts')

let account = new Web3Accounts().create()
//do not do this on prd env
console.log(`account generated. address: ${account.address}, private key: ${account.privateKey}`)
return account
}

// Build transaction
async function transfer(fromAccount, to, value){
const web3 = new Web3('https://testnet-rpc.circlelayer.com')
let chainId = await web3.eth.getChainId()
let nonce = await web3.eth.getTransactionCount(fromAccount.address)
let gasPrice = await web3.eth.getGasPrice()

let unsigned = {
from: fromAccount.address,
to,
value: web3.utils.numberToHex(web3.utils.toWei(value, 'ether')),
gasPrice,
nonce,
chainId,
}

unsigned.gas = await web3.eth.estimateGas(unsigned)

let signed = await fromAccount.signTransaction(unsigned)
return signed
}

// Check CLAYER balance
const balance = await web3.eth.getBalance(account.address);
console.log('Balance:', web3.utils.fromWei(balance, 'ether'), 'CLAYER');

3. Web3.py​

from web3 import Web3

# HTTP Provider
w3 = Web3(Web3.HTTPProvider('https://testnet-rpc.circlelayer.com'))

# Account setup
account = w3.eth.account.from_key(private_key)

# Check CLAYER balance
balance = w3.eth.get_balance(account.address)
print(f'Balance: {w3.from_wei(balance, "ether")} CLAYER')

Circle Layer Specific Features​

Network Information​

// Get network information
const network = await provider.getNetwork();
console.log('Chain ID:', network.chainId); // 28525
console.log('Name:', network.name);

// Current gas price (in CLAYER)
const gasPrice = await provider.getGasPrice();
console.log('Gas Price:', ethers.utils.formatUnits(gasPrice, 'gwei'), 'Gwei');

Transaction with CLAYER​

// Send CLAYER transaction
const transaction = {
to: '0x...',
value: ethers.utils.parseEther('1'), // 1 CLAYER
gasPrice: ethers.utils.parseUnits('21', 'gwei'), // 0.000021 CLAYER
gasLimit: 21000
};

const tx = await wallet.sendTransaction(transaction);
const receipt = await tx.wait();
console.log('Transaction confirmed:', receipt.transactionHash);

Smart Contract Interaction​

// Example contract interaction
const contractAddress = '0xfCb4Ce5953dE22cbF04d015df88a3a9895E86bEB';
const contract = new ethers.Contract(contractAddress, abi, wallet);

// Call contract method
const result = await contract.someMethod();

// Send transaction to contract (using CLAYER for gas)
const tx = await contract.someWriteMethod(params, {
gasPrice: ethers.utils.parseUnits('21', 'gwei'), // CLAYER gas price
gasLimit: 100000
});

Best Practices​

1. Network Configuration​

// Always verify network
const network = await provider.getNetwork();
if (network.chainId !== 28525) {
throw new Error('Please connect to Circle Layer Testnet');
}

2. Gas Management​

// Use appropriate gas settings for Circle Layer
const gasPrice = ethers.utils.parseUnits('21', 'gwei'); // 0.000021 CLAYER
const gasLimit = await contract.estimateGas.methodName(params);

3. Error Handling​

try {
const tx = await contract.someMethod(params);
const receipt = await tx.wait();
} catch (error) {
if (error.code === 'INSUFFICIENT_FUNDS') {
console.error('Insufficient CLAYER balance');
} else {
console.error('Transaction failed:', error.message);
}
}

4. Environment Setup​

// Use environment variables
const provider = new ethers.providers.JsonRpcProvider(
process.env.CIRCLE_LAYER_RPC || 'https://testnet-rpc.circlelayer.com'
);

Integration Examples​

React Integration​

import { ethers } from 'ethers';
import { useState, useEffect } from 'react';

function useCircleLayer() {
const [provider, setProvider] = useState(null);
const [balance, setBalance] = useState('0');

useEffect(() => {
const initProvider = async () => {
if (window.ethereum) {
const web3Provider = new ethers.providers.Web3Provider(window.ethereum);
setProvider(web3Provider);

// Add Circle Layer Testnet to MetaMask if needed
await window.ethereum.request({
method: 'wallet_addEthereumChain',
params: [{
chainId: '0x6F75', // 28525 in hex
chainName: 'Circle Layer Testnet',
nativeCurrency: {
name: 'CLAYER',
symbol: 'CLAYER',
decimals: 18
},
rpcUrls: ['https://testnet-rpc.circlelayer.com'],
blockExplorerUrls: ['https://explorer-testnet.circlelayer.com/']
}]
});
}
};

initProvider();
}, []);

return { provider, balance };
}

Node.js Backend​

const { ethers } = require('ethers');

class CircleLayerService {
constructor() {
this.provider = new ethers.providers.JsonRpcProvider('https://testnet-rpc.circlelayer.com');
this.wallet = new ethers.Wallet(process.env.PRIVATE_KEY, this.provider);
}

async sendCLAYER(to, amount) {
const tx = await this.wallet.sendTransaction({
to,
value: ethers.utils.parseEther(amount),
gasPrice: ethers.utils.parseUnits('21', 'gwei')
});

return await tx.wait();
}
}

Resources​

Example Contract​

Additional Guides​