arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/common/l2tx.go
common/l2tx.goRaw
 1package common
 2
 3import (
 4	"math/big"
 5)
 6
 7// L2Tx is a struct that represents an already forged L2 tx
 8type L2Tx struct {
 9	// Stored in DB: mandatory fileds
10	TxID        TxID
11	BatchNum    BatchNum // batchNum in which this tx was forged.
12	Position    int
13	FromIdx     Idx
14	ToIdx       Idx
15	Amount      *big.Int
16	Fee         FeeSelector
17	Nonce       Nonce
18	Type        TxType
19	EthBlockNum int64 // Ethereum Block Number in which this L2Tx was added to the queue
20}
21
22// Tx returns a *Tx from the L2Tx
23func (tx *L2Tx) Tx() *Tx {
24	f := new(big.Float).SetInt(tx.Amount)
25	amountFloat, _ := f.Float64()
26	return &Tx{
27		IsL1:        false,
28		TxID:        tx.TxID,
29		Type:        tx.Type,
30		Position:    tx.Position,
31		FromIdx:     tx.FromIdx,
32		ToIdx:       tx.ToIdx,
33		Amount:      tx.Amount,
34		AmountFloat: amountFloat,
35		BatchNum:    tx.BatchNum,
36		EthBlockNum: tx.EthBlockNum,
37		Fee:         tx.Fee,
38		Nonce:       tx.Nonce,
39	}
40}
41
42// PoolL2Tx returns the data structure of PoolL2Tx with the parameters of a
43// L2Tx filled
44func (tx *L2Tx) PoolL2Tx() *PoolL2Tx {
45	return &PoolL2Tx{
46		TxID:     tx.TxID,
47		BatchNum: tx.BatchNum,
48		FromIdx:  tx.FromIdx,
49		ToIdx:    tx.ToIdx,
50		Amount:   tx.Amount,
51		Fee:      tx.Fee,
52		Nonce:    tx.Nonce,
53		Type:     tx.Type,
54	}
55}
56
57// L2TxsToPoolL2Txs returns an array of []*PoolL2Tx from an array of []*L2Tx,
58// where the PoolL2Tx only have the parameters of a L2Tx filled.
59func L2TxsToPoolL2Txs(txs []*L2Tx) []*PoolL2Tx {
60	var r []*PoolL2Tx
61	for _, tx := range txs {
62		r = append(r, tx.PoolL2Tx())
63	}
64	return r
65}