本页面使用机器翻译自英语,可能包含错误或不清楚的语言。如需最准确的信息,请参阅英文原文。由于更新频繁,部分内容可能与英文原文有出入。请加入我们在 Crowdin 上的努力,帮助我们改进本页面的翻译。 (Crowdin translation page, Contributing guide)
如何使用 Kaia SDK 发送 ERC-20 代币(USDT 示例)
概述
在本指南中,您将学习如何在 Kaia 区块链上使用 ethers-ext SDK 以 USDT 为例,以编程方式发送 ERC 20 代币。 Kaia SDK 是一系列库的集合,包括 ethers-ext、web3js-ext、viem-ext、web3j-ext 和 web3py-ext,可帮助开发人员在不同的编程环境中与 Kaia 节点交互。
本指南主要介绍如何使用 ethers-ext SDK 通过智能合约调用发送 USDT 代币。
先决条件
- 一个 MetaMask 账户(本例中使用的是一个开发账户)。
- 从 [水龙头] 获取测试 KAIA(https://faucet.kaia.io)
步骤 1:设置项目并安装 ethers-ext 和 ethers.js
mkdir send-usdt-kaiasdkcd send-usdt-kaiasdknpm init -y npm install --save @kaiachain/ethers-ext ethers@6 dotenv
步骤 2:设置提供程序和钱包实例
创建一个名为 index.js
的新文件,并将下面的代码粘贴到该文件中。
import { ethers } from "ethers";import { Wallet, JsonRpcProvider, parseKaiaUnits } from "@kaiachain/ethers-ext/v6";import "dotenv/config";const senderPriv = process.env.USDT_SENDER;const recipientAddress = "PASTE_RECIPIENT_ADDRESS"const amount = parseKaiaUnits("0.01", 6);const provider = new JsonRpcProvider("https://public-en.node.kaia.io");const wallet = new Wallet(senderPriv, provider);
步骤 3:创建 USDT 合约实例
const USDT_CONTRACT_ABI = [{ "inputs": [ { "internalType": "address", "name": "recipient", "type": "address" }, { "internalType": "uint256", "name": "amount", "type": "uint256" } ], "name": "transfer", "outputs": [ { "internalType": "bool", "name": "", "type": "bool" } ], "stateMutability": "nonpayable", "type": "function"},{ "inputs": [ { "internalType": "address", "name": "account", "type": "address" } ], "name": "balanceOf", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "stateMutability": "view", "type": "function"}]const USDT_CONTRACT_ADDRESS = "0xd077a400968890eacc75cdc901f0356c943e4fdb";async function main() { const USDT_CONTRACT = new ethers.Contract(USDT_CONTRACT_ADDRESS, USDT_CONTRACT_ABI, wallet);}
步骤 4:发送令牌
async function main() { const USDT_CONTRACT = new ethers.Contract(USDT_CONTRACT_ADDRESS, USDT_CONTRACT_ABI, wallet); console.log("balance of recipient before", (await USDT_CONTRACT.balanceOf(recipientAddress)).toString()); const sentTx = await USDT_CONTRACT.transfer(recipientAddress, amount); const receipt = await sentTx.wait(); console.log("receipt", receipt.hash); console.log("balance of recipient after", (await USDT_CONTRACT.balanceOf(recipientAddress)).toString());}main();
完整代码
import { ethers } from "ethers";import { Wallet, JsonRpcProvider, parseKaiaUnits } from "@kaiachain/ethers-ext/v6";import "dotenv/config";const senderPriv = process.env.USDT_SENDER;const recipientAddress = "PASTE RECIPIENT ADDRESS"const amount = parseKaiaUnits("0.01", 6);const provider = new JsonRpcProvider("https://public-en.node.kaia.io");const wallet = new Wallet(senderPriv, provider);/* Get USDT ABI here: https://kaiascan.io/address/0xd077a400968890eacc75cdc901f0356c943e4fdb?tabId=contract&page=1*/const USDT_CONTRACT_ABI = [ { "inputs": [ { "internalType": "address", "name": "recipient", "type": "address" }, { "internalType": "uint256", "name": "amount", "type": "uint256" } ], "name": "transfer", "outputs": [ { "internalType": "bool", "name": "", "type": "bool" } ], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "account", "type": "address" } ], "name": "balanceOf", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "stateMutability": "view", "type": "function" }]const USDT_CONTRACT_ADDRESS = "0xd077a400968890eacc75cdc901f0356c943e4fdb";async function main() { const USDT_CONTRACT = new ethers.Contract(USDT_CONTRACT_ADDRESS, USDT_CONTRACT_ABI, wallet); console.log("balance of recipient before", (await USDT_CONTRACT.balanceOf(recipientAddress)).toString()); const sentTx = await USDT_CONTRACT.transfer(recipientAddress, amount); const receipt = await sentTx.wait(); console.log("receipt", receipt.hash); console.log("balance of recipient after", (await USDT_CONTRACT.balanceOf(recipientAddress)).toString());}main();
在终端运行 node index.js
查看事务执行情况。