From 93dd8fa2e06b51b1681213b909dab9359bcaf658 Mon Sep 17 00:00:00 2001 From: Akash Gianchandani Date: Sun, 8 Oct 2023 17:03:51 +0530 Subject: [PATCH] feat(contracts): add new forwarder contracts WIN-506 --- test/updatedForwarder.js | 75 +++++++++++++++++++++------ test/updatedForwarderFactory.js | 90 ++++++++++++++++----------------- 2 files changed, 104 insertions(+), 61 deletions(-) diff --git a/test/updatedForwarder.js b/test/updatedForwarder.js index b9cb5d5..d7abff8 100644 --- a/test/updatedForwarder.js +++ b/test/updatedForwarder.js @@ -6,7 +6,9 @@ const BigNumber = require('bignumber.js'); const { makeInterfaceId } = require('@openzeppelin/test-helpers'); const UpdatedForwarder = artifacts.require('./UpdatedForwarder.sol'); -const UpdatedForwarderFactory = artifacts.require('./UpdatedForwarderFactory.sol'); +const UpdatedForwarderFactory = artifacts.require( + './UpdatedForwarderFactory.sol' +); const ERC721 = artifacts.require('./MockERC721'); const ERC1155 = artifacts.require('./MockERC1155'); const AlwaysFalseERC165 = artifacts.require('./AlwaysFalseERC165.sol'); @@ -36,13 +38,13 @@ const getForwarderAddressCreate2 = async ( factory, implementationAddress, parent, - salt, + salt ) => { const inputSalt = util.setLengthLeft( Buffer.from(util.stripHexPrefix(salt), 'hex'), 32 ); - + const calculationSalt = abi.soliditySHA3( ['address', 'bytes32'], [parent, inputSalt] @@ -60,7 +62,6 @@ const getForwarderAddressCreate2 = async ( return forwarderAddress; }; - const getBalanceInWei = async (address) => { return new BigNumber(await web3.eth.getBalance(address)); }; @@ -90,7 +91,11 @@ describe('UpdatedForwarder', function () { }); it('Basic forwarding test', async function () { - const forwarder = await createForwarder(accounts[0], accounts[0], accounts[2]); + const forwarder = await createForwarder( + accounts[0], + accounts[0], + accounts[2] + ); const startBalance = await getBalanceInWei(accounts[0]); const amount = web3.utils.toWei('2', 'ether'); @@ -146,7 +151,7 @@ describe('UpdatedForwarder', function () { feeAddress, inputSalt, true, - true, + true ); // Check that the ether was automatically flushed to the base address @@ -157,7 +162,11 @@ describe('UpdatedForwarder', function () { }); it('Should forward with data passed', async function () { - const forwarder = await createForwarder(accounts[0], accounts[0], accounts[2]); + const forwarder = await createForwarder( + accounts[0], + accounts[0], + accounts[2] + ); const startBalance = await getBalanceInWei(accounts[0]); const amount = web3.utils.toWei('2', 'ether'); @@ -175,7 +184,11 @@ describe('UpdatedForwarder', function () { it('Should not init twice', async function () { const baseAddress = accounts[3]; const feeAddress = accounts[4]; - const forwarder = await createForwarder(baseAddress, baseAddress, feeAddress); + const forwarder = await createForwarder( + baseAddress, + baseAddress, + feeAddress + ); await truffleAssert.reverts( forwarder.init(baseAddress, feeAddress, true, { from: baseAddress }) @@ -185,7 +198,11 @@ describe('UpdatedForwarder', function () { it('Should not init if fee address is invalid address', async function () { const baseAddress = accounts[3]; const feeAddress = accounts[4]; - const forwarder = await createForwarder(baseAddress, baseAddress, feeAddress); + const forwarder = await createForwarder( + baseAddress, + baseAddress, + feeAddress + ); console.log('forwarder', forwarder); }); @@ -193,7 +210,11 @@ describe('UpdatedForwarder', function () { it('should change autoFlush721 when calling setAutoFlush721 from allowed address', async () => { const baseAddress = accounts[3]; const feeAddress = accounts[4]; - const forwarder = await createForwarder(baseAddress, baseAddress, feeAddress); + const forwarder = await createForwarder( + baseAddress, + baseAddress, + feeAddress + ); const initialState = await forwarder.autoFlush721(); await forwarder.setAutoFlush721(!initialState, { from: baseAddress }); @@ -205,7 +226,11 @@ describe('UpdatedForwarder', function () { it('should fail to toggle autoFlush721 if caller is not allowed address', async () => { const baseAddress = accounts[3]; const feeAddress = accounts[4]; - const forwarder = await createForwarder(baseAddress, baseAddress, feeAddress); + const forwarder = await createForwarder( + baseAddress, + baseAddress, + feeAddress + ); await truffleAssert.reverts( forwarder.setAutoFlush721(false, { from: accounts[5] }) @@ -215,7 +240,11 @@ describe('UpdatedForwarder', function () { it('should toggle autoFlush1155 when calling setAutoFlush1155 from allowed address', async () => { const baseAddress = accounts[3]; const feeAddress = accounts[4]; - const forwarder = await createForwarder(baseAddress, baseAddress, feeAddress); + const forwarder = await createForwarder( + baseAddress, + baseAddress, + feeAddress + ); const initialState = await forwarder.autoFlush1155(); await forwarder.setAutoFlush1155(!initialState, { from: feeAddress }); @@ -227,7 +256,11 @@ describe('UpdatedForwarder', function () { it('should fail to toggle autoFlush1155 if caller is not allowed address', async () => { const baseAddress = accounts[3]; const feeAddress = accounts[4]; - const forwarder = await createForwarder(baseAddress, baseAddress, feeAddress); + const forwarder = await createForwarder( + baseAddress, + baseAddress, + feeAddress + ); await truffleAssert.reverts( forwarder.setAutoFlush1155(false, { from: accounts[5] }) @@ -249,8 +282,14 @@ describe('UpdatedForwarder', function () { baseAddress = accounts[0]; feeAddress = accounts[2]; - autoFlushForwarder = await createForwarder(baseAddress, baseAddress, feeAddress); - noAutoFlushForwarder = await UpdatedForwarder.new([], { from: accounts[1] }); + autoFlushForwarder = await createForwarder( + baseAddress, + baseAddress, + feeAddress + ); + noAutoFlushForwarder = await UpdatedForwarder.new([], { + from: accounts[1] + }); await noAutoFlushForwarder.init(baseAddress, feeAddress, false, false); }); @@ -661,7 +700,11 @@ describe('UpdatedForwarder', function () { it(`should support ${eipInterface}`, async function () { const baseAddress = accounts[3]; const feeAddress = accounts[4]; - const forwarder = await createForwarder(baseAddress, baseAddress, feeAddress); + const forwarder = await createForwarder( + baseAddress, + baseAddress, + feeAddress + ); const supportsInterface = await forwarder.supportsInterface( interfaceId diff --git a/test/updatedForwarderFactory.js b/test/updatedForwarderFactory.js index f161981..2cb8d85 100644 --- a/test/updatedForwarderFactory.js +++ b/test/updatedForwarderFactory.js @@ -36,13 +36,13 @@ const createForwarder = async ( salt, shouldAutoFlushERC721 = true, shouldAutoFlushERC1155 = true, - sender, + sender ) => { const inputSalt = util.setLengthLeft( Buffer.from(util.stripHexPrefix(salt), 'hex'), 32 ); - + const calculationSalt = abi.soliditySHA3( ['address', 'bytes32'], [parent, inputSalt] @@ -62,7 +62,7 @@ const createForwarder = async ( feeAddress, inputSalt, shouldAutoFlushERC721, - shouldAutoFlushERC1155, + shouldAutoFlushERC1155 ); return forwarderAddress; @@ -137,7 +137,7 @@ describe('UpdatedForwarderFactory', function () { ); forwarderAddress.should.not.equal(forwarderAddress2); - }); + }); it('Different creators should create forwarders at different addresses', async function () { const { factory, implementationAddress } = await createForwarderFactory(); @@ -169,7 +169,7 @@ describe('UpdatedForwarderFactory', function () { ); forwarderAddress.should.not.equal(forwarderAddress2); - }); + }); it('Different parents should create forwarders at different addresses', async function () { const { factory, implementationAddress } = await createForwarderFactory(); @@ -231,35 +231,9 @@ describe('UpdatedForwarderFactory', function () { const autoFlush721 = await forwarderContract.autoFlush721(); autoFlush721.should.equal(shouldAutoFlush); - }); + }); it(`should assign the create a forwarder with ${label} autoflush1155 params`, async () => { - const { factory, implementationAddress } = await createForwarderFactory(); - - const parent = accounts[0]; - const feeAddress = accounts[2]; - const salt = '0x1234'; - const forwarderAddress = await createForwarder( - factory, - implementationAddress, - parent, - feeAddress, - salt, - undefined, - shouldAutoFlush, - accounts[1] - ); - - const forwarderContract = await hre.ethers.getContractAt( - 'Forwarder', - forwarderAddress - ); - const autoFlush1155 = await forwarderContract.autoFlush1155(); - autoFlush1155.should.equal(shouldAutoFlush); - }); - }); - - it('Should fail to create two contracts with the same inputs', async function () { const { factory, implementationAddress } = await createForwarderFactory(); const parent = accounts[0]; @@ -272,21 +246,47 @@ describe('UpdatedForwarderFactory', function () { feeAddress, salt, undefined, - undefined, + shouldAutoFlush, accounts[1] ); - await helpers.assertVMException( - async () => - await createForwarder( - factory, - implementationAddress, - parent, - feeAddress, - salt, - undefined, - undefined, - accounts[1] - ) + + const forwarderContract = await hre.ethers.getContractAt( + 'Forwarder', + forwarderAddress ); + const autoFlush1155 = await forwarderContract.autoFlush1155(); + autoFlush1155.should.equal(shouldAutoFlush); }); + }); + + it('Should fail to create two contracts with the same inputs', async function () { + const { factory, implementationAddress } = await createForwarderFactory(); + + const parent = accounts[0]; + const feeAddress = accounts[2]; + const salt = '0x1234'; + const forwarderAddress = await createForwarder( + factory, + implementationAddress, + parent, + feeAddress, + salt, + undefined, + undefined, + accounts[1] + ); + await helpers.assertVMException( + async () => + await createForwarder( + factory, + implementationAddress, + parent, + feeAddress, + salt, + undefined, + undefined, + accounts[1] + ) + ); + }); });