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.

142 lines
4.2 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. ToIdx: idxToHez(dbTxs[i].ToIdx, dbTxs[i].TokenSymbol),
  67. Amount: dbTxs[i].Amount.String(),
  68. HistoricUSD: dbTxs[i].HistoricUSD,
  69. BatchNum: dbTxs[i].BatchNum,
  70. Timestamp: dbTxs[i].Timestamp,
  71. Token: common.Token{
  72. TokenID: dbTxs[i].TokenID,
  73. EthBlockNum: dbTxs[i].TokenEthBlockNum,
  74. EthAddr: dbTxs[i].TokenEthAddr,
  75. Name: dbTxs[i].TokenName,
  76. Symbol: dbTxs[i].TokenSymbol,
  77. Decimals: dbTxs[i].TokenDecimals,
  78. USD: dbTxs[i].TokenUSD,
  79. USDUpdate: dbTxs[i].TokenUSDUpdate,
  80. },
  81. L1Info: nil,
  82. L2Info: nil,
  83. }
  84. if dbTxs[i].FromIdx != nil {
  85. fromIdx := new(string)
  86. *fromIdx = idxToHez(*dbTxs[i].FromIdx, dbTxs[i].TokenSymbol)
  87. apiTx.FromIdx = fromIdx
  88. }
  89. if dbTxs[i].IsL1 {
  90. apiTx.IsL1 = "L1"
  91. apiTx.L1Info = &l1Info{
  92. ToForgeL1TxsNum: dbTxs[i].ToForgeL1TxsNum,
  93. UserOrigin: *dbTxs[i].UserOrigin,
  94. FromEthAddr: ethAddrToHez(*dbTxs[i].FromEthAddr),
  95. FromBJJ: bjjToString(dbTxs[i].FromBJJ),
  96. LoadAmount: dbTxs[i].LoadAmount.String(),
  97. HistoricLoadAmountUSD: dbTxs[i].HistoricLoadAmountUSD,
  98. EthBlockNum: dbTxs[i].EthBlockNum,
  99. }
  100. } else {
  101. apiTx.IsL1 = "L2"
  102. apiTx.L2Info = &l2Info{
  103. Fee: *dbTxs[i].Fee,
  104. HistoricFeeUSD: dbTxs[i].HistoricFeeUSD,
  105. Nonce: *dbTxs[i].Nonce,
  106. }
  107. }
  108. apiTxs = append(apiTxs, apiTx)
  109. }
  110. return apiTxs
  111. }
  112. func bjjToString(bjj *babyjub.PublicKey) string {
  113. pkComp := [32]byte(bjj.Compress())
  114. sum := pkComp[0]
  115. for i := 1; i < len(pkComp); i++ {
  116. sum += pkComp[i]
  117. }
  118. bjjSum := append(pkComp[:], sum)
  119. return "hez:" + base64.RawURLEncoding.EncodeToString(bjjSum)
  120. }
  121. func ethAddrToHez(addr ethCommon.Address) string {
  122. return "hez:" + addr.String()
  123. }
  124. func idxToHez(idx common.Idx, tokenSymbol string) string {
  125. return "hez:" + tokenSymbol + ":" + strconv.Itoa(int(idx))
  126. }