compassDAO
  • Welcome to compassDAO
  • General Information
    • What is compassDAO?
    • Vision
    • Mission
    • Value
    • Why compassDAO?
    • What we offer?
  • How to use
    • compassStats
      • For All Users
        • Connect Wallet
        • Set Up Notifiers
        • Schedule Tasks
        • Set Alerts
      • For Devs (Data Contributor)
        • API
        • Best Practices
          • Get the balance of an address
          • Curve 3pool Proportion Monitoring
          • Curve 3pool Big Withdrawn Monitoring
    • compassSafe
      • Get Started
      • Set Up Safe Module
      • Set Role
      • Set Member
      • Interact With Dapps
      • Disable Safe Module
      • Best Practices
        • DeFi - Lending (Aave)
        • DeFi - Dex (Uniswap V3)
      • For Devs
        • execTransactionFromModule
        • execTransactionsFromModule
      • Versions
      • FAQ
  • FAQ
    • Untitled
  • Links
    • Website
    • Github
    • Twitter
    • Discord
Powered by GitBook
On this page
  1. How to use
  2. compassStats
  3. For Devs (Data Contributor)
  4. Best Practices

Get the balance of an address

PreviousBest PracticesNextCurve 3pool Proportion Monitoring

Last updated 1 year ago

The default Sample Script is a simple script to get the balance of an address, users can edit the script and run or save it.

Check the script here:

The script code is as following:

export const name = "My Awesome Script";
export const description = "Get balance of address";
export const tag = "eth balance";

const formatBalance = (wei: ethers.BigNumber) =>
  Number(Number(ethers.utils.formatEther(wei)).toFixed(4));

export const run = async ({ address }: Record<string, string>) => {
  const cacheKey = 'balanceOf' + String(address);
  const cachedBalance = cache[cacheKey] || 0;
  const prevBalance = ethers.BigNumber.from(cachedBalance);

  const provider = evm.getProvider("ethereum");
  const curBalance = await provider.getBalance(address);
  cache[cacheKey] = curBalance.toString();

  return {
    address,
    prevBalance: formatBalance(prevBalance),
    curBalance: formatBalance(curBalance),
  };
};

Once the script is saved, users can fill in the parameters to run the script.

https://compassdao.com/scripts/0xb41154b301f7437c