hermez-node/common/tx.go
common/tx.goRaw
1package common
2
3import (
4 "math/big"
5
6 ethCommon "github.com/ethereum/go-ethereum/common"
7 "github.com/iden3/go-iden3-crypto/babyjub"
8)
9
10// TxID is the identifier of a Hermez network transaction
11type TxID Hash // Hash is a guess
12
13// TxType is a string that represents the type of a Hermez network transaction
14type TxType string
15
16const (
17 // TxTypeExit represents L2->L1 token transfer. A leaf for this account appears in the exit tree of the block
18 TxTypeExit TxType = "Exit"
19 // TxTypeWithdrawn represents the balance that was moved from L2->L1 has been widthrawn from the smart contract
20 TxTypeWithdrawn TxType = "Withdrawn"
21 // TxTypeTransfer represents L2->L2 token transfer
22 TxTypeTransfer TxType = "Transfer"
23 // TxTypeDeposit represents L1->L2 transfer
24 TxTypeDeposit TxType = "Deposit"
25 // TxTypeCreateAccountDeposit represents creation of a new leaf in the state tree (newAcconut) + L1->L2 transfer
26 TxTypeCreateAccountDeposit TxType = "CreateAccountDeposit"
27 // TxTypeCreateAccountDepositTransfer represents L1->L2 transfer + L2->L2 transfer
28 TxTypeCreateAccountDepositTransfer TxType = "CreateAccountDepositTransfer"
29 // TxTypeDepositTransfer TBD
30 TxTypeDepositTransfer TxType = "DepositTransfer"
31 // TxTypeForceTransfer TBD
32 TxTypeForceTransfer TxType = "ForceTransfer"
33 // TxTypeForceExit TBD
34 TxTypeForceExit TxType = "ForceExit"
35 // TxTypeTransferToEthAddr TBD
36 TxTypeTransferToEthAddr TxType = "TransferToEthAddr"
37 // TxTypeTransferToBJJ TBD
38 TxTypeTransferToBJJ TxType = "TransferToBJJ"
39)
40
41// Tx is a struct used by the TxSelector & BatchBuilder as a generic type generated from L1Tx & PoolL2Tx
42type Tx struct {
43 // Generic
44 IsL1 bool `meddler:"is_l1"`
45 TxID TxID `meddler:"id"`
46 Type TxType `meddler:"type"`
47 Position int `meddler:"position"`
48 FromIdx Idx `meddler:"from_idx"`
49 ToIdx Idx `meddler:"to_idx"`
50 Amount *big.Int `meddler:"amount,bigint"`
51 AmountFloat float64 `meddler:"amount_f"`
52 TokenID TokenID `meddler:"token_id"`
53 USD *float64 `meddler:"amount_usd"`
54 BatchNum *BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
55 EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
56 // L1
57 ToForgeL1TxsNum int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
58 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
59 FromEthAddr ethCommon.Address `meddler:"from_eth_addr"`
60 FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
61 LoadAmount *big.Int `meddler:"load_amount,bigintnull"`
62 LoadAmountFloat *float64 `meddler:"load_amount_f"`
63 LoadAmountUSD *float64 `meddler:"load_amount_usd"`
64 // L2
65 Fee *FeeSelector `meddler:"fee"`
66 FeeUSD *float64 `meddler:"fee_usd"`
67 Nonce *Nonce `meddler:"nonce"`
68}
69
70// L1Tx returns a *L1Tx from the Tx
71func (tx *Tx) L1Tx() *L1Tx {
72 l1Tx := &L1Tx{
73 TxID: tx.TxID,
74 ToForgeL1TxsNum: tx.ToForgeL1TxsNum,
75 Position: tx.Position,
76 UserOrigin: tx.UserOrigin,
77 FromIdx: tx.FromIdx,
78 FromEthAddr: tx.FromEthAddr,
79 FromBJJ: tx.FromBJJ,
80 ToIdx: tx.ToIdx,
81 TokenID: tx.TokenID,
82 Amount: tx.Amount,
83 LoadAmount: tx.LoadAmount,
84 EthBlockNum: tx.EthBlockNum,
85 Type: tx.Type,
86 BatchNum: tx.BatchNum,
87 }
88 return l1Tx
89}