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.

952 lines
40 KiB

  1. package eth
  2. import (
  3. "context"
  4. "crypto/ecdsa"
  5. "encoding/binary"
  6. "encoding/hex"
  7. "math/big"
  8. "testing"
  9. ethCommon "github.com/ethereum/go-ethereum/common"
  10. ethCrypto "github.com/ethereum/go-ethereum/crypto"
  11. "github.com/hermeznetwork/hermez-node/common"
  12. "github.com/iden3/go-iden3-crypto/babyjub"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/stretchr/testify/require"
  15. )
  16. var rollupClient *RollupClient
  17. var auctionClient *AuctionClient
  18. var ethHashForge ethCommon.Hash
  19. var argsForge *RollupForgeBatchArgs
  20. var absoluteMaxL1L2BatchTimeout = int64(240)
  21. var maxTx = int64(512)
  22. var nLevels = int64(32)
  23. var tokenIDERC777 uint32
  24. var tokenHEZID uint32
  25. var L1UserTxs []common.L1Tx
  26. var blockStampBucket int64
  27. type keys struct {
  28. BJJSecretKey *babyjub.PrivateKey
  29. BJJPublicKey *babyjub.PublicKey
  30. Addr ethCommon.Address
  31. }
  32. func genKeysBjj(i int64) *keys {
  33. i++ // i = 0 doesn't work for the ecdsa key generation
  34. var sk babyjub.PrivateKey
  35. binary.LittleEndian.PutUint64(sk[:], uint64(i))
  36. // eth address
  37. var key ecdsa.PrivateKey
  38. key.D = big.NewInt(i) // only for testing
  39. key.PublicKey.X, key.PublicKey.Y = ethCrypto.S256().ScalarBaseMult(key.D.Bytes())
  40. key.Curve = ethCrypto.S256()
  41. return &keys{
  42. BJJSecretKey: &sk,
  43. BJJPublicKey: sk.Public(),
  44. }
  45. }
  46. func TestRollupConstants(t *testing.T) {
  47. rollupConstants, err := rollupClient.RollupConstants()
  48. require.NoError(t, err)
  49. assert.Equal(t, absoluteMaxL1L2BatchTimeout, rollupConstants.AbsoluteMaxL1L2BatchTimeout)
  50. assert.Equal(t, auctionAddressConst, rollupConstants.HermezAuctionContract)
  51. assert.Equal(t, tokenHEZAddressConst, rollupConstants.TokenHEZ)
  52. assert.Equal(t, maxTx, rollupConstants.Verifiers[0].MaxTx)
  53. assert.Equal(t, nLevels, rollupConstants.Verifiers[0].NLevels)
  54. assert.Equal(t, governanceAddressConst, rollupConstants.HermezGovernanceAddress)
  55. assert.Equal(t, wdelayerAddressConst, rollupConstants.WithdrawDelayerContract)
  56. }
  57. func TestRollupRegisterTokensCount(t *testing.T) {
  58. registerTokensCount, err := rollupClient.RollupRegisterTokensCount()
  59. require.NoError(t, err)
  60. assert.Equal(t, big.NewInt(1), registerTokensCount)
  61. }
  62. func TestRollupAddToken(t *testing.T) {
  63. feeAddToken := big.NewInt(10)
  64. // Addtoken ERC20Permit
  65. registerTokensCount, err := rollupClient.RollupRegisterTokensCount()
  66. require.NoError(t, err)
  67. _, err = rollupClient.RollupAddToken(tokenHEZAddressConst, feeAddToken, deadline)
  68. require.NoError(t, err)
  69. currentBlockNum, err := rollupClient.client.EthLastBlock()
  70. require.NoError(t, err)
  71. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  72. require.NoError(t, err)
  73. assert.Equal(t, tokenHEZAddressConst, rollupEvents.AddToken[0].TokenAddress)
  74. assert.Equal(t, registerTokensCount, common.TokenID(rollupEvents.AddToken[0].TokenID).BigInt())
  75. tokenHEZID = rollupEvents.AddToken[0].TokenID
  76. }
  77. func TestRollupForgeBatch(t *testing.T) {
  78. chainid, _ := auctionClient.client.Client().ChainID(context.Background())
  79. // Register Coordinator
  80. forgerAddress := governanceAddressConst
  81. _, err := auctionClient.AuctionSetCoordinator(forgerAddress, URL)
  82. require.NoError(t, err)
  83. // MultiBid
  84. currentSlot, err := auctionClient.AuctionGetCurrentSlotNumber()
  85. require.NoError(t, err)
  86. slotSet := [6]bool{true, false, true, false, true, false}
  87. maxBid := new(big.Int)
  88. maxBid.SetString("15000000000000000000", 10)
  89. minBid := new(big.Int)
  90. minBid.SetString("11000000000000000000", 10)
  91. budget := new(big.Int)
  92. budget.SetString("45200000000000000000", 10)
  93. _, err = auctionClient.AuctionMultiBid(budget, currentSlot+4, currentSlot+10, slotSet, maxBid, minBid, deadline)
  94. require.NoError(t, err)
  95. // Add Blocks
  96. blockNum := int64(int(blocksPerSlot)*int(currentSlot+4) + int(genesisBlock))
  97. currentBlockNum, err := auctionClient.client.EthLastBlock()
  98. require.NoError(t, err)
  99. blocksToAdd := blockNum - currentBlockNum
  100. addBlocks(blocksToAdd, ethClientDialURL)
  101. // Forge Batch 1
  102. args := new(RollupForgeBatchArgs)
  103. args.FeeIdxCoordinator = []common.Idx{} // When encoded, 64 times the 0 idx means that no idx to collect fees is specified.
  104. l1CoordinatorBytes, err := hex.DecodeString("1c660323607bb113e586183609964a333d07ebe4bef3be82ec13af453bae9590bd7711cdb6abf42f176eadfbe5506fbef5e092e5543733f91b0061d9a7747fa10694a915a6470fa230de387b51e6f4db0b09787867778687b55197ad6d6a86eac000000001")
  105. require.NoError(t, err)
  106. numTxsL1 := len(l1CoordinatorBytes) / common.L1CoordinatorTxBytesLen
  107. for i := 0; i < numTxsL1; i++ {
  108. bytesL1Coordinator := l1CoordinatorBytes[i*common.L1CoordinatorTxBytesLen : (i+1)*common.L1CoordinatorTxBytesLen]
  109. var signature []byte
  110. v := bytesL1Coordinator[0]
  111. s := bytesL1Coordinator[1:33]
  112. r := bytesL1Coordinator[33:65]
  113. signature = append(signature, r[:]...)
  114. signature = append(signature, s[:]...)
  115. signature = append(signature, v)
  116. l1Tx, err := common.L1CoordinatorTxFromBytes(bytesL1Coordinator, chainid, rollupClient.address)
  117. require.NoError(t, err)
  118. args.L1CoordinatorTxs = append(args.L1CoordinatorTxs, *l1Tx)
  119. args.L1CoordinatorTxsAuths = append(args.L1CoordinatorTxsAuths, signature)
  120. }
  121. args.L1UserTxs = []common.L1Tx{}
  122. args.L2TxsData = []common.L2Tx{}
  123. newStateRoot := new(big.Int)
  124. newStateRoot.SetString("18317824016047294649053625209337295956588174734569560016974612130063629505228", 10)
  125. newExitRoot := new(big.Int)
  126. bytesNumExitRoot, err := hex.DecodeString("10a89d5fe8d488eda1ba371d633515739933c706c210c604f5bd209180daa43b")
  127. require.NoError(t, err)
  128. newExitRoot.SetBytes(bytesNumExitRoot)
  129. args.NewLastIdx = int64(300)
  130. args.NewStRoot = newStateRoot
  131. args.NewExitRoot = newExitRoot
  132. args.L1Batch = true
  133. args.VerifierIdx = 0
  134. args.ProofA[0] = big.NewInt(0)
  135. args.ProofA[1] = big.NewInt(0)
  136. args.ProofB[0][0] = big.NewInt(0)
  137. args.ProofB[0][1] = big.NewInt(0)
  138. args.ProofB[1][0] = big.NewInt(0)
  139. args.ProofB[1][1] = big.NewInt(0)
  140. args.ProofC[0] = big.NewInt(0)
  141. args.ProofC[1] = big.NewInt(0)
  142. argsForge = args
  143. _, err = rollupClient.RollupForgeBatch(argsForge)
  144. require.NoError(t, err)
  145. currentBlockNum, err = rollupClient.client.EthLastBlock()
  146. require.NoError(t, err)
  147. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  148. require.NoError(t, err)
  149. assert.Equal(t, int64(1), rollupEvents.ForgeBatch[0].BatchNum)
  150. assert.Equal(t, uint16(len(L1UserTxs)), rollupEvents.ForgeBatch[0].L1UserTxsLen)
  151. ethHashForge = rollupEvents.ForgeBatch[0].EthTxHash
  152. }
  153. func TestRollupForgeBatchArgs(t *testing.T) {
  154. args, sender, err := rollupClient.RollupForgeBatchArgs(ethHashForge, uint16(len(L1UserTxs)))
  155. require.NoError(t, err)
  156. assert.Equal(t, *sender, rollupClient.client.account.Address)
  157. assert.Equal(t, argsForge.FeeIdxCoordinator, args.FeeIdxCoordinator)
  158. assert.Equal(t, argsForge.L1Batch, args.L1Batch)
  159. assert.Equal(t, argsForge.L1CoordinatorTxs, args.L1CoordinatorTxs)
  160. assert.Equal(t, argsForge.L1CoordinatorTxsAuths, args.L1CoordinatorTxsAuths)
  161. assert.Equal(t, argsForge.L2TxsData, args.L2TxsData)
  162. assert.Equal(t, argsForge.NewLastIdx, args.NewLastIdx)
  163. assert.Equal(t, argsForge.NewStRoot, args.NewStRoot)
  164. assert.Equal(t, argsForge.VerifierIdx, args.VerifierIdx)
  165. }
  166. func TestRollupUpdateForgeL1L2BatchTimeout(t *testing.T) {
  167. newForgeL1L2BatchTimeout := int64(222)
  168. _, err := rollupClient.RollupUpdateForgeL1L2BatchTimeout(newForgeL1L2BatchTimeout)
  169. require.NoError(t, err)
  170. currentBlockNum, err := rollupClient.client.EthLastBlock()
  171. require.NoError(t, err)
  172. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  173. require.NoError(t, err)
  174. assert.Equal(t, newForgeL1L2BatchTimeout, rollupEvents.UpdateForgeL1L2BatchTimeout[0].NewForgeL1L2BatchTimeout)
  175. }
  176. func TestRollupUpdateFeeAddToken(t *testing.T) {
  177. newFeeAddToken := big.NewInt(12)
  178. _, err := rollupClient.RollupUpdateFeeAddToken(newFeeAddToken)
  179. require.NoError(t, err)
  180. currentBlockNum, err := rollupClient.client.EthLastBlock()
  181. require.NoError(t, err)
  182. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  183. require.NoError(t, err)
  184. assert.Equal(t, newFeeAddToken, rollupEvents.UpdateFeeAddToken[0].NewFeeAddToken)
  185. }
  186. func TestRollupUpdateBucketsParameters(t *testing.T) {
  187. var bucketsParameters [common.RollupConstNumBuckets]RollupUpdateBucketsParameters
  188. for i := range bucketsParameters {
  189. bucketsParameters[i].CeilUSD = big.NewInt(int64((i + 1) * 100))
  190. bucketsParameters[i].Withdrawals = big.NewInt(int64(i + 1))
  191. bucketsParameters[i].BlockWithdrawalRate = big.NewInt(int64(i+1) * 100)
  192. bucketsParameters[i].MaxWithdrawals = big.NewInt(int64(100000000000))
  193. }
  194. _, err := rollupClient.RollupUpdateBucketsParameters(bucketsParameters)
  195. require.NoError(t, err)
  196. currentBlockNum, err := rollupClient.client.EthLastBlock()
  197. require.NoError(t, err)
  198. blockStampBucket = currentBlockNum
  199. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  200. require.NoError(t, err)
  201. assert.Equal(t, bucketsParameters, rollupEvents.UpdateBucketsParameters[0].ArrayBuckets)
  202. }
  203. func TestRollupUpdateWithdrawalDelay(t *testing.T) {
  204. newWithdrawalDelay := int64(100000)
  205. _, err := rollupClient.RollupUpdateWithdrawalDelay(newWithdrawalDelay)
  206. require.NoError(t, err)
  207. currentBlockNum, err := rollupClient.client.EthLastBlock()
  208. require.NoError(t, err)
  209. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  210. require.NoError(t, err)
  211. assert.Equal(t, newWithdrawalDelay, int64(rollupEvents.UpdateWithdrawalDelay[0].NewWithdrawalDelay))
  212. }
  213. func TestRollupUpdateTokenExchange(t *testing.T) {
  214. var addressArray []ethCommon.Address
  215. var valueArray []uint64
  216. addressToken1, err := rollupClient.hermez.TokenList(nil, big.NewInt(1))
  217. addressArray = append(addressArray, addressToken1)
  218. tokenPrice := 10
  219. valueArray = append(valueArray, uint64(tokenPrice*1e14))
  220. require.NoError(t, err)
  221. _, err = rollupClient.RollupUpdateTokenExchange(addressArray, valueArray)
  222. require.NoError(t, err)
  223. currentBlockNum, err := rollupClient.client.EthLastBlock()
  224. require.NoError(t, err)
  225. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  226. require.NoError(t, err)
  227. assert.Equal(t, addressArray, rollupEvents.UpdateTokenExchange[0].AddressArray)
  228. assert.Equal(t, valueArray, rollupEvents.UpdateTokenExchange[0].ValueArray)
  229. }
  230. func TestRollupL1UserTxETHCreateAccountDeposit(t *testing.T) {
  231. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  232. require.NoError(t, err)
  233. key := genKeysBjj(2)
  234. fromIdxInt64 := int64(0)
  235. toIdxInt64 := int64(0)
  236. tokenIDUint32 := uint32(0)
  237. depositAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  238. l1Tx := common.L1Tx{
  239. FromBJJ: key.BJJPublicKey,
  240. FromIdx: common.Idx(fromIdxInt64),
  241. ToIdx: common.Idx(toIdxInt64),
  242. DepositAmount: depositAmount,
  243. TokenID: common.TokenID(tokenIDUint32),
  244. Amount: big.NewInt(0),
  245. }
  246. L1UserTxs = append(L1UserTxs, l1Tx)
  247. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.DepositAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  248. require.NoError(t, err)
  249. currentBlockNum, err := rollupClient.client.EthLastBlock()
  250. require.NoError(t, err)
  251. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  252. require.NoError(t, err)
  253. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  254. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  255. assert.Equal(t, l1Tx.DepositAmount, rollupEvents.L1UserTx[0].L1UserTx.DepositAmount)
  256. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  257. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  258. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  259. }
  260. func TestRollupL1UserTxERC20CreateAccountDeposit(t *testing.T) {
  261. rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
  262. require.NoError(t, err)
  263. key := genKeysBjj(1)
  264. fromIdxInt64 := int64(0)
  265. toIdxInt64 := int64(0)
  266. depositAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  267. l1Tx := common.L1Tx{
  268. FromBJJ: key.BJJPublicKey,
  269. FromIdx: common.Idx(fromIdxInt64),
  270. ToIdx: common.Idx(toIdxInt64),
  271. DepositAmount: depositAmount,
  272. TokenID: common.TokenID(tokenHEZID),
  273. Amount: big.NewInt(0),
  274. }
  275. L1UserTxs = append(L1UserTxs, l1Tx)
  276. _, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.DepositAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
  277. require.NoError(t, err)
  278. currentBlockNum, err := rollupClient.client.EthLastBlock()
  279. require.NoError(t, err)
  280. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  281. require.NoError(t, err)
  282. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  283. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  284. assert.Equal(t, l1Tx.DepositAmount, rollupEvents.L1UserTx[0].L1UserTx.DepositAmount)
  285. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  286. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  287. assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  288. }
  289. func TestRollupL1UserTxERC20PermitCreateAccountDeposit(t *testing.T) {
  290. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  291. require.NoError(t, err)
  292. key := genKeysBjj(3)
  293. fromIdxInt64 := int64(0)
  294. toIdxInt64 := int64(0)
  295. depositAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  296. l1Tx := common.L1Tx{
  297. FromBJJ: key.BJJPublicKey,
  298. FromIdx: common.Idx(fromIdxInt64),
  299. ToIdx: common.Idx(toIdxInt64),
  300. DepositAmount: depositAmount,
  301. TokenID: common.TokenID(tokenIDERC777),
  302. Amount: big.NewInt(0),
  303. }
  304. L1UserTxs = append(L1UserTxs, l1Tx)
  305. _, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.DepositAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
  306. require.NoError(t, err)
  307. currentBlockNum, err := rollupClient.client.EthLastBlock()
  308. require.NoError(t, err)
  309. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  310. require.NoError(t, err)
  311. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  312. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  313. assert.Equal(t, l1Tx.DepositAmount, rollupEvents.L1UserTx[0].L1UserTx.DepositAmount)
  314. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  315. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  316. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  317. }
  318. func TestRollupL1UserTxETHDeposit(t *testing.T) {
  319. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  320. require.NoError(t, err)
  321. fromIdxInt64 := int64(256)
  322. toIdxInt64 := int64(0)
  323. tokenIDUint32 := uint32(0)
  324. depositAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  325. l1Tx := common.L1Tx{
  326. FromBJJ: nil,
  327. FromIdx: common.Idx(fromIdxInt64),
  328. ToIdx: common.Idx(toIdxInt64),
  329. DepositAmount: depositAmount,
  330. TokenID: common.TokenID(tokenIDUint32),
  331. Amount: big.NewInt(0),
  332. }
  333. L1UserTxs = append(L1UserTxs, l1Tx)
  334. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.DepositAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  335. require.NoError(t, err)
  336. currentBlockNum, err := rollupClient.client.EthLastBlock()
  337. require.NoError(t, err)
  338. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  339. require.NoError(t, err)
  340. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  341. assert.Equal(t, l1Tx.DepositAmount, rollupEvents.L1UserTx[0].L1UserTx.DepositAmount)
  342. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  343. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  344. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  345. }
  346. func TestRollupL1UserTxERC20Deposit(t *testing.T) {
  347. rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
  348. require.NoError(t, err)
  349. fromIdxInt64 := int64(257)
  350. toIdxInt64 := int64(0)
  351. depositAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  352. l1Tx := common.L1Tx{
  353. FromBJJ: nil,
  354. FromIdx: common.Idx(fromIdxInt64),
  355. ToIdx: common.Idx(toIdxInt64),
  356. DepositAmount: depositAmount,
  357. TokenID: common.TokenID(tokenHEZID),
  358. Amount: big.NewInt(0),
  359. }
  360. L1UserTxs = append(L1UserTxs, l1Tx)
  361. _, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.DepositAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
  362. require.NoError(t, err)
  363. currentBlockNum, err := rollupClient.client.EthLastBlock()
  364. require.NoError(t, err)
  365. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  366. require.NoError(t, err)
  367. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  368. assert.Equal(t, l1Tx.DepositAmount, rollupEvents.L1UserTx[0].L1UserTx.DepositAmount)
  369. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  370. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  371. assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  372. }
  373. func TestRollupL1UserTxERC20PermitDeposit(t *testing.T) {
  374. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  375. require.NoError(t, err)
  376. fromIdxInt64 := int64(258)
  377. toIdxInt64 := int64(0)
  378. depositAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  379. l1Tx := common.L1Tx{
  380. FromIdx: common.Idx(fromIdxInt64),
  381. ToIdx: common.Idx(toIdxInt64),
  382. DepositAmount: depositAmount,
  383. TokenID: common.TokenID(tokenIDERC777),
  384. Amount: big.NewInt(0),
  385. }
  386. L1UserTxs = append(L1UserTxs, l1Tx)
  387. _, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.DepositAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
  388. require.NoError(t, err)
  389. currentBlockNum, err := rollupClient.client.EthLastBlock()
  390. require.NoError(t, err)
  391. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  392. require.NoError(t, err)
  393. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  394. assert.Equal(t, l1Tx.DepositAmount, rollupEvents.L1UserTx[0].L1UserTx.DepositAmount)
  395. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  396. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  397. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  398. }
  399. func TestRollupL1UserTxETHDepositTransfer(t *testing.T) {
  400. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  401. require.NoError(t, err)
  402. fromIdxInt64 := int64(256)
  403. toIdxInt64 := int64(257)
  404. tokenIDUint32 := uint32(0)
  405. depositAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  406. amount, _ := new(big.Int).SetString("100000000000000000000", 10)
  407. l1Tx := common.L1Tx{
  408. FromIdx: common.Idx(fromIdxInt64),
  409. ToIdx: common.Idx(toIdxInt64),
  410. DepositAmount: depositAmount,
  411. TokenID: common.TokenID(tokenIDUint32),
  412. Amount: amount,
  413. }
  414. L1UserTxs = append(L1UserTxs, l1Tx)
  415. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.DepositAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  416. require.NoError(t, err)
  417. currentBlockNum, err := rollupClient.client.EthLastBlock()
  418. require.NoError(t, err)
  419. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  420. require.NoError(t, err)
  421. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  422. assert.Equal(t, l1Tx.DepositAmount, rollupEvents.L1UserTx[0].L1UserTx.DepositAmount)
  423. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  424. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  425. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  426. }
  427. func TestRollupL1UserTxERC20DepositTransfer(t *testing.T) {
  428. rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
  429. require.NoError(t, err)
  430. fromIdxInt64 := int64(257)
  431. toIdxInt64 := int64(258)
  432. depositAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  433. amount, _ := new(big.Int).SetString("100000000000000000000", 10)
  434. l1Tx := common.L1Tx{
  435. FromIdx: common.Idx(fromIdxInt64),
  436. ToIdx: common.Idx(toIdxInt64),
  437. DepositAmount: depositAmount,
  438. TokenID: common.TokenID(tokenHEZID),
  439. Amount: amount,
  440. }
  441. L1UserTxs = append(L1UserTxs, l1Tx)
  442. _, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.DepositAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
  443. require.NoError(t, err)
  444. currentBlockNum, err := rollupClient.client.EthLastBlock()
  445. require.NoError(t, err)
  446. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  447. require.NoError(t, err)
  448. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  449. assert.Equal(t, l1Tx.DepositAmount, rollupEvents.L1UserTx[0].L1UserTx.DepositAmount)
  450. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  451. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  452. assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  453. }
  454. func TestRollupL1UserTxERC20PermitDepositTransfer(t *testing.T) {
  455. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  456. require.NoError(t, err)
  457. fromIdxInt64 := int64(258)
  458. toIdxInt64 := int64(259)
  459. depositAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  460. amount, _ := new(big.Int).SetString("100000000000000000000", 10)
  461. l1Tx := common.L1Tx{
  462. FromIdx: common.Idx(fromIdxInt64),
  463. ToIdx: common.Idx(toIdxInt64),
  464. DepositAmount: depositAmount,
  465. TokenID: common.TokenID(tokenIDERC777),
  466. Amount: amount,
  467. }
  468. L1UserTxs = append(L1UserTxs, l1Tx)
  469. _, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.DepositAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
  470. require.NoError(t, err)
  471. currentBlockNum, err := rollupClient.client.EthLastBlock()
  472. require.NoError(t, err)
  473. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  474. require.NoError(t, err)
  475. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  476. assert.Equal(t, l1Tx.DepositAmount, rollupEvents.L1UserTx[0].L1UserTx.DepositAmount)
  477. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  478. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  479. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  480. }
  481. func TestRollupL1UserTxETHCreateAccountDepositTransfer(t *testing.T) {
  482. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  483. require.NoError(t, err)
  484. fromIdxInt64 := int64(256)
  485. toIdxInt64 := int64(257)
  486. tokenIDUint32 := uint32(0)
  487. depositAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  488. amount, _ := new(big.Int).SetString("20000000000000000000", 10)
  489. l1Tx := common.L1Tx{
  490. FromIdx: common.Idx(fromIdxInt64),
  491. ToIdx: common.Idx(toIdxInt64),
  492. DepositAmount: depositAmount,
  493. TokenID: common.TokenID(tokenIDUint32),
  494. Amount: amount,
  495. }
  496. L1UserTxs = append(L1UserTxs, l1Tx)
  497. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.DepositAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  498. require.NoError(t, err)
  499. currentBlockNum, err := rollupClient.client.EthLastBlock()
  500. require.NoError(t, err)
  501. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  502. require.NoError(t, err)
  503. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  504. assert.Equal(t, l1Tx.DepositAmount, rollupEvents.L1UserTx[0].L1UserTx.DepositAmount)
  505. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  506. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  507. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  508. }
  509. func TestRollupL1UserTxERC20CreateAccountDepositTransfer(t *testing.T) {
  510. rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
  511. require.NoError(t, err)
  512. fromIdxInt64 := int64(257)
  513. toIdxInt64 := int64(258)
  514. depositAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  515. amount, _ := new(big.Int).SetString("30000000000000000000", 10)
  516. l1Tx := common.L1Tx{
  517. FromIdx: common.Idx(fromIdxInt64),
  518. ToIdx: common.Idx(toIdxInt64),
  519. DepositAmount: depositAmount,
  520. TokenID: common.TokenID(tokenHEZID),
  521. Amount: amount,
  522. }
  523. L1UserTxs = append(L1UserTxs, l1Tx)
  524. _, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.DepositAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
  525. require.NoError(t, err)
  526. currentBlockNum, err := rollupClient.client.EthLastBlock()
  527. require.NoError(t, err)
  528. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  529. require.NoError(t, err)
  530. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  531. assert.Equal(t, l1Tx.DepositAmount, rollupEvents.L1UserTx[0].L1UserTx.DepositAmount)
  532. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  533. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  534. assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  535. }
  536. func TestRollupL1UserTxERC20PermitCreateAccountDepositTransfer(t *testing.T) {
  537. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  538. require.NoError(t, err)
  539. fromIdxInt64 := int64(258)
  540. toIdxInt64 := int64(259)
  541. depositAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  542. amount, _ := new(big.Int).SetString("40000000000000000000", 10)
  543. l1Tx := common.L1Tx{
  544. FromIdx: common.Idx(fromIdxInt64),
  545. ToIdx: common.Idx(toIdxInt64),
  546. DepositAmount: depositAmount,
  547. TokenID: common.TokenID(tokenIDERC777),
  548. Amount: amount,
  549. }
  550. L1UserTxs = append(L1UserTxs, l1Tx)
  551. _, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.DepositAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
  552. require.NoError(t, err)
  553. currentBlockNum, err := rollupClient.client.EthLastBlock()
  554. require.NoError(t, err)
  555. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  556. require.NoError(t, err)
  557. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  558. assert.Equal(t, l1Tx.DepositAmount, rollupEvents.L1UserTx[0].L1UserTx.DepositAmount)
  559. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  560. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  561. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  562. }
  563. func TestRollupL1UserTxETHForceTransfer(t *testing.T) {
  564. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  565. require.NoError(t, err)
  566. fromIdxInt64 := int64(256)
  567. toIdxInt64 := int64(257)
  568. tokenIDUint32 := uint32(0)
  569. amount, _ := new(big.Int).SetString("20000000000000000000", 10)
  570. l1Tx := common.L1Tx{
  571. FromIdx: common.Idx(fromIdxInt64),
  572. ToIdx: common.Idx(toIdxInt64),
  573. DepositAmount: big.NewInt(0),
  574. TokenID: common.TokenID(tokenIDUint32),
  575. Amount: amount,
  576. }
  577. L1UserTxs = append(L1UserTxs, l1Tx)
  578. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.DepositAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  579. require.NoError(t, err)
  580. currentBlockNum, err := rollupClient.client.EthLastBlock()
  581. require.NoError(t, err)
  582. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  583. require.NoError(t, err)
  584. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  585. assert.Equal(t, l1Tx.DepositAmount, rollupEvents.L1UserTx[0].L1UserTx.DepositAmount)
  586. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  587. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  588. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  589. }
  590. func TestRollupL1UserTxERC20ForceTransfer(t *testing.T) {
  591. rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
  592. require.NoError(t, err)
  593. fromIdxInt64 := int64(257)
  594. toIdxInt64 := int64(258)
  595. amount, _ := new(big.Int).SetString("10000000000000000000", 10)
  596. l1Tx := common.L1Tx{
  597. FromIdx: common.Idx(fromIdxInt64),
  598. ToIdx: common.Idx(toIdxInt64),
  599. DepositAmount: big.NewInt(0),
  600. TokenID: common.TokenID(tokenHEZID),
  601. Amount: amount,
  602. }
  603. L1UserTxs = append(L1UserTxs, l1Tx)
  604. _, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.DepositAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
  605. require.NoError(t, err)
  606. currentBlockNum, err := rollupClient.client.EthLastBlock()
  607. require.NoError(t, err)
  608. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  609. require.NoError(t, err)
  610. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  611. assert.Equal(t, l1Tx.DepositAmount, rollupEvents.L1UserTx[0].L1UserTx.DepositAmount)
  612. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  613. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  614. assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  615. }
  616. func TestRollupL1UserTxERC20PermitForceTransfer(t *testing.T) {
  617. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  618. require.NoError(t, err)
  619. fromIdxInt64 := int64(259)
  620. toIdxInt64 := int64(260)
  621. amount, _ := new(big.Int).SetString("30000000000000000000", 10)
  622. l1Tx := common.L1Tx{
  623. FromIdx: common.Idx(fromIdxInt64),
  624. ToIdx: common.Idx(toIdxInt64),
  625. DepositAmount: big.NewInt(0),
  626. TokenID: common.TokenID(tokenIDERC777),
  627. Amount: amount,
  628. }
  629. L1UserTxs = append(L1UserTxs, l1Tx)
  630. _, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.DepositAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
  631. require.NoError(t, err)
  632. currentBlockNum, err := rollupClient.client.EthLastBlock()
  633. require.NoError(t, err)
  634. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  635. require.NoError(t, err)
  636. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  637. assert.Equal(t, l1Tx.DepositAmount, rollupEvents.L1UserTx[0].L1UserTx.DepositAmount)
  638. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  639. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  640. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  641. }
  642. func TestRollupL1UserTxETHForceExit(t *testing.T) {
  643. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  644. require.NoError(t, err)
  645. fromIdxInt64 := int64(256)
  646. toIdxInt64 := int64(1)
  647. tokenIDUint32 := uint32(0)
  648. amount, _ := new(big.Int).SetString("10000000000000000000", 10)
  649. l1Tx := common.L1Tx{
  650. FromIdx: common.Idx(fromIdxInt64),
  651. ToIdx: common.Idx(toIdxInt64),
  652. DepositAmount: big.NewInt(0),
  653. TokenID: common.TokenID(tokenIDUint32),
  654. Amount: amount,
  655. }
  656. L1UserTxs = append(L1UserTxs, l1Tx)
  657. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.DepositAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  658. require.NoError(t, err)
  659. currentBlockNum, err := rollupClient.client.EthLastBlock()
  660. require.NoError(t, err)
  661. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  662. require.NoError(t, err)
  663. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  664. assert.Equal(t, l1Tx.DepositAmount, rollupEvents.L1UserTx[0].L1UserTx.DepositAmount)
  665. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  666. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  667. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  668. }
  669. func TestRollupL1UserTxERC20ForceExit(t *testing.T) {
  670. rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
  671. require.NoError(t, err)
  672. fromIdxInt64 := int64(257)
  673. toIdxInt64 := int64(1)
  674. amount, _ := new(big.Int).SetString("20000000000000000000", 10)
  675. l1Tx := common.L1Tx{
  676. FromIdx: common.Idx(fromIdxInt64),
  677. ToIdx: common.Idx(toIdxInt64),
  678. DepositAmount: big.NewInt(0),
  679. TokenID: common.TokenID(tokenHEZID),
  680. Amount: amount,
  681. }
  682. L1UserTxs = append(L1UserTxs, l1Tx)
  683. _, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.DepositAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
  684. require.NoError(t, err)
  685. currentBlockNum, err := rollupClient.client.EthLastBlock()
  686. require.NoError(t, err)
  687. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  688. require.NoError(t, err)
  689. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  690. assert.Equal(t, l1Tx.DepositAmount, rollupEvents.L1UserTx[0].L1UserTx.DepositAmount)
  691. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  692. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  693. assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  694. }
  695. func TestRollupL1UserTxERC20PermitForceExit(t *testing.T) {
  696. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  697. require.NoError(t, err)
  698. fromIdxInt64 := int64(258)
  699. toIdxInt64 := int64(1)
  700. fromIdx := new(common.Idx)
  701. *fromIdx = 0
  702. amount, _ := new(big.Int).SetString("30000000000000000000", 10)
  703. l1Tx := common.L1Tx{
  704. FromIdx: common.Idx(fromIdxInt64),
  705. ToIdx: common.Idx(toIdxInt64),
  706. DepositAmount: big.NewInt(0),
  707. TokenID: common.TokenID(tokenIDERC777),
  708. Amount: amount,
  709. }
  710. L1UserTxs = append(L1UserTxs, l1Tx)
  711. _, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.DepositAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
  712. require.NoError(t, err)
  713. currentBlockNum, err := rollupClient.client.EthLastBlock()
  714. require.NoError(t, err)
  715. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  716. require.NoError(t, err)
  717. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  718. assert.Equal(t, l1Tx.DepositAmount, rollupEvents.L1UserTx[0].L1UserTx.DepositAmount)
  719. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  720. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  721. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  722. }
  723. func TestRollupForgeBatch2(t *testing.T) {
  724. // Forge Batch 2
  725. _, err := rollupClient.RollupForgeBatch(argsForge)
  726. require.NoError(t, err)
  727. currentBlockNum, err := rollupClient.client.EthLastBlock()
  728. require.NoError(t, err)
  729. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  730. require.NoError(t, err)
  731. assert.Equal(t, int64(2), rollupEvents.ForgeBatch[0].BatchNum)
  732. // Forge Batch 3
  733. args := new(RollupForgeBatchArgs)
  734. args.FeeIdxCoordinator = []common.Idx{} // When encoded, 64 times the 0 idx means that no idx to collect fees is specified.
  735. args.L1CoordinatorTxs = argsForge.L1CoordinatorTxs
  736. args.L1CoordinatorTxsAuths = argsForge.L1CoordinatorTxsAuths
  737. for i := 0; i < len(L1UserTxs); i++ {
  738. l1UserTx := L1UserTxs[i]
  739. l1UserTx.EffectiveAmount = l1UserTx.Amount
  740. l1Bytes, err := l1UserTx.BytesDataAvailability(uint32(nLevels))
  741. require.NoError(t, err)
  742. l1UserTxDataAvailability, err := common.L1TxFromDataAvailability(l1Bytes, uint32(nLevels))
  743. require.NoError(t, err)
  744. args.L1UserTxs = append(args.L1UserTxs, *l1UserTxDataAvailability)
  745. }
  746. newStateRoot := new(big.Int)
  747. newStateRoot.SetString("18317824016047294649053625209337295956588174734569560016974612130063629505228", 10)
  748. newExitRoot := new(big.Int)
  749. newExitRoot.SetString("1114281409737474688393837964161044726766678436313681099613347372031079422302", 10)
  750. amount := new(big.Int)
  751. amount.SetString("79000000", 10)
  752. l2Tx := common.L2Tx{
  753. ToIdx: 256,
  754. Amount: amount,
  755. FromIdx: 257,
  756. Fee: 201,
  757. }
  758. l2Txs := []common.L2Tx{}
  759. l2Txs = append(l2Txs, l2Tx)
  760. l2Txs = append(l2Txs, l2Tx)
  761. args.L2TxsData = l2Txs
  762. args.NewLastIdx = int64(1000)
  763. args.NewStRoot = newStateRoot
  764. args.NewExitRoot = newExitRoot
  765. args.L1Batch = true
  766. args.VerifierIdx = 0
  767. args.ProofA[0] = big.NewInt(0)
  768. args.ProofA[1] = big.NewInt(0)
  769. args.ProofB[0] = [2]*big.Int{big.NewInt(0), big.NewInt(0)}
  770. args.ProofB[1] = [2]*big.Int{big.NewInt(0), big.NewInt(0)}
  771. args.ProofC[0] = big.NewInt(0)
  772. args.ProofC[1] = big.NewInt(0)
  773. argsForge = args
  774. _, err = rollupClient.RollupForgeBatch(argsForge)
  775. require.NoError(t, err)
  776. currentBlockNum, err = rollupClient.client.EthLastBlock()
  777. require.NoError(t, err)
  778. rollupEvents, _, err = rollupClient.RollupEventsByBlock(currentBlockNum)
  779. require.NoError(t, err)
  780. assert.Equal(t, int64(3), rollupEvents.ForgeBatch[0].BatchNum)
  781. assert.Equal(t, uint16(len(L1UserTxs)), rollupEvents.ForgeBatch[0].L1UserTxsLen)
  782. ethHashForge = rollupEvents.ForgeBatch[0].EthTxHash
  783. }
  784. func TestRollupForgeBatchArgs2(t *testing.T) {
  785. args, sender, err := rollupClient.RollupForgeBatchArgs(ethHashForge, uint16(len(L1UserTxs)))
  786. require.NoError(t, err)
  787. assert.Equal(t, *sender, rollupClient.client.account.Address)
  788. assert.Equal(t, argsForge.FeeIdxCoordinator, args.FeeIdxCoordinator)
  789. assert.Equal(t, argsForge.L1Batch, args.L1Batch)
  790. assert.Equal(t, argsForge.L1UserTxs, args.L1UserTxs)
  791. assert.Equal(t, argsForge.L1CoordinatorTxs, args.L1CoordinatorTxs)
  792. assert.Equal(t, argsForge.L1CoordinatorTxsAuths, args.L1CoordinatorTxsAuths)
  793. assert.Equal(t, argsForge.L2TxsData, args.L2TxsData)
  794. assert.Equal(t, argsForge.NewLastIdx, args.NewLastIdx)
  795. assert.Equal(t, argsForge.NewStRoot, args.NewStRoot)
  796. assert.Equal(t, argsForge.VerifierIdx, args.VerifierIdx)
  797. }
  798. func TestRollupWithdrawMerkleProof(t *testing.T) {
  799. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  800. require.NoError(t, err)
  801. var pkComp babyjub.PublicKeyComp
  802. pkCompBE, err := hex.DecodeString("adc3b754f8da621967b073a787bef8eec7052f2ba712b23af57d98f65beea8b2")
  803. require.NoError(t, err)
  804. pkCompLE := common.SwapEndianness(pkCompBE)
  805. copy(pkComp[:], pkCompLE)
  806. pk, err := pkComp.Decompress()
  807. require.NoError(t, err)
  808. require.NoError(t, err)
  809. tokenID := uint32(tokenHEZID)
  810. numExitRoot := int64(3)
  811. fromIdx := int64(256)
  812. amount, _ := new(big.Int).SetString("20000000000000000000", 10)
  813. // siblingBytes0, err := new(big.Int).SetString("19508838618377323910556678335932426220272947530531646682154552299216398748115", 10)
  814. // require.NoError(t, err)
  815. // siblingBytes1, err := new(big.Int).SetString("15198806719713909654457742294233381653226080862567104272457668857208564789571", 10)
  816. // require.NoError(t, err)
  817. var siblings []*big.Int
  818. // siblings = append(siblings, siblingBytes0)
  819. // siblings = append(siblings, siblingBytes1)
  820. instantWithdraw := true
  821. _, err = rollupClientAux.RollupWithdrawMerkleProof(pk, tokenID, numExitRoot, fromIdx, amount, siblings, instantWithdraw)
  822. require.NoError(t, err)
  823. currentBlockNum, err := rollupClient.client.EthLastBlock()
  824. require.NoError(t, err)
  825. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  826. require.NoError(t, err)
  827. assert.Equal(t, uint64(fromIdx), rollupEvents.Withdraw[0].Idx)
  828. assert.Equal(t, instantWithdraw, rollupEvents.Withdraw[0].InstantWithdraw)
  829. assert.Equal(t, uint64(numExitRoot), rollupEvents.Withdraw[0].NumExitRoot)
  830. // tokenAmount = 20
  831. // amountUSD = tokenAmount * tokenPrice = 20 * 10 = 200
  832. // Bucket[0].ceilUSD = 100, Bucket[1].ceilUSD = 200, ...
  833. // Bucket 1
  834. // Bucket[0].withdrawals = 1, Bucket[1].withdrawals = 2, ...
  835. // Bucket[1].withdrawals - 1 = 1
  836. assert.Equal(t, 1, rollupEvents.UpdateBucketWithdraw[0].NumBucket)
  837. assert.Equal(t, blockStampBucket, rollupEvents.UpdateBucketWithdraw[0].BlockStamp)
  838. assert.Equal(t, big.NewInt(1), rollupEvents.UpdateBucketWithdraw[0].Withdrawals)
  839. }
  840. func TestRollupSafeMode(t *testing.T) {
  841. _, err := rollupClient.RollupSafeMode()
  842. require.NoError(t, err)
  843. currentBlockNum, err := rollupClient.client.EthLastBlock()
  844. require.NoError(t, err)
  845. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  846. require.NoError(t, err)
  847. auxEvent := new(RollupEventSafeMode)
  848. assert.Equal(t, auxEvent, &rollupEvents.SafeMode[0])
  849. }