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        `meddler:"tx_id"`
11	BatchNum BatchNum    `meddler:"batch_num"` // batchNum in which this tx was forged.
12	Position int         `meddler:"position"`
13	FromIdx  Idx         `meddler:"from_idx"`
14	ToIdx    Idx         `meddler:"to_idx"`
15	Amount   *big.Int    `meddler:"amount,bigint"`
16	Fee      FeeSelector `meddler:"fee"`
17	Nonce    Nonce       `meddler:"nonce"`
18	Type     TxType      `meddler:"tx_type"`
19}
20
21// Tx returns a *Tx from the L2Tx
22func (tx *L2Tx) Tx() *Tx {
23	return &Tx{
24		TxID:    tx.TxID,
25		FromIdx: tx.FromIdx,
26		ToIdx:   tx.ToIdx,
27		Amount:  tx.Amount,
28		Nonce:   tx.Nonce,
29		Fee:     tx.Fee,
30		Type:    tx.Type,
31	}
32}
33
34// PoolL2Tx returns the data structure of PoolL2Tx with the parameters of a
35// L2Tx filled
36func (tx *L2Tx) PoolL2Tx() *PoolL2Tx {
37	return &PoolL2Tx{
38		TxID:     tx.TxID,
39		BatchNum: tx.BatchNum,
40		FromIdx:  tx.FromIdx,
41		ToIdx:    tx.ToIdx,
42		Amount:   tx.Amount,
43		Fee:      tx.Fee,
44		Nonce:    tx.Nonce,
45		Type:     tx.Type,
46	}
47}
48
49// L2TxsToPoolL2Txs returns an array of []*PoolL2Tx from an array of []*L2Tx,
50// where the PoolL2Tx only have the parameters of a L2Tx filled.
51func L2TxsToPoolL2Txs(txs []*L2Tx) []*PoolL2Tx {
52	var r []*PoolL2Tx
53	for _, tx := range txs {
54		r = append(r, tx.PoolL2Tx())
55	}
56	return r
57}