Merge pull request #629 from hermeznetwork/feature/indivisual-token-config

Allow price update configuration to be specified per token
This commit is contained in:
Eduard S
2021-03-10 14:11:57 +01:00
committed by GitHub
9 changed files with 258 additions and 119 deletions

View File

@@ -456,13 +456,10 @@ func (hdb *HistoryDB) addTokens(d meddler.DB, tokens []common.Token) error {
// UpdateTokenValue updates the USD value of a token. Value is the price in
// USD of a normalized token (1 token = 10^decimals units)
func (hdb *HistoryDB) UpdateTokenValue(tokenSymbol string, value float64) error {
// Sanitize symbol
tokenSymbol = strings.ToValidUTF8(tokenSymbol, " ")
func (hdb *HistoryDB) UpdateTokenValue(tokenAddr ethCommon.Address, value float64) error {
_, err := hdb.dbWrite.Exec(
"UPDATE token SET usd = $1 WHERE symbol = $2;",
value, tokenSymbol,
"UPDATE token SET usd = $1 WHERE eth_addr = $2;",
value, tokenAddr,
)
return tracerr.Wrap(err)
}
@@ -1161,7 +1158,7 @@ func (hdb *HistoryDB) GetTokensTest() ([]TokenWithUSD, error) {
tokens := []*TokenWithUSD{}
if err := meddler.QueryAll(
hdb.dbRead, &tokens,
"SELECT * FROM TOKEN",
"SELECT * FROM token ORDER BY token_id ASC",
); err != nil {
return nil, tracerr.Wrap(err)
}

View File

@@ -166,7 +166,7 @@ func TestBatches(t *testing.T) {
if i%2 != 0 {
// Set value to the token
value := (float64(i) + 5) * 5.389329
assert.NoError(t, historyDB.UpdateTokenValue(token.Symbol, value))
assert.NoError(t, historyDB.UpdateTokenValue(token.EthAddr, value))
tokensValue[token.TokenID] = value / math.Pow(10, float64(token.Decimals))
}
}
@@ -276,7 +276,7 @@ func TestTokens(t *testing.T) {
// Update token value
for i, token := range tokens {
value := 1.01 * float64(i)
assert.NoError(t, historyDB.UpdateTokenValue(token.Symbol, value))
assert.NoError(t, historyDB.UpdateTokenValue(token.EthAddr, value))
}
// Fetch tokens
fetchedTokens, err = historyDB.GetTokensTest()
@@ -302,7 +302,7 @@ func TestTokensUTF8(t *testing.T) {
// Generate fake tokens
const nTokens = 5
tokens, ethToken := test.GenTokens(nTokens, blocks)
nonUTFTokens := make([]common.Token, len(tokens)+1)
nonUTFTokens := make([]common.Token, len(tokens))
// Force token.name and token.symbol to be non UTF-8 Strings
for i, token := range tokens {
token.Name = fmt.Sprint("NON-UTF8-NAME-\xc5-", i)
@@ -332,7 +332,7 @@ func TestTokensUTF8(t *testing.T) {
// Update token value
for i, token := range nonUTFTokens {
value := 1.01 * float64(i)
assert.NoError(t, historyDB.UpdateTokenValue(token.Symbol, value))
assert.NoError(t, historyDB.UpdateTokenValue(token.EthAddr, value))
}
// Fetch tokens
fetchedTokens, err = historyDB.GetTokensTest()

View File

@@ -121,7 +121,7 @@ func prepareHistoryDB(historyDB *historydb.HistoryDB) error {
}
tokens[token.TokenID] = readToken
// Set value to the tokens
err := historyDB.UpdateTokenValue(readToken.Symbol, *readToken.USD)
err := historyDB.UpdateTokenValue(readToken.EthAddr, *readToken.USD)
if err != nil {
return tracerr.Wrap(err)
}