Using Web3 Libraries

If you’re building an application, web3 libraries provide a higher-level interface for interacting with Conduit RPC endpoints. They handle request formatting, response parsing, and type safety so you don’t have to work with raw JSON-RPC directly.

Replace [YOUR_RPC_URL] and [YOUR_API_KEY] with your values from the Conduit App.

eth_blockNumber

Get the latest block number.

1import { createPublicClient, http } from 'viem'
2
3const client = createPublicClient({
4 transport: http('https://[YOUR_RPC_URL]/[YOUR_API_KEY]'),
5})
6
7const blockNumber = await client.getBlockNumber()
8console.log(blockNumber)

eth_getBalance

Get the ETH balance of an address.

1const balance = await client.getBalance({
2 address: '0xYOUR_ADDRESS',
3})
4console.log(balance)

eth_call

Call a contract function without sending a transaction.

1const result = await client.call({
2 to: '0xCONTRACT_ADDRESS',
3 data: '0xCALLDATA',
4})
5console.log(result.data)

eth_getLogs

Fetch logs matching a filter. See Throughput for block range and event count limits.

1const logs = await client.getLogs({
2 address: '0xCONTRACT_ADDRESS',
3 fromBlock: 0n,
4 toBlock: 'latest',
5})
6console.log(logs)

eth_sendRawTransaction

Sign and broadcast a transaction.

1import { createWalletClient, http } from 'viem'
2import { privateKeyToAccount } from 'viem/accounts'
3
4const account = privateKeyToAccount('0xYOUR_PRIVATE_KEY')
5const walletClient = createWalletClient({
6 account,
7 transport: http('https://[YOUR_RPC_URL]/[YOUR_API_KEY]'),
8})
9
10const hash = await walletClient.sendTransaction({
11 to: '0xRECIPIENT_ADDRESS',
12 value: 1000000000000000n,
13})
14console.log(hash)

eth_subscribe (WebSocket)

Subscribe to new block headers in real time. This requires a WebSocket connection. Please see WebSockets for connection management guidance.

1import { createPublicClient, webSocket } from 'viem'
2
3const client = createPublicClient({
4 transport: webSocket('wss://[YOUR_RPC_URL]/[YOUR_API_KEY]'),
5})
6
7const unwatch = client.watchBlocks({
8 onBlock: (block) => console.log(block),
9})