From 74b1e7a6d15b8b35d8ee15d402ff1f85204ef963 Mon Sep 17 00:00:00 2001 From: Akash Gianchandani Date: Wed, 4 Oct 2023 21:22:23 +0530 Subject: [PATCH] feat(contracts): add new forwarder contracts WIN-506 --- contracts/Forwarder.sol | 6 +- contracts/UpdatedForwarder.sol | 21 +- contracts/UpdatedForwarderFactory.sol | 6 +- contracts/WalletSimple.sol | 15 +- test/updatedForwarderFactory.js | 336 +++++++++++++------------- 5 files changed, 196 insertions(+), 188 deletions(-) diff --git a/contracts/Forwarder.sol b/contracts/Forwarder.sol index 9a79bd9..3e2a6eb 100644 --- a/contracts/Forwarder.sol +++ b/contracts/Forwarder.sol @@ -52,7 +52,7 @@ contract Forwarder is IERC721Receiver, ERC1155Receiver, IForwarder { /** * Modifier that will execute internal code block only if the sender is the parent address */ - modifier onlyParent { + modifier onlyParent() { require(msg.sender == parentAddress, 'Only Parent'); _; } @@ -60,7 +60,7 @@ contract Forwarder is IERC721Receiver, ERC1155Receiver, IForwarder { /** * Modifier that will execute internal code block only if the contract has not been initialized yet */ - modifier onlyUninitialized { + modifier onlyUninitialized() { require(parentAddress == address(0x0), 'Already initialized'); _; } @@ -313,9 +313,9 @@ contract Forwarder is IERC721Receiver, ERC1155Receiver, IForwarder { */ function supportsInterface(bytes4 interfaceId) public + view virtual override(ERC1155Receiver, IERC165) - view returns (bool) { return diff --git a/contracts/UpdatedForwarder.sol b/contracts/UpdatedForwarder.sol index 4bc9598..ccc7a7f 100644 --- a/contracts/UpdatedForwarder.sol +++ b/contracts/UpdatedForwarder.sol @@ -24,17 +24,17 @@ contract UpdatedForwarder is IERC721Receiver, ERC1155Receiver, IForwarder { /** * Initialize the contract, and sets the destination address to that of the parent address */ - function init ( + function init( address _parentAddress, address _feeAddress, bool _autoFlush721, bool _autoFlush1155 ) external onlyUninitialized { - require(_parentAddress != address(0x0), "Invalid address"); + require(_parentAddress != address(0x0), 'Invalid address'); parentAddress = _parentAddress; - require(_feeAddress != address(0x0), "Invalid address"); + require(_feeAddress != address(0x0), 'Invalid address'); feeAddress = _feeAddress; - + uint256 value = address(this).balance; // set whether we want to automatically flush erc721/erc1155 tokens or not @@ -58,15 +58,18 @@ contract UpdatedForwarder is IERC721Receiver, ERC1155Receiver, IForwarder { /** * Modifier that will execute internal code block only if the sender is from the allowed addresses */ - modifier onlyAllowedAddress { - require(msg.sender == parentAddress || msg.sender == feeAddress, 'Address is not allowed'); + modifier onlyAllowedAddress() { + require( + msg.sender == parentAddress || msg.sender == feeAddress, + 'Address is not allowed' + ); _; } /** * Modifier that will execute internal code block only if the contract has not been initialized yet */ - modifier onlyUninitialized { + modifier onlyUninitialized() { require(parentAddress == address(0x0), 'Already initialized'); _; } @@ -319,13 +322,13 @@ contract UpdatedForwarder is IERC721Receiver, ERC1155Receiver, IForwarder { */ function supportsInterface(bytes4 interfaceId) public + view virtual override(ERC1155Receiver, IERC165) - view returns (bool) { return interfaceId == type(IForwarder).interfaceId || super.supportsInterface(interfaceId); } -} \ No newline at end of file +} diff --git a/contracts/UpdatedForwarderFactory.sol b/contracts/UpdatedForwarderFactory.sol index 89ac1eb..4eeb9ce 100644 --- a/contracts/UpdatedForwarderFactory.sol +++ b/contracts/UpdatedForwarderFactory.sol @@ -18,7 +18,11 @@ contract UpdatedForwarderFactory is CloneFactory { implementationAddress = _implementationAddress; } - function createForwarder(address parent, address feeAddress, bytes32 salt) external { + function createForwarder( + address parent, + address feeAddress, + bytes32 salt + ) external { this.createForwarder(parent, feeAddress, salt, true, true); } diff --git a/contracts/WalletSimple.sol b/contracts/WalletSimple.sol index 9d0c5ac..aeff429 100644 --- a/contracts/WalletSimple.sol +++ b/contracts/WalletSimple.sol @@ -93,7 +93,7 @@ contract WalletSimple is IERC721Receiver, ERC1155Receiver { * to allow this contract to be used by proxy with delegatecall, which will * not pick up on state variables */ - function getNetworkId() internal virtual pure returns (string memory) { + function getNetworkId() internal pure virtual returns (string memory) { return 'ETHER'; } @@ -105,7 +105,7 @@ contract WalletSimple is IERC721Receiver, ERC1155Receiver { * to allow this contract to be used by proxy with delegatecall, which will * not pick up on state variables */ - function getTokenNetworkId() internal virtual pure returns (string memory) { + function getTokenNetworkId() internal pure virtual returns (string memory) { return 'ERC20'; } @@ -117,7 +117,7 @@ contract WalletSimple is IERC721Receiver, ERC1155Receiver { * to allow this contract to be used by proxy with delegatecall, which will * not pick up on state variables */ - function getBatchNetworkId() internal virtual pure returns (string memory) { + function getBatchNetworkId() internal pure virtual returns (string memory) { return 'ETHER-Batch'; } @@ -133,7 +133,7 @@ contract WalletSimple is IERC721Receiver, ERC1155Receiver { /** * Modifier that will execute internal code block only if the sender is an authorized signer on this wallet */ - modifier onlySigner { + modifier onlySigner() { require(isSigner(msg.sender), 'Non-signer in onlySigner method'); _; } @@ -141,7 +141,7 @@ contract WalletSimple is IERC721Receiver, ERC1155Receiver { /** * Modifier that will execute internal code block only if the contract has not been initialized yet */ - modifier onlyUninitialized { + modifier onlyUninitialized() { require(!initialized, 'Contract already initialized'); _; } @@ -557,9 +557,8 @@ contract WalletSimple is IERC721Receiver, ERC1155Receiver { uint256 lowestValueIndex = 0; // fetch recentSequenceIds into memory for function context to avoid unnecessary sloads - - uint256[SEQUENCE_ID_WINDOW_SIZE] memory _recentSequenceIds - = recentSequenceIds; + uint256[SEQUENCE_ID_WINDOW_SIZE] + memory _recentSequenceIds = recentSequenceIds; for (uint256 i = 0; i < SEQUENCE_ID_WINDOW_SIZE; i++) { require(_recentSequenceIds[i] != sequenceId, 'Sequence ID already used'); diff --git a/test/updatedForwarderFactory.js b/test/updatedForwarderFactory.js index 9f4789a..2ed09ab 100644 --- a/test/updatedForwarderFactory.js +++ b/test/updatedForwarderFactory.js @@ -7,7 +7,9 @@ const abi = require('ethereumjs-abi'); const BigNumber = require('bignumber.js'); const UpdatedForwarder = artifacts.require('./UpdatedForwarder.sol'); -const UpdatedForwarderFactory = artifacts.require('./UpdatedForwarderFactory.sol'); +const UpdatedForwarderFactory = artifacts.require( + './UpdatedForwarderFactory.sol' +); const hre = require('hardhat'); @@ -107,170 +109,170 @@ describe('ForwarderFactory', function () { endForwarderBalance.eq(startForwarderBalance).should.be.true(); }); -// it('Different salt should create at different addresses', async function () { -// const { factory, implementationAddress } = await createForwarderFactory(); - -// const parent = accounts[0]; -// const salt = '0x1234'; -// const forwarderAddress = await createForwarder( -// factory, -// implementationAddress, -// parent, -// salt, -// undefined, -// undefined, -// accounts[1] -// ); - -// const salt2 = '0x12345678'; -// const forwarderAddress2 = await createForwarder( -// factory, -// implementationAddress, -// parent, -// salt2, -// undefined, -// undefined, -// accounts[1] -// ); - -// forwarderAddress.should.not.equal(forwarderAddress2); -// }); - -// it('Different creators should create at different addresses', async function () { -// const { factory, implementationAddress } = await createForwarderFactory(); -// const { factory: factory2, implementationAddress: implementationAddress2 } = -// await createForwarderFactory(); - -// const parent = accounts[0]; -// const salt = '0x1234'; -// const forwarderAddress = await createForwarder( -// factory, -// implementationAddress, -// parent, -// salt, -// undefined, -// undefined, -// accounts[1] -// ); -// const forwarderAddress2 = await createForwarder( -// factory2, -// implementationAddress2, -// parent, -// salt, -// undefined, -// undefined, -// accounts[1] -// ); - -// forwarderAddress.should.not.equal(forwarderAddress2); -// }); - -// it('Different parents should create at different addresses', async function () { -// const { factory, implementationAddress } = await createForwarderFactory(); - -// const parent = accounts[0]; -// const salt = '0x1234'; -// const forwarderAddress = await createForwarder( -// factory, -// implementationAddress, -// parent, -// salt, -// undefined, -// undefined, -// accounts[1] -// ); - -// const parent2 = accounts[1]; -// const forwarderAddress2 = await createForwarder( -// factory, -// implementationAddress, -// parent2, -// salt, -// undefined, -// undefined, -// accounts[1] -// ); - -// forwarderAddress.should.not.equal(forwarderAddress2); -// }); - -// [ -// [true, 'true'], -// [false, 'false'] -// ].map(([shouldAutoFlush, label]) => { -// it(`should assign the create a forwarder with ${label} autoflush721 params`, async () => { -// const { factory, implementationAddress } = await createForwarderFactory(); - -// const parent = accounts[0]; -// const salt = '0x1234'; -// const forwarderAddress = await createForwarder( -// factory, -// implementationAddress, -// parent, -// salt, -// shouldAutoFlush, -// undefined, -// accounts[1] -// ); - -// const forwarderContract = await hre.ethers.getContractAt( -// 'Forwarder', -// forwarderAddress -// ); -// 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 salt = '0x1234'; -// const forwarderAddress = await createForwarder( -// factory, -// implementationAddress, -// parent, -// 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]; -// const salt = '0x1234'; -// const forwarderAddress = await createForwarder( -// factory, -// implementationAddress, -// parent, -// salt, -// undefined, -// undefined, -// accounts[1] -// ); -// await helpers.assertVMException( -// async () => -// await createForwarder( -// factory, -// implementationAddress, -// parent, -// salt, -// undefined, -// undefined, -// accounts[1] -// ) -// ); -// }); + // it('Different salt should create at different addresses', async function () { + // const { factory, implementationAddress } = await createForwarderFactory(); + + // const parent = accounts[0]; + // const salt = '0x1234'; + // const forwarderAddress = await createForwarder( + // factory, + // implementationAddress, + // parent, + // salt, + // undefined, + // undefined, + // accounts[1] + // ); + + // const salt2 = '0x12345678'; + // const forwarderAddress2 = await createForwarder( + // factory, + // implementationAddress, + // parent, + // salt2, + // undefined, + // undefined, + // accounts[1] + // ); + + // forwarderAddress.should.not.equal(forwarderAddress2); + // }); + + // it('Different creators should create at different addresses', async function () { + // const { factory, implementationAddress } = await createForwarderFactory(); + // const { factory: factory2, implementationAddress: implementationAddress2 } = + // await createForwarderFactory(); + + // const parent = accounts[0]; + // const salt = '0x1234'; + // const forwarderAddress = await createForwarder( + // factory, + // implementationAddress, + // parent, + // salt, + // undefined, + // undefined, + // accounts[1] + // ); + // const forwarderAddress2 = await createForwarder( + // factory2, + // implementationAddress2, + // parent, + // salt, + // undefined, + // undefined, + // accounts[1] + // ); + + // forwarderAddress.should.not.equal(forwarderAddress2); + // }); + + // it('Different parents should create at different addresses', async function () { + // const { factory, implementationAddress } = await createForwarderFactory(); + + // const parent = accounts[0]; + // const salt = '0x1234'; + // const forwarderAddress = await createForwarder( + // factory, + // implementationAddress, + // parent, + // salt, + // undefined, + // undefined, + // accounts[1] + // ); + + // const parent2 = accounts[1]; + // const forwarderAddress2 = await createForwarder( + // factory, + // implementationAddress, + // parent2, + // salt, + // undefined, + // undefined, + // accounts[1] + // ); + + // forwarderAddress.should.not.equal(forwarderAddress2); + // }); + + // [ + // [true, 'true'], + // [false, 'false'] + // ].map(([shouldAutoFlush, label]) => { + // it(`should assign the create a forwarder with ${label} autoflush721 params`, async () => { + // const { factory, implementationAddress } = await createForwarderFactory(); + + // const parent = accounts[0]; + // const salt = '0x1234'; + // const forwarderAddress = await createForwarder( + // factory, + // implementationAddress, + // parent, + // salt, + // shouldAutoFlush, + // undefined, + // accounts[1] + // ); + + // const forwarderContract = await hre.ethers.getContractAt( + // 'Forwarder', + // forwarderAddress + // ); + // 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 salt = '0x1234'; + // const forwarderAddress = await createForwarder( + // factory, + // implementationAddress, + // parent, + // 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]; + // const salt = '0x1234'; + // const forwarderAddress = await createForwarder( + // factory, + // implementationAddress, + // parent, + // salt, + // undefined, + // undefined, + // accounts[1] + // ); + // await helpers.assertVMException( + // async () => + // await createForwarder( + // factory, + // implementationAddress, + // parent, + // salt, + // undefined, + // undefined, + // accounts[1] + // ) + // ); + // }); });