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.

130 lines
4.6 KiB

  1. package common
  2. import (
  3. "database/sql/driver"
  4. "encoding/hex"
  5. "fmt"
  6. "math/big"
  7. ethCommon "github.com/ethereum/go-ethereum/common"
  8. "github.com/iden3/go-iden3-crypto/babyjub"
  9. )
  10. const (
  11. // TXIDPrefixL1CoordTx is the prefix that determines that the TxID is
  12. // for a L1CoordinatorTx
  13. //nolinter:gomnd
  14. TxIDPrefixL1CoordTx = byte(1)
  15. // TxIDPrefixL2Tx is the prefix that determines that the TxID is for a
  16. // L2Tx (or PoolL2Tx)
  17. //nolinter:gomnd
  18. TxIDPrefixL2Tx = byte(2)
  19. // TxIDLen is the length of the TxID byte array
  20. TxIDLen = 12
  21. )
  22. // TxID is the identifier of a Hermez network transaction
  23. type TxID [TxIDLen]byte
  24. // Scan implements Scanner for database/sql.
  25. func (txid *TxID) Scan(src interface{}) error {
  26. srcB, ok := src.([]byte)
  27. if !ok {
  28. return fmt.Errorf("can't scan %T into TxID", src)
  29. }
  30. if len(srcB) != TxIDLen {
  31. return fmt.Errorf("can't scan []byte of len %d into TxID, need %d", len(srcB), TxIDLen)
  32. }
  33. copy(txid[:], srcB)
  34. return nil
  35. }
  36. // Value implements valuer for database/sql.
  37. func (txid TxID) Value() (driver.Value, error) {
  38. return txid[:], nil
  39. }
  40. // String returns a string hexadecimal representation of the TxID
  41. func (txid TxID) String() string {
  42. return "0x" + hex.EncodeToString(txid[:])
  43. }
  44. // TxType is a string that represents the type of a Hermez network transaction
  45. type TxType string
  46. const (
  47. // TxTypeExit represents L2->L1 token transfer. A leaf for this account appears in the exit tree of the block
  48. TxTypeExit TxType = "Exit"
  49. // TxTypeWithdrawn represents the balance that was moved from L2->L1 has been widthrawn from the smart contract
  50. TxTypeWithdrawn TxType = "Withdrawn"
  51. // TxTypeTransfer represents L2->L2 token transfer
  52. TxTypeTransfer TxType = "Transfer"
  53. // TxTypeDeposit represents L1->L2 transfer
  54. TxTypeDeposit TxType = "Deposit"
  55. // TxTypeCreateAccountDeposit represents creation of a new leaf in the state tree (newAcconut) + L1->L2 transfer
  56. TxTypeCreateAccountDeposit TxType = "CreateAccountDeposit"
  57. // TxTypeCreateAccountDepositTransfer represents L1->L2 transfer + L2->L2 transfer
  58. TxTypeCreateAccountDepositTransfer TxType = "CreateAccountDepositTransfer"
  59. // TxTypeDepositTransfer TBD
  60. TxTypeDepositTransfer TxType = "DepositTransfer"
  61. // TxTypeForceTransfer TBD
  62. TxTypeForceTransfer TxType = "ForceTransfer"
  63. // TxTypeForceExit TBD
  64. TxTypeForceExit TxType = "ForceExit"
  65. // TxTypeTransferToEthAddr TBD
  66. TxTypeTransferToEthAddr TxType = "TransferToEthAddr"
  67. // TxTypeTransferToBJJ TBD
  68. TxTypeTransferToBJJ TxType = "TransferToBJJ"
  69. )
  70. // Tx is a struct used by the TxSelector & BatchBuilder as a generic type generated from L1Tx & PoolL2Tx
  71. type Tx struct {
  72. // Generic
  73. IsL1 bool `meddler:"is_l1"`
  74. TxID TxID `meddler:"id"`
  75. Type TxType `meddler:"type"`
  76. Position int `meddler:"position"`
  77. FromIdx Idx `meddler:"from_idx"`
  78. ToIdx Idx `meddler:"to_idx"`
  79. Amount *big.Int `meddler:"amount,bigint"`
  80. AmountFloat float64 `meddler:"amount_f"`
  81. TokenID TokenID `meddler:"token_id"`
  82. USD *float64 `meddler:"amount_usd"`
  83. BatchNum *BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
  84. EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
  85. // L1
  86. ToForgeL1TxsNum int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
  87. 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
  88. FromEthAddr ethCommon.Address `meddler:"from_eth_addr"`
  89. FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
  90. LoadAmount *big.Int `meddler:"load_amount,bigintnull"`
  91. LoadAmountFloat *float64 `meddler:"load_amount_f"`
  92. LoadAmountUSD *float64 `meddler:"load_amount_usd"`
  93. // L2
  94. Fee *FeeSelector `meddler:"fee"`
  95. FeeUSD *float64 `meddler:"fee_usd"`
  96. Nonce *Nonce `meddler:"nonce"`
  97. }
  98. // L1Tx returns a *L1Tx from the Tx
  99. func (tx *Tx) L1Tx() *L1Tx {
  100. l1Tx := &L1Tx{
  101. TxID: tx.TxID,
  102. ToForgeL1TxsNum: tx.ToForgeL1TxsNum,
  103. Position: tx.Position,
  104. UserOrigin: tx.UserOrigin,
  105. FromIdx: tx.FromIdx,
  106. FromEthAddr: tx.FromEthAddr,
  107. FromBJJ: tx.FromBJJ,
  108. ToIdx: tx.ToIdx,
  109. TokenID: tx.TokenID,
  110. Amount: tx.Amount,
  111. LoadAmount: tx.LoadAmount,
  112. EthBlockNum: tx.EthBlockNum,
  113. Type: tx.Type,
  114. BatchNum: tx.BatchNum,
  115. }
  116. return l1Tx
  117. }