Skip to content

Commit

Permalink
feat(contracts): add new forwarder contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
gianchandania committed Oct 8, 2023
1 parent 0923ba1 commit 93dd8fa
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 61 deletions.
75 changes: 59 additions & 16 deletions test/updatedForwarder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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]
Expand All @@ -60,7 +62,6 @@ const getForwarderAddressCreate2 = async (
return forwarderAddress;
};


const getBalanceInWei = async (address) => {
return new BigNumber(await web3.eth.getBalance(address));
};
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -146,7 +151,7 @@ describe('UpdatedForwarder', function () {
feeAddress,
inputSalt,
true,
true,
true
);

// Check that the ether was automatically flushed to the base address
Expand All @@ -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');

Expand All @@ -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 })
Expand All @@ -185,15 +198,23 @@ 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);
});

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 });
Expand All @@ -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] })
Expand All @@ -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 });
Expand All @@ -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] })
Expand All @@ -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);
});

Expand Down Expand Up @@ -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
Expand Down
90 changes: 45 additions & 45 deletions test/updatedForwarderFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -62,7 +62,7 @@ const createForwarder = async (
feeAddress,
inputSalt,
shouldAutoFlushERC721,
shouldAutoFlushERC1155,
shouldAutoFlushERC1155
);

return forwarderAddress;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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];
Expand All @@ -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]
)
);
});
});

0 comments on commit 93dd8fa

Please sign in to comment.