Skip to content

Commit

Permalink
Removed redundant RFQ interaction tests
Browse files Browse the repository at this point in the history
  • Loading branch information
byshape committed Oct 17, 2023
1 parent 378491a commit ef30d62
Show file tree
Hide file tree
Showing 2 changed files with 246 additions and 268 deletions.
248 changes: 246 additions & 2 deletions test/Interactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,78 @@ describe('Interactions', function () {
expect(await dai.balanceOf(addr1.address)).to.equal(addr1dai.add(ether('100')));
});

it('opposite direction recursive swap, checkAndInvalidate', async function () {
const { dai, weth, swap, chainId, matcher } = await loadFixture(initContractsWithRecursiveMatcher);

const order = buildOrder({
makerAsset: dai.address,
takerAsset: weth.address,
makingAmount: ether('100'),
takingAmount: ether('0.1'),
maker: addr.address,
makerTraits: buildMakerTraits({ nonce: 0, allowMultipleFills: false }),
});

const backOrder = buildOrder({
makerAsset: weth.address,
takerAsset: dai.address,
makingAmount: ether('0.1'),
takingAmount: ether('100'),
maker: addr1.address,
makerTraits: buildMakerTraits({ nonce: 0, allowMultipleFills: false }),
});

const signature = await signOrder(order, chainId, swap.address, addr);
const signatureBackOrder = await signOrder(backOrder, chainId, swap.address, addr1);

const matchingParams = matcher.address + '01' + abiCoder.encode(
['address[]', 'bytes[]'],
[
[
weth.address,
dai.address,
],
[
weth.interface.encodeFunctionData('approve', [swap.address, ether('0.1')]),
dai.interface.encodeFunctionData('approve', [swap.address, ether('100')]),
],
],
).substring(2);

const { r: backOrderR, _vs: backOrderVs } = ethers.utils.splitSignature(signatureBackOrder);
const takerTraits = buildTakerTraits({
interaction: matchingParams,
makingAmount: true,
minReturn: ether('100'),
});
const interaction = matcher.address + '00' + swap.interface.encodeFunctionData('fillOrderArgs', [
backOrder,
backOrderR,
backOrderVs,
ether('0.1'),
takerTraits.traits,
takerTraits.args,
]).substring(10);

const addrweth = await weth.balanceOf(addr.address);
const addr1weth = await weth.balanceOf(addr1.address);
const addrdai = await dai.balanceOf(addr.address);
const addr1dai = await dai.balanceOf(addr1.address);

const { r, _vs: vs } = ethers.utils.splitSignature(signature);
const matcherTraits = buildTakerTraits({
interaction,
makingAmount: true,
minReturn: ether('0.1'),
});
await matcher.matchOrders(swap.address, order, r, vs, ether('100'), matcherTraits.traits, matcherTraits.args);

expect(await weth.balanceOf(addr.address)).to.equal(addrweth.add(ether('0.1')));
expect(await weth.balanceOf(addr1.address)).to.equal(addr1weth.sub(ether('0.1')));
expect(await dai.balanceOf(addr.address)).to.equal(addrdai.sub(ether('100')));
expect(await dai.balanceOf(addr1.address)).to.equal(addr1dai.add(ether('100')));
});

it('unidirectional recursive swap', async function () {
const { dai, weth, swap, chainId, matcher } = await loadFixture(initContractsWithRecursiveMatcher);

Expand Down Expand Up @@ -144,8 +216,83 @@ describe('Interactions', function () {
],
[
weth.interface.encodeFunctionData('transferFrom', [addr.address, matcher.address, ether('0.025')]),
dai.interface.encodeFunctionData('approve', [swap.address, ether('0.025')]),
weth.interface.encodeFunctionData('transfer', [addr.address, ether('25')]),
weth.interface.encodeFunctionData('approve', [swap.address, ether('0.025')]),
dai.interface.encodeFunctionData('transfer', [addr.address, ether('25')]),
],
],
).substring(2);

const { r: backOrderR, _vs: backOrderVs } = ethers.utils.splitSignature(signatureBackOrder);
const takerTraits = buildTakerTraits({
interaction: matchingParams,
makingAmount: true,
minReturn: ether('0.015'),
});
const interaction = matcher.address + '00' + swap.interface.encodeFunctionData('fillOrderArgs', [
backOrder,
backOrderR,
backOrderVs,
ether('15'),
takerTraits.traits,
takerTraits.args,
]).substring(10);

const addrweth = await weth.balanceOf(addr.address);
const addr1weth = await weth.balanceOf(addr1.address);
const addrdai = await dai.balanceOf(addr.address);
const addr1dai = await dai.balanceOf(addr1.address);

await weth.approve(matcher.address, ether('0.025'));
const { r, _vs: vs } = ethers.utils.splitSignature(signature);
const matcherTraits = buildTakerTraits({
interaction,
makingAmount: true,
minReturn: ether('0.01'),
});
await matcher.matchOrders(swap.address, order, r, vs, ether('10'), matcherTraits.traits, matcherTraits.args);

expect(await weth.balanceOf(addr.address)).to.equal(addrweth.sub(ether('0.025')));
expect(await weth.balanceOf(addr1.address)).to.equal(addr1weth.add(ether('0.025')));
expect(await dai.balanceOf(addr.address)).to.equal(addrdai.add(ether('25')));
expect(await dai.balanceOf(addr1.address)).to.equal(addr1dai.sub(ether('25')));
});

it('unidirectional recursive swap, checkAndInvalidate', async function () {
const { dai, weth, swap, chainId, matcher } = await loadFixture(initContractsWithRecursiveMatcher);

const order = buildOrder({
makerAsset: dai.address,
takerAsset: weth.address,
makingAmount: ether('10'),
takingAmount: ether('0.01'),
maker: addr1.address,
makerTraits: buildMakerTraits({ nonce: 0, allowMultipleFills: false }),
});

const backOrder = buildOrder({
makerAsset: dai.address,
takerAsset: weth.address,
makingAmount: ether('15'),
takingAmount: ether('0.015'),
maker: addr1.address,
makerTraits: buildMakerTraits({ nonce: 1, allowMultipleFills: false }),
});

const signature = await signOrder(order, chainId, swap.address, addr1);
const signatureBackOrder = await signOrder(backOrder, chainId, swap.address, addr1);

const matchingParams = matcher.address + '01' + abiCoder.encode(
['address[]', 'bytes[]'],
[
[
weth.address,
weth.address,
dai.address,
],
[
weth.interface.encodeFunctionData('transferFrom', [addr.address, matcher.address, ether('0.025')]),
weth.interface.encodeFunctionData('approve', [swap.address, ether('0.025')]),
dai.interface.encodeFunctionData('transfer', [addr.address, ether('25')]),
],
],
).substring(2);
Expand Down Expand Up @@ -281,6 +428,103 @@ describe('Interactions', function () {
expect(await dai.balanceOf(addr.address)).to.equal(addrdai.add(ether('25')));
expect(await dai.balanceOf(addr1.address)).to.equal(addr1dai.sub(ether('25')));
});

it('triple recursive swap, checkAndInvalidate', async function () {
const { dai, weth, swap, chainId, matcher } = await loadFixture(initContractsWithRecursiveMatcher);

const order1 = buildOrder({
makerAsset: dai.address,
takerAsset: weth.address,
makingAmount: ether('10'),
takingAmount: ether('0.01'),
maker: addr1.address,
makerTraits: buildMakerTraits({ nonce: 0, allowMultipleFills: false }),
});

const order2 = buildOrder({
makerAsset: dai.address,
takerAsset: weth.address,
makingAmount: ether('15'),
takingAmount: ether('0.015'),
maker: addr1.address,
makerTraits: buildMakerTraits({ nonce: 1, allowMultipleFills: false }),
});

const backOrder = buildOrder({
makerAsset: weth.address,
takerAsset: dai.address,
makingAmount: ether('0.025'),
takingAmount: ether('25'),
maker: addr.address,
makerTraits: buildMakerTraits({ nonce: 0, allowMultipleFills: false }),
});

const signature1 = await signOrder(order1, chainId, swap.address, addr1);
const signature2 = await signOrder(order2, chainId, swap.address, addr1);
const signatureBackOrder = await signOrder(backOrder, chainId, swap.address, addr);

const matchingParams = matcher.address + '01' + abiCoder.encode(
['address[]', 'bytes[]'],
[
[
weth.address,
dai.address,
],
[
weth.interface.encodeFunctionData('approve', [swap.address, ether('0.025')]),
dai.interface.encodeFunctionData('approve', [swap.address, ether('25')]),
],
],
).substring(2);

const { r: backOrderR, _vs: backOrderVs } = ethers.utils.splitSignature(signatureBackOrder);
const internalTakerTraits = buildTakerTraits({
interaction: matchingParams,
makingAmount: true,
minReturn: ether('25'),
});
const internalInteraction = matcher.address + '00' + swap.interface.encodeFunctionData('fillOrderArgs', [
backOrder,
backOrderR,
backOrderVs,
ether('0.025'),
internalTakerTraits.traits,
internalTakerTraits.args,
]).substring(10);

const { r: order2R, _vs: order2Vs } = ethers.utils.splitSignature(signature2);
const externalTakerTraits = buildTakerTraits({
interaction: internalInteraction,
makingAmount: true,
minReturn: ether('0.015'),
});
const externalInteraction = matcher.address + '00' + swap.interface.encodeFunctionData('fillOrderArgs', [
order2,
order2R,
order2Vs,
ether('15'),
externalTakerTraits.traits,
externalTakerTraits.args,
]).substring(10);

const addrweth = await weth.balanceOf(addr.address);
const addr1weth = await weth.balanceOf(addr1.address);
const addrdai = await dai.balanceOf(addr.address);
const addr1dai = await dai.balanceOf(addr1.address);

const { r, _vs: vs } = ethers.utils.splitSignature(signature1);
const matcherTraits = buildTakerTraits({
interaction: externalInteraction,
makingAmount: true,
minReturn: ether('0.01'),
});
await matcher.matchOrders(swap.address, order1, r, vs, ether('10'), matcherTraits.traits, matcherTraits.args);

expect(await weth.balanceOf(addr.address)).to.equal(addrweth.sub(ether('0.025')));
expect(await weth.balanceOf(addr1.address)).to.equal(addr1weth.add(ether('0.025')));
expect(await dai.balanceOf(addr.address)).to.equal(addrdai.add(ether('25')));
expect(await dai.balanceOf(addr1.address)).to.equal(addr1dai.sub(ether('25')));
});
});

describe('check hash', function () {
Expand Down
Loading

0 comments on commit ef30d62

Please sign in to comment.