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