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.

150 lines
5.1 KiB

  1. package api
  2. import (
  3. "encoding/base64"
  4. "math/big"
  5. "strconv"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/hermeznetwork/hermez-node/common"
  8. "github.com/hermeznetwork/hermez-node/db"
  9. "github.com/hermeznetwork/hermez-node/db/historydb"
  10. "github.com/hermeznetwork/hermez-node/eth"
  11. "github.com/iden3/go-iden3-crypto/babyjub"
  12. )
  13. const exitIdx = "hez:EXIT:1"
  14. type errorMsg struct {
  15. Message string
  16. }
  17. func bjjToString(bjj *babyjub.PublicKey) string {
  18. pkComp := [32]byte(bjj.Compress())
  19. sum := pkComp[0]
  20. for i := 1; i < len(pkComp); i++ {
  21. sum += pkComp[i]
  22. }
  23. bjjSum := append(pkComp[:], sum)
  24. return "hez:" + base64.RawURLEncoding.EncodeToString(bjjSum)
  25. }
  26. func ethAddrToHez(addr ethCommon.Address) string {
  27. return "hez:" + addr.String()
  28. }
  29. func idxToHez(idx common.Idx, tokenSymbol string) string {
  30. if idx == 1 {
  31. return exitIdx
  32. }
  33. return "hez:" + tokenSymbol + ":" + strconv.Itoa(int(idx))
  34. }
  35. // Exit
  36. type exitsAPI struct {
  37. Exits []exitAPI `json:"exits"`
  38. Pagination *db.Pagination `json:"pagination"`
  39. }
  40. func (e *exitsAPI) GetPagination() *db.Pagination {
  41. if e.Exits[0].ItemID < e.Exits[len(e.Exits)-1].ItemID {
  42. e.Pagination.FirstReturnedItem = e.Exits[0].ItemID
  43. e.Pagination.LastReturnedItem = e.Exits[len(e.Exits)-1].ItemID
  44. } else {
  45. e.Pagination.LastReturnedItem = e.Exits[0].ItemID
  46. e.Pagination.FirstReturnedItem = e.Exits[len(e.Exits)-1].ItemID
  47. }
  48. return e.Pagination
  49. }
  50. func (e *exitsAPI) Len() int { return len(e.Exits) }
  51. type merkleProofAPI struct {
  52. Root string
  53. Siblings []string
  54. OldKey string
  55. OldValue string
  56. IsOld0 bool
  57. Key string
  58. Value string
  59. Fnc int
  60. }
  61. type exitAPI struct {
  62. ItemID int `json:"itemId"`
  63. BatchNum common.BatchNum `json:"batchNum"`
  64. AccountIdx string `json:"accountIndex"`
  65. MerkleProof merkleProofAPI `json:"merkleProof"`
  66. Balance string `json:"balance"`
  67. InstantWithdrawn *int64 `json:"instantWithdrawn"`
  68. DelayedWithdrawRequest *int64 `json:"delayedWithdrawRequest"`
  69. DelayedWithdrawn *int64 `json:"delayedWithdrawn"`
  70. Token historydb.TokenWithUSD `json:"token"`
  71. }
  72. func historyExitsToAPI(dbExits []historydb.HistoryExit) []exitAPI {
  73. apiExits := []exitAPI{}
  74. for i := 0; i < len(dbExits); i++ {
  75. exit := exitAPI{
  76. ItemID: dbExits[i].ItemID,
  77. BatchNum: dbExits[i].BatchNum,
  78. AccountIdx: idxToHez(dbExits[i].AccountIdx, dbExits[i].TokenSymbol),
  79. MerkleProof: merkleProofAPI{
  80. Root: dbExits[i].MerkleProof.Root.String(),
  81. OldKey: dbExits[i].MerkleProof.OldKey.String(),
  82. OldValue: dbExits[i].MerkleProof.OldValue.String(),
  83. IsOld0: dbExits[i].MerkleProof.IsOld0,
  84. Key: dbExits[i].MerkleProof.Key.String(),
  85. Value: dbExits[i].MerkleProof.Value.String(),
  86. Fnc: dbExits[i].MerkleProof.Fnc,
  87. },
  88. Balance: dbExits[i].Balance.String(),
  89. InstantWithdrawn: dbExits[i].InstantWithdrawn,
  90. DelayedWithdrawRequest: dbExits[i].DelayedWithdrawRequest,
  91. DelayedWithdrawn: dbExits[i].DelayedWithdrawn,
  92. Token: historydb.TokenWithUSD{
  93. TokenID: dbExits[i].TokenID,
  94. EthBlockNum: dbExits[i].TokenEthBlockNum,
  95. EthAddr: dbExits[i].TokenEthAddr,
  96. Name: dbExits[i].TokenName,
  97. Symbol: dbExits[i].TokenSymbol,
  98. Decimals: dbExits[i].TokenDecimals,
  99. USD: dbExits[i].TokenUSD,
  100. USDUpdate: dbExits[i].TokenUSDUpdate,
  101. },
  102. }
  103. siblings := []string{}
  104. for j := 0; j < len(dbExits[i].MerkleProof.Siblings); j++ {
  105. siblings = append(siblings, dbExits[i].MerkleProof.Siblings[j].String())
  106. }
  107. exit.MerkleProof.Siblings = siblings
  108. apiExits = append(apiExits, exit)
  109. }
  110. return apiExits
  111. }
  112. // Config
  113. type rollupConstants struct {
  114. PublicConstants eth.RollupPublicConstants `json:"publicConstants"`
  115. MaxFeeIdxCoordinator int `json:"maxFeeIdxCoordinator"`
  116. ReservedIdx int `json:"reservedIdx"`
  117. ExitIdx int `json:"exitIdx"`
  118. LimitLoadAmount *big.Int `json:"limitLoadAmount"`
  119. LimitL2TransferAmount *big.Int `json:"limitL2TransferAmount"`
  120. LimitTokens int `json:"limitTokens"`
  121. L1CoordinatorTotalBytes int `json:"l1CoordinatorTotalBytes"`
  122. L1UserTotalBytes int `json:"l1UserTotalBytes"`
  123. MaxL1UserTx int `json:"maxL1UserTx"`
  124. MaxL1Tx int `json:"maxL1Tx"`
  125. InputSHAConstantBytes int `json:"inputSHAConstantBytes"`
  126. NumBuckets int `json:"numBuckets"`
  127. MaxWithdrawalDelay int `json:"maxWithdrawalDelay"`
  128. ExchangeMultiplier int `json:"exchangeMultiplier"`
  129. }
  130. type configAPI struct {
  131. RollupConstants rollupConstants `json:"hermez"`
  132. AuctionConstants eth.AuctionConstants `json:"auction"`
  133. WDelayerConstants eth.WDelayerConstants `json:"withdrawalDelayer"`
  134. }