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.

143 lines
3.9 KiB

  1. package priceupdater
  2. import (
  3. "context"
  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. "github.com/stretchr/testify/require"
  13. )
  14. var historyDB *historydb.HistoryDB
  15. const usdtAddr = "0xdac17f958d2ee523a2206206994597c13d831ec7"
  16. func TestPriceUpdaterBitfinex(t *testing.T) {
  17. // Init DB
  18. pass := os.Getenv("POSTGRES_PASS")
  19. db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
  20. if err != nil {
  21. panic(err)
  22. }
  23. historyDB = historydb.NewHistoryDB(db, db, nil)
  24. // Clean DB
  25. test.WipeDB(historyDB.DB())
  26. // Populate DB
  27. // Gen blocks and add them to DB
  28. blocks := test.GenBlocks(1, 2)
  29. require.NoError(t, historyDB.AddBlocks(blocks))
  30. // Gen tokens and add them to DB
  31. tokens := []common.Token{
  32. {
  33. TokenID: 1,
  34. EthBlockNum: blocks[0].Num,
  35. EthAddr: ethCommon.HexToAddress("0x1"),
  36. Name: "DAI",
  37. Symbol: "DAI",
  38. Decimals: 18,
  39. }, // Used to test get by SC addr
  40. {
  41. TokenID: 2,
  42. EthBlockNum: blocks[0].Num,
  43. EthAddr: ethCommon.HexToAddress(usdtAddr),
  44. Name: "Tether",
  45. Symbol: "USDT",
  46. Decimals: 18,
  47. }, // Used to test get by token symbol
  48. {
  49. TokenID: 3,
  50. EthBlockNum: blocks[0].Num,
  51. EthAddr: ethCommon.HexToAddress("0x2"),
  52. Name: "FOO",
  53. Symbol: "FOO",
  54. Decimals: 18,
  55. }, // Used to test ignore
  56. {
  57. TokenID: 4,
  58. EthBlockNum: blocks[0].Num,
  59. EthAddr: ethCommon.HexToAddress("0x3"),
  60. Name: "BAR",
  61. Symbol: "BAR",
  62. Decimals: 18,
  63. }, // Used to test static
  64. {
  65. TokenID: 5,
  66. EthBlockNum: blocks[0].Num,
  67. EthAddr: ethCommon.HexToAddress("0x1f9840a85d5af5bf1d1762f925bdaddc4201f984"),
  68. Name: "Uniswap",
  69. Symbol: "UNI",
  70. Decimals: 18,
  71. }, // Used to test default
  72. }
  73. require.NoError(t, historyDB.AddTokens(tokens)) // ETH token exist in DB by default
  74. // Update token price used to test ignore
  75. ignoreValue := 44.44
  76. require.NoError(t, historyDB.UpdateTokenValue(tokens[2].EthAddr, ignoreValue))
  77. // Prepare token config
  78. staticValue := 0.12345
  79. tc := []TokenConfig{
  80. // ETH and UNI tokens use default method
  81. { // DAI uses SC addr
  82. UpdateMethod: UpdateMethodTypeBitFinexV2,
  83. Addr: ethCommon.HexToAddress("0x1"),
  84. Symbol: "DAI",
  85. },
  86. { // USDT uses symbol
  87. UpdateMethod: UpdateMethodTypeCoingeckoV3,
  88. Addr: ethCommon.HexToAddress(usdtAddr),
  89. },
  90. { // FOO uses ignore
  91. UpdateMethod: UpdateMethodTypeIgnore,
  92. Addr: ethCommon.HexToAddress("0x2"),
  93. },
  94. { // BAR uses static
  95. UpdateMethod: UpdateMethodTypeStatic,
  96. Addr: ethCommon.HexToAddress("0x3"),
  97. StaticValue: staticValue,
  98. },
  99. }
  100. bitfinexV2URL := "https://api-pub.bitfinex.com/v2/"
  101. coingeckoV3URL := "https://api.coingecko.com/api/v3/"
  102. // Init price updater
  103. pu, err := NewPriceUpdater(
  104. UpdateMethodTypeCoingeckoV3,
  105. tc,
  106. historyDB,
  107. bitfinexV2URL,
  108. coingeckoV3URL,
  109. )
  110. require.NoError(t, err)
  111. // Update token list
  112. require.NoError(t, pu.UpdateTokenList())
  113. // Update prices
  114. pu.UpdatePrices(context.Background())
  115. // Check results: get tokens from DB
  116. fetchedTokens, err := historyDB.GetTokensTest()
  117. require.NoError(t, err)
  118. // Check that tokens that are updated via API have value:
  119. // ETH
  120. require.NotNil(t, fetchedTokens[0].USDUpdate)
  121. assert.Greater(t, *fetchedTokens[0].USD, 0.0)
  122. // DAI
  123. require.NotNil(t, fetchedTokens[1].USDUpdate)
  124. assert.Greater(t, *fetchedTokens[1].USD, 0.0)
  125. // USDT
  126. require.NotNil(t, fetchedTokens[2].USDUpdate)
  127. assert.Greater(t, *fetchedTokens[2].USD, 0.0)
  128. // UNI
  129. require.NotNil(t, fetchedTokens[5].USDUpdate)
  130. assert.Greater(t, *fetchedTokens[5].USD, 0.0)
  131. // Check ignored token
  132. assert.Equal(t, ignoreValue, *fetchedTokens[3].USD)
  133. // Check static value
  134. assert.Equal(t, staticValue, *fetchedTokens[4].USD)
  135. }