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.

124 lines
5.5 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.PublicKey `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.PublicKey `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. FromEthAddr *apitypes.HezEthAddr `meddler:"from_eth_addr"`
  40. FromBJJ *apitypes.HezBJJ `meddler:"from_bjj"`
  41. ToIdx *apitypes.HezIdx `meddler:"to_idx"`
  42. ToEthAddr *apitypes.HezEthAddr `meddler:"to_eth_addr"`
  43. ToBJJ *apitypes.HezBJJ `meddler:"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. Signature babyjub.SignatureComp `meddler:"signature"`
  49. RqFromIdx *apitypes.HezIdx `meddler:"rq_from_idx"`
  50. RqToIdx *apitypes.HezIdx `meddler:"rq_to_idx"`
  51. RqToEthAddr *apitypes.HezEthAddr `meddler:"rq_to_eth_addr"`
  52. RqToBJJ *apitypes.HezBJJ `meddler:"rq_to_bjj"`
  53. RqTokenID *common.TokenID `meddler:"rq_token_id"`
  54. RqAmount *apitypes.BigIntStr `meddler:"rq_amount"`
  55. RqFee *common.FeeSelector `meddler:"rq_fee"`
  56. RqNonce *common.Nonce `meddler:"rq_nonce"`
  57. Type common.TxType `meddler:"tx_type"`
  58. // Extra read fileds
  59. BatchNum *common.BatchNum `meddler:"batch_num"`
  60. Timestamp time.Time `meddler:"timestamp,utctime"`
  61. TotalItems uint64 `meddler:"total_items"`
  62. TokenID common.TokenID `meddler:"token_id"`
  63. TokenItemID uint64 `meddler:"token_item_id"`
  64. TokenEthBlockNum int64 `meddler:"eth_block_num"`
  65. TokenEthAddr ethCommon.Address `meddler:"eth_addr"`
  66. TokenName string `meddler:"name"`
  67. TokenSymbol string `meddler:"symbol"`
  68. TokenDecimals uint64 `meddler:"decimals"`
  69. TokenUSD *float64 `meddler:"usd"`
  70. TokenUSDUpdate *time.Time `meddler:"usd_update"`
  71. }
  72. // MarshalJSON is used to neast some of the fields of PoolTxAPI
  73. // without the need of auxiliar structs
  74. func (tx PoolTxAPI) MarshalJSON() ([]byte, error) {
  75. return json.Marshal(map[string]interface{}{
  76. "id": tx.TxID,
  77. "type": tx.Type,
  78. "fromAccountIndex": tx.FromIdx,
  79. "fromHezEthereumAddress": tx.FromEthAddr,
  80. "fromBJJ": tx.FromBJJ,
  81. "toAccountIndex": tx.ToIdx,
  82. "toHezEthereumAddress": tx.ToEthAddr,
  83. "toBjj": tx.ToBJJ,
  84. "amount": tx.Amount,
  85. "fee": tx.Fee,
  86. "nonce": tx.Nonce,
  87. "state": tx.State,
  88. "signature": tx.Signature,
  89. "timestamp": tx.Timestamp,
  90. "batchNum": tx.BatchNum,
  91. "requestFromAccountIndex": tx.RqFromIdx,
  92. "requestToAccountIndex": tx.RqToIdx,
  93. "requestToHezEthereumAddress": tx.RqToEthAddr,
  94. "requestToBJJ": tx.RqToBJJ,
  95. "requestTokenId": tx.RqTokenID,
  96. "requestAmount": tx.RqAmount,
  97. "requestFee": tx.RqFee,
  98. "requestNonce": tx.RqNonce,
  99. "token": map[string]interface{}{
  100. "id": tx.TokenID,
  101. "itemId": tx.TokenItemID,
  102. "ethereumBlockNum": tx.TokenEthBlockNum,
  103. "ethereumAddress": tx.TokenEthAddr,
  104. "name": tx.TokenName,
  105. "symbol": tx.TokenSymbol,
  106. "decimals": tx.TokenDecimals,
  107. "USD": tx.TokenUSD,
  108. "fiatUpdate": tx.TokenUSDUpdate,
  109. },
  110. })
  111. }
  112. // AccountCreationAuthAPI represents an account creation auth in the expected format by the API
  113. type AccountCreationAuthAPI struct {
  114. EthAddr apitypes.HezEthAddr `json:"hezEthereumAddress" meddler:"eth_addr" `
  115. BJJ apitypes.HezBJJ `json:"bjj" meddler:"bjj" `
  116. Signature apitypes.EthSignature `json:"signature" meddler:"signature" `
  117. Timestamp time.Time `json:"timestamp" meddler:"timestamp,utctime"`
  118. }