测试客户端
测试客户端是一个用于测试JSON-RPC API方法可访问性的接口,可通过本地以太坊测试节点,例如Anvil 或 Hardhat通过测试操作进行挖块、伪装账户、设置费用等。
createTestClient
函数使用给定的 Transport 来设置一个测试RPC客户端。
导入
ts
import { createTestClient } from 'viem'
import { createTestClient } from 'viem'
用例
使用想要的Chain、Transport(例如http
)和mode(例如"anvil"
)初始化客户端 )。
ts
import { createTestClient, http } from 'viem'
import { foundry } from 'viem/chains'
const client = createTestClient({
chain: foundry,
mode: 'anvil',
transport: http(),
})
import { createTestClient, http } from 'viem'
import { foundry } from 'viem/chains'
const client = createTestClient({
chain: foundry,
mode: 'anvil',
transport: http(),
})
现在可以使用 测试操作了:
ts
const mine = await client.mine({ blocks: 1 })
const mine = await client.mine({ blocks: 1 })
使用公共/钱包操作来拓展测试客户端
当与以太坊测试节点交互时,可能还会发现自己想要使用相同的链和传输与公共操作和钱包操作进行交互。 可以使用以下操作扩展测试客户端,而不是创建三个不同的客户端:
ts
import { createTestClient, http, publicActions, walletActions } from 'viem'
import { foundry } from 'viem/chains'
const client = createTestClient({
chain: foundry,
mode: 'anvil',
transport: http(),
})
.extend(publicActions)
.extend(walletActions)
const blockNumber = await client.getBlockNumber() // Public Action
const hash = await client.sendTransaction({ ... }) // Wallet Action
const mine = await client.mine({ blocks: 1 }) // Test Action
import { createTestClient, http, publicActions, walletActions } from 'viem'
import { foundry } from 'viem/chains'
const client = createTestClient({
chain: foundry,
mode: 'anvil',
transport: http(),
})
.extend(publicActions)
.extend(walletActions)
const blockNumber = await client.getBlockNumber() // Public Action
const hash = await client.sendTransaction({ ... }) // Wallet Action
const mine = await client.mine({ blocks: 1 }) // Test Action
Parameters
mode
- Type:
"anvil" | "hardhat" | "ganache"
测试客户端的模式。
ts
const client = createTestClient({
chain: foundry,
mode: 'anvil',
transport: http(),
})
const client = createTestClient({
chain: foundry,
mode: 'anvil',
transport: http(),
})
transport
- Type: Transport
测试客户端的Transport。
ts
const client = createTestClient({
chain: foundry,
mode: 'anvil',
transport: http(),
})
const client = createTestClient({
chain: foundry,
mode: 'anvil',
transport: http(),
})
account(可选的)
- Type:
Account | Address
供客户使用的帐户。 这将用于需要account
作为参数的操作。
account的值可以是JSON-RPC账户或者本地账户(私钥,助记词等)。
ts
import { privateKeyToAccount } from 'viem/accounts'
const client = createTestClient({
account: privateKeyToAccount('0x...')
chain: foundry,
mode: 'anvil',
transport: http(),
})
import { privateKeyToAccount } from 'viem/accounts'
const client = createTestClient({
account: privateKeyToAccount('0x...')
chain: foundry,
mode: 'anvil',
transport: http(),
})
chain(可选的)
- Type: Chain
测试客户端中的Chain。
ts
const client = createTestClient({
chain: foundry,
mode: 'anvil',
transport: http(),
})
const client = createTestClient({
chain: foundry,
mode: 'anvil',
transport: http(),
})
cacheTime(可选的)
- Type:
number
- Default:
client.pollingInterval
缓存数据在内存中保留的时间(以毫秒为单位)。
ts
const client = createTestClient({
cacheTime: 10_000,
chain: foundry,
mode: 'anvil',
transport: http(),
})
const client = createTestClient({
cacheTime: 10_000,
chain: foundry,
mode: 'anvil',
transport: http(),
})
name(可选的)
- Type:
string
- Default:
"Test Client"
客户端的名字。
ts
const client = createTestClient({
chain: foundry,
mode: 'anvil',
name: 'Anvil Client',
transport: http(),
})
const client = createTestClient({
chain: foundry,
mode: 'anvil',
name: 'Anvil Client',
transport: http(),
})
pollingInterval(可选的)
- Type:
number
- Default:
4_000
轮询启用的操作的间隔时间(毫秒)
ts
const client = createTestClient({
chain: foundry,
mode: 'anvil',
pollingInterval: 10_000,
transport: http(),
})
const client = createTestClient({
chain: foundry,
mode: 'anvil',
pollingInterval: 10_000,
transport: http(),
})