You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.6 KiB

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