You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.7 KiB

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