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.

112 lines
2.6 KiB

  1. package common
  2. import (
  3. "fmt"
  4. "math/big"
  5. )
  6. // L2Tx is a struct that represents an already forged L2 tx
  7. type L2Tx struct {
  8. // Stored in DB: mandatory fileds
  9. TxID TxID
  10. BatchNum BatchNum // batchNum in which this tx was forged.
  11. Position int
  12. FromIdx Idx
  13. ToIdx Idx
  14. Amount *big.Int
  15. USD *float64
  16. Fee FeeSelector
  17. FeeUSD *float64
  18. Nonce Nonce
  19. Type TxType
  20. EthBlockNum int64 // Ethereum Block Number in which this L2Tx was added to the queue
  21. }
  22. // NewL2Tx returns the given L2Tx with the TxId & Type parameters calculated
  23. // from the L2Tx values
  24. func NewL2Tx(l2Tx *L2Tx) (*L2Tx, error) {
  25. // calculate TxType
  26. var txType TxType
  27. if l2Tx.ToIdx == Idx(1) {
  28. txType = TxTypeExit
  29. } else if l2Tx.ToIdx >= IdxUserThreshold {
  30. txType = TxTypeTransfer
  31. } else {
  32. return l2Tx, fmt.Errorf("Can not determine type of L2Tx, invalid ToIdx value: %d", l2Tx.ToIdx)
  33. }
  34. // if TxType!=l2Tx.TxType return error
  35. if l2Tx.Type != "" && l2Tx.Type != txType {
  36. return l2Tx, fmt.Errorf("L2Tx.Type: %s, should be: %s", l2Tx.Type, txType)
  37. }
  38. l2Tx.Type = txType
  39. var txid [TxIDLen]byte
  40. txid[0] = TxIDPrefixL2Tx
  41. fromIdxBytes, err := l2Tx.FromIdx.Bytes()
  42. if err != nil {
  43. return l2Tx, err
  44. }
  45. copy(txid[1:7], fromIdxBytes[:])
  46. nonceBytes, err := l2Tx.Nonce.Bytes()
  47. if err != nil {
  48. return l2Tx, err
  49. }
  50. copy(txid[7:12], nonceBytes[:])
  51. l2Tx.TxID = TxID(txid)
  52. return l2Tx, nil
  53. }
  54. // Tx returns a *Tx from the L2Tx
  55. func (tx *L2Tx) Tx() *Tx {
  56. f := new(big.Float).SetInt(tx.Amount)
  57. amountFloat, _ := f.Float64()
  58. batchNum := new(BatchNum)
  59. *batchNum = tx.BatchNum
  60. fee := new(FeeSelector)
  61. *fee = tx.Fee
  62. nonce := new(Nonce)
  63. *nonce = tx.Nonce
  64. return &Tx{
  65. IsL1: false,
  66. TxID: tx.TxID,
  67. Type: tx.Type,
  68. Position: tx.Position,
  69. FromIdx: tx.FromIdx,
  70. ToIdx: tx.ToIdx,
  71. Amount: tx.Amount,
  72. USD: tx.USD,
  73. AmountFloat: amountFloat,
  74. BatchNum: batchNum,
  75. EthBlockNum: tx.EthBlockNum,
  76. Fee: fee,
  77. FeeUSD: tx.FeeUSD,
  78. Nonce: nonce,
  79. }
  80. }
  81. // PoolL2Tx returns the data structure of PoolL2Tx with the parameters of a
  82. // L2Tx filled
  83. func (tx *L2Tx) PoolL2Tx() *PoolL2Tx {
  84. return &PoolL2Tx{
  85. TxID: tx.TxID,
  86. BatchNum: tx.BatchNum,
  87. FromIdx: tx.FromIdx,
  88. ToIdx: tx.ToIdx,
  89. Amount: tx.Amount,
  90. Fee: tx.Fee,
  91. Nonce: tx.Nonce,
  92. Type: tx.Type,
  93. }
  94. }
  95. // L2TxsToPoolL2Txs returns an array of []*PoolL2Tx from an array of []*L2Tx,
  96. // where the PoolL2Tx only have the parameters of a L2Tx filled.
  97. func L2TxsToPoolL2Txs(txs []*L2Tx) []*PoolL2Tx {
  98. var r []*PoolL2Tx
  99. for _, tx := range txs {
  100. r = append(r, tx.PoolL2Tx())
  101. }
  102. return r
  103. }