Skip to main content
edited tags
Link
Source Link

Cannot use transfer method of ERC20 in map function

I'm trying to iterate through receiver array to send ERC20 token in bulk.

I tried to debug with console.log and seems like the problem is line const tx = await contract.transfer(receiver[i], ammount)

Then I tried running the script outside of the map (as comment) and it's work.

So it must be something with map function (I even tried to console.log(addr === receiver[0]) and it returned true)

Zintea.sol

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract Zintea is ERC20 {
    uint constant _initial_supply = 1000 * (10**18);
    
    constructor() ERC20("Zintea", "ZIN"){
        _mint(msg.sender, _initial_supply);
    }

}

transfer.js

const { ethers } = require('hardhat');

require('dotenv').config();

const CONTRACT_ADDRESS = "0x0e6e60EA3D66421dfBCfB17a6F7f5844059c4948"
const receiver = ["0xAf29CeBfb433925A114f53dE2f347fdD50393443", "0x98Ed91465dD68fBeC574805eC9B6da63eeA380C0"]

async function main() {
    const [signer] = await ethers.getSigners();
    const contract = await ethers.getContractAt("Zintea", CONTRACT_ADDRESS, signer);
    console.log("Balance of sender: ", await contract.balanceOf(signer.getAddress()) / (10**18), "$ZIN" )

    await receiver.map(async (addr) => {
        console.log("Transfering to ", addr)
        const ammount = ethers.utils.parseUnits("10", 18)
        const tx = await contract.transfer(addr, ammount)
        await tx.wait()
        console.log(tx)
        console.log(`Transfered ${10} $ZIN to ${addr} `)
    })

    // Worked
    // console.log("Transfering to ", receiver[0])
    // const ammount = ethers.utils.parseUnits("1", 18)
    // const transfered = await contract.transfer(receiver[1], ammount );
    // await transfered.wait();
    // console.log(`Transfered to ${receiver[0]} `);
    
    console.log("Balance of sender: ", await contract.balanceOf(signer.getAddress()) / (10**18), "$ZIN" )
}

main()
.then(() => process.exit(0))
.catch((error) => {
  console.error(error);
  process.exit(1);
});

Console

Balance of sender:  974 $ZIN
Transfering to  0xAf29CeBfb433925A114f53dE2f347fdD50393443
Transfering to  0x98Ed91465dD68fBeC574805eC9B6da63eeA380C0
Balance of sender:  974 $ZIN