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/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. }
  35. // PoolTxAPI represents a L2 Tx pool with extra metadata used by the API
  36. type PoolTxAPI struct {
  37. TxID common.TxID `meddler:"tx_id"`
  38. FromIdx apitypes.HezIdx `meddler:"from_idx"`
  39. EffectiveFromEthAddr *apitypes.HezEthAddr `meddler:"effective_from_eth_addr"`
  40. EffectiveFromBJJ *apitypes.HezBJJ `meddler:"effective_from_bjj"`
  41. ToIdx *apitypes.HezIdx `meddler:"to_idx"`
  42. EffectiveToEthAddr *apitypes.HezEthAddr `meddler:"effective_to_eth_addr"`
  43. EffectiveToBJJ *apitypes.HezBJJ `meddler:"effective_to_bjj"`
  44. Amount apitypes.BigIntStr `meddler:"amount"`
  45. Fee common.FeeSelector `meddler:"fee"`
  46. Nonce common.Nonce `meddler:"nonce"`
  47. State common.PoolL2TxState `meddler:"state"`
  48. Info *string `meddler:"info"`
  49. Signature babyjub.SignatureComp `meddler:"signature"`
  50. RqFromIdx *apitypes.HezIdx `meddler:"rq_from_idx"`
  51. RqToIdx *apitypes.HezIdx `meddler:"rq_to_idx"`
  52. RqToEthAddr *apitypes.HezEthAddr `meddler:"rq_to_eth_addr"`
  53. RqToBJJ *apitypes.HezBJJ `meddler:"rq_to_bjj"`
  54. RqTokenID *common.TokenID `meddler:"rq_token_id"`
  55. RqAmount *apitypes.BigIntStr `meddler:"rq_amount"`
  56. RqFee *common.FeeSelector `meddler:"rq_fee"`
  57. RqNonce *common.Nonce `meddler:"rq_nonce"`
  58. Type common.TxType `meddler:"tx_type"`
  59. // Extra read fileds
  60. BatchNum *common.BatchNum `meddler:"batch_num"`
  61. Timestamp time.Time `meddler:"timestamp,utctime"`
  62. TotalItems uint64 `meddler:"total_items"`
  63. TokenID common.TokenID `meddler:"token_id"`
  64. TokenItemID uint64 `meddler:"token_item_id"`
  65. TokenEthBlockNum int64 `meddler:"eth_block_num"`
  66. TokenEthAddr ethCommon.Address `meddler:"eth_addr"`
  67. TokenName string `meddler:"name"`
  68. TokenSymbol string `meddler:"symbol"`
  69. TokenDecimals uint64 `meddler:"decimals"`
  70. TokenUSD *float64 `meddler:"usd"`
  71. TokenUSDUpdate *time.Time `meddler:"usd_update"`
  72. }
  73. // MarshalJSON is used to neast some of the fields of PoolTxAPI
  74. // without the need of auxiliar structs
  75. func (tx PoolTxAPI) MarshalJSON() ([]byte, error) {
  76. return json.Marshal(map[string]interface{}{
  77. "id": tx.TxID,
  78. "type": tx.Type,
  79. "fromAccountIndex": tx.FromIdx,
  80. "fromHezEthereumAddress": tx.EffectiveFromEthAddr,
  81. "fromBJJ": tx.EffectiveFromBJJ,
  82. "toAccountIndex": tx.ToIdx,
  83. "toHezEthereumAddress": tx.EffectiveToEthAddr,
  84. "toBjj": tx.EffectiveToBJJ,
  85. "amount": tx.Amount,
  86. "fee": tx.Fee,
  87. "nonce": tx.Nonce,
  88. "state": tx.State,
  89. "info": tx.Info,
  90. "signature": tx.Signature,
  91. "timestamp": tx.Timestamp,
  92. "batchNum": tx.BatchNum,
  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. }