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.

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