Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Pull balances /w block-height | fetch tokenPrice for native token #119

Merged
merged 30 commits into from
Dec 4, 2023

Conversation

abhidtu2014
Copy link
Contributor

@abhidtu2014 abhidtu2014 commented Nov 20, 2023

  • Adds field (walletBalanceConfig) in wallet config to control pull balances on-demand vs scheduled behaviour
  • Adds field priceFeedConfig per chain and priceFeedOptions to control fetching token-priceon-demand vs scheduled behaviour
  • Adds methods on walletManager to pullBalances and pullBalancesAtBlockHeight
  • Adds getBlockHeight as public method on walletManager instance

Note: For those who would think using bigint would have been better than Number. Initially, I was using bigint for balances and tokenPrice across the code, but we were losing simplicity in the code and it was more prone to conversion errors (eg: stringified number to bigint). Though, adding pricePrecision was an option to tokenPrice but data can become unusable when it will be required to convert the balances in tokenAmount with decimals to actual USD amount without decimals, which can become bottleneck while calculating profits and loss and visualizing balances in grafana.

Result of pullBalancesAtBlockHeight:

{
  "ethereum": {
    "0x80C67432656d59144cEFf962E8fAF8926599bCF8": {
      "isNative": true,
      "rawBalance": "164732882239391068004",
      "address": "0x80C67432656d59144cEFf962E8fAF8926599bCF8",
      "formattedBalance": "164.732882239391068004",
      "tokens": [
        {
          "isNative": false,
          "rawBalance": "20000000",
          "address": "0x80C67432656d59144cEFf962E8fAF8926599bCF8",
          "tokenAddress": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
          "formattedBalance": "20.0",
          "symbol": "USDC",
          "balanceUsd": 19.99932,
          "tokenUsdPrice": 0.999966
        }
      ],
      "symbol": "ETH",
      "balanceUsd": 346054.3657202888,
      "tokenUsdPrice": 2100.7,
      "blockHeight": 18641694
    },
    "0x8d0d970225597085A59ADCcd7032113226C0419d": {
      "isNative": true,
      "rawBalance": "60372718604099355",
      "address": "0x8d0d970225597085A59ADCcd7032113226C0419d",
      "formattedBalance": "0.060372718604099355",
      "tokens": [
        {
          "isNative": false,
          "rawBalance": "0",
          "address": "0x8d0d970225597085A59ADCcd7032113226C0419d",
          "tokenAddress": "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599",
          "formattedBalance": "0.0",
          "symbol": "WBTC",
          "balanceUsd": 0,
          "tokenUsdPrice": 37763
        }
      ],
      "symbol": "ETH",
      "balanceUsd": 126.8249699716315,
      "tokenUsdPrice": 2100.7,
      "blockHeight": 18641694
    }
  },
  "sui": {
    "0x934042d46762fadf9f61ef07aa265fc14d28525c7051224a5f1cba2409aef307": {
      "isNative": true,
      "rawBalance": "0",
      "address": "0x934042d46762fadf9f61ef07aa265fc14d28525c7051224a5f1cba2409aef307",
      "formattedBalance": "0",
      "tokens": [
        
      ],
      "symbol": "SUI",
      "balanceUsd": 0,
      "tokenUsdPrice": 0.620551,
      "blockHeight": 16910801
    },
    "0x341ab296bbe653b426e996b17af33718db62af3c250c04fe186c9124db5bd8b7": {
      "isNative": true,
      "rawBalance": "2000000000",
      "address": "0x341ab296bbe653b426e996b17af33718db62af3c250c04fe186c9124db5bd8b7",
      "formattedBalance": "2",
      "tokens": [
        
      ],
      "symbol": "SUI",
      "balanceUsd": 1.241102,
      "tokenUsdPrice": 0.620551,
      "blockHeight": 16910801
    },
    "0x7d20dcdb2bca4f508ea9613994683eb4e76e9c4ed371169677c1be02aaf0b58e": {
      "isNative": true,
      "rawBalance": "867162979509298587",
      "address": "0x7d20dcdb2bca4f508ea9613994683eb4e76e9c4ed371169677c1be02aaf0b58e",
      "formattedBalance": "867162979.5092986",
      "tokens": [
        
      ],
      "symbol": "SUI",
      "balanceUsd": 538118854.0974747,
      "tokenUsdPrice": 0.620551,
      "blockHeight": 16910801
    },
    "0x18337b4c5b964b7645506542589c5ed496e794af82f98b3789fed96f61a94c96": {
      "isNative": true,
      "rawBalance": "169174545440",
      "address": "0x18337b4c5b964b7645506542589c5ed496e794af82f98b3789fed96f61a94c96",
      "formattedBalance": "169.17454544",
      "tokens": [
        
      ],
      "symbol": "SUI",
      "balanceUsd": 104.98143334733743,
      "tokenUsdPrice": 0.620551,
      "blockHeight": 16910801
    },
    "0x9ae846f88db3476d7c9f2d8fc49722f7085a3b46aad998120dd11ebeab83e021": {
      "isNative": true,
      "rawBalance": "140163212952",
      "address": "0x9ae846f88db3476d7c9f2d8fc49722f7085a3b46aad998120dd11ebeab83e021",
      "formattedBalance": "140.163212952",
      "tokens": [
        
      ],
      "symbol": "SUI",
      "balanceUsd": 86.97842196057655,
      "tokenUsdPrice": 0.620551,
      "blockHeight": 16910801
    },
    "0xcb39d897bf0561af7531d37db9781e54528269fed4761275931ce32f20352977": {
      "isNative": true,
      "rawBalance": "182193598344",
      "address": "0xcb39d897bf0561af7531d37db9781e54528269fed4761275931ce32f20352977",
      "formattedBalance": "182.193598344",
      "tokens": [
        
      ],
      "symbol": "SUI",
      "balanceUsd": 113.06041964596754,
      "tokenUsdPrice": 0.620551,
      "blockHeight": 16910801
    },
    "0x8f11fe7121be742f46e2b3bc2eba081efdc3027697c317a917a2d16fd9b59ab1": {
      "isNative": true,
      "rawBalance": "0",
      "address": "0x8f11fe7121be742f46e2b3bc2eba081efdc3027697c317a917a2d16fd9b59ab1",
      "formattedBalance": "0",
      "tokens": [
        
      ],
      "symbol": "SUI",
      "balanceUsd": 0,
      "tokenUsdPrice": 0.620551,
      "blockHeight": 16910801
    }
  },
  "solana": {
    "6VnfVsLdLwNuuCmooLTziQ99PFXZ5vc3yyqyb9tMDhhw": {
      "isNative": true,
      "rawBalance": "1000000002",
      "address": "6VnfVsLdLwNuuCmooLTziQ99PFXZ5vc3yyqyb9tMDhhw",
      "formattedBalance": "1.000000002",
      "tokens": [
        {
          "isNative": false,
          "rawBalance": "00",
          "address": "6VnfVsLdLwNuuCmooLTziQ99PFXZ5vc3yyqyb9tMDhhw",
          "formattedBalance": "0",
          "symbol": "USDC"
        }
      ],
      "symbol": "SOL",
      "balanceUsd": 58.16000011631999,
      "tokenUsdPrice": 58.16,
      "blockHeight": 213669224
    }
  }
}

@abhidtu2014 abhidtu2014 requested a review from a team November 20, 2023 13:15
@abhidtu2014 abhidtu2014 marked this pull request as draft November 21, 2023 13:16
@abhidtu2014 abhidtu2014 marked this pull request as ready for review November 24, 2023 15:12
chainName: "klaytn",
coingeckoId: "klay-token",
symbol: "KLAY",
tokenContract: "",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: we are not using tokenContract, only coingeckoId

@abhidtu2014 abhidtu2014 changed the title Feat: Pull balances | Add opt block height /w fetching native balance Feat: Pull balances /w block-height | fetch tokenPrice for native token Nov 24, 2023

return {
...balance,
address,
formattedBalance,
tokens: [],
symbol: this.chainConfig.nativeCurrencySymbol,
...(tokenUsdPrice && {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we are introducing the blockHeight parameter when asking for balances, does it make sense to include the block height at which that balance was observed as a return value?

For example:

return {
      ...balance,
      address,
      formattedBalance,
      blockHeight,
      tokens: [],
      symbol: this.chainConfig.nativeCurrencySymbol,
      ...(tokenUsdPrice && {
          balanceUsd: Number(formattedBalance) * tokenUsdPrice,
          tokenUsdPrice
      })
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are actually injecting blockHeight in base-wallet.ts in pullNativeBalance method, but we can definitely move it here.

@abhidtu2014 abhidtu2014 merged commit b386051 into main Dec 4, 2023
1 check passed
@abhidtu2014 abhidtu2014 deleted the feat/improvements branch December 4, 2023 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants