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.

959 lines
39 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.Nil(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.Nil(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.Nil(t, err)
  67. _, err = rollupClient.RollupAddToken(tokenHEZAddressConst, feeAddToken, deadline)
  68. require.Nil(t, err)
  69. currentBlockNum, err := rollupClient.client.EthLastBlock()
  70. require.Nil(t, err)
  71. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  72. require.Nil(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.Nil(t, err)
  83. // MultiBid
  84. currentSlot, err := auctionClient.AuctionGetCurrentSlotNumber()
  85. require.Nil(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.Nil(t, err)
  95. // Add Blocks
  96. blockNum := int64(int(blocksPerSlot)*int(currentSlot+4) + int(genesisBlock))
  97. currentBlockNum, err := auctionClient.client.EthLastBlock()
  98. require.Nil(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.Nil(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.Nil(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.Nil(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.Nil(t, err)
  145. currentBlockNum, err = rollupClient.client.EthLastBlock()
  146. require.Nil(t, err)
  147. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  148. require.Nil(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.Nil(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.Nil(t, err)
  170. currentBlockNum, err := rollupClient.client.EthLastBlock()
  171. require.Nil(t, err)
  172. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  173. require.Nil(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.Nil(t, err)
  180. currentBlockNum, err := rollupClient.client.EthLastBlock()
  181. require.Nil(t, err)
  182. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  183. require.Nil(t, err)
  184. assert.Equal(t, newFeeAddToken, rollupEvents.UpdateFeeAddToken[0].NewFeeAddToken)
  185. }
  186. func TestRollupUpdateBucketsParameters(t *testing.T) {
  187. var bucketsParameters [common.RollupConstNumBuckets][4]*big.Int
  188. for i := range bucketsParameters {
  189. bucketsParameters[i][0] = big.NewInt(int64((i + 1) * 100)) // ceilUSD
  190. bucketsParameters[i][1] = big.NewInt(int64(i + 1)) // withdrawals
  191. bucketsParameters[i][2] = big.NewInt(int64(i+1) * 100) // blockWithdrawalRate
  192. bucketsParameters[i][3] = big.NewInt(int64(100000000000)) // maxWithdrawals
  193. }
  194. _, err := rollupClient.RollupUpdateBucketsParameters(bucketsParameters)
  195. require.Nil(t, err)
  196. currentBlockNum, err := rollupClient.client.EthLastBlock()
  197. require.Nil(t, err)
  198. blockStampBucket = currentBlockNum
  199. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  200. require.Nil(t, err)
  201. for i := range bucketsParameters {
  202. assert.Equal(t, bucketsParameters[i][0].String(), rollupEvents.UpdateBucketsParameters[0].ArrayBuckets[i][0].String())
  203. assert.Equal(t, bucketsParameters[i][1].String(), rollupEvents.UpdateBucketsParameters[0].ArrayBuckets[i][1].String())
  204. assert.Equal(t, bucketsParameters[i][2].String(), rollupEvents.UpdateBucketsParameters[0].ArrayBuckets[i][2].String())
  205. assert.Equal(t, bucketsParameters[i][3].String(), rollupEvents.UpdateBucketsParameters[0].ArrayBuckets[i][3].String())
  206. }
  207. }
  208. func TestRollupUpdateWithdrawalDelay(t *testing.T) {
  209. newWithdrawalDelay := uint64(100000)
  210. _, err := rollupClient.RollupUpdateWithdrawalDelay(newWithdrawalDelay)
  211. require.Nil(t, err)
  212. currentBlockNum, err := rollupClient.client.EthLastBlock()
  213. require.Nil(t, err)
  214. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  215. require.Nil(t, err)
  216. assert.Equal(t, newWithdrawalDelay, rollupEvents.UpdateWithdrawalDelay[0].NewWithdrawalDelay)
  217. }
  218. func TestRollupUpdateTokenExchange(t *testing.T) {
  219. var addressArray []ethCommon.Address
  220. var valueArray []uint64
  221. addressToken1, err := rollupClient.hermez.TokenList(nil, big.NewInt(1))
  222. addressArray = append(addressArray, addressToken1)
  223. tokenPrice := 10
  224. valueArray = append(valueArray, uint64(tokenPrice*1e14))
  225. require.Nil(t, err)
  226. _, err = rollupClient.RollupUpdateTokenExchange(addressArray, valueArray)
  227. require.Nil(t, err)
  228. currentBlockNum, err := rollupClient.client.EthLastBlock()
  229. require.Nil(t, err)
  230. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  231. require.Nil(t, err)
  232. assert.Equal(t, addressArray, rollupEvents.UpdateTokenExchange[0].AddressArray)
  233. assert.Equal(t, valueArray, rollupEvents.UpdateTokenExchange[0].ValueArray)
  234. }
  235. func TestRollupL1UserTxETHCreateAccountDeposit(t *testing.T) {
  236. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  237. require.Nil(t, err)
  238. key := genKeysBjj(2)
  239. fromIdxInt64 := int64(0)
  240. toIdxInt64 := int64(0)
  241. tokenIDUint32 := uint32(0)
  242. loadAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  243. l1Tx := common.L1Tx{
  244. FromBJJ: key.BJJPublicKey,
  245. FromIdx: common.Idx(fromIdxInt64),
  246. ToIdx: common.Idx(toIdxInt64),
  247. LoadAmount: loadAmount,
  248. TokenID: common.TokenID(tokenIDUint32),
  249. Amount: big.NewInt(0),
  250. }
  251. L1UserTxs = append(L1UserTxs, l1Tx)
  252. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  253. require.Nil(t, err)
  254. currentBlockNum, err := rollupClient.client.EthLastBlock()
  255. require.Nil(t, err)
  256. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  257. require.Nil(t, err)
  258. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  259. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  260. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  261. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  262. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  263. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  264. }
  265. func TestRollupL1UserTxERC20CreateAccountDeposit(t *testing.T) {
  266. rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
  267. require.Nil(t, err)
  268. key := genKeysBjj(1)
  269. fromIdxInt64 := int64(0)
  270. toIdxInt64 := int64(0)
  271. loadAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  272. l1Tx := common.L1Tx{
  273. FromBJJ: key.BJJPublicKey,
  274. FromIdx: common.Idx(fromIdxInt64),
  275. ToIdx: common.Idx(toIdxInt64),
  276. LoadAmount: loadAmount,
  277. TokenID: common.TokenID(tokenHEZID),
  278. Amount: big.NewInt(0),
  279. }
  280. L1UserTxs = append(L1UserTxs, l1Tx)
  281. _, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
  282. require.Nil(t, err)
  283. currentBlockNum, err := rollupClient.client.EthLastBlock()
  284. require.Nil(t, err)
  285. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  286. require.Nil(t, err)
  287. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  288. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  289. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  290. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  291. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  292. assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  293. }
  294. func TestRollupL1UserTxERC20PermitCreateAccountDeposit(t *testing.T) {
  295. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  296. require.Nil(t, err)
  297. key := genKeysBjj(3)
  298. fromIdxInt64 := int64(0)
  299. toIdxInt64 := int64(0)
  300. loadAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  301. l1Tx := common.L1Tx{
  302. FromBJJ: key.BJJPublicKey,
  303. FromIdx: common.Idx(fromIdxInt64),
  304. ToIdx: common.Idx(toIdxInt64),
  305. LoadAmount: loadAmount,
  306. TokenID: common.TokenID(tokenIDERC777),
  307. Amount: big.NewInt(0),
  308. }
  309. L1UserTxs = append(L1UserTxs, l1Tx)
  310. _, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
  311. require.Nil(t, err)
  312. currentBlockNum, err := rollupClient.client.EthLastBlock()
  313. require.Nil(t, err)
  314. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  315. require.Nil(t, err)
  316. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  317. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  318. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  319. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  320. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  321. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  322. }
  323. func TestRollupL1UserTxETHDeposit(t *testing.T) {
  324. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  325. require.Nil(t, err)
  326. fromIdxInt64 := int64(256)
  327. toIdxInt64 := int64(0)
  328. tokenIDUint32 := uint32(0)
  329. loadAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  330. l1Tx := common.L1Tx{
  331. FromBJJ: nil,
  332. FromIdx: common.Idx(fromIdxInt64),
  333. ToIdx: common.Idx(toIdxInt64),
  334. LoadAmount: loadAmount,
  335. TokenID: common.TokenID(tokenIDUint32),
  336. Amount: big.NewInt(0),
  337. }
  338. L1UserTxs = append(L1UserTxs, l1Tx)
  339. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  340. require.Nil(t, err)
  341. currentBlockNum, err := rollupClient.client.EthLastBlock()
  342. require.Nil(t, err)
  343. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  344. require.Nil(t, err)
  345. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  346. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  347. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  348. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  349. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  350. }
  351. func TestRollupL1UserTxERC20Deposit(t *testing.T) {
  352. rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
  353. require.Nil(t, err)
  354. fromIdxInt64 := int64(257)
  355. toIdxInt64 := int64(0)
  356. loadAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  357. l1Tx := common.L1Tx{
  358. FromBJJ: nil,
  359. FromIdx: common.Idx(fromIdxInt64),
  360. ToIdx: common.Idx(toIdxInt64),
  361. LoadAmount: loadAmount,
  362. TokenID: common.TokenID(tokenHEZID),
  363. Amount: big.NewInt(0),
  364. }
  365. L1UserTxs = append(L1UserTxs, l1Tx)
  366. _, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
  367. require.Nil(t, err)
  368. currentBlockNum, err := rollupClient.client.EthLastBlock()
  369. require.Nil(t, err)
  370. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  371. require.Nil(t, err)
  372. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  373. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  374. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  375. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  376. assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  377. }
  378. func TestRollupL1UserTxERC20PermitDeposit(t *testing.T) {
  379. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  380. require.Nil(t, err)
  381. fromIdxInt64 := int64(258)
  382. toIdxInt64 := int64(0)
  383. loadAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  384. l1Tx := common.L1Tx{
  385. FromIdx: common.Idx(fromIdxInt64),
  386. ToIdx: common.Idx(toIdxInt64),
  387. LoadAmount: loadAmount,
  388. TokenID: common.TokenID(tokenIDERC777),
  389. Amount: big.NewInt(0),
  390. }
  391. L1UserTxs = append(L1UserTxs, l1Tx)
  392. _, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
  393. require.Nil(t, err)
  394. currentBlockNum, err := rollupClient.client.EthLastBlock()
  395. require.Nil(t, err)
  396. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  397. require.Nil(t, err)
  398. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  399. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  400. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  401. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  402. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  403. }
  404. func TestRollupL1UserTxETHDepositTransfer(t *testing.T) {
  405. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  406. require.Nil(t, err)
  407. fromIdxInt64 := int64(256)
  408. toIdxInt64 := int64(257)
  409. tokenIDUint32 := uint32(0)
  410. loadAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  411. amount, _ := new(big.Int).SetString("100000000000000000000", 10)
  412. l1Tx := common.L1Tx{
  413. FromIdx: common.Idx(fromIdxInt64),
  414. ToIdx: common.Idx(toIdxInt64),
  415. LoadAmount: loadAmount,
  416. TokenID: common.TokenID(tokenIDUint32),
  417. Amount: amount,
  418. }
  419. L1UserTxs = append(L1UserTxs, l1Tx)
  420. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  421. require.Nil(t, err)
  422. currentBlockNum, err := rollupClient.client.EthLastBlock()
  423. require.Nil(t, err)
  424. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  425. require.Nil(t, err)
  426. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  427. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  428. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  429. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  430. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  431. }
  432. func TestRollupL1UserTxERC20DepositTransfer(t *testing.T) {
  433. rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
  434. require.Nil(t, err)
  435. fromIdxInt64 := int64(257)
  436. toIdxInt64 := int64(258)
  437. loadAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  438. amount, _ := new(big.Int).SetString("100000000000000000000", 10)
  439. l1Tx := common.L1Tx{
  440. FromIdx: common.Idx(fromIdxInt64),
  441. ToIdx: common.Idx(toIdxInt64),
  442. LoadAmount: loadAmount,
  443. TokenID: common.TokenID(tokenHEZID),
  444. Amount: amount,
  445. }
  446. L1UserTxs = append(L1UserTxs, l1Tx)
  447. _, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
  448. require.Nil(t, err)
  449. currentBlockNum, err := rollupClient.client.EthLastBlock()
  450. require.Nil(t, err)
  451. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  452. require.Nil(t, err)
  453. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  454. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  455. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  456. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  457. assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  458. }
  459. func TestRollupL1UserTxERC20PermitDepositTransfer(t *testing.T) {
  460. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  461. require.Nil(t, err)
  462. fromIdxInt64 := int64(258)
  463. toIdxInt64 := int64(259)
  464. loadAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  465. amount, _ := new(big.Int).SetString("100000000000000000000", 10)
  466. l1Tx := common.L1Tx{
  467. FromIdx: common.Idx(fromIdxInt64),
  468. ToIdx: common.Idx(toIdxInt64),
  469. LoadAmount: loadAmount,
  470. TokenID: common.TokenID(tokenIDERC777),
  471. Amount: amount,
  472. }
  473. L1UserTxs = append(L1UserTxs, l1Tx)
  474. _, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
  475. require.Nil(t, err)
  476. currentBlockNum, err := rollupClient.client.EthLastBlock()
  477. require.Nil(t, err)
  478. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  479. require.Nil(t, err)
  480. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  481. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  482. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  483. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  484. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  485. }
  486. func TestRollupL1UserTxETHCreateAccountDepositTransfer(t *testing.T) {
  487. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  488. require.Nil(t, err)
  489. fromIdxInt64 := int64(256)
  490. toIdxInt64 := int64(257)
  491. tokenIDUint32 := uint32(0)
  492. loadAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  493. amount, _ := new(big.Int).SetString("20000000000000000000", 10)
  494. l1Tx := common.L1Tx{
  495. FromIdx: common.Idx(fromIdxInt64),
  496. ToIdx: common.Idx(toIdxInt64),
  497. LoadAmount: loadAmount,
  498. TokenID: common.TokenID(tokenIDUint32),
  499. Amount: amount,
  500. }
  501. L1UserTxs = append(L1UserTxs, l1Tx)
  502. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  503. require.Nil(t, err)
  504. currentBlockNum, err := rollupClient.client.EthLastBlock()
  505. require.Nil(t, err)
  506. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  507. require.Nil(t, err)
  508. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  509. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  510. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  511. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  512. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  513. }
  514. func TestRollupL1UserTxERC20CreateAccountDepositTransfer(t *testing.T) {
  515. rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
  516. require.Nil(t, err)
  517. fromIdxInt64 := int64(257)
  518. toIdxInt64 := int64(258)
  519. loadAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  520. amount, _ := new(big.Int).SetString("30000000000000000000", 10)
  521. l1Tx := common.L1Tx{
  522. FromIdx: common.Idx(fromIdxInt64),
  523. ToIdx: common.Idx(toIdxInt64),
  524. LoadAmount: loadAmount,
  525. TokenID: common.TokenID(tokenHEZID),
  526. Amount: amount,
  527. }
  528. L1UserTxs = append(L1UserTxs, l1Tx)
  529. _, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
  530. require.Nil(t, err)
  531. currentBlockNum, err := rollupClient.client.EthLastBlock()
  532. require.Nil(t, err)
  533. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  534. require.Nil(t, err)
  535. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  536. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  537. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  538. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  539. assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  540. }
  541. func TestRollupL1UserTxERC20PermitCreateAccountDepositTransfer(t *testing.T) {
  542. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  543. require.Nil(t, err)
  544. fromIdxInt64 := int64(258)
  545. toIdxInt64 := int64(259)
  546. loadAmount, _ := new(big.Int).SetString("1000000000000000000000", 10)
  547. amount, _ := new(big.Int).SetString("40000000000000000000", 10)
  548. l1Tx := common.L1Tx{
  549. FromIdx: common.Idx(fromIdxInt64),
  550. ToIdx: common.Idx(toIdxInt64),
  551. LoadAmount: loadAmount,
  552. TokenID: common.TokenID(tokenIDERC777),
  553. Amount: amount,
  554. }
  555. L1UserTxs = append(L1UserTxs, l1Tx)
  556. _, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
  557. require.Nil(t, err)
  558. currentBlockNum, err := rollupClient.client.EthLastBlock()
  559. require.Nil(t, err)
  560. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  561. require.Nil(t, err)
  562. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  563. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  564. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  565. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  566. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  567. }
  568. func TestRollupL1UserTxETHForceTransfer(t *testing.T) {
  569. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  570. require.Nil(t, err)
  571. fromIdxInt64 := int64(256)
  572. toIdxInt64 := int64(257)
  573. tokenIDUint32 := uint32(0)
  574. amount, _ := new(big.Int).SetString("20000000000000000000", 10)
  575. l1Tx := common.L1Tx{
  576. FromIdx: common.Idx(fromIdxInt64),
  577. ToIdx: common.Idx(toIdxInt64),
  578. LoadAmount: big.NewInt(0),
  579. TokenID: common.TokenID(tokenIDUint32),
  580. Amount: amount,
  581. }
  582. L1UserTxs = append(L1UserTxs, l1Tx)
  583. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  584. require.Nil(t, err)
  585. currentBlockNum, err := rollupClient.client.EthLastBlock()
  586. require.Nil(t, err)
  587. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  588. require.Nil(t, err)
  589. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  590. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  591. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  592. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  593. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  594. }
  595. func TestRollupL1UserTxERC20ForceTransfer(t *testing.T) {
  596. rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
  597. require.Nil(t, err)
  598. fromIdxInt64 := int64(257)
  599. toIdxInt64 := int64(258)
  600. amount, _ := new(big.Int).SetString("10000000000000000000", 10)
  601. l1Tx := common.L1Tx{
  602. FromIdx: common.Idx(fromIdxInt64),
  603. ToIdx: common.Idx(toIdxInt64),
  604. LoadAmount: big.NewInt(0),
  605. TokenID: common.TokenID(tokenHEZID),
  606. Amount: amount,
  607. }
  608. L1UserTxs = append(L1UserTxs, l1Tx)
  609. _, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
  610. require.Nil(t, err)
  611. currentBlockNum, err := rollupClient.client.EthLastBlock()
  612. require.Nil(t, err)
  613. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  614. require.Nil(t, err)
  615. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  616. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  617. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  618. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  619. assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  620. }
  621. func TestRollupL1UserTxERC20PermitForceTransfer(t *testing.T) {
  622. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  623. require.Nil(t, err)
  624. fromIdxInt64 := int64(259)
  625. toIdxInt64 := int64(260)
  626. amount, _ := new(big.Int).SetString("30000000000000000000", 10)
  627. l1Tx := common.L1Tx{
  628. FromIdx: common.Idx(fromIdxInt64),
  629. ToIdx: common.Idx(toIdxInt64),
  630. LoadAmount: big.NewInt(0),
  631. TokenID: common.TokenID(tokenIDERC777),
  632. Amount: amount,
  633. }
  634. L1UserTxs = append(L1UserTxs, l1Tx)
  635. _, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
  636. require.Nil(t, err)
  637. currentBlockNum, err := rollupClient.client.EthLastBlock()
  638. require.Nil(t, err)
  639. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  640. require.Nil(t, err)
  641. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  642. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  643. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  644. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  645. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  646. }
  647. func TestRollupL1UserTxETHForceExit(t *testing.T) {
  648. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  649. require.Nil(t, err)
  650. fromIdxInt64 := int64(256)
  651. toIdxInt64 := int64(1)
  652. tokenIDUint32 := uint32(0)
  653. amount, _ := new(big.Int).SetString("10000000000000000000", 10)
  654. l1Tx := common.L1Tx{
  655. FromIdx: common.Idx(fromIdxInt64),
  656. ToIdx: common.Idx(toIdxInt64),
  657. LoadAmount: big.NewInt(0),
  658. TokenID: common.TokenID(tokenIDUint32),
  659. Amount: amount,
  660. }
  661. L1UserTxs = append(L1UserTxs, l1Tx)
  662. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  663. require.Nil(t, err)
  664. currentBlockNum, err := rollupClient.client.EthLastBlock()
  665. require.Nil(t, err)
  666. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  667. require.Nil(t, err)
  668. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  669. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  670. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  671. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  672. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  673. }
  674. func TestRollupL1UserTxERC20ForceExit(t *testing.T) {
  675. rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
  676. require.Nil(t, err)
  677. fromIdxInt64 := int64(257)
  678. toIdxInt64 := int64(1)
  679. amount, _ := new(big.Int).SetString("20000000000000000000", 10)
  680. l1Tx := common.L1Tx{
  681. FromIdx: common.Idx(fromIdxInt64),
  682. ToIdx: common.Idx(toIdxInt64),
  683. LoadAmount: big.NewInt(0),
  684. TokenID: common.TokenID(tokenHEZID),
  685. Amount: amount,
  686. }
  687. L1UserTxs = append(L1UserTxs, l1Tx)
  688. _, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
  689. require.Nil(t, err)
  690. currentBlockNum, err := rollupClient.client.EthLastBlock()
  691. require.Nil(t, err)
  692. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  693. require.Nil(t, err)
  694. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  695. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  696. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  697. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  698. assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  699. }
  700. func TestRollupL1UserTxERC20PermitForceExit(t *testing.T) {
  701. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  702. require.Nil(t, err)
  703. fromIdxInt64 := int64(258)
  704. toIdxInt64 := int64(1)
  705. fromIdx := new(common.Idx)
  706. *fromIdx = 0
  707. amount, _ := new(big.Int).SetString("30000000000000000000", 10)
  708. l1Tx := common.L1Tx{
  709. FromIdx: common.Idx(fromIdxInt64),
  710. ToIdx: common.Idx(toIdxInt64),
  711. LoadAmount: big.NewInt(0),
  712. TokenID: common.TokenID(tokenIDERC777),
  713. Amount: amount,
  714. }
  715. L1UserTxs = append(L1UserTxs, l1Tx)
  716. _, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
  717. require.Nil(t, err)
  718. currentBlockNum, err := rollupClient.client.EthLastBlock()
  719. require.Nil(t, err)
  720. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  721. require.Nil(t, err)
  722. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  723. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  724. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  725. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  726. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  727. }
  728. func TestRollupForgeBatch2(t *testing.T) {
  729. // Forge Batch 2
  730. _, err := rollupClient.RollupForgeBatch(argsForge)
  731. require.Nil(t, err)
  732. currentBlockNum, err := rollupClient.client.EthLastBlock()
  733. require.Nil(t, err)
  734. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  735. require.Nil(t, err)
  736. assert.Equal(t, int64(2), rollupEvents.ForgeBatch[0].BatchNum)
  737. // Forge Batch 3
  738. args := new(RollupForgeBatchArgs)
  739. args.FeeIdxCoordinator = []common.Idx{} // When encoded, 64 times the 0 idx means that no idx to collect fees is specified.
  740. args.L1CoordinatorTxs = argsForge.L1CoordinatorTxs
  741. args.L1CoordinatorTxsAuths = argsForge.L1CoordinatorTxsAuths
  742. for i := 0; i < len(L1UserTxs); i++ {
  743. l1UserTx := L1UserTxs[i]
  744. l1UserTx.EffectiveAmount = l1UserTx.Amount
  745. l1Bytes, err := l1UserTx.BytesDataAvailability(uint32(nLevels))
  746. require.Nil(t, err)
  747. l1UserTxDataAvailability, err := common.L1TxFromDataAvailability(l1Bytes, uint32(nLevels))
  748. require.Nil(t, err)
  749. args.L1UserTxs = append(args.L1UserTxs, *l1UserTxDataAvailability)
  750. }
  751. newStateRoot := new(big.Int)
  752. newStateRoot.SetString("18317824016047294649053625209337295956588174734569560016974612130063629505228", 10)
  753. newExitRoot := new(big.Int)
  754. newExitRoot.SetString("1114281409737474688393837964161044726766678436313681099613347372031079422302", 10)
  755. amount := new(big.Int)
  756. amount.SetString("79000000", 10)
  757. l2Tx := common.L2Tx{
  758. ToIdx: 256,
  759. Amount: amount,
  760. FromIdx: 257,
  761. Fee: 201,
  762. }
  763. l2Txs := []common.L2Tx{}
  764. l2Txs = append(l2Txs, l2Tx)
  765. l2Txs = append(l2Txs, l2Tx)
  766. args.L2TxsData = l2Txs
  767. args.NewLastIdx = int64(1000)
  768. args.NewStRoot = newStateRoot
  769. args.NewExitRoot = newExitRoot
  770. args.L1Batch = true
  771. args.VerifierIdx = 0
  772. args.ProofA[0] = big.NewInt(0)
  773. args.ProofA[1] = big.NewInt(0)
  774. args.ProofB[0] = [2]*big.Int{big.NewInt(0), big.NewInt(0)}
  775. args.ProofB[1] = [2]*big.Int{big.NewInt(0), big.NewInt(0)}
  776. args.ProofC[0] = big.NewInt(0)
  777. args.ProofC[1] = big.NewInt(0)
  778. argsForge = args
  779. _, err = rollupClient.RollupForgeBatch(argsForge)
  780. require.Nil(t, err)
  781. currentBlockNum, err = rollupClient.client.EthLastBlock()
  782. require.Nil(t, err)
  783. rollupEvents, _, err = rollupClient.RollupEventsByBlock(currentBlockNum)
  784. require.Nil(t, err)
  785. assert.Equal(t, int64(3), rollupEvents.ForgeBatch[0].BatchNum)
  786. assert.Equal(t, uint16(len(L1UserTxs)), rollupEvents.ForgeBatch[0].L1UserTxsLen)
  787. ethHashForge = rollupEvents.ForgeBatch[0].EthTxHash
  788. }
  789. func TestRollupForgeBatchArgs2(t *testing.T) {
  790. args, sender, err := rollupClient.RollupForgeBatchArgs(ethHashForge, uint16(len(L1UserTxs)))
  791. require.Nil(t, err)
  792. assert.Equal(t, *sender, rollupClient.client.account.Address)
  793. assert.Equal(t, argsForge.FeeIdxCoordinator, args.FeeIdxCoordinator)
  794. assert.Equal(t, argsForge.L1Batch, args.L1Batch)
  795. assert.Equal(t, argsForge.L1UserTxs, args.L1UserTxs)
  796. assert.Equal(t, argsForge.L1CoordinatorTxs, args.L1CoordinatorTxs)
  797. assert.Equal(t, argsForge.L1CoordinatorTxsAuths, args.L1CoordinatorTxsAuths)
  798. assert.Equal(t, argsForge.L2TxsData, args.L2TxsData)
  799. assert.Equal(t, argsForge.NewLastIdx, args.NewLastIdx)
  800. assert.Equal(t, argsForge.NewStRoot, args.NewStRoot)
  801. assert.Equal(t, argsForge.VerifierIdx, args.VerifierIdx)
  802. }
  803. func TestRollupWithdrawMerkleProof(t *testing.T) {
  804. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  805. require.Nil(t, err)
  806. var pkComp babyjub.PublicKeyComp
  807. pkCompBE, err := hex.DecodeString("adc3b754f8da621967b073a787bef8eec7052f2ba712b23af57d98f65beea8b2")
  808. require.Nil(t, err)
  809. pkCompLE := common.SwapEndianness(pkCompBE)
  810. copy(pkComp[:], pkCompLE)
  811. pk, err := pkComp.Decompress()
  812. require.Nil(t, err)
  813. require.Nil(t, err)
  814. tokenID := uint32(tokenHEZID)
  815. numExitRoot := int64(3)
  816. fromIdx := int64(256)
  817. amount, _ := new(big.Int).SetString("20000000000000000000", 10)
  818. // siblingBytes0, err := new(big.Int).SetString("19508838618377323910556678335932426220272947530531646682154552299216398748115", 10)
  819. // require.Nil(t, err)
  820. // siblingBytes1, err := new(big.Int).SetString("15198806719713909654457742294233381653226080862567104272457668857208564789571", 10)
  821. // require.Nil(t, err)
  822. var siblings []*big.Int
  823. // siblings = append(siblings, siblingBytes0)
  824. // siblings = append(siblings, siblingBytes1)
  825. instantWithdraw := true
  826. _, err = rollupClientAux.RollupWithdrawMerkleProof(pk, tokenID, numExitRoot, fromIdx, amount, siblings, instantWithdraw)
  827. require.Nil(t, err)
  828. currentBlockNum, err := rollupClient.client.EthLastBlock()
  829. require.Nil(t, err)
  830. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  831. require.Nil(t, err)
  832. assert.Equal(t, uint64(fromIdx), rollupEvents.Withdraw[0].Idx)
  833. assert.Equal(t, instantWithdraw, rollupEvents.Withdraw[0].InstantWithdraw)
  834. assert.Equal(t, uint64(numExitRoot), rollupEvents.Withdraw[0].NumExitRoot)
  835. // tokenAmount = 20
  836. // amountUSD = tokenAmount * tokenPrice = 20 * 10 = 200
  837. // Bucket[0].ceilUSD = 100, Bucket[1].ceilUSD = 200, ...
  838. // Bucket 1
  839. // Bucket[0].withdrawals = 1, Bucket[1].withdrawals = 2, ...
  840. // Bucket[1].withdrawals - 1 = 1
  841. assert.Equal(t, uint8(1), rollupEvents.UpdateBucketWithdraw[0].NumBucket)
  842. assert.Equal(t, big.NewInt(blockStampBucket), rollupEvents.UpdateBucketWithdraw[0].BlockStamp)
  843. assert.Equal(t, big.NewInt(1), rollupEvents.UpdateBucketWithdraw[0].Withdrawals)
  844. }
  845. func TestRollupSafeMode(t *testing.T) {
  846. _, err := rollupClient.RollupSafeMode()
  847. require.Nil(t, err)
  848. currentBlockNum, err := rollupClient.client.EthLastBlock()
  849. require.Nil(t, err)
  850. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  851. require.Nil(t, err)
  852. auxEvent := new(RollupEventSafeMode)
  853. assert.Equal(t, auxEvent, &rollupEvents.SafeMode[0])
  854. }