繁体   English   中英

如何使用 Ether.js 传输 ERC20 代币?

[英]How do I transfer ERC20 tokens using Ether.js?

我正在尝试在 Hardhat 中测试我的智能合约,但为此我首先需要向我的合约发送一些 ERC20 代币(对于此测试,我使用的是 USDC)。

在我的测试中,我模拟了一只 USDC 鲸鱼,但我如何实际将 USDC 转移到我的合约中?

it("USDC test", async function () {
    const testContract =
        await ethers.getContractFactory("TestContract")
            .then(contract => contract.deploy());
    await testContract.deployed();

    // Impersonate USDC whale
    await network.provider.request({
        method: "hardhat_impersonateAccount",
        params: [USDC_WHALE_ADDRESS],
    });
    const usdcWhale = await ethers.provider.getSigner(USDC_WHALE_ADDRESS);

    // Need to transfer USDC from usdcWhale to testContract
});

要转移 ERC20 代币,您首先需要部署代币的主合约。 您需要代币合约地址以及ERC20 ABI

const USDC_ADDRESS = "0x6262998ced04146fa42253a5c0af90ca02dfd2a3";
const ERC20ABI = require('./ERC20ABI.json');

const provider = ethers.provider;
const USDC = new ethers.Contract(USDC_ADDRESS, ERC20ABI, provider);

然后从usdcWhale转移 100 USDC 到testContract做:

await USDC.connect(usdcWhale).transfer(testContract.address, 100);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM