Skip to content

Commit

Permalink
feat: add snippet to check cUSD balance
Browse files Browse the repository at this point in the history
  • Loading branch information
therealharpaljadeja committed Nov 30, 2023
1 parent 3f3be87 commit 3b97743
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 2 deletions.
5 changes: 5 additions & 0 deletions constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const STABLE_TOKEN_ADDRESS = "0x765DE816845861e75A25fCA122bb6898B8B1282a";

module.exports = {
STABLE_TOKEN_ADDRESS,
};
26 changes: 26 additions & 0 deletions ethers/checkCUSDBalance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const ethers = require("ethers");
const StableToken = require("@celo/abis/StableToken.json");
const { STABLE_TOKEN_ADDRESS } = require("../constants");

const { Contract, utils } = ethers;
const { formatEther } = utils;

async function checkCUSDBalance(provider, address) {
const StableTokenContract = new Contract(
STABLE_TOKEN_ADDRESS,
StableToken.abi,
provider
);

let balanceInBigNumber = await StableTokenContract.balanceOf(address);

let balanceInWei = balanceInBigNumber.toString();

let balanceInEthers = formatEther(balanceInWei); // Ether is a unit = 10 ** 18 wei

return balanceInEthers;
}

module.exports = {
checkCUSDBalance,
};
8 changes: 7 additions & 1 deletion ethers/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const ethers = require("ethers");
const { providers } = ethers;
const { checkCUSDBalance } = require("./checkCUSDBalance");

const provider = new providers.JsonRpcProvider("https://forno.celo.org");

async function main() {}
async function main() {
// return await checkCUSDBalance(
// provider,
// "0x765de816845861e75a25fca122bb6898b8b1282a"
// );
}

main()
.then((result) => console.log(result))
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"author": "Harpal Jadeja <[email protected]>",
"license": "MIT",
"dependencies": {
"@celo/abis": "^9.0.6",
"ethers": "5.7.2",
"viem": "^1.19.9"
}
Expand Down
25 changes: 25 additions & 0 deletions viem/checkCUSDBalance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { getContract, formatEther } = require("viem");
const StableToken = require("@celo/abis/StableToken.json");
const { STABLE_TOKEN_ADDRESS } = require("../constants");

async function checkCUSDBalance(publicClient, address) {
let StableTokenContract = getContract({
abi: StableToken.abi,
address: STABLE_TOKEN_ADDRESS,
publicClient,
});

let balanceInBigNumber = await StableTokenContract.read.balanceOf([
address,
]);

let balanceInWei = balanceInBigNumber.toString();

let balanceInEthers = formatEther(balanceInWei);

return balanceInEthers;
}

module.exports = {
checkCUSDBalance,
};
6 changes: 5 additions & 1 deletion viem/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const { createPublicClient, http } = require("viem");
const { celo } = require("viem/chains");
const { checkCUSDBalance } = require("./checkCUSDBalance");

const publicClient = createPublicClient({
chain: celo,
transport: http(),
});

async function main() {
return publicClient;
// return await checkCUSDBalance(
// publicClient,
// "0x765de816845861e75a25fca122bb6898b8b1282a"
// );
}

main()
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
resolved "https://registry.yarnpkg.com/@adraffy/ens-normalize/-/ens-normalize-1.10.0.tgz#d2a39395c587e092d77cbbc80acf956a54f38bf7"
integrity sha512-nA9XHtlAkYfJxY7bce8DcN7eKxWWCWkU+1GR9d+U6MbNpfwQp8TI7vqOsBsMcHoT4mBu2kypKoSKnghEzOOq5Q==

"@celo/abis@^9.0.6":
version "9.0.6"
resolved "https://registry.yarnpkg.com/@celo/abis/-/abis-9.0.6.tgz#3586f578394cb0408f17335cdaedf98fd9f47d7c"
integrity sha512-w/zQWAnJMUfthiFyzUoSKgZ1DQDKjXaSmImwGRDi6U9SPyEx5rWsHm7tsoVXoxYzAdPbtpkeFykQPvPqCJfHmA==

"@ethersproject/[email protected]", "@ethersproject/abi@^5.7.0":
version "5.7.0"
resolved "https://registry.yarnpkg.com/@ethersproject/abi/-/abi-5.7.0.tgz#b3f3e045bbbeed1af3947335c247ad625a44e449"
Expand Down

0 comments on commit 3b97743

Please sign in to comment.