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.
the transaction still shows as null
? What's exactly null? Also, more questions: doesimpersonatedAddress
have enough ether on mainnet to pay for the transaction?