【问题标题】:How do I transfer ERC20 tokens using Ether.js?如何使用 Ether.js 传输 ERC20 代币?
【发布时间】:2022-12-18 14:22:19
【问题描述】:

我正在尝试在 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
});

【问题讨论】:

    标签: ethereum blockchain ethers.js hardhat erc20


    【解决方案1】:

    要转移 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);
    

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

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

    【讨论】:

      猜你喜欢
      • 2018-04-10
      • 2021-10-08
      • 2020-01-23
      • 2019-02-12
      • 2019-02-09
      • 2018-04-04
      • 2021-08-31
      • 2019-02-01
      • 2022-01-24
      相关资源
      最近更新 更多