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	USD         *float64
17	Fee         FeeSelector
18	FeeUSD      *float64
19	Nonce       Nonce
20	Type        TxType
21	EthBlockNum int64 // Ethereum Block Number in which this L2Tx was added to the queue
22}
23
24// Tx returns a *Tx from the L2Tx
25func (tx *L2Tx) Tx() *Tx {
26	f := new(big.Float).SetInt(tx.Amount)
27	amountFloat, _ := f.Float64()
28	batchNum := new(BatchNum)
29	*batchNum = tx.BatchNum
30	fee := new(FeeSelector)
31	*fee = tx.Fee
32	nonce := new(Nonce)
33	*nonce = tx.Nonce
34	return &Tx{
35		IsL1:        false,
36		TxID:        tx.TxID,
37		Type:        tx.Type,
38		Position:    tx.Position,
39		FromIdx:     tx.FromIdx,
40		ToIdx:       tx.ToIdx,
41		Amount:      tx.Amount,
42		USD:         tx.USD,
43		AmountFloat: amountFloat,
44		BatchNum:    batchNum,
45		EthBlockNum: tx.EthBlockNum,
46		Fee:         fee,
47		FeeUSD:      tx.FeeUSD,
48		Nonce:       nonce,
49	}
50}
51
52// PoolL2Tx returns the data structure of PoolL2Tx with the parameters of a
53// L2Tx filled
54func (tx *L2Tx) PoolL2Tx() *PoolL2Tx {
55	return &PoolL2Tx{
56		TxID:     tx.TxID,
57		BatchNum: tx.BatchNum,
58		FromIdx:  tx.FromIdx,
59		ToIdx:    tx.ToIdx,
60		Amount:   tx.Amount,
61		Fee:      tx.Fee,
62		Nonce:    tx.Nonce,
63		Type:     tx.Type,
64	}
65}
66
67// L2TxsToPoolL2Txs returns an array of []*PoolL2Tx from an array of []*L2Tx,
68// where the PoolL2Tx only have the parameters of a L2Tx filled.
69func L2TxsToPoolL2Txs(txs []*L2Tx) []*PoolL2Tx {
70	var r []*PoolL2Tx
71	for _, tx := range txs {
72		r = append(r, tx.PoolL2Tx())
73	}
74	return r
75}