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.

120 lines
2.8 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. fromIdx := new(Idx)
  65. *fromIdx = tx.FromIdx
  66. toIdx := new(Idx)
  67. *toIdx = tx.ToIdx
  68. return &Tx{
  69. IsL1: false,
  70. TxID: tx.TxID,
  71. Type: tx.Type,
  72. Position: tx.Position,
  73. FromIdx: fromIdx,
  74. ToIdx: toIdx,
  75. Amount: tx.Amount,
  76. USD: tx.USD,
  77. AmountFloat: amountFloat,
  78. BatchNum: batchNum,
  79. EthBlockNum: tx.EthBlockNum,
  80. Fee: fee,
  81. FeeUSD: tx.FeeUSD,
  82. Nonce: nonce,
  83. }
  84. }
  85. // PoolL2Tx returns the data structure of PoolL2Tx with the parameters of a
  86. // L2Tx filled
  87. func (tx *L2Tx) PoolL2Tx() *PoolL2Tx {
  88. batchNum := new(BatchNum)
  89. *batchNum = tx.BatchNum
  90. toIdx := new(Idx)
  91. *toIdx = tx.ToIdx
  92. return &PoolL2Tx{
  93. TxID: tx.TxID,
  94. BatchNum: batchNum,
  95. FromIdx: tx.FromIdx,
  96. ToIdx: toIdx,
  97. Amount: tx.Amount,
  98. Fee: tx.Fee,
  99. Nonce: tx.Nonce,
  100. Type: tx.Type,
  101. }
  102. }
  103. // L2TxsToPoolL2Txs returns an array of []*PoolL2Tx from an array of []*L2Tx,
  104. // where the PoolL2Tx only have the parameters of a L2Tx filled.
  105. func L2TxsToPoolL2Txs(txs []*L2Tx) []*PoolL2Tx {
  106. var r []*PoolL2Tx
  107. for _, tx := range txs {
  108. r = append(r, tx.PoolL2Tx())
  109. }
  110. return r
  111. }