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.

73 lines
2.6 KiB

  1. package common
  2. import (
  3. "encoding/binary"
  4. "math/big"
  5. )
  6. // Idx represents the account Index in the MerkleTree
  7. type Idx uint32
  8. // Bytes returns a byte array representing the Idx
  9. func (idx Idx) Bytes() []byte {
  10. var b [4]byte
  11. binary.LittleEndian.PutUint32(b[:], uint32(idx))
  12. return b[:]
  13. }
  14. // BigInt returns a *big.Int representing the Idx
  15. func (idx Idx) BigInt() *big.Int {
  16. return big.NewInt(int64(idx))
  17. }
  18. // IdxFromBigInt converts a *big.Int to Idx type
  19. func IdxFromBigInt(b *big.Int) (Idx, error) {
  20. if b.Int64() > 0xffffffff { // 2**32-1
  21. return 0, ErrNumOverflow
  22. }
  23. return Idx(uint32(b.Int64())), nil
  24. }
  25. // TxID is the identifier of a Hermez network transaction
  26. type TxID Hash // Hash is a guess
  27. // TxType is a string that represents the type of a Hermez network transaction
  28. type TxType string
  29. const (
  30. // TxTypeExit represents L2->L1 token transfer. A leaf for this account appears in the exit tree of the block
  31. TxTypeExit TxType = "Exit"
  32. // TxTypeWithdrawn represents the balance that was moved from L2->L1 has been widthrawn from the smart contract
  33. TxTypeWithdrawn TxType = "Withdrawn"
  34. // TxTypeTransfer represents L2->L2 token transfer
  35. TxTypeTransfer TxType = "Transfer"
  36. // TxTypeDeposit represents L1->L2 transfer
  37. TxTypeDeposit TxType = "Deposit"
  38. // TxTypeCreateAccountDeposit represents creation of a new leaf in the state tree (newAcconut) + L1->L2 transfer
  39. TxTypeCreateAccountDeposit TxType = "CreateAccountDeposit"
  40. // TxTypeCreateAccountDepositTransfer represents L1->L2 transfer + L2->L2 transfer
  41. TxTypeCreateAccountDepositTransfer TxType = "CreateAccountDepositTransfer"
  42. // TxTypeDepositTransfer TBD
  43. TxTypeDepositTransfer TxType = "TxTypeDepositTransfer"
  44. // TxTypeForceTransfer TBD
  45. TxTypeForceTransfer TxType = "TxTypeForceTransfer"
  46. // TxTypeForceExit TBD
  47. TxTypeForceExit TxType = "TxTypeForceExit"
  48. // TxTypeTransferToEthAddr TBD
  49. TxTypeTransferToEthAddr TxType = "TxTypeTransferToEthAddr"
  50. // TxTypeTransferToBJJ TBD
  51. TxTypeTransferToBJJ TxType = "TxTypeTransferToBJJ"
  52. )
  53. // Tx is a struct used by the TxSelector & BatchBuilder as a generic type generated from L1Tx & PoolL2Tx
  54. type Tx struct {
  55. TxID TxID
  56. FromIdx Idx // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
  57. ToIdx Idx // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositTransfer
  58. TokenID TokenID
  59. Amount *big.Int
  60. Nonce Nonce // effective 40 bits used
  61. Fee FeeSelector
  62. Type TxType // optional, descrives which kind of tx it's
  63. BatchNum BatchNum // batchNum in which this tx was forged. Presence indicates "forged" state.
  64. }