Skip to content

Commit

Permalink
Merge pull request #181 from 1inch/feature/updates-to-new-fee-flow
Browse files Browse the repository at this point in the history
[SC-1367] Feature/updates to new fee flow
  • Loading branch information
ZumZoom authored Dec 24, 2024
2 parents c36c75f + 45ee0cb commit 80950a9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"dependencies": {
"@1inch/delegating": "1.1.0",
"@1inch/limit-order-protocol-contract": "4.2.1",
"@1inch/limit-order-protocol-contract": "4.2.2",
"@1inch/solidity-utils": "6.2.0",
"@1inch/st1inch": "2.2.0",
"@openzeppelin/contracts": "5.1.0"
Expand Down
81 changes: 50 additions & 31 deletions test/Settlement.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe('Settlement', function () {
const setupData = { ...dataFormFixture, auction };
const {
contracts: { dai, weth, resolver },
accounts: { owner, alice, bob },
accounts: { owner, alice, bob, charlie },
} = setupData;

const fillOrderToData1 = await buildCalldataForOrder({
Expand All @@ -180,7 +180,8 @@ describe('Settlement', function () {
setupData,
threshold: ether('101'),
isInnermostOrder: true,
integrator: bob.address,
integratorFeeRecipient: bob.address,
protocolFeeRecipient: charlie.address,
integratorFee: 100,
});

Expand All @@ -197,13 +198,14 @@ describe('Settlement', function () {
setupData,
threshold: ether('0.11'),
additionalDataForSettlement: fillOrderToData1,
integrator: bob.address,
integratorFeeRecipient: bob.address,
protocolFeeRecipient: charlie.address,
integratorFee: 100,
});

const txn = await resolver.settleOrders(fillOrderToData0);
await expect(txn).to.changeTokenBalances(dai, [owner, alice, bob], [ether('-100.1'), ether('100'), ether('0.1')]);
await expect(txn).to.changeTokenBalances(weth, [owner, alice, bob, resolver], [ether('0.1'), ether('-0.11'), ether('0.0001'), ether('0.0099')]);
await expect(txn).to.changeTokenBalances(dai, [owner, alice, bob, charlie], [ether('-100.1'), ether('100'), ether('0.05'), ether('0.05')]);
await expect(txn).to.changeTokenBalances(weth, [owner, alice, bob, charlie, resolver], [ether('0.1'), ether('-0.11'), ether('0.00005'), ether('0.00005'), ether('0.0099')]);
});

it('unidirectional recursive swap', async function () {
Expand Down Expand Up @@ -276,7 +278,7 @@ describe('Settlement', function () {
const setupData = { ...dataFormFixture, auction };
const {
contracts: { dai, weth, resolver },
accounts: { owner, alice, bob },
accounts: { owner, alice, bob, charlie },
} = setupData;

const INTEGRATOR_FEE = 115n;
Expand All @@ -294,8 +296,9 @@ describe('Settlement', function () {
threshold: ether('101'),
isInnermostOrder: true,
resolverFee: BACK_ORDER_FEE,
integrator: bob.address,
integratorFee: INTEGRATOR_FEE,
integratorFeeRecipient: bob.address,
protocolFeeRecipient: charlie.address,
});

const fillOrderToData0 = await buildCalldataForOrder({
Expand All @@ -312,13 +315,14 @@ describe('Settlement', function () {
threshold: ether('0.11'),
additionalDataForSettlement: fillOrderToData1,
resolverFee: ORDER_FEE,
integrator: bob.address,
integratorFeeRecipient: bob.address,
protocolFeeRecipient: charlie.address,
});

const tx = await resolver.settleOrders(fillOrderToData0);

await expect(tx).to.changeTokenBalances(dai, [owner, alice, bob], [ether('-100.24'), ether('100'), ether('0.24')]);
await expect(tx).to.changeTokenBalances(weth, [owner, alice, bob, resolver], [ether('0.1'), ether('-0.11'), ether('0.0001'), ether('0.0099')]);
await expect(tx).to.changeTokenBalances(dai, [owner, alice, bob, charlie], [ether('-100.24'), ether('100'), ether('0.115') / 2n, ether('0.125') + ether('0.115') / 2n]);
await expect(tx).to.changeTokenBalances(weth, [owner, alice, bob, charlie, resolver], [ether('0.1'), ether('-0.11'), 0, ether('0.0001'), ether('0.0099')]);
});

it('opposite direction recursive swap with resolverFee and integratorFee and custom receiver', async function () {
Expand All @@ -327,10 +331,10 @@ describe('Settlement', function () {
const setupData = { ...dataFormFixture, auction };
const {
contracts: { dai, weth, resolver },
accounts: { owner, alice, bob },
accounts: { owner, alice, bob, charlie },
} = setupData;

const [,,, aliceReciever, ownerReciever] = await ethers.getSigners();
const [,,,, aliceReciever, ownerReciever] = await ethers.getSigners();

const INTEGRATOR_FEE = 115n;
const fillOrderToData1 = await buildCalldataForOrder({
Expand All @@ -347,9 +351,10 @@ describe('Settlement', function () {
setupData,
threshold: ether('101'),
isInnermostOrder: true,
resolverFee: BACK_ORDER_FEE,
integrator: bob.address,
integratorFee: INTEGRATOR_FEE,
resolverFee: BACK_ORDER_FEE,
integratorFeeRecipient: bob.address,
protocolFeeRecipient: charlie.address,
});

const fillOrderToData0 = await buildCalldataForOrder({
Expand All @@ -367,13 +372,14 @@ describe('Settlement', function () {
threshold: ether('0.11'),
additionalDataForSettlement: fillOrderToData1,
resolverFee: ORDER_FEE,
integrator: bob.address,
integratorFeeRecipient: bob.address,
protocolFeeRecipient: charlie.address,
});

const tx = await resolver.settleOrders(fillOrderToData0);

await expect(tx).to.changeTokenBalances(dai, [owner, aliceReciever, bob], [ether('-100.24'), ether('100'), ether('0.24')]);
await expect(tx).to.changeTokenBalances(weth, [alice, ownerReciever, bob, resolver], [ether('-0.11'), ether('0.1'), ether('0.0001'), ether('0.0099')]);
await expect(tx).to.changeTokenBalances(dai, [owner, aliceReciever, bob, charlie], [ether('-100.24'), ether('100'), ether('0.115') / 2n, ether('0.125') + ether('0.115') / 2n]);
await expect(tx).to.changeTokenBalances(weth, [alice, ownerReciever, bob, charlie, resolver], [ether('-0.11'), ether('0.1'), 0, ether('0.0001'), ether('0.0099')]);
});

it('opposite direction recursive swap with resolverFee and integratorFee and custom receiver and weth unwrapping', async function () {
Expand All @@ -382,10 +388,10 @@ describe('Settlement', function () {
const setupData = { ...dataFormFixture, auction };
const {
contracts: { dai, weth, resolver },
accounts: { owner, alice, bob },
accounts: { owner, alice, bob, charlie },
} = setupData;

const [,,, aliceReciever, ownerReciever] = await ethers.getSigners();
const [,,,, aliceReciever, ownerReciever] = await ethers.getSigners();

const INTEGRATOR_FEE = 115n;
const fillOrderToData1 = await buildCalldataForOrder({
Expand All @@ -403,7 +409,8 @@ describe('Settlement', function () {
threshold: ether('101'),
isInnermostOrder: true,
resolverFee: BACK_ORDER_FEE,
integrator: bob.address,
integratorFeeRecipient: bob.address,
protocolFeeRecipient: charlie.address,
});

const fillOrderToData0 = await buildCalldataForOrder({
Expand All @@ -420,16 +427,17 @@ describe('Settlement', function () {
setupData,
threshold: ether('0.11'),
additionalDataForSettlement: fillOrderToData1,
resolverFee: ORDER_FEE,
integrator: bob.address,
integratorFee: INTEGRATOR_FEE,
resolverFee: ORDER_FEE,
integratorFeeRecipient: bob.address,
protocolFeeRecipient: charlie.address,
});

const tx = await resolver.settleOrders(fillOrderToData0);

await expect(tx).to.changeTokenBalances(dai, [owner, aliceReciever, bob], [ether('-100.125'), ether('100'), ether('0.125')]);
await expect(tx).to.changeTokenBalances(weth, [alice, ownerReciever, bob, resolver], [ether('-0.11'), 0, 0, ether('0.009785')]);
await expect(tx).to.changeEtherBalances([alice, ownerReciever, bob], [0, ether('0.1'), ether('0.000215')]);
await expect(tx).to.changeTokenBalances(dai, [owner, aliceReciever, bob, charlie], [ether('-100.125'), ether('100'), 0, ether('0.125')]);
await expect(tx).to.changeTokenBalances(weth, [alice, ownerReciever, bob, charlie, resolver], [ether('-0.11'), 0, 0, 0, ether('0.009785')]);
await expect(tx).to.changeEtherBalances([alice, ownerReciever, bob, charlie], [0, ether('0.1'), ether('0.000115') / 2n, ether('0.0001') + ether('0.000115') / 2n]);
});

it('triple recursive swap', async function () {
Expand Down Expand Up @@ -693,7 +701,7 @@ describe('Settlement', function () {
const setupData = { ...dataFormFixture, auction };
const {
contracts: { dai, weth, resolver },
accounts: { owner, alice, bob },
accounts: { owner, alice, bob, charlie },
others: { abiCoder },
} = setupData;

Expand Down Expand Up @@ -730,14 +738,24 @@ describe('Settlement', function () {
isInnermostOrder: true,
fillingAmount: ether('10') * partialModifier / points,
resolverFee: ORDER_FEE,
integrator: bob.address,
integratorFeeRecipient: bob.address,
protocolFeeRecipient: charlie.address,
});

await weth.approve(resolver, ether('0.01'));

const txn = await resolver.settleOrders(fillOrderToData0);
await expect(txn).to.changeTokenBalances(dai, [resolver, alice], [ether('10') * partialModifier / points, ether('-10') * partialModifier / points]);
await expect(txn).to.changeTokenBalances(weth, [owner, alice], [ether('-0.01') * partialModifier / points * (ORDER_FEE + FEE_BASE) / FEE_BASE, ether('0.01') * partialModifier / points]);
await expect(txn).to.changeTokenBalances(
weth,
[owner, alice, bob, charlie],
[
ether('-0.01') * partialModifier / points * (ORDER_FEE + FEE_BASE) / FEE_BASE,
ether('0.01') * partialModifier / points,
0,
ether('0.01') * partialModifier / points * ORDER_FEE / FEE_BASE,
],
);
});

it('should not pay resolver fee when whitelisted address and it has accessToken', async function () {
Expand Down Expand Up @@ -814,7 +832,7 @@ describe('Settlement', function () {
const setupData = { ...dataFormFixture, auction };
const {
contracts: { dai, weth, resolver },
accounts: { alice, bob },
accounts: { alice, bob, charlie },
} = setupData;

weth.transfer(resolver, ether('0.1001'));
Expand All @@ -833,12 +851,13 @@ describe('Settlement', function () {
threshold: ether('0.11'),
isInnermostOrder: true,
resolverFee: ORDER_FEE,
integrator: bob.address,
integratorFeeRecipient: bob.address,
protocolFeeRecipient: charlie.address,
});

const txn = await resolver.settleOrders(fillOrderToData);
await expect(txn).to.changeTokenBalances(dai, [alice, resolver], [ether('-100'), ether('100')]);
await expect(txn).to.changeTokenBalances(weth, [alice, resolver, bob], [ether('0.1'), ether('-0.1001'), ether('0.0001')]);
await expect(txn).to.changeTokenBalances(weth, [alice, resolver, bob, charlie], [ether('0.1'), ether('-0.1001'), 0, ether('0.0001')]);
});

it('should revert when non-whitelisted address and it has not accessToken', async function () {
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function deploySwapTokens() {
async function initContractsForSettlement() {
const abiCoder = ethers.AbiCoder.defaultAbiCoder();
const chainId = await getChainId();
const [owner, alice, bob] = await ethers.getSigners();
const [owner, alice, bob, charlie] = await ethers.getSigners();

const { dai, weth, accessToken, lopv4 } = await deploySwapTokens();

Expand All @@ -50,7 +50,7 @@ async function initContractsForSettlement() {

return {
contracts: { dai, weth, accessToken, lopv4, settlement, resolver },
accounts: { owner, alice, bob },
accounts: { owner, alice, bob, charlie },
others: { chainId, abiCoder },
};
}
Expand Down
6 changes: 4 additions & 2 deletions test/helpers/fusionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ async function buildCalldataForOrder({
isInnermostOrder = false,
isMakingAmount = true,
fillingAmount = isMakingAmount ? orderData.makingAmount : orderData.takingAmount,
integrator = orderSigner.address,
integratorFeeRecipient = orderSigner.address,
protocolFeeRecipient = orderSigner.address,
resolverFee = 0,
integratorFee = 0,
whitelistResolvers = [], // bytes10[]
Expand Down Expand Up @@ -44,7 +45,8 @@ async function buildCalldataForOrder({
buildFeeTakerExtensions({
feeTaker: await settlement.getAddress(),
getterExtraPrefix: auctionDetails,
feeRecipient: integrator,
integratorFeeRecipient,
protocolFeeRecipient,
makerReceiver: (makerReceiver && makerReceiver !== constants.ZERO_ADDRESS) ? makerReceiver : undefined,
integratorFee,
resolverFee,
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
"@1inch/token-plugins" "1.3.0"
"@openzeppelin/contracts" "5.0.1"

"@1inch/[email protected].1":
version "4.2.1"
resolved "https://registry.yarnpkg.com/@1inch/limit-order-protocol-contract/-/limit-order-protocol-contract-4.2.1.tgz#255a4bebf84e537b7c862a578748e48e1fbd3461"
integrity sha512-a8ST22sjxMiA9uCHWc+2OAdlTqWps2nJMHhgFi4e2VfwqcOf4JoIJGxwEHFU1fFuOq3xMyxSZONinmyFsINYWg==
"@1inch/[email protected].2":
version "4.2.2"
resolved "https://registry.yarnpkg.com/@1inch/limit-order-protocol-contract/-/limit-order-protocol-contract-4.2.2.tgz#237c3cf34942ee39ea1c29d57204b11e741660a7"
integrity sha512-5KcR0O00WP1Z79zX9/xeudzVStLPtiQurmLYmoV+cepTx/Qr67FTKHJKenyIlSQ1p5gg2EOc022gQvSZ2K7m4A==
dependencies:
"@1inch/solidity-utils" "6.0.0"
"@chainlink/contracts" "0.8.0"
Expand Down

0 comments on commit 80950a9

Please sign in to comment.