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