I have a Hardhat script that queries the RIF token balance on Rootstock. However the RIF address is hardcoded in my script:
const rifTokenAddress = '0x2aCc95758f8b5F583470bA265Eb685a8f45fC9D5';
async function main() {
const erc20 = await ethers.getContractAt(
['function balanceOf(address owner) view returns (uint)'],
rifTokenAddress.toLowerCase(),
);
const walletAddress = (await ethers.getSigner(0)).address;
const rifBalance = await erc20.balanceOf(walletAddress);
console.log(ethers.utils.formatEther(rifBalance));
}
main();
Now I am using this command to run the script:
npx hardhat run scripts/balances.js --network rskmainnet
I would like to be able to specify a token address in the command line like this:
npx hardhat run scripts/balances.js --network rskmainnet --token 0x2d919f19D4892381d58EdEbEcA66D5642ceF1A1F
Is there a way to modify Hardhat script
so that it could read token address from
the command line,
similar to how I select the network with --network parameter?
For reference, this is my hardhat.config.js file:
require('@nomicfoundation/hardhat-toolbox');
const { mnemonic } = require('./.secret.json');
const accounts = {
mnemonic,
path: "m/44'/60'/0'/0",
};
module.exports = {
solidity: '0.8.9',
networks: {
hardhat: {},
rsktestnet: {
chainId: 31,
url: 'https://public-node.testnet.rsk.co/',
accounts,
},
rskmainnet: {
chainId: 30,
url: 'https://public-node.rsk.co/',
accounts,
},
},
};
One quick solution that I can think of is, put your npx hardhat command as a script in your package.json.
Now you can invoke this using the following command. Now pass your arg 'token'.
In your js code, you will get this as env variable.
IMO, hardhat does not accept custom args hence passing any args directly will lead to HH308 error. This may change in future though.
Note: This solution has been tested on NPM 7 only.
Cheers.