-
Notifications
You must be signed in to change notification settings - Fork 128
Description
Inconsistency between AccountBalance and AccountHistory
Summary
- The
AuctionBidentries that according to the AccountHistory (provided by FullNode) were sent todf1qvzdgd85m67eym95l0jxrdn6aue5cjthpg78r4zin block1438560were 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
df1qvzdgd85m67eym95l0jxrdn6aue5cjthpg78r4zfor example for the tokenSPY.
Steps to Reproduce
-
Ensure you have
npmandnodeinstalled. -
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-HEREto 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
accountHistorythe 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