feat: Pass 1 day balance change (#4858)

This commit is contained in:
Cuteivist 2024-03-08 13:52:39 +01:00 committed by GitHub
parent 7f671f7632
commit 6c792a0e73
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 82 additions and 10 deletions

View File

@ -93,6 +93,7 @@ type TokenMarketValues struct {
type ChainBalance struct {
RawBalance string `json:"rawBalance"`
Balance *big.Float `json:"balance"`
Balance1DayAgo string `json:"balance1DayAgo"`
Address common.Address `json:"address"`
ChainID uint64 `json:"chainId"`
HasError bool `json:"hasError"`
@ -386,6 +387,7 @@ func (r *Reader) getWalletTokenBalances(ctx context.Context, addresses []common.
result := make(map[common.Address][]Token)
communities := make(map[string]bool)
dayAgoTimestamp := time.Now().Add(-24 * time.Hour).Unix()
for _, address := range addresses {
for _, tokenList := range [][]*token.Token{verifiedTokens, unverifiedTokens} {
@ -415,9 +417,18 @@ func (r *Reader) getWalletTokenBalances(ctx context.Context, addresses []common.
if !isVisible {
isVisible = balance.Cmp(big.NewFloat(0.0)) > 0 || r.isCachedToken(cachedTokens, address, token.Symbol, token.ChainID)
}
balance1DayAgo, err := r.tokenManager.GetTokenHistoricalBalance(address, token.ChainID, token.Symbol, dayAgoTimestamp)
if err != nil {
return nil, err
}
balance1DayAgoStr := "0"
if balance1DayAgo != nil {
balance1DayAgoStr = balance1DayAgo.String()
}
balancesPerChain[token.ChainID] = ChainBalance{
RawBalance: hexBalance.ToInt().String(),
Balance: balance,
Balance1DayAgo: balance1DayAgoStr,
Address: token.Address,
ChainID: token.ChainID,
HasError: hasError,

View File

@ -30,6 +30,7 @@ import (
"github.com/status-im/status-go/services/communitytokens"
"github.com/status-im/status-go/services/utils"
"github.com/status-im/status-go/services/wallet/async"
"github.com/status-im/status-go/services/wallet/bigint"
"github.com/status-im/status-go/services/wallet/community"
"github.com/status-im/status-go/services/wallet/walletevent"
)
@ -873,3 +874,14 @@ func (tm *Manager) fillCommunityData(token *Token) error {
}
return nil
}
func (tm *Manager) GetTokenHistoricalBalance(account common.Address, chainID uint64, symbol string, timestamp int64) (*big.Int, error) {
var balance big.Int
err := tm.db.QueryRow("SELECT balance FROM balance_history WHERE currency = ? AND chain_id = ? AND address = ? AND timestamp < ? order by timestamp DESC LIMIT 1", symbol, chainID, account, timestamp).Scan((*bigint.SQLBigIntBytes)(&balance))
if err == sql.ErrNoRows {
return nil, nil
} else if err != nil {
return nil, err
}
return &balance, nil
}

View File

@ -1,6 +1,7 @@
package token
import (
"math/big"
"testing"
"github.com/stretchr/testify/require"
@ -8,6 +9,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/services/wallet/bigint"
"github.com/status-im/status-go/services/wallet/community"
"github.com/status-im/status-go/t/helpers"
"github.com/status-im/status-go/walletdatabase"
@ -248,3 +250,50 @@ func TestMarkAsPreviouslyOwnedToken(t *testing.T) {
require.Equal(t, 2, count)
require.True(t, isFirst)
}
func TestGetTokenHistoricalBalance(t *testing.T) {
manager, stop := setupTestTokenDB(t)
defer stop()
account := common.HexToAddress("0x1234567890abcdef")
chainID := uint64(1)
testSymbol := "TEST"
block := int64(1)
timestamp := int64(1629878400) // Replace with desired timestamp
historyBalance := big.NewInt(0)
// Test case when no rows are returned
balance, err := manager.GetTokenHistoricalBalance(account, chainID, testSymbol, timestamp)
require.NoError(t, err)
require.Nil(t, balance)
// Test case when a row is returned
historyBalance.SetInt64(int64(100))
_, err = manager.db.Exec("INSERT INTO balance_history (currency, chain_id, address, timestamp, balance, block) VALUES (?, ?, ?, ?, ?, ?)", testSymbol, chainID, account, timestamp-100, (*bigint.SQLBigIntBytes)(historyBalance), block)
require.NoError(t, err)
expectedBalance := big.NewInt(100)
balance, err = manager.GetTokenHistoricalBalance(account, chainID, testSymbol, timestamp)
require.NoError(t, err)
require.Equal(t, expectedBalance, balance)
// Test multiple values. Must return the most recent one
historyBalance.SetInt64(int64(100))
_, err = manager.db.Exec("INSERT INTO balance_history (currency, chain_id, address, timestamp, balance, block) VALUES (?, ?, ?, ?, ?, ?)", testSymbol, chainID, account, timestamp-200, (*bigint.SQLBigIntBytes)(historyBalance), block)
require.NoError(t, err)
historyBalance.SetInt64(int64(50))
symbol := "TEST2"
_, err = manager.db.Exec("INSERT INTO balance_history (currency, chain_id, address, timestamp, balance, block) VALUES (?, ?, ?, ?, ?, ?)", symbol, chainID, account, timestamp-1, (*bigint.SQLBigIntBytes)(historyBalance), block)
require.NoError(t, err)
historyBalance.SetInt64(int64(50))
chainID = uint64(2)
_, err = manager.db.Exec("INSERT INTO balance_history (currency, chain_id, address, timestamp, balance, block) VALUES (?, ?, ?, ?, ?, ?)", testSymbol, chainID, account, timestamp-1, (*bigint.SQLBigIntBytes)(historyBalance), block)
require.NoError(t, err)
chainID = uint64(1)
balance, err = manager.GetTokenHistoricalBalance(account, chainID, testSymbol, timestamp)
require.NoError(t, err)
require.Equal(t, expectedBalance, balance)
}