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.

126 lines
5.9 KiB

  1. package l2db
  2. import (
  3. "encoding/json"
  4. "math/big"
  5. "time"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/hermeznetwork/hermez-node/api/apitypes"
  8. "github.com/hermeznetwork/hermez-node/common"
  9. "github.com/iden3/go-iden3-crypto/babyjub"
  10. )
  11. // PoolL2TxWrite holds the necessary data to perform inserts in tx_pool
  12. type PoolL2TxWrite struct {
  13. TxID common.TxID `meddler:"tx_id"`
  14. FromIdx common.Idx `meddler:"from_idx"`
  15. ToIdx *common.Idx `meddler:"to_idx"`
  16. ToEthAddr *ethCommon.Address `meddler:"to_eth_addr"`
  17. ToBJJ *babyjub.PublicKeyComp `meddler:"to_bjj"`
  18. TokenID common.TokenID `meddler:"token_id"`
  19. Amount *big.Int `meddler:"amount,bigint"`
  20. AmountFloat float64 `meddler:"amount_f"`
  21. Fee common.FeeSelector `meddler:"fee"`
  22. Nonce common.Nonce `meddler:"nonce"`
  23. State common.PoolL2TxState `meddler:"state"`
  24. Signature babyjub.SignatureComp `meddler:"signature"`
  25. RqFromIdx *common.Idx `meddler:"rq_from_idx"`
  26. RqToIdx *common.Idx `meddler:"rq_to_idx"`
  27. RqToEthAddr *ethCommon.Address `meddler:"rq_to_eth_addr"`
  28. RqToBJJ *babyjub.PublicKeyComp `meddler:"rq_to_bjj"`
  29. RqTokenID *common.TokenID `meddler:"rq_token_id"`
  30. RqAmount *big.Int `meddler:"rq_amount,bigintnull"`
  31. RqFee *common.FeeSelector `meddler:"rq_fee"`
  32. RqNonce *common.Nonce `meddler:"rq_nonce"`
  33. Type common.TxType `meddler:"tx_type"`
  34. ClientIP string `meddler:"client_ip"`
  35. }
  36. // PoolTxAPI represents a L2 Tx pool with extra metadata used by the API
  37. type PoolTxAPI struct {
  38. TxID common.TxID `meddler:"tx_id"`
  39. FromIdx apitypes.HezIdx `meddler:"from_idx"`
  40. EffectiveFromEthAddr *apitypes.HezEthAddr `meddler:"effective_from_eth_addr"`
  41. EffectiveFromBJJ *apitypes.HezBJJ `meddler:"effective_from_bjj"`
  42. ToIdx *apitypes.HezIdx `meddler:"to_idx"`
  43. EffectiveToEthAddr *apitypes.HezEthAddr `meddler:"effective_to_eth_addr"`
  44. EffectiveToBJJ *apitypes.HezBJJ `meddler:"effective_to_bjj"`
  45. Amount apitypes.BigIntStr `meddler:"amount"`
  46. Fee common.FeeSelector `meddler:"fee"`
  47. Nonce common.Nonce `meddler:"nonce"`
  48. State common.PoolL2TxState `meddler:"state"`
  49. Info *string `meddler:"info"`
  50. Signature babyjub.SignatureComp `meddler:"signature"`
  51. RqFromIdx *apitypes.HezIdx `meddler:"rq_from_idx"`
  52. RqToIdx *apitypes.HezIdx `meddler:"rq_to_idx"`
  53. RqToEthAddr *apitypes.HezEthAddr `meddler:"rq_to_eth_addr"`
  54. RqToBJJ *apitypes.HezBJJ `meddler:"rq_to_bjj"`
  55. RqTokenID *common.TokenID `meddler:"rq_token_id"`
  56. RqAmount *apitypes.BigIntStr `meddler:"rq_amount"`
  57. RqFee *common.FeeSelector `meddler:"rq_fee"`
  58. RqNonce *common.Nonce `meddler:"rq_nonce"`
  59. Type common.TxType `meddler:"tx_type"`
  60. // Extra read fileds
  61. BatchNum *common.BatchNum `meddler:"batch_num"`
  62. Timestamp time.Time `meddler:"timestamp,utctime"`
  63. TotalItems uint64 `meddler:"total_items"`
  64. TokenID common.TokenID `meddler:"token_id"`
  65. TokenItemID uint64 `meddler:"token_item_id"`
  66. TokenEthBlockNum int64 `meddler:"eth_block_num"`
  67. TokenEthAddr ethCommon.Address `meddler:"eth_addr"`
  68. TokenName string `meddler:"name"`
  69. TokenSymbol string `meddler:"symbol"`
  70. TokenDecimals uint64 `meddler:"decimals"`
  71. TokenUSD *float64 `meddler:"usd"`
  72. TokenUSDUpdate *time.Time `meddler:"usd_update"`
  73. }
  74. // MarshalJSON is used to neast some of the fields of PoolTxAPI
  75. // without the need of auxiliar structs
  76. func (tx PoolTxAPI) MarshalJSON() ([]byte, error) {
  77. return json.Marshal(map[string]interface{}{
  78. "id": tx.TxID,
  79. "type": tx.Type,
  80. "fromAccountIndex": tx.FromIdx,
  81. "fromHezEthereumAddress": tx.EffectiveFromEthAddr,
  82. "fromBJJ": tx.EffectiveFromBJJ,
  83. "toAccountIndex": tx.ToIdx,
  84. "toHezEthereumAddress": tx.EffectiveToEthAddr,
  85. "toBjj": tx.EffectiveToBJJ,
  86. "amount": tx.Amount,
  87. "fee": tx.Fee,
  88. "nonce": tx.Nonce,
  89. "state": tx.State,
  90. "info": tx.Info,
  91. "signature": tx.Signature,
  92. "timestamp": tx.Timestamp,
  93. "requestFromAccountIndex": tx.RqFromIdx,
  94. "requestToAccountIndex": tx.RqToIdx,
  95. "requestToHezEthereumAddress": tx.RqToEthAddr,
  96. "requestToBJJ": tx.RqToBJJ,
  97. "requestTokenId": tx.RqTokenID,
  98. "requestAmount": tx.RqAmount,
  99. "requestFee": tx.RqFee,
  100. "requestNonce": tx.RqNonce,
  101. "token": map[string]interface{}{
  102. "id": tx.TokenID,
  103. "itemId": tx.TokenItemID,
  104. "ethereumBlockNum": tx.TokenEthBlockNum,
  105. "ethereumAddress": tx.TokenEthAddr,
  106. "name": tx.TokenName,
  107. "symbol": tx.TokenSymbol,
  108. "decimals": tx.TokenDecimals,
  109. "USD": tx.TokenUSD,
  110. "fiatUpdate": tx.TokenUSDUpdate,
  111. },
  112. })
  113. }
  114. // AccountCreationAuthAPI represents an account creation auth in the expected format by the API
  115. type AccountCreationAuthAPI struct {
  116. EthAddr apitypes.HezEthAddr `json:"hezEthereumAddress" meddler:"eth_addr" `
  117. BJJ apitypes.HezBJJ `json:"bjj" meddler:"bjj" `
  118. Signature apitypes.EthSignature `json:"signature" meddler:"signature" `
  119. Timestamp time.Time `json:"timestamp" meddler:"timestamp,utctime"`
  120. }