Skip to main content

Balance.js

const { ethers } = require("ethers");
const dotenv = require("dotenv");

const chains = require("../constants/chains");
/* Get contract addresses from the file we generated by running the deploy script */
const {
AVAX_TOKEN_ADDRESS,
AVAX_BRIDGE_ADDRESS,
} = require("../variables/contractAddresses");
/* Get ABIs of the contracts directly from the artifact folder created by hardhat after each compilation */
const AVAX_TOKEN_ABI =
require("../artifacts/contracts/Token/AvaxToken.sol/AvaxToken").abi;
dotenv.config();

/*
Balance script that allows us to check balances on both chains
On Avax it prints out the ERC20 balance of the user and the bridge.
(We are printing out the balance of the bridge because as users lock tokens bridge's balance of the ERC20 will increase)
On Subnet it prints out the native token balance of the user
*/
module.exports = balance = async (from) => {
let provider;
let signer;
let contract;
/* This script takes command line argument to indicate which chain we are using */
if (from === "avax") {
/* Initialize; provider, signer and contract */
provider = new ethers.providers.JsonRpcProvider(chains.avax.rpcUrl);
signer = new ethers.Wallet(process.env.BRIDGE_USER_PRIVATE_KEY, provider);
contract = new ethers.Contract(AVAX_TOKEN_ADDRESS, AVAX_TOKEN_ABI, signer);
/* Get ERC20 balance of the user */
const newUserBalance = await contract.balanceOf(signer.address);
/* Get ERC20 balance of the bridge */
const newBridgeBalance = await contract.balanceOf(AVAX_BRIDGE_ADDRESS);
console.log(
"MERC20 balance of the user: ",
ethers.utils.formatEther(newUserBalance)
);
console.log(
"MERC20 balance of the bridge: ",
ethers.utils.formatEther(newBridgeBalance)
);
} else if (from === "subnet") {
/* Initialize; provider, signer */
provider = new ethers.providers.JsonRpcProvider(chains.subnet.rpcUrl);
signer = new ethers.Wallet(process.env.BRIDGE_USER_PRIVATE_KEY, provider);
/* Get native token balance of the user */
const balance = await signer.getBalance();
console.log(
"Native token balance on Subnet: ",
ethers.utils.formatEther(balance)
);
} else {
return;
}
};

Was this page helpful?