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.

138 lines
4.1 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. type paginationer interface {
  17. GetPagination() pagination
  18. Len() int
  19. }
  20. type errorMsg struct {
  21. Message string
  22. }
  23. // History Tx related
  24. type historyTxsAPI struct {
  25. Txs []historyTxAPI `json:"transactions"`
  26. Pagination pagination `json:"pagination"`
  27. }
  28. func (htx *historyTxsAPI) GetPagination() pagination { return htx.Pagination }
  29. func (htx *historyTxsAPI) Len() int { return len(htx.Txs) }
  30. type l1Info struct {
  31. ToForgeL1TxsNum int64 `json:"toForgeL1TransactionsNum"`
  32. UserOrigin bool `json:"userOrigin"`
  33. FromEthAddr string `json:"fromHezEthereumAddress"`
  34. FromBJJ string `json:"fromBJJ"`
  35. LoadAmount string `json:"loadAmount"`
  36. HistoricLoadAmountUSD *float64 `json:"historicLoadAmountUSD"`
  37. EthBlockNum int64 `json:"ethereumBlockNum"`
  38. }
  39. type l2Info struct {
  40. Fee common.FeeSelector `json:"fee"`
  41. HistoricFeeUSD *float64 `json:"historicFeeUSD"`
  42. Nonce common.Nonce `json:"nonce"`
  43. }
  44. type historyTxAPI struct {
  45. IsL1 string `json:"L1orL2"`
  46. TxID string `json:"id"`
  47. Type common.TxType `json:"type"`
  48. Position int `json:"position"`
  49. FromIdx string `json:"fromAccountIndex"`
  50. ToIdx string `json:"toAccountIndex"`
  51. Amount string `json:"amount"`
  52. BatchNum *common.BatchNum `json:"batchNum"`
  53. HistoricUSD *float64 `json:"historicUSD"`
  54. Timestamp time.Time `json:"timestamp"`
  55. L1Info *l1Info `json:"L1Info"`
  56. L2Info *l2Info `json:"L2Info"`
  57. Token common.Token `json:"token"`
  58. }
  59. func historyTxsToAPI(dbTxs []*historydb.HistoryTx) []historyTxAPI {
  60. apiTxs := []historyTxAPI{}
  61. for i := 0; i < len(dbTxs); i++ {
  62. apiTx := historyTxAPI{
  63. TxID: dbTxs[i].TxID.String(),
  64. Type: dbTxs[i].Type,
  65. Position: dbTxs[i].Position,
  66. FromIdx: idxToHez(dbTxs[i].FromIdx, dbTxs[i].TokenSymbol),
  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: common.Token{
  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].IsL1 {
  86. apiTx.IsL1 = "L1"
  87. apiTx.L1Info = &l1Info{
  88. ToForgeL1TxsNum: dbTxs[i].ToForgeL1TxsNum,
  89. UserOrigin: dbTxs[i].UserOrigin,
  90. FromEthAddr: ethAddrToHez(dbTxs[i].FromEthAddr),
  91. FromBJJ: bjjToString(dbTxs[i].FromBJJ),
  92. LoadAmount: dbTxs[i].LoadAmount.String(),
  93. HistoricLoadAmountUSD: dbTxs[i].HistoricLoadAmountUSD,
  94. EthBlockNum: dbTxs[i].EthBlockNum,
  95. }
  96. } else {
  97. apiTx.IsL1 = "L2"
  98. apiTx.L2Info = &l2Info{
  99. Fee: *dbTxs[i].Fee,
  100. HistoricFeeUSD: dbTxs[i].HistoricFeeUSD,
  101. Nonce: *dbTxs[i].Nonce,
  102. }
  103. }
  104. apiTxs = append(apiTxs, apiTx)
  105. }
  106. return apiTxs
  107. }
  108. func bjjToString(bjj *babyjub.PublicKey) string {
  109. pkComp := [32]byte(bjj.Compress())
  110. sum := pkComp[0]
  111. for i := 1; i < len(pkComp); i++ {
  112. sum += pkComp[i]
  113. }
  114. bjjSum := append(pkComp[:], sum)
  115. return "hez:" + base64.RawURLEncoding.EncodeToString(bjjSum)
  116. }
  117. func ethAddrToHez(addr ethCommon.Address) string {
  118. return "hez:" + addr.String()
  119. }
  120. func idxToHez(idx common.Idx, tokenSymbol string) string {
  121. return "hez:" + tokenSymbol + ":" + strconv.Itoa(int(idx))
  122. }