第22章 从 Web3.js 迁移到 Viem
const client = createWalletClient({
account,
chain: mainnet,
transport: http(),
});
消息签名 更新消息签名的处理方式,以下是 web3.js 的示例: const signature = web3.eth.accounts.sign('Some data', privateKey).signature;
console.log(signature);
使用 Viem 进行消息签名时,可以调用 signMessage() 方法。 import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
const walletClient = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum!),
});
const [account] = await walletClient.getAddresses();
const signature = await walletClient.signMessage({
account,
message: 'Some data',
});
交易 迁移交易发送代码时,需要更新交易的签名和发送方式。 const tx = await web3.eth.sendTransaction({
from: account,
to: '0x92d3267215Ec56542b985473E73C8417403B15ac',
value: web3.utils.toWei('0.001', 'ether'),
});
Viem 提供了 sendTransaction() 函数,可通过 walletClient 使用。 import { createWalletClient, custom, parseEther } from 'viem'
import { mainnet } from 'viem/chains'
const walletClient = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum!),
});
const [account] = await walletClient.getAddresses();
const hash = await walletClient.sendTransaction({
account,
to: '0x92d3267215Ec56542b985473E73C8417403B15ac',
value: parseEther('0.001')
});
合约 部署合约 迁移合约部署代码时,需要更新 Web3.js 的部署和发送模式: // use existing web3 instance connected with provider
const contract = new web3.eth.Contract(abi);
const deployTx = await contract
.deploy({
data: bytecode,
arguments: ['constructor param'],
})
.send({
from: account,
gas: '1000000',
});
console.log(deployTx.options.address);
Viem 提供了 deployContract() 函数用于部署合约。 //import { deployContract } from 'viem'
import { createWalletClient, custom } from 'viem';
import { mainnet } from 'viem/chains';
const walletClient = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum),
});
const hash = await walletClient.deployContract({
abi,
account, //给定的账户
args: ['构造器参数'],
bytecode: bytecode,
});
合约方法调用
迁移合约方法调用时,需要将 Web3.js 的 contract.methods.someFunction().call() 更新为:
const contract = new web3.eth.Contract(ABI, CONTRACT_ADDRESS);
const result = await contract.methods.someFunction().call();
console.log(result);
在 Viem 中,可以使用 readContract() 函数进行方法调用。
const data = await publicClient.readContract({
address: '0x92d3267215Ec56542b985473E73C8417403B15ac',
abi: wagmiAbi,
functionName: 'tokenTotalSupply',
});
合约事件
迁移事件处理代码时,需要将 Web3.js 的事件代码更新为:
const event = contract.events.SomeEvent({ fromBlock: 0 });
event.on('data', resolve);
event.on('error', reject);
在 Viem 中,可以使用 watchContractEvent() 函数。
const unwatch = publicClient.watchContractEvent({
address: '0x92d3267215Ec56542b985473E73C8417403B15ac',
abi: wagmiAbi,
eventName: 'Transfer',
args: { from: '0x34d3267215Ec56542b985473E73C8417403B1533' },
onLogs: logs => func(logs),
});
工具方法
哈希计算
迁移计算 Keccak-256 哈希值的代码时,需要将 Web3.js 的工具方法更新为:
// keccak256 方法在 web3.js 中支持更广泛的输入类型
const hash = web3.utils.keccak256('hello world');
在 Viem 中,可以使用 keccak256() 函数计算 keccak256。
import { keccak256 , toHex } from 'viem'
keccak256(toHex('hello world'));