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.

127 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/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. "batchNum": tx.BatchNum,
  94. "requestFromAccountIndex": tx.RqFromIdx,
  95. "requestToAccountIndex": tx.RqToIdx,
  96. "requestToHezEthereumAddress": tx.RqToEthAddr,
  97. "requestToBJJ": tx.RqToBJJ,
  98. "requestTokenId": tx.RqTokenID,
  99. "requestAmount": tx.RqAmount,
  100. "requestFee": tx.RqFee,
  101. "requestNonce": tx.RqNonce,
  102. "token": map[string]interface{}{
  103. "id": tx.TokenID,
  104. "itemId": tx.TokenItemID,
  105. "ethereumBlockNum": tx.TokenEthBlockNum,
  106. "ethereumAddress": tx.TokenEthAddr,
  107. "name": tx.TokenName,
  108. "symbol": tx.TokenSymbol,
  109. "decimals": tx.TokenDecimals,
  110. "USD": tx.TokenUSD,
  111. "fiatUpdate": tx.TokenUSDUpdate,
  112. },
  113. })
  114. }
  115. // AccountCreationAuthAPI represents an account creation auth in the expected format by the API
  116. type AccountCreationAuthAPI struct {
  117. EthAddr apitypes.HezEthAddr `json:"hezEthereumAddress" meddler:"eth_addr" `
  118. BJJ apitypes.HezBJJ `json:"bjj" meddler:"bjj" `
  119. Signature apitypes.EthSignature `json:"signature" meddler:"signature" `
  120. Timestamp time.Time `json:"timestamp" meddler:"timestamp,utctime"`
  121. }