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.

61 lines
1.7 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. assert.NoError(t, historyDB.Reorg(-1))
  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: 0,
  29. EthBlockNum: blocks[0].EthBlockNum,
  30. EthAddr: ethCommon.BigToAddress(big.NewInt(1)),
  31. Name: "Ether",
  32. Symbol: "ETH",
  33. Decimals: 18,
  34. })
  35. tokens = append(tokens, common.Token{
  36. TokenID: 1,
  37. EthBlockNum: blocks[0].EthBlockNum,
  38. EthAddr: ethCommon.BigToAddress(big.NewInt(2)),
  39. Name: "DAI",
  40. Symbol: "DAI",
  41. Decimals: 18,
  42. })
  43. assert.NoError(t, historyDB.AddTokens(tokens))
  44. // Init price updater
  45. pu := NewPriceUpdater("https://api-pub.bitfinex.com/v2/", historyDB)
  46. // Update token list
  47. assert.NoError(t, pu.UpdateTokenList())
  48. // Update prices
  49. pu.UpdatePrices()
  50. // Check that prices have been updated
  51. limit := uint(10)
  52. fetchedTokens, _, err := historyDB.GetTokens(nil, nil, "", nil, &limit, historydb.OrderAsc)
  53. assert.NoError(t, err)
  54. for _, token := range fetchedTokens {
  55. assert.NotNil(t, token.USD)
  56. assert.NotNil(t, token.USDUpdate)
  57. }
  58. }