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

Inconsistency between AccountBalance and AccountHistory #2386

Open
DATFrederik opened this issue Aug 25, 2023 · 0 comments
Open

Inconsistency between AccountBalance and AccountHistory #2386

DATFrederik opened this issue Aug 25, 2023 · 0 comments
Labels

Comments

@DATFrederik
Copy link

Inconsistency between AccountBalance and AccountHistory

Summary

  • The AuctionBid entries that according to the AccountHistory (provided by FullNode) were sent to df1qvzdgd85m67eym95l0jxrdn6aue5cjthpg78r4z in block 1438560 were not included in the balance.
  • If all accountHistory entries for a certain DToken (see steps to reproduce) are summed up, you should get the present tokenBalance as result.
  • However, the example provided below, clearly shows, that the data provided by the FullNode have a inconsistency here.
  • The issue I found was with address df1qvzdgd85m67eym95l0jxrdn6aue5cjthpg78r4z for example for the token SPY.

Steps to Reproduce

  • Ensure you have npm and node installed.

  • Install the required packages:

    npm install @defichain/jellyfish-api-jsonrpc
    npm install bignumber.js
    
  • Change the RPC-Endpoint within the fol where it says ADD-YOUR-RPC-ENDPOINT-HERE to one you have access to.

  • Run the example:

    const { JsonRpcClient } = require('@defichain/jellyfish-api-jsonrpc');
    const { BigNumber } = require('bignumber.js'); 
    
    // This function will sum up every single accountHistory entry for a certain symbol and address
    // from present blockHeight to genesis Block. 
    
    async function compareBalanceAndHistorySum(address, symbol) {
      const fullNode = new JsonRpcClient('ADD-YOUR-RPC-ENDPOINT-HERE', {
        timeout: 60000,
      });
    
      const limit = 20000;
      let amountSum = BigNumber('0');
      let roundCounter = 0;
      let symbolBalance = BigNumber(0);
      // Determine present balance for the token with corresponding symbol
      const balances = await fullNode.account.getAccount(address);
      const blockHeight = await fullNode.blockchain.getBlockCount();
      balances.forEach((amount) => {
        const [tokenAmount, tokenSymbol] = amount.split('@');
        if (tokenSymbol === symbol){
          symbolBalance = BigNumber(tokenAmount);
        }
      });
      console.log(`Present Balance of ${symbol}: ${symbolBalance.toString()} at blockHeight: ${blockHeight}`);
    
    
    
      console.log(`Starting accountHistory fetching. This may take a while...`);
    
      while (true) {
        // Fetch entire accountHistory for the symbol
        const accountHistory = await fullNode.account.listAccountHistory(
          address,
          {
            maxBlockHeight: blockHeight,
            limit,
            start: limit * roundCounter,
            including_start: true,
            token: symbol,
          },
        );
        roundCounter++;
        console.log(`${roundCounter}. round of accountHistory fetching done.`);
    
    
        for (const accountHistoryElement of accountHistory) {
          // Split Amount@Symbol
          const convertedAmounts = accountHistoryElement.amounts.map(
            (amount) => {
              const [tokenAmount, tokenSymbol] = amount.split('@');
              return { amount: BigNumber(tokenAmount), symbol: tokenSymbol };
            },
          );
    
          // Sum up the amounts.
          convertedAmounts.forEach((element) => {
            if (element.symbol === symbol) {
              amountSum = amountSum.plus(element.amount);
            }
          });
        }
    
        // As soon as the length of accountHistory isn't equal to the limit we have reached the end.
        if(accountHistory.length != limit) break;
      
      }
      console.log(`Adding up every single accountHistoryEntry results to ${amountSum} ${symbol}`);
      console.log(`Comparing this to the actual present balance of ${symbolBalance} ${symbol} results to a difference of ${symbolBalance-amountSum} ${symbol}`);
    }
    
    // Call the function with the appropriate values
    compareBalanceAndHistorySum('df1qvzdgd85m67eym95l0jxrdn6aue5cjthpg78r4z', 'SPY');

Results (detailed):

  • The example (df1qvzdgd85m67eym95l0jxrdn6aue5cjthpg78r4z, SPY) shows that based on the accountHistory the account should have a balance of 0.00000621 SPY. And yet the FullNode reports a present balance of 0 Spy.

  • This example can also be done with different setups:

    • (df1qvzdgd85m67eym95l0jxrdn6aue5cjthpg78r4z, SPY),
    • (df1qvzdgd85m67eym95l0jxrdn6aue5cjthpg78r4z, BABA),
    • (df1qvzdgd85m67eym95l0jxrdn6aue5cjthpg78r4z, AAPL).
  • All of these show a different balance than the one reported by the fullNode.

  • I could pinpoint this down to the wrong entry:

{
  owner: 'df1qvzdgd85m67eym95l0jxrdn6aue5cjthpg78r4z',
  blockHeight: 1438560,
  blockHash: 'ce90f9d7b10de454742a02d44dac09db2ae83e96bacc466513a875a6b3bf7d6f',
  blockTime: 1639160330,
  type: 'AuctionBid',
  txn: 4294967295,
  txid: 'ce90f9d7b10de454742a02d44dac09db2ae83e96bacc466513a875a6b3bf7d6f',
  amounts: [
    '208.69680820@DFI',
    '0.00000006@BTC',
    '1.36337349@DUSD',
    '0.00020320@BABA',
    '0.00152688@GME/v1',
    '0.00001676@AAPL',
    '0.00011532@GOOGL/v1',
    '0.00005973@ARKK',
    '0.00000621@SPY',
    '0.00000092@QQQ',
    '0.03665958@PDBC'
  ]
}

The values within this entry represent exactly the amount by which the accountHistory and the accountBalance differ. It seems, that this entry isn't valid and should not be part of the account History.

Environment

[Please fill all of the following or NA if not applicable]

  • Node Version: v16.13.1
  • Block height on bug if applicable: 1438560
  • TX or TX type on bug if applicable: AuctionBid
  • OS with version: NA
  • Any other relevant environment info: NA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant