From ef513d5203bfa0cc0c1684b317ce09568185e63f Mon Sep 17 00:00:00 2001 From: Kiryl Yermakou Date: Wed, 13 Dec 2023 18:09:42 -0500 Subject: [PATCH] test(GMPExecutable): adding wait for live tests --- test/executable/GMPExecutable.js | 199 +++++----- test/executable/GMPExecutableWithToken.js | 441 ++++++++++------------ 2 files changed, 285 insertions(+), 355 deletions(-) diff --git a/test/executable/GMPExecutable.js b/test/executable/GMPExecutable.js index 09993bdb..1226da6c 100644 --- a/test/executable/GMPExecutable.js +++ b/test/executable/GMPExecutable.js @@ -2,120 +2,101 @@ const chai = require('chai'); const { - utils: { defaultAbiCoder, keccak256, id }, + utils: { defaultAbiCoder, keccak256, id }, } = require('ethers'); const { expect } = chai; const { ethers } = require('hardhat'); const getRandomID = () => id(Math.floor(Math.random() * 1e10).toString()); describe('GMPExecutable', () => { - let gatewayFactory; - - let destinationChainGateway; - - let GMPExecutableFactory; - let GMPExecutable; - - let ownerWallet; - let userWallet; - - const sourceChain = 'chainA'; - const num = 10; - - before(async () => { - [ownerWallet, userWallet] = await ethers.getSigners(); - - gatewayFactory = await ethers.getContractFactory( - 'MockGateway', - ownerWallet, - ); - GMPExecutableFactory = await ethers.getContractFactory( - 'GMPExecutableTest', - ownerWallet, - ); - }); - - describe('AxelarGMPExecutable', () => { - describe('Call Contract', () => { - beforeEach(async () => { - destinationChainGateway = await gatewayFactory - .deploy() - .then((d) => d.deployed()); - - GMPExecutable = await GMPExecutableFactory.deploy( - destinationChainGateway.address, - ).then((d) => d.deployed()); - }); - - it('should revert without gateway approval', async () => { - const payload = defaultAbiCoder.encode(['uint256'], [num]); - - const approveCommandId = getRandomID(); - - const receive = GMPExecutable.execute( - approveCommandId, - sourceChain, - userWallet.address.toString(), - payload, - ); - - await expect(receive).to.be.revertedWithCustomError( - GMPExecutable, - 'NotApprovedByGateway', - ); - }); - - it('should call contract on another chain', async () => { - const payload = defaultAbiCoder.encode(['uint256'], [num]); - const payloadHash = keccak256(payload); - - const approveCommandId = getRandomID(); - const sourceTxHash = keccak256('0x123abc123abc'); - const sourceEventIndex = 17; - - const approveData = defaultAbiCoder.encode( - ['string', 'string', 'address', 'bytes32', 'bytes32', 'uint256'], - [ - sourceChain, - userWallet.address, - GMPExecutable.address, - payloadHash, - sourceTxHash, - sourceEventIndex, - ], - ); - - const approveExecute = - await destinationChainGateway.approveContractCall( - approveData, - approveCommandId, - ); - - await approveExecute.wait(); - - await expect(approveExecute) - .to.emit(destinationChainGateway, 'ContractCallApproved') - .withArgs( - approveCommandId, - sourceChain, - userWallet.address.toString(), - GMPExecutable.address, - payloadHash, - sourceTxHash, - sourceEventIndex, - ); - - const receive = await GMPExecutable.execute( - approveCommandId, - sourceChain, - userWallet.address.toString(), - payload, - ); - - await receive.wait(); - - await expect(receive).to.emit(GMPExecutable, 'Received').withArgs(num); - }); + let gatewayFactory; + + let destinationChainGateway; + + let GMPExecutableFactory; + let GMPExecutable; + + let ownerWallet; + let userWallet; + + const sourceChain = 'chainA'; + const num = 10; + + before(async () => { + [ownerWallet, userWallet] = await ethers.getSigners(); + + gatewayFactory = await ethers.getContractFactory('MockGateway', ownerWallet); + GMPExecutableFactory = await ethers.getContractFactory('GMPExecutableTest', ownerWallet); + }); + + describe('AxelarGMPExecutable', () => { + describe('Call Contract', () => { + beforeEach(async () => { + destinationChainGateway = await gatewayFactory.deploy().then((d) => d.deployed()); + + GMPExecutable = await GMPExecutableFactory.deploy(destinationChainGateway.address).then((d) => + d.deployed(), + ); + }); + + it('should revert without gateway approval', async () => { + const payload = defaultAbiCoder.encode(['uint256'], [num]); + + const approveCommandId = getRandomID(); + + const receive = GMPExecutable.execute( + approveCommandId, + sourceChain, + userWallet.address.toString(), + payload, + ); + + await expect(receive).to.be.revertedWithCustomError(GMPExecutable, 'NotApprovedByGateway'); + }); + + it('should call contract on another chain', async () => { + const payload = defaultAbiCoder.encode(['uint256'], [num]); + const payloadHash = keccak256(payload); + + const approveCommandId = getRandomID(); + const sourceTxHash = keccak256('0x123abc123abc'); + const sourceEventIndex = 17; + + const approveData = defaultAbiCoder.encode( + ['string', 'string', 'address', 'bytes32', 'bytes32', 'uint256'], + [ + sourceChain, + userWallet.address, + GMPExecutable.address, + payloadHash, + sourceTxHash, + sourceEventIndex, + ], + ); + + const approveExecute = await destinationChainGateway.approveContractCall(approveData, approveCommandId); + + await expect(approveExecute) + .to.emit(destinationChainGateway, 'ContractCallApproved') + .withArgs( + approveCommandId, + sourceChain, + userWallet.address.toString(), + GMPExecutable.address, + payloadHash, + sourceTxHash, + sourceEventIndex, + ); + + const receive = await GMPExecutable.execute( + approveCommandId, + sourceChain, + userWallet.address.toString(), + payload, + ); + + await expect(receive).to.emit(GMPExecutable, 'Received').withArgs(num); + }); + }); }); - }); }); diff --git a/test/executable/GMPExecutableWithToken.js b/test/executable/GMPExecutableWithToken.js index fa1705f7..f0b18183 100644 --- a/test/executable/GMPExecutableWithToken.js +++ b/test/executable/GMPExecutableWithToken.js @@ -2,259 +2,208 @@ const chai = require('chai'); const { - utils: { defaultAbiCoder, keccak256, id }, + utils: { defaultAbiCoder, keccak256, id }, } = require('ethers'); const { expect } = chai; const { ethers } = require('hardhat'); const getRandomID = () => id(Math.floor(Math.random() * 1e10).toString()); describe('GMPExecutableWithToken', () => { - let gatewayFactory; - let tokenFactory; - - let destinationChainGateway; - let tokenA; - - let GMPExecutableFactory; - let GMPExecutable; - let GMPExecutableWithTokenFactory; - let GMPExecutableWithToken; - - let ownerWallet; - let userWallet; - - const sourceChain = 'chainA'; - const nameA = 'testTokenX'; - const symbolA = 'testTokenX'; - const decimals = 16; - const capacity = 0; - const num = 10; - - before(async () => { - [ownerWallet, userWallet] = await ethers.getSigners(); - - gatewayFactory = await ethers.getContractFactory( - 'MockGateway', - ownerWallet, - ); - tokenFactory = await ethers.getContractFactory( - 'ERC20MintableBurnable', - ownerWallet, - ); - GMPExecutableFactory = await ethers.getContractFactory( - 'GMPExecutableTest', - ownerWallet, - ); - GMPExecutableWithTokenFactory = await ethers.getContractFactory( - 'GMPExecutableWithTokenTest', - ownerWallet, - ); - }); - - describe('AxelarGMPExecutableWithToken', () => { - describe('Call Contract with Token', () => { - beforeEach(async () => { - destinationChainGateway = await gatewayFactory - .deploy() - .then((d) => d.deployed()); - - tokenA = await tokenFactory - .deploy(nameA, symbolA, decimals) - .then((d) => d.deployed()); - - await destinationChainGateway.deployToken( - defaultAbiCoder.encode( - ['string', 'string', 'uint8', 'uint256', ' address', 'uint256'], - [nameA, symbolA, decimals, capacity, tokenA.address, 0], - ), - keccak256('0x'), - ).then((t) => t.wait()); - - GMPExecutableWithToken = await GMPExecutableWithTokenFactory.deploy( - destinationChainGateway.address, - ).then((d) => d.deployed()); - - await tokenA.mint(destinationChainGateway.address, 1e9).then((t) => t.wait()); - await tokenA.mint(userWallet.address, 1e9).then((t) => t.wait()); - }); - - it('should revert without gateway approval', async () => { - const swapAmount = 1e6; - const payload = defaultAbiCoder.encode(['uint256'], [num]); - - const approveCommandId = getRandomID(); - - const execute = GMPExecutableWithToken.executeWithToken( - approveCommandId, - sourceChain, - userWallet.address.toString(), - payload, - symbolA, - swapAmount, - ); - await expect(execute).to.be.revertedWithCustomError( - GMPExecutableWithToken, - 'NotApprovedByGateway', - ); - }); - - it('should execute with token on remote chain', async () => { - const swapAmount = 1e6; - const payload = defaultAbiCoder.encode(['uint256'], [num]); - const payloadHash = keccak256(payload); - - const approveCommandId = getRandomID(); - const sourceTxHash = keccak256('0x123abc123abc'); - const sourceEventIndex = 17; - - const approveWithMintData = defaultAbiCoder.encode( - [ - 'string', - 'string', - 'address', - 'bytes32', - 'string', - 'uint256', - 'bytes32', - 'uint256', - ], - [ - sourceChain, - userWallet.address, - GMPExecutableWithToken.address, - payloadHash, - symbolA, - swapAmount, - sourceTxHash, - sourceEventIndex, - ], - ); - - const approveExecute = - await destinationChainGateway.approveContractCallWithMint( - approveWithMintData, - approveCommandId, - ); - - await approveExecute.wait(); - - await expect(approveExecute) - .to.emit(destinationChainGateway, 'ContractCallApprovedWithMint') - .withArgs( - approveCommandId, - sourceChain, - userWallet.address.toString(), - GMPExecutableWithToken.address, - payloadHash, - symbolA, - swapAmount, - sourceTxHash, - sourceEventIndex, - ); - - const execute = await GMPExecutableWithToken.executeWithToken( - approveCommandId, - sourceChain, - userWallet.address.toString(), - payload, - symbolA, - swapAmount, - ) - - await execute.wait(); - - await expect(execute) - .to.emit(GMPExecutableWithToken, 'ReceivedWithToken') - .withArgs(num, tokenA.address, swapAmount) - .to.emit(tokenA, 'Transfer') - .withArgs( - destinationChainGateway.address, - GMPExecutableWithToken.address, - swapAmount, - ); - }); + let gatewayFactory; + let tokenFactory; + + let destinationChainGateway; + let tokenA; + + let GMPExecutableWithTokenFactory; + let GMPExecutableWithToken; + + let ownerWallet; + let userWallet; + + const sourceChain = 'chainA'; + const nameA = 'testTokenX'; + const symbolA = 'testTokenX'; + const decimals = 16; + const capacity = 0; + const num = 10; + + before(async () => { + [ownerWallet, userWallet] = await ethers.getSigners(); + + gatewayFactory = await ethers.getContractFactory('MockGateway', ownerWallet); + tokenFactory = await ethers.getContractFactory('ERC20MintableBurnable', ownerWallet); + GMPExecutableWithTokenFactory = await ethers.getContractFactory('GMPExecutableWithTokenTest', ownerWallet); }); - describe('Call Contract', () => { - beforeEach(async () => { - destinationChainGateway = await gatewayFactory - .deploy() - .then((d) => d.deployed()); - - GMPExecutable = await GMPExecutableFactory.deploy( - destinationChainGateway.address, - ).then((d) => d.deployed()); - }); - - it('should revert without gateway approval', async () => { - const payload = defaultAbiCoder.encode(['uint256'], [num]); - - const approveCommandId = getRandomID(); - - const receive = GMPExecutable.execute( - approveCommandId, - sourceChain, - userWallet.address.toString(), - payload, - ).then((t) => t.wait()); - - await expect(receive).to.be.revertedWithCustomError( - GMPExecutable, - 'NotApprovedByGateway', - ); - }); - - it('should call contract on another chain', async () => { - const payload = defaultAbiCoder.encode(['uint256'], [num]); - const payloadHash = keccak256(payload); - - const approveCommandId = getRandomID(); - const sourceTxHash = keccak256('0x123abc123abc'); - const sourceEventIndex = 17; - - const approveData = defaultAbiCoder.encode( - ['string', 'string', 'address', 'bytes32', 'bytes32', 'uint256'], - [ - sourceChain, - userWallet.address, - GMPExecutable.address, - payloadHash, - sourceTxHash, - sourceEventIndex, - ], - ); - - const approveExecute = - await destinationChainGateway.approveContractCall( - approveData, - approveCommandId, - ); - - await approveExecute.wait(); - - await expect(approveExecute) - .to.emit(destinationChainGateway, 'ContractCallApproved') - .withArgs( - approveCommandId, - sourceChain, - userWallet.address.toString(), - GMPExecutable.address, - payloadHash, - sourceTxHash, - sourceEventIndex, - ); - - const receive = await GMPExecutable.execute( - approveCommandId, - sourceChain, - userWallet.address.toString(), - payload, - ); - - await receive.wait(); - - await expect(receive).to.emit(GMPExecutable, 'Received').withArgs(num); - }); + describe('AxelarGMPExecutableWithToken', () => { + describe('Call Contract with Token', () => { + beforeEach(async () => { + destinationChainGateway = await gatewayFactory.deploy().then((d) => d.deployed()); + + tokenA = await tokenFactory.deploy(nameA, symbolA, decimals).then((d) => d.deployed()); + + await destinationChainGateway + .deployToken( + defaultAbiCoder.encode( + ['string', 'string', 'uint8', 'uint256', ' address', 'uint256'], + [nameA, symbolA, decimals, capacity, tokenA.address, 0], + ), + keccak256('0x'), + ) + .then((t) => t.wait()); + + GMPExecutableWithToken = await GMPExecutableWithTokenFactory.deploy( + destinationChainGateway.address, + ).then((d) => d.deployed()); + + await tokenA.mint(destinationChainGateway.address, 1e9).then((t) => t.wait()); + await tokenA.mint(userWallet.address, 1e9).then((t) => t.wait()); + }); + + it('should revert without gateway approval', async () => { + const swapAmount = 1e6; + const payload = defaultAbiCoder.encode(['uint256'], [num]); + + const approveCommandId = getRandomID(); + + const execute = GMPExecutableWithToken.executeWithToken( + approveCommandId, + sourceChain, + userWallet.address.toString(), + payload, + symbolA, + swapAmount, + ); + await expect(execute).to.be.revertedWithCustomError(GMPExecutableWithToken, 'NotApprovedByGateway'); + }); + + it('should execute with token on remote chain', async () => { + const swapAmount = 1e6; + const payload = defaultAbiCoder.encode(['uint256'], [num]); + const payloadHash = keccak256(payload); + + const approveCommandId = getRandomID(); + const sourceTxHash = keccak256('0x123abc123abc'); + const sourceEventIndex = 17; + + const approveWithMintData = defaultAbiCoder.encode( + ['string', 'string', 'address', 'bytes32', 'string', 'uint256', 'bytes32', 'uint256'], + [ + sourceChain, + userWallet.address, + GMPExecutableWithToken.address, + payloadHash, + symbolA, + swapAmount, + sourceTxHash, + sourceEventIndex, + ], + ); + + const approveExecute = await destinationChainGateway.approveContractCallWithMint( + approveWithMintData, + approveCommandId, + ); + + await expect(approveExecute) + .to.emit(destinationChainGateway, 'ContractCallApprovedWithMint') + .withArgs( + approveCommandId, + sourceChain, + userWallet.address.toString(), + GMPExecutableWithToken.address, + payloadHash, + symbolA, + swapAmount, + sourceTxHash, + sourceEventIndex, + ); + + const execute = await GMPExecutableWithToken.executeWithToken( + approveCommandId, + sourceChain, + userWallet.address.toString(), + payload, + symbolA, + swapAmount, + ); + + await expect(execute) + .to.emit(GMPExecutableWithToken, 'ReceivedWithToken') + .withArgs(num, tokenA.address, swapAmount) + .to.emit(tokenA, 'Transfer') + .withArgs(destinationChainGateway.address, GMPExecutableWithToken.address, swapAmount); + }); + }); + + describe('Call Contract', () => { + beforeEach(async () => { + destinationChainGateway = await gatewayFactory.deploy().then((d) => d.deployed()); + + GMPExecutableWithToken = await GMPExecutableWithTokenFactory.deploy(destinationChainGateway.address).then((d) => + d.deployed(), + ); + }); + + it('should revert without gateway approval', async () => { + const payload = defaultAbiCoder.encode(['uint256'], [num]); + + const approveCommandId = getRandomID(); + + const receive = GMPExecutableWithToken.execute( + approveCommandId, + sourceChain, + userWallet.address.toString(), + payload, + ); + + await expect(receive).to.be.revertedWithCustomError(GMPExecutableWithToken, 'NotApprovedByGateway'); + }); + + it('should call contract on another chain', async () => { + const payload = defaultAbiCoder.encode(['uint256'], [num]); + const payloadHash = keccak256(payload); + + const approveCommandId = getRandomID(); + const sourceTxHash = keccak256('0x123abc123abc'); + const sourceEventIndex = 17; + + const approveData = defaultAbiCoder.encode( + ['string', 'string', 'address', 'bytes32', 'bytes32', 'uint256'], + [ + sourceChain, + userWallet.address, + GMPExecutableWithToken.address, + payloadHash, + sourceTxHash, + sourceEventIndex, + ], + ); + + const approveExecute = await destinationChainGateway.approveContractCall(approveData, approveCommandId); + + await expect(approveExecute) + .to.emit(destinationChainGateway, 'ContractCallApproved') + .withArgs( + approveCommandId, + sourceChain, + userWallet.address.toString(), + GMPExecutableWithToken.address, + payloadHash, + sourceTxHash, + sourceEventIndex, + ); + + const receive = await GMPExecutableWithToken.execute( + approveCommandId, + sourceChain, + userWallet.address.toString(), + payload, + ); + + await expect(receive).to.emit(GMPExecutableWithToken, 'Received').withArgs(num); + }); + }); }); - }); });