Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
"dependencies": {
"@1inch/solidity-utils": "2.2.21",
"@chainlink/contracts": "0.6.1",
"@nomicfoundation/hardhat-chai-matchers": "../hardhat/packages/hardhat-chai-matchers",
"@openzeppelin/contracts": "4.8.2"
},
"devDependencies": {
"@metamask/eth-sig-util": "5.0.2",
"@nomicfoundation/hardhat-chai-matchers": "1.0.6",
"@nomicfoundation/hardhat-network-helpers": "1.0.8",
"@nomiclabs/hardhat-ethers": "2.2.2",
"@nomiclabs/hardhat-etherscan": "3.1.7",
Expand All @@ -34,7 +34,7 @@
"eslint-plugin-n": "15.6.1",
"eslint-plugin-promise": "6.1.1",
"ethers": "5.7.2",
"hardhat": "^2.13.0",
"hardhat": "2.13.0",
"hardhat-dependency-compiler": "1.1.3",
"hardhat-deploy": "0.11.24",
"hardhat-gas-reporter": "1.0.9",
Expand Down
20 changes: 10 additions & 10 deletions test/ChainLinkExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ describe('ChainLinkExample', function () {
const signature = await signOrder(order, chainId, swap.address, addr1);

const { r, vs } = compactSignature(signature);
await expect(swap.fillOrderExt(order, r, vs, ether('1'), fillWithMakingAmount(ether('4040.01')), order.extension)) // taking threshold = 4000 + 1% + eps
.to.changeTokenBalances(dai, [addr, addr1], [ether('-4040'), ether('4040')])
.to.changeTokenBalances(weth, [addr, addr1], [ether('1'), ether('-1')]);
const tx = await swap.fillOrderExt(order, r, vs, ether('1'), fillWithMakingAmount(ether('4040.01')), order.extension);
await expect(tx).to.changeTokenBalances(dai, [addr, addr1], [ether('-4040'), ether('4040')]); // taking threshold = 4000 + 1% + eps
await expect(tx).to.changeTokenBalances(weth, [addr, addr1], [ether('1'), ether('-1')]);
});

it('dai -> 1inch stop loss order', async function () {
Expand All @@ -104,9 +104,9 @@ describe('ChainLinkExample', function () {
const signature = await signOrder(order, chainId, swap.address, addr1);

const { r, vs } = compactSignature(signature);
await expect(swap.fillOrderExt(order, r, vs, makingAmount, fillWithMakingAmount(takingAmount.add(ether('0.01'))), order.extension)) // taking threshold = exact taker amount + eps
.to.changeTokenBalances(dai, [addr, addr1], [takingAmount.mul(-1), takingAmount])
.to.changeTokenBalances(inch, [addr, addr1], [makingAmount, makingAmount.mul(-1)]);
const tx = await swap.fillOrderExt(order, r, vs, makingAmount, fillWithMakingAmount(takingAmount + ether('0.01')), order.extension);
await expect(tx).to.changeTokenBalances(dai, [addr, addr1], [-takingAmount, takingAmount]); // taking threshold = exact taker amount + eps
await expect(tx).to.changeTokenBalances(inch, [addr, addr1], [makingAmount, -makingAmount]);
});

it('dai -> 1inch stop loss order predicate is invalid', async function () {
Expand All @@ -132,7 +132,7 @@ describe('ChainLinkExample', function () {

const { r, vs } = compactSignature(signature);
await expect(
swap.fillOrderExt(order, r, vs, fillWithMakingAmount(makingAmount), takingAmount.add(ether('0.01')), order.extension), // taking threshold = exact taker amount + eps
swap.fillOrderExt(order, r, vs, fillWithMakingAmount(makingAmount), takingAmount + ether('0.01'), order.extension), // taking threshold = exact taker amount + eps
).to.be.revertedWithCustomError(swap, 'PredicateIsNotTrue');
});

Expand Down Expand Up @@ -161,8 +161,8 @@ describe('ChainLinkExample', function () {
const signature = await signOrder(order, chainId, swap.address, addr1);

const { r, vs } = compactSignature(signature);
await expect(swap.fillOrderExt(order, r, vs, makingAmount, fillWithMakingAmount(takingAmount), order.extension))
.to.changeTokenBalances(dai, [addr, addr1], [takingAmount.mul(-1), takingAmount])
.to.changeTokenBalances(weth, [addr, addr1], [makingAmount, makingAmount.mul(-1)]);
const tx = await swap.fillOrderExt(order, r, vs, makingAmount, fillWithMakingAmount(takingAmount), order.extension);
await expect(tx).to.changeTokenBalances(dai, [addr, addr1], [-takingAmount, takingAmount]);
await expect(tx).to.changeTokenBalances(weth, [addr, addr1], [makingAmount, -makingAmount]);
});
});
104 changes: 57 additions & 47 deletions test/DutchAuctionCalculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,90 +50,100 @@ describe('Dutch auction', function () {
);
const signature = await signOrder(order, chainId, swap.address, addr);

const makerDaiBefore = await dai.balanceOf(addr.address);
const takerDaiBefore = await dai.balanceOf(addr1.address);
const makerWethBefore = await weth.balanceOf(addr.address);
const takerWethBefore = await weth.balanceOf(addr1.address);
return { dai, weth, swap, ts, order, signature, makerDaiBefore, takerDaiBefore, makerWethBefore, takerWethBefore };
return { dai, weth, swap, ts, order, signature };
};

it('swap with makingAmount 50% time passed', async function () {
const { dai, weth, swap, ts, order, signature, makerDaiBefore, takerDaiBefore, makerWethBefore, takerWethBefore } = await loadFixture(deployAndBuildOrder);
const { dai, weth, swap, ts, order, signature } = await loadFixture(deployAndBuildOrder);

await time.increaseTo(ts + 43200n); // 50% auction time

const { r, vs } = compactSignature(signature);
await swap.connect(addr1).fillOrderExt(order, r, vs, ether('100'), fillWithMakingAmount(ether('0.08')), order.extension);

expect(await dai.balanceOf(addr.address)).to.equal(makerDaiBefore.sub(ether('100')));
expect(await dai.balanceOf(addr1.address)).to.equal(takerDaiBefore.add(ether('100')));
assertRoughlyEqualValues(await weth.balanceOf(addr.address), makerWethBefore.add(ether('0.075')), 1e-6);
assertRoughlyEqualValues(await weth.balanceOf(addr1.address), takerWethBefore.sub(ether('0.075')), 1e-6);
const tx = await swap.connect(addr1).fillOrderExt(order, r, vs, ether('100'), fillWithMakingAmount(ether('0.08')), order.extension);
await expect(tx).to.changeTokenBalances(dai, [addr.address, addr1.address], [-ether('100'), ether('100')]);
await expect(tx).to.changeTokenBalances(weth, [addr.address, addr1.address],
([change, change1]) => {
assertRoughlyEqualValues(change, ether('0.075'), 1e-4);
assertRoughlyEqualValues(change1, -ether('0.075'), 1e-4);
},
);
});

it('swap with takingAmount 50% time passed', async function () {
const { dai, weth, swap, ts, order, signature, makerDaiBefore, takerDaiBefore, makerWethBefore, takerWethBefore } = await loadFixture(deployAndBuildOrder);
const { dai, weth, swap, ts, order, signature } = await loadFixture(deployAndBuildOrder);

await time.increaseTo(ts + 43200n); // 50% auction time

const { r, vs } = compactSignature(signature);
await swap.connect(addr1).fillOrderExt(order, r, vs, ether('0.075'), ether('100'), order.extension);

expect(await dai.balanceOf(addr.address)).to.equal(makerDaiBefore.sub(ether('100')));
expect(await dai.balanceOf(addr1.address)).to.equal(takerDaiBefore.add(ether('100')));
assertRoughlyEqualValues(await weth.balanceOf(addr.address), makerWethBefore.add(ether('0.075')), 1e-6);
assertRoughlyEqualValues(await weth.balanceOf(addr1.address), takerWethBefore.sub(ether('0.075')), 1e-6);
const tx = await swap.connect(addr1).fillOrderExt(order, r, vs, ether('0.075'), ether('100'), order.extension);
await expect(tx).to.changeTokenBalances(dai, [addr.address, addr1.address], [-ether('100'), ether('100')]);
await expect(tx).to.changeTokenBalances(weth, [addr.address, addr1.address],
([change, change1]) => {
assertRoughlyEqualValues(change, ether('0.075'), 1e-4);
assertRoughlyEqualValues(change1, -ether('0.075'), 1e-4);
},
);
});

it('swap with makingAmount 0% time passed', async function () {
const { dai, weth, swap, order, signature, makerDaiBefore, takerDaiBefore, makerWethBefore, takerWethBefore } = await loadFixture(deployAndBuildOrder);
const { dai, weth, swap, order, signature } = await loadFixture(deployAndBuildOrder);

const { r, vs } = compactSignature(signature);
await swap.connect(addr1).fillOrderExt(order, r, vs, ether('100'), fillWithMakingAmount(ether('0.1')), order.extension);

expect(await dai.balanceOf(addr.address)).to.equal(makerDaiBefore.sub(ether('100')));
expect(await dai.balanceOf(addr1.address)).to.equal(takerDaiBefore.add(ether('100')));
assertRoughlyEqualValues(await weth.balanceOf(addr.address), makerWethBefore.add(ether('0.1')), 1e-6);
assertRoughlyEqualValues(await weth.balanceOf(addr1.address), takerWethBefore.sub(ether('0.1')), 1e-6);
const tx = await swap.connect(addr1).fillOrderExt(order, r, vs, ether('100'), fillWithMakingAmount(ether('0.1')), order.extension);
await expect(tx).to.changeTokenBalances(dai, [addr.address, addr1.address], [-ether('100'), ether('100')]);
await expect(tx).to.changeTokenBalances(weth, [addr.address, addr1.address],
([change, change1]) => {
assertRoughlyEqualValues(change, ether('0.1'), 1e-4);
assertRoughlyEqualValues(change1, -ether('0.1'), 1e-4);
},
);
});

it('swap with takingAmount 0% time passed', async function () {
const { dai, weth, swap, order, signature, makerDaiBefore, takerDaiBefore, makerWethBefore, takerWethBefore } = await loadFixture(deployAndBuildOrder);
const { dai, weth, swap, order, signature } = await loadFixture(deployAndBuildOrder);

const { r, vs } = compactSignature(signature);
await swap.connect(addr1).fillOrderExt(order, r, vs, ether('0.1'), ether('100'), order.extension);

expect(await dai.balanceOf(addr.address)).to.equal(makerDaiBefore.sub(ether('100')));
expect(await dai.balanceOf(addr1.address)).to.equal(takerDaiBefore.add(ether('100')));
assertRoughlyEqualValues(await weth.balanceOf(addr.address), makerWethBefore.add(ether('0.1')), 1e-6);
assertRoughlyEqualValues(await weth.balanceOf(addr1.address), takerWethBefore.sub(ether('0.1')), 1e-6);
const tx = await swap.connect(addr1).fillOrderExt(order, r, vs, ether('0.1'), ether('100'), order.extension);
await expect(tx).to.changeTokenBalances(dai, [addr.address, addr1.address], [-ether('100'), ether('100')]);
await expect(tx).to.changeTokenBalances(weth, [addr.address, addr1.address],
([change, change1]) => {
assertRoughlyEqualValues(change, ether('0.1'), 1e-4);
assertRoughlyEqualValues(change1, -ether('0.1'), 1e-4);
},
);
});

it('swap with makingAmount 100% time passed', async function () {
const { dai, weth, swap, ts, order, signature, makerDaiBefore, takerDaiBefore, makerWethBefore, takerWethBefore } = await loadFixture(deployAndBuildOrder);
const { dai, weth, swap, ts, order, signature } = await loadFixture(deployAndBuildOrder);

await time.increaseTo(ts + 86500n); // >100% auction time

const { r, vs } = compactSignature(signature);
await swap.connect(addr1).fillOrderExt(order, r, vs, ether('100'), fillWithMakingAmount(ether('0.05')), order.extension);

expect(await dai.balanceOf(addr.address)).to.equal(makerDaiBefore.sub(ether('100')));
expect(await dai.balanceOf(addr1.address)).to.equal(takerDaiBefore.add(ether('100')));
assertRoughlyEqualValues(await weth.balanceOf(addr.address), makerWethBefore.add(ether('0.05')), 1e-6);
assertRoughlyEqualValues(await weth.balanceOf(addr1.address), takerWethBefore.sub(ether('0.05')), 1e-6);
const tx = await swap.connect(addr1).fillOrderExt(order, r, vs, ether('100'), fillWithMakingAmount(ether('0.05')), order.extension);
await expect(tx).to.changeTokenBalances(dai, [addr.address, addr1.address], [-ether('100'), ether('100')]);
await expect(tx).to.changeTokenBalances(
weth,
[addr.address, addr1.address],
([change, change1]) => {
assertRoughlyEqualValues(change, ether('0.05'), 1e-4);
assertRoughlyEqualValues(change1, -ether('0.05'), 1e-4);
},
);
});

it('swap with takingAmount 100% time passed', async function () {
const { dai, weth, swap, ts, order, signature, makerDaiBefore, takerDaiBefore, makerWethBefore, takerWethBefore } = await loadFixture(deployAndBuildOrder);
const { dai, weth, swap, ts, order, signature } = await loadFixture(deployAndBuildOrder);

await time.increaseTo(ts + 86500n); // >100% auction time

const { r, vs } = compactSignature(signature);
await swap.connect(addr1).fillOrderExt(order, r, vs, ether('0.05'), ether('100'), order.extension);

expect(await dai.balanceOf(addr.address)).to.equal(makerDaiBefore.sub(ether('100')));
expect(await dai.balanceOf(addr1.address)).to.equal(takerDaiBefore.add(ether('100')));
assertRoughlyEqualValues(await weth.balanceOf(addr.address), makerWethBefore.add(ether('0.05')), 1e-6);
assertRoughlyEqualValues(await weth.balanceOf(addr1.address), takerWethBefore.sub(ether('0.05')), 1e-6);
const tx = await swap.connect(addr1).fillOrderExt(order, r, vs, ether('0.05'), ether('100'), order.extension);
await expect(tx).to.changeTokenBalances(dai, [addr.address, addr1.address], [-ether('100'), ether('100')]);
await expect(tx).to.changeTokenBalances(weth, [addr.address, addr1.address],
([change, change1]) => {
assertRoughlyEqualValues(change, ether('0.05'), 1e-4);
assertRoughlyEqualValues(change1, -ether('0.05'), 1e-4);
},
);
});
});
Loading