@@ -22,21 +22,24 @@ export const addBalanceForAccountMovementHistory = (
2222 data : AccountMovementHistory [ ] ,
2323 symbol : NetworkSymbol ,
2424) : AccountHistoryBalancePoint [ ] => {
25- let balance = '0' ;
25+ let balance = new BigNumber ( '0' ) ;
2626 const historyWithBalance = data . map ( dataPoint => {
2727 // subtract sentToSelf field as we don't want to include amounts received/sent to the same account
2828 const normalizedReceived = dataPoint . sentToSelf
29- ? new BigNumber ( dataPoint . received ) . minus ( dataPoint . sentToSelf || 0 ) . toFixed ( )
29+ ? new BigNumber ( dataPoint . received ) . minus ( dataPoint . sentToSelf || 0 )
3030 : dataPoint . received ;
3131 const normalizedSent = dataPoint . sentToSelf
32- ? new BigNumber ( dataPoint . sent ) . minus ( dataPoint . sentToSelf || 0 ) . toFixed ( )
32+ ? new BigNumber ( dataPoint . sent ) . minus ( dataPoint . sentToSelf || 0 )
3333 : dataPoint . sent ;
3434
35- balance = new BigNumber ( balance ) . plus ( normalizedReceived ) . minus ( normalizedSent ) . toFixed ( ) ;
35+ balance = new BigNumber ( balance ) . plus ( normalizedReceived ) . minus ( normalizedSent ) ;
36+
37+ // for some coins like ETH, simple sum of received and sent is not enough and could result in nonsense like negative balance
38+ balance = balance . isNegative ( ) ? new BigNumber ( '0' ) : balance ;
3639
3740 return {
3841 time : dataPoint . time ,
39- cryptoBalance : formatNetworkAmount ( balance , symbol ) ,
42+ cryptoBalance : formatNetworkAmount ( balance . toFixed ( ) , symbol ) ,
4043 } ;
4144 } ) ;
4245
@@ -61,24 +64,40 @@ export const getAccountBalanceHistory = async ({
6164 return accountBalanceHistoryCache [ cacheKey ] ;
6265 }
6366
64- const accountMovementHistory = await TrezorConnect . blockchainGetAccountBalanceHistory ( {
65- coin,
66- descriptor,
67- to : endTimeFrameTimestamp ,
68- // we don't need currencies at all here, this will just reduce transferred data size
69- // TODO: doesn't work at all, fix it in connect or blockchain-link?
70- currencies : [ 'usd' ] ,
71- } ) ;
67+ const [ accountMovementHistory , accountInfo ] = await Promise . all ( [
68+ TrezorConnect . blockchainGetAccountBalanceHistory ( {
69+ coin,
70+ descriptor,
71+ to : endTimeFrameTimestamp ,
72+ // we don't need currencies at all here, this will just reduce transferred data size
73+ // TODO: doesn't work at all, fix it in connect or blockchain-link?
74+ currencies : [ 'usd' ] ,
75+ } ) ,
76+ TrezorConnect . getAccountInfo ( { coin, descriptor } ) ,
77+ ] ) ;
7278
7379 if ( ! accountMovementHistory ?. success ) {
74- throw new Error ( `Get account balance error: ${ accountMovementHistory . payload . error } ` ) ;
80+ throw new Error (
81+ `Get account balance movement error: ${ accountMovementHistory . payload . error } ` ,
82+ ) ;
83+ }
84+
85+ if ( ! accountInfo ?. success ) {
86+ throw new Error ( `Get account balance info error: ${ accountInfo . payload . error } ` ) ;
7587 }
7688
7789 const accountMovementHistoryWithBalance = addBalanceForAccountMovementHistory (
7890 accountMovementHistory . payload ,
7991 coin ,
8092 ) ;
8193
94+ // Last point must be balance from getAccountInfo because blockchainGetAccountBalanceHistory it's not always reliable for coins like ETH.
95+ // TODO: We can get value from redux store instead of fetching it again?
96+ accountMovementHistoryWithBalance . push ( {
97+ time : endTimeFrameTimestamp ,
98+ cryptoBalance : formatNetworkAmount ( accountInfo . payload . balance , coin ) ,
99+ } ) ;
100+
82101 accountBalanceHistoryCache [ cacheKey ] = accountMovementHistoryWithBalance ;
83102
84103 return accountMovementHistoryWithBalance ;
@@ -166,11 +185,12 @@ export const getMultipleAccountBalanceHistoryWithFiat = async ({
166185 ) ;
167186 }
168187
169- const timestamps = getTimestampsInTimeFrame (
170- startOfTimeFrameDate ,
171- endOfTimeFrameDate ,
172- numberOfPoints ,
173- ) ;
188+ // Last timestamp must be endOfTimeFrameDate because blockchainGetAccountBalanceHistory it's not always reliable for coins like ETH.
189+ // So we manually add balance from getAccountInfo for last point in getAccountBalanceHistory.
190+ const timestamps = [
191+ ...getTimestampsInTimeFrame ( startOfTimeFrameDate , endOfTimeFrameDate , numberOfPoints - 1 ) ,
192+ getUnixTime ( endOfTimeFrameDate ) ,
193+ ] ;
174194
175195 const coins = pipe (
176196 accounts ,
0 commit comments