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.

89 lines
3.6 KiB

  1. package common
  2. import (
  3. "math/big"
  4. ethCommon "github.com/ethereum/go-ethereum/common"
  5. "github.com/iden3/go-iden3-crypto/babyjub"
  6. )
  7. // TxID is the identifier of a Hermez network transaction
  8. type TxID Hash // Hash is a guess
  9. // TxType is a string that represents the type of a Hermez network transaction
  10. type TxType string
  11. const (
  12. // TxTypeExit represents L2->L1 token transfer. A leaf for this account appears in the exit tree of the block
  13. TxTypeExit TxType = "Exit"
  14. // TxTypeWithdrawn represents the balance that was moved from L2->L1 has been widthrawn from the smart contract
  15. TxTypeWithdrawn TxType = "Withdrawn"
  16. // TxTypeTransfer represents L2->L2 token transfer
  17. TxTypeTransfer TxType = "Transfer"
  18. // TxTypeDeposit represents L1->L2 transfer
  19. TxTypeDeposit TxType = "Deposit"
  20. // TxTypeCreateAccountDeposit represents creation of a new leaf in the state tree (newAcconut) + L1->L2 transfer
  21. TxTypeCreateAccountDeposit TxType = "CreateAccountDeposit"
  22. // TxTypeCreateAccountDepositTransfer represents L1->L2 transfer + L2->L2 transfer
  23. TxTypeCreateAccountDepositTransfer TxType = "CreateAccountDepositTransfer"
  24. // TxTypeDepositTransfer TBD
  25. TxTypeDepositTransfer TxType = "DepositTransfer"
  26. // TxTypeForceTransfer TBD
  27. TxTypeForceTransfer TxType = "ForceTransfer"
  28. // TxTypeForceExit TBD
  29. TxTypeForceExit TxType = "ForceExit"
  30. // TxTypeTransferToEthAddr TBD
  31. TxTypeTransferToEthAddr TxType = "TransferToEthAddr"
  32. // TxTypeTransferToBJJ TBD
  33. TxTypeTransferToBJJ TxType = "TransferToBJJ"
  34. )
  35. // Tx is a struct used by the TxSelector & BatchBuilder as a generic type generated from L1Tx & PoolL2Tx
  36. type Tx struct {
  37. // Generic
  38. IsL1 bool `meddler:"is_l1"`
  39. TxID TxID `meddler:"id"`
  40. Type TxType `meddler:"type"`
  41. Position int `meddler:"position"`
  42. FromIdx Idx `meddler:"from_idx"`
  43. ToIdx Idx `meddler:"to_idx"`
  44. Amount *big.Int `meddler:"amount,bigint"`
  45. AmountFloat float64 `meddler:"amount_f"`
  46. TokenID TokenID `meddler:"token_id"`
  47. USD *float64 `meddler:"amount_usd"`
  48. BatchNum *BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
  49. EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
  50. // L1
  51. ToForgeL1TxsNum int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
  52. UserOrigin bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
  53. FromEthAddr ethCommon.Address `meddler:"from_eth_addr"`
  54. FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
  55. LoadAmount *big.Int `meddler:"load_amount,bigintnull"`
  56. LoadAmountFloat *float64 `meddler:"load_amount_f"`
  57. LoadAmountUSD *float64 `meddler:"load_amount_usd"`
  58. // L2
  59. Fee *FeeSelector `meddler:"fee"`
  60. FeeUSD *float64 `meddler:"fee_usd"`
  61. Nonce *Nonce `meddler:"nonce"`
  62. }
  63. // L1Tx returns a *L1Tx from the Tx
  64. func (tx *Tx) L1Tx() *L1Tx {
  65. l1Tx := &L1Tx{
  66. TxID: tx.TxID,
  67. ToForgeL1TxsNum: tx.ToForgeL1TxsNum,
  68. Position: tx.Position,
  69. UserOrigin: tx.UserOrigin,
  70. FromIdx: tx.FromIdx,
  71. FromEthAddr: tx.FromEthAddr,
  72. FromBJJ: tx.FromBJJ,
  73. ToIdx: tx.ToIdx,
  74. TokenID: tx.TokenID,
  75. Amount: tx.Amount,
  76. LoadAmount: tx.LoadAmount,
  77. EthBlockNum: tx.EthBlockNum,
  78. Type: tx.Type,
  79. BatchNum: tx.BatchNum,
  80. }
  81. return l1Tx
  82. }