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.

130 lines
3.7 KiB

  1. package api
  2. import (
  3. "encoding/base64"
  4. "strconv"
  5. "time"
  6. "github.com/hermeznetwork/hermez-node/common"
  7. "github.com/hermeznetwork/hermez-node/db/historydb"
  8. "github.com/iden3/go-iden3-crypto/babyjub"
  9. )
  10. // Commons of the API
  11. type pagination struct {
  12. TotalItems int `json:"totalItems"`
  13. LastReturnedItem int `json:"lastReturnedItem"`
  14. }
  15. type paginationer interface {
  16. GetPagination() pagination
  17. Len() int
  18. }
  19. type errorMsg struct {
  20. Message string
  21. }
  22. // History Tx related
  23. type historyTxsAPI struct {
  24. Txs []historyTxAPI `json:"transactions"`
  25. Pagination pagination `json:"pagination"`
  26. }
  27. func (htx *historyTxsAPI) GetPagination() pagination { return htx.Pagination }
  28. func (htx *historyTxsAPI) Len() int { return len(htx.Txs) }
  29. type l1Info struct {
  30. ToForgeL1TxsNum int64 `json:"toForgeL1TransactionsNum"`
  31. UserOrigin bool `json:"userOrigin"`
  32. FromEthAddr string `json:"fromEthereumAddress"`
  33. FromBJJ string `json:"fromBJJ"`
  34. LoadAmount string `json:"loadAmount"`
  35. LoadAmountUSD float64 `json:"loadAmountUSD"`
  36. EthBlockNum int64 `json:"ethereumBlockNum"`
  37. }
  38. type l2Info struct {
  39. Fee common.FeeSelector `json:"fee"`
  40. FeeUSD float64 `json:"feeUSD"`
  41. Nonce common.Nonce `json:"nonce"`
  42. }
  43. type historyTxAPI struct {
  44. IsL1 string `json:"L1orL2"`
  45. TxID common.TxID `json:"id"`
  46. Type common.TxType `json:"type"`
  47. Position int `json:"position"`
  48. FromIdx string `json:"fromAccountIndex"`
  49. ToIdx string `json:"toAccountIndex"`
  50. Amount string `json:"amount"`
  51. BatchNum *common.BatchNum `json:"batchNum"`
  52. TokenID common.TokenID `json:"tokenId"`
  53. TokenSymbol string `json:"tokenSymbol"`
  54. USD float64 `json:"historicUSD"`
  55. Timestamp time.Time `json:"timestamp"`
  56. CurrentUSD float64 `json:"currentUSD"`
  57. USDUpdate time.Time `json:"fiatUpdate"`
  58. L1Info *l1Info `json:"L1Info"`
  59. L2Info *l2Info `json:"L2Info"`
  60. }
  61. func historyTxsToAPI(dbTxs []*historydb.HistoryTx) []historyTxAPI {
  62. apiTxs := []historyTxAPI{}
  63. for i := 0; i < len(dbTxs); i++ {
  64. apiTx := historyTxAPI{
  65. TxID: dbTxs[i].TxID,
  66. Type: dbTxs[i].Type,
  67. Position: dbTxs[i].Position,
  68. FromIdx: "hez:" + dbTxs[i].TokenSymbol + ":" + strconv.Itoa(int(dbTxs[i].FromIdx)),
  69. ToIdx: "hez:" + dbTxs[i].TokenSymbol + ":" + strconv.Itoa(int(dbTxs[i].ToIdx)),
  70. Amount: dbTxs[i].Amount.String(),
  71. TokenID: dbTxs[i].TokenID,
  72. USD: dbTxs[i].USD,
  73. BatchNum: nil,
  74. Timestamp: dbTxs[i].Timestamp,
  75. TokenSymbol: dbTxs[i].TokenSymbol,
  76. CurrentUSD: dbTxs[i].CurrentUSD,
  77. USDUpdate: dbTxs[i].USDUpdate,
  78. L1Info: nil,
  79. L2Info: nil,
  80. }
  81. bn := dbTxs[i].BatchNum
  82. if dbTxs[i].BatchNum != 0 {
  83. apiTx.BatchNum = &bn
  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: "hez:" + dbTxs[i].FromEthAddr.String(),
  91. FromBJJ: bjjToString(dbTxs[i].FromBJJ),
  92. LoadAmount: dbTxs[i].LoadAmount.String(),
  93. LoadAmountUSD: dbTxs[i].LoadAmountUSD,
  94. EthBlockNum: dbTxs[i].EthBlockNum,
  95. }
  96. } else {
  97. apiTx.IsL1 = "L2"
  98. apiTx.L2Info = &l2Info{
  99. Fee: dbTxs[i].Fee,
  100. FeeUSD: dbTxs[i].FeeUSD,
  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. }