2

I've been learning about ethereum for a short time, besides my English could be better; if I'm wrong about something, please correct me. 😁 I am trying to test the interaction of different contract functions, but for that, I need a specific ERC20 token; how can I get the particular token in my forked environment?

So far, I have tried to use the impersonatedAccounts method of hardhat; here is an example of the script I tried to use:

const hre = require("hardhat");
const ethers = hre.ethers;

async function main() {
  const erc20Address = "0x...";
  const impersonatedAddress = "0x...";
  const recipientAddress = "0x...";
  const tokenAmount = ethers.utils.parseUnits("100", 18); 
  const abi = ["abi here"]
  const signer = await ethers.getImpersonatedSigner(impersonatedAddress);

 
  const erc20 = new ethers.Contract(erc20Address, abi, signer);

  
  const initialBalance = await erc20.balanceOf(impersonatedAddress);
  console.log(`Initial balance of impersonated account: ${ethers.utils.formatUnits(initialBalance)}`);


  const tx = await erc20.transfer(recipientAddress, tokenAmount);
  console.log(`Transaction hash: ${tx.hash}`);
 
  await tx.wait();

  
  const finalBalance = await erc20.balanceOf(impersonatedAddress);
  console.log(`Final balance of impersonated account: ${ethers.utils.formatUnits(finalBalance)}`);


  const recipientBalance = await erc20.balanceOf(recipientAddress);
  console.log(`Final balance of recipient account: ${ethers.utils.formatUnits(recipientBalance)}`);
}

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

I would appreciate any help you can give me.

Even when establishing a

tx.wait()

right after transfer, the transaction still shows as null I am not sure if it could be a problem with my environment configuration, here is the configuration of my hardaht.conf:

require("@nomiclabs/hardhat-ethers");
require("hardhat-deal");
/**
 * @type import('hardhat/config').HardhatUserConfig
 */
module.exports = {
  solidity: "0.8.18",
  networks: {
    hardhat: {
      chainId: 31337,
      forking: {
        enabled: true,
        url: "https://mainnet.infura.io/v3/<api here>",
      }
    }
  }
};

I update the question, when I mean that the transaction is null, I mean that although the scripts run correctly, the balances are never updated, the transaction seems to succeed correctly by scanning the transaction hash, but it never happens, the balance is NEVER updated.

I have tried to change the provider using the JSONRPC pointing to my local address, instead of hre, this does not work either. I have run the scripts with node, and run them with npx hardhat run --network hardhat, this also does not solve the problem. I am using a Kali Linux, as operating system. If someone needs some more information to clarify the question, please don't hesitate to ask and try to give you as much information as possible.

2
  • When I use the script it seems to run correctly but the transaction hash is null Other than that there doesn't seem to be any obvious errors.
    – 4nibhal
    Mar 26, 2023 at 20:18
  • Please define the transaction still shows as null? What's exactly null? Also, more questions: does impersonatedAddress have enough ether on mainnet to pay for the transaction? Mar 27, 2023 at 22:53

1 Answer 1

0

If tx.hash returns null, the transaction hasn't been successfully mined yet. Only after the transaction is successfully mined will the transaction hash be available.

You can wait for the transaction to be mined by adding a call to await tx.wait() after sending the transaction. This will wait until the transaction is included in a block and then return the receipt. Once the transaction is successfully mined, you can get the hash by accessing tx.hash. Here is an updated version of your code with the await tx.wait() added:

const recipientAddress = "0x...";
const tokenAmount = ethers.utils.parseUnits("100", 18); 
const abi = ["abi here"]
const signer = await ethers.getImpersonatedSigner(impersonatedAddress);

const erc20 = new ethers.Contract(erc20Address, abi, signer);

const initialBalance = await erc20.balanceOf(impersonatedAddress);
console.log(`Initial balance of impersonated account: ${ethers.utils.formatUnits(initialBalance)}`);

const tx = await erc20.transfer(recipientAddress, tokenAmount);
await tx.wait();
console.log(`Transaction hash: ${tx.hash}`);

const finalBalance = await erc20.balanceOf(impersonatedAddress);
console.log(`Final balance of impersonated account: ${ethers.utils.formatUnits(finalBalance)}`);

const recipientBalance = await erc20.balanceOf(recipientAddress);
console.log(`Final balance of recipient account: ${ethers.utils.formatUnits(recipientBalance)}`);

I hope this helps!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.