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.5 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. // Tx is a struct that represents a Hermez network transaction
  26. type Tx struct {
  27. TxID TxID
  28. FromIdx Idx // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
  29. ToIdx Idx // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositAndTransfer
  30. TokenID TokenID
  31. Amount *big.Int
  32. Nonce uint64 // effective 48 bits used
  33. Fee FeeSelector
  34. Type TxType // optional, descrives which kind of tx it's
  35. BatchNum BatchNum // batchNum in which this tx was forged. Presence indicates "forged" state.
  36. }
  37. // TxID is the identifier of a Hermez network transaction
  38. type TxID Hash // Hash is a guess
  39. // TxType is a string that represents the type of a Hermez network transaction
  40. type TxType string
  41. const (
  42. // TxTypeExit represents L2->L1 token transfer. A leaf for this account appears in the exit tree of the block
  43. TxTypeExit TxType = "Exit"
  44. // TxTypeWithdrawn represents the balance that was moved from L2->L1 has been widthrawn from the smart contract
  45. TxTypeWithdrawn TxType = "Withdrawn"
  46. // TxTypeTransfer represents L2->L2 token transfer
  47. TxTypeTransfer TxType = "Transfer"
  48. // TxTypeDeposit represents L1->L2 transfer
  49. TxTypeDeposit TxType = "Deposit"
  50. // TxTypeCreateAccountDeposit represents creation of a new leaf in the state tree (newAcconut) + L1->L2 transfer
  51. TxTypeCreateAccountDeposit TxType = "CreateAccountDeposit"
  52. // TxTypeCreateAccountDepositAndTransfer represents L1->L2 transfer + L2->L2 transfer
  53. TxTypeCreateAccountDepositAndTransfer TxType = "CreateAccountDepositAndTransfer"
  54. // TxTypeDepositAndTransfer TBD
  55. TxTypeDepositAndTransfer TxType = "TxTypeDepositAndTransfer"
  56. // TxTypeForceTransfer TBD
  57. TxTypeForceTransfer TxType = "TxTypeForceTransfer"
  58. // TxTypeForceExit TBD
  59. TxTypeForceExit TxType = "TxTypeForceExit"
  60. // TxTypeTransferToEthAddr TBD
  61. TxTypeTransferToEthAddr TxType = "TxTypeTransferToEthAddr"
  62. // TxTypeTransferToBJJ TBD
  63. TxTypeTransferToBJJ TxType = "TxTypeTransferToBJJ"
  64. )