Connect price updater to historydb

This commit is contained in:
Arnau B
2020-10-01 20:52:35 +02:00
parent 0362afc194
commit f653ff8a73
3 changed files with 101 additions and 118 deletions

View File

@@ -1,38 +1,60 @@
package priceupdater
import (
"math/big"
"os"
"testing"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/hermez-node/common"
dbUtils "github.com/hermeznetwork/hermez-node/db"
"github.com/hermeznetwork/hermez-node/db/historydb"
"github.com/hermeznetwork/hermez-node/test"
"github.com/stretchr/testify/assert"
)
func TestCon(t *testing.T) {
config := ConfigPriceUpdater{
RecommendedFee: 1,
RecommendedCreateAccountFee: 1,
TokensList: []string{"ETH", "NEC"},
APIURL: "https://api-pub.bitfinex.com/v2/",
func TestPriceUpdater(t *testing.T) {
// Init DB
pass := os.Getenv("POSTGRES_PASS")
db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
assert.NoError(t, err)
historyDB := historydb.NewHistoryDB(db)
// Clean DB
assert.NoError(t, historyDB.Reorg(-1))
// Populate DB
// Gen blocks and add them to DB
blocks := test.GenBlocks(1, 2)
assert.NoError(t, historyDB.AddBlocks(blocks))
// Gen tokens and add them to DB
tokens := []common.Token{}
tokens = append(tokens, common.Token{
TokenID: 0,
EthBlockNum: blocks[0].EthBlockNum,
EthAddr: ethCommon.BigToAddress(big.NewInt(1)),
Name: "Ether",
Symbol: "ETH",
Decimals: 18,
})
tokens = append(tokens, common.Token{
TokenID: 1,
EthBlockNum: blocks[0].EthBlockNum,
EthAddr: ethCommon.BigToAddress(big.NewInt(2)),
Name: "DAI",
Symbol: "DAI",
Decimals: 18,
})
assert.NoError(t, historyDB.AddTokens(tokens))
// Init price updater
pu := NewPriceUpdater("https://api-pub.bitfinex.com/v2/", historyDB)
// Update token list
assert.NoError(t, pu.UpdateTokenList())
// Update prices
pu.UpdatePrices()
// Check that prices have been updated
fetchedTokens, err := historyDB.GetTokens()
assert.NoError(t, err)
for _, token := range fetchedTokens {
assert.NotNil(t, token.USD)
assert.NotNil(t, token.USDUpdate)
}
pud := NewPriceUpdater(config)
err := pud.UpdatePrices()
assert.Equal(t, err, nil)
info, _ := pud.Get("ETH")
assert.NotZero(t, info.Value)
info2, _ := pud.Get("NEC")
assert.NotZero(t, info2.Value)
info3, err := pud.Get("INVENTED")
if assert.Error(t, err) {
assert.Equal(t, ErrSymbolDoesNotExistInDatabase, err)
}
assert.Equal(t, info3.Value, float64(0))
prices := pud.GetPrices()
assert.Equal(t, prices["ETH"], info)
assert.Equal(t, prices["NEC"], info2)
}