hermez-node/priceupdater/priceupdater_test.go
priceupdater/priceupdater_test.goRaw
1package priceupdater
2
3import (
4 "math/big"
5 "os"
6 "testing"
7
8 ethCommon "github.com/ethereum/go-ethereum/common"
9 "github.com/hermeznetwork/hermez-node/common"
10 dbUtils "github.com/hermeznetwork/hermez-node/db"
11 "github.com/hermeznetwork/hermez-node/db/historydb"
12 "github.com/hermeznetwork/hermez-node/test"
13 "github.com/stretchr/testify/assert"
14)
15
16func TestPriceUpdater(t *testing.T) {
17 // Init DB
18 pass := os.Getenv("POSTGRES_PASS")
19 db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
20 assert.NoError(t, err)
21 historyDB := historydb.NewHistoryDB(db)
22 // Clean DB
23 assert.NoError(t, historyDB.Reorg(-1))
24 // Populate DB
25 // Gen blocks and add them to DB
26 blocks := test.GenBlocks(1, 2)
27 assert.NoError(t, historyDB.AddBlocks(blocks))
28 // Gen tokens and add them to DB
29 tokens := []common.Token{}
30 tokens = append(tokens, common.Token{
31 TokenID: 0,
32 EthBlockNum: blocks[0].EthBlockNum,
33 EthAddr: ethCommon.BigToAddress(big.NewInt(1)),
34 Name: "Ether",
35 Symbol: "ETH",
36 Decimals: 18,
37 })
38 tokens = append(tokens, common.Token{
39 TokenID: 1,
40 EthBlockNum: blocks[0].EthBlockNum,
41 EthAddr: ethCommon.BigToAddress(big.NewInt(2)),
42 Name: "DAI",
43 Symbol: "DAI",
44 Decimals: 18,
45 })
46 assert.NoError(t, historyDB.AddTokens(tokens))
47 // Init price updater
48 pu := NewPriceUpdater("https://api-pub.bitfinex.com/v2/", historyDB)
49 // Update token list
50 assert.NoError(t, pu.UpdateTokenList())
51 // Update prices
52 pu.UpdatePrices()
53 // Check that prices have been updated
54 fetchedTokens, err := historyDB.GetTokens()
55 assert.NoError(t, err)
56 for _, token := range fetchedTokens {
57 assert.NotNil(t, token.USD)
58 assert.NotNil(t, token.USDUpdate)
59 }
60}