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
4.3 KiB

  1. package api
  2. import (
  3. "encoding/base64"
  4. "strconv"
  5. "time"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/hermeznetwork/hermez-node/common"
  8. "github.com/hermeznetwork/hermez-node/db/historydb"
  9. "github.com/iden3/go-iden3-crypto/babyjub"
  10. )
  11. // Commons of the API
  12. type pagination struct {
  13. TotalItems int `json:"totalItems"`
  14. LastReturnedItem int `json:"lastReturnedItem"`
  15. }
  16. //nolint:govet this is a temp patch to avoid running the test
  17. type paginationer interface {
  18. GetPagination() pagination
  19. Len() int
  20. }
  21. type errorMsg struct {
  22. Message string
  23. }
  24. // History Tx related
  25. type historyTxsAPI struct {
  26. Txs []historyTxAPI `json:"transactions"`
  27. Pagination pagination `json:"pagination"`
  28. }
  29. func (htx *historyTxsAPI) GetPagination() pagination { return htx.Pagination }
  30. func (htx *historyTxsAPI) Len() int { return len(htx.Txs) }
  31. type l1Info struct {
  32. ToForgeL1TxsNum *int64 `json:"toForgeL1TransactionsNum"`
  33. UserOrigin bool `json:"userOrigin"`
  34. FromEthAddr string `json:"fromHezEthereumAddress"`
  35. FromBJJ string `json:"fromBJJ"`
  36. LoadAmount string `json:"loadAmount"`
  37. HistoricLoadAmountUSD *float64 `json:"historicLoadAmountUSD"`
  38. EthBlockNum int64 `json:"ethereumBlockNum"`
  39. }
  40. type l2Info struct {
  41. Fee common.FeeSelector `json:"fee"`
  42. HistoricFeeUSD *float64 `json:"historicFeeUSD"`
  43. Nonce common.Nonce `json:"nonce"`
  44. }
  45. type historyTxAPI struct {
  46. IsL1 string `json:"L1orL2"`
  47. TxID string `json:"id"`
  48. Type common.TxType `json:"type"`
  49. Position int `json:"position"`
  50. FromIdx *string `json:"fromAccountIndex"`
  51. ToIdx string `json:"toAccountIndex"`
  52. Amount string `json:"amount"`
  53. BatchNum *common.BatchNum `json:"batchNum"`
  54. HistoricUSD *float64 `json:"historicUSD"`
  55. Timestamp time.Time `json:"timestamp"`
  56. L1Info *l1Info `json:"L1Info"`
  57. L2Info *l2Info `json:"L2Info"`
  58. Token historydb.TokenRead `json:"token"`
  59. }
  60. func historyTxsToAPI(dbTxs []historydb.HistoryTx) []historyTxAPI {
  61. apiTxs := []historyTxAPI{}
  62. for i := 0; i < len(dbTxs); i++ {
  63. apiTx := historyTxAPI{
  64. TxID: dbTxs[i].TxID.String(),
  65. Type: dbTxs[i].Type,
  66. Position: dbTxs[i].Position,
  67. ToIdx: idxToHez(dbTxs[i].ToIdx, dbTxs[i].TokenSymbol),
  68. Amount: dbTxs[i].Amount.String(),
  69. HistoricUSD: dbTxs[i].HistoricUSD,
  70. BatchNum: dbTxs[i].BatchNum,
  71. Timestamp: dbTxs[i].Timestamp,
  72. Token: historydb.TokenRead{
  73. TokenID: dbTxs[i].TokenID,
  74. EthBlockNum: dbTxs[i].TokenEthBlockNum,
  75. EthAddr: dbTxs[i].TokenEthAddr,
  76. Name: dbTxs[i].TokenName,
  77. Symbol: dbTxs[i].TokenSymbol,
  78. Decimals: dbTxs[i].TokenDecimals,
  79. USD: dbTxs[i].TokenUSD,
  80. USDUpdate: dbTxs[i].TokenUSDUpdate,
  81. },
  82. L1Info: nil,
  83. L2Info: nil,
  84. }
  85. if dbTxs[i].FromIdx != nil {
  86. fromIdx := new(string)
  87. *fromIdx = idxToHez(*dbTxs[i].FromIdx, dbTxs[i].TokenSymbol)
  88. apiTx.FromIdx = fromIdx
  89. }
  90. if dbTxs[i].IsL1 {
  91. apiTx.IsL1 = "L1"
  92. apiTx.L1Info = &l1Info{
  93. ToForgeL1TxsNum: dbTxs[i].ToForgeL1TxsNum,
  94. UserOrigin: *dbTxs[i].UserOrigin,
  95. FromEthAddr: ethAddrToHez(*dbTxs[i].FromEthAddr),
  96. FromBJJ: bjjToString(dbTxs[i].FromBJJ),
  97. LoadAmount: dbTxs[i].LoadAmount.String(),
  98. HistoricLoadAmountUSD: dbTxs[i].HistoricLoadAmountUSD,
  99. EthBlockNum: dbTxs[i].EthBlockNum,
  100. }
  101. } else {
  102. apiTx.IsL1 = "L2"
  103. apiTx.L2Info = &l2Info{
  104. Fee: *dbTxs[i].Fee,
  105. HistoricFeeUSD: dbTxs[i].HistoricFeeUSD,
  106. Nonce: *dbTxs[i].Nonce,
  107. }
  108. }
  109. apiTxs = append(apiTxs, apiTx)
  110. }
  111. return apiTxs
  112. }
  113. func bjjToString(bjj *babyjub.PublicKey) string {
  114. pkComp := [32]byte(bjj.Compress())
  115. sum := pkComp[0]
  116. for i := 1; i < len(pkComp); i++ {
  117. sum += pkComp[i]
  118. }
  119. bjjSum := append(pkComp[:], sum)
  120. return "hez:" + base64.RawURLEncoding.EncodeToString(bjjSum)
  121. }
  122. func ethAddrToHez(addr ethCommon.Address) string {
  123. return "hez:" + addr.String()
  124. }
  125. func idxToHez(idx common.Idx, tokenSymbol string) string {
  126. return "hez:" + tokenSymbol + ":" + strconv.Itoa(int(idx))
  127. }