Through BlockChain RPC to call Ethereum contract
Get RPC Address
First, you need to visit https://zan.top/service/apikeys to obtain the RPC address:
You will need to log in to ZAN. ZAN will automatically create a free Access Key for you. For most other Blockchain RPC service providers, the logic is similar—you first need to obtain an address, which serves as the Endpoint for calling the RPC service.
Access via Command Line
You can use the following code in the Shell command line to test the RPC connection:
curl --location 'https://api.zan.top/node/v1/eth/mainnet/${YOUR_ACCEEE_KEY}' \
--header 'Content-Type: application/json' \
--data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'
Note that ${YOUR_ACCEEE_KEY}
should be replaced with your own Key. You can also directly run the code provided on the ZAN website:
You will then see output similar to the following:
{"result":"0x15a0d89","id":1,"jsonrpc":"2.0"}
This indicates that the RPC call was successful.
Call Contracts via JS SDK
You can use SDKs like Viem or Ethers to interact with contracts. For frontend development, especially with React, you can also use SDKs and component libraries like wagmi and Ant Design Web3.
For example, with Viem, you can initialize the SDK and call the getBlockNumber
method as follows:
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http('https://api.zan.top/node/v1/eth/mainnet/${YOUR_ACCEEE_KEY}')
})
const blockNumber = await publicClient.getBlockNumber()
To interact with contracts, you need to use the SDK's readContract
, simulateContract
, and writeContract
methods.
The readContract
method in the Viem SDK essentially calls the eth_call
method in the Ethereum JSON-RPC interface. It does not consume Gas and does not write to the blockchain, making it suitable for read-only contract calls. The writeContract
method calls eth_sendTransaction
for contract methods that involve write operations. The simulateContract
method in Viem simulates write operations by calling eth_call
+ eth_estimateGas
.
For writeContract
, signing the transaction is required, so you need to initialize it as follows:
import { wagmiAbi } from './abi'
import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
const walletClient = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum!)
})
const publicClient = createPublicClient({
chain: mainnet,
transport: http('https://api.zan.top/node/v1/eth/mainnet/${YOUR_ACCEEE_KEY}')
})
const { request } = await publicClient.simulateContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
args: [69420],
account
})
await walletClient.writeContract(request)
Configure Wallet RPC
Wallets also need to interact with the blockchain. You can configure a custom network in your wallet to use a custom RPC service. Below is an example using MetaMask:
First, click on the network icon in the top-left corner of the wallet:
You can then edit an existing network or add a custom network:
For example, you can replace the Ethereum RPC with ZAN's:
This way, when transactions or requests are initiated within the wallet, ZAN's RPC service will be used. This is particularly useful for users with specific speed and performance requirements. Additionally, for chains not natively supported by the wallet, this method can also be used to add them.