Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/tests #127

Merged
merged 5 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
192 changes: 96 additions & 96 deletions test/FeeBank.js

Large diffs are not rendered by default.

47 changes: 23 additions & 24 deletions test/FeeBankCharger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,37 @@ const { ethers } = require('hardhat');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');

describe('FeeBankCharger', function () {
let addr, addr1;

before(async function () {
[addr, addr1] = await ethers.getSigners();
});

async function initContracts() {
const inch = await deployContract('ERC20PermitMock', ['1INCH', '1INCH', addr.address, ether('1000')]);
const [owner, alice] = await ethers.getSigners();
const inch = await deployContract('ERC20PermitMock', ['1INCH', '1INCH', owner.address, ether('1000')]);

const charger = await deployContract('FeeBankCharger', [inch.address]);

const FeeBank = await ethers.getContractFactory('FeeBank');
const feeBank = FeeBank.attach(await charger.feeBank());

await inch.transfer(addr1.address, ether('100'));
await inch.transfer(alice.address, ether('100'));
await inch.approve(feeBank.address, ether('1000'));
await inch.connect(addr1).approve(feeBank.address, ether('1000'));
await inch.connect(alice).approve(feeBank.address, ether('1000'));

return { inch, charger, feeBank };
return {
contracts: { inch, charger, feeBank },
accounts: { owner, alice },
};
}

describe('increaseAvailableCredit', function () {
it('should increase credit', async function () {
const { charger, feeBank } = await loadFixture(initContracts);
const { contracts: { charger, feeBank }, accounts: { alice } } = await loadFixture(initContracts);
const amount = ether('100');
expect(await charger.availableCredit(addr1.address)).to.equal('0');
await feeBank.depositFor(addr1.address, amount);
expect(await charger.availableCredit(addr1.address)).to.equal(amount);
expect(await charger.availableCredit(alice.address)).to.equal('0');
await feeBank.depositFor(alice.address, amount);
expect(await charger.availableCredit(alice.address)).to.equal(amount);
});

it('should not increase credit by non-feeBank address', async function () {
const { charger } = await loadFixture(initContracts);
await expect(charger.increaseAvailableCredit(addr1.address, ether('100'))).to.be.revertedWithCustomError(
const { contracts: { charger }, accounts: { alice } } = await loadFixture(initContracts);
await expect(charger.increaseAvailableCredit(alice.address, ether('100'))).to.be.revertedWithCustomError(
charger,
'OnlyFeeBankAccess',
);
Expand All @@ -44,23 +42,24 @@ describe('FeeBankCharger', function () {

describe('decreaseAvailableCredit', function () {
async function initContractsAndAllowance() {
const { charger, feeBank } = await initContracts();
const data = await initContracts();
const { contracts: { feeBank } } = data;
const creditAmount = ether('100');
await feeBank.deposit(creditAmount);
return { charger, feeBank, creditAmount };
return { ...data, others: { creditAmount } };
}

it('should decrease credit', async function () {
const { charger, feeBank, creditAmount } = await loadFixture(initContractsAndAllowance);
const { contracts: { charger, feeBank }, accounts: { owner, alice }, others: { creditAmount } } = await loadFixture(initContractsAndAllowance);
const amount = ether('10');
expect(await charger.availableCredit(addr.address)).to.equal(creditAmount);
await feeBank.withdrawTo(addr1.address, amount);
expect(await charger.availableCredit(addr.address)).to.equal(creditAmount - amount);
expect(await charger.availableCredit(owner.address)).to.equal(creditAmount);
await feeBank.withdrawTo(alice.address, amount);
expect(await charger.availableCredit(owner.address)).to.equal(creditAmount - amount);
});

it('should not deccrease credit by non-feeBank address', async function () {
const { charger } = await loadFixture(initContractsAndAllowance);
await expect(charger.decreaseAvailableCredit(addr1.address, ether('10'))).to.be.revertedWithCustomError(
const { contracts: { charger }, accounts: { alice } } = await loadFixture(initContractsAndAllowance);
await expect(charger.decreaseAvailableCredit(alice.address, ether('10'))).to.be.revertedWithCustomError(
charger,
'OnlyFeeBankAccess',
);
Expand Down
61 changes: 32 additions & 29 deletions test/MeasureGas.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,72 +6,75 @@ const { deploySwapTokens, getChainId, deployContract } = require('./helpers/fixt
const { buildOrder, signOrder, compactSignature, fillWithMakingAmount, buildMakerTraits } = require('@1inch/limit-order-protocol-contract/test/helpers/orderUtils');
const { buildFusions } = require('./helpers/fusionUtils');

describe('MeasureGas', function () {
const resolversNumber = 10;
let addrs;
let chainId;
const abiCoder = ethers.utils.defaultAbiCoder;
const RESOLVERS_NUMBER = 10;

describe('MeasureGas', function () {
before(async function () {
if (hre.__SOLIDITY_COVERAGE_RUNNING) { this.skip(); }
chainId = await getChainId();
addrs = await ethers.getSigners();
});

async function initContracts() {
const [owner, alice] = await ethers.getSigners();
const chainId = await getChainId();
const abiCoder = ethers.utils.defaultAbiCoder;
const { dai, weth, inch, swap } = await deploySwapTokens();

await dai.transfer(addrs[1].address, ether('100'));
await inch.mint(addrs[0].address, ether('100'));
await dai.transfer(alice.address, ether('100'));
await inch.mint(owner.address, ether('100'));
await weth.deposit({ value: ether('1') });
await weth.connect(addrs[1]).deposit({ value: ether('1') });
await weth.connect(alice).deposit({ value: ether('1') });

const settlement = await deployContract('Settlement', [swap.address, inch.address]);
const resolvers = [];
for (let i = 0; i < resolversNumber; i++) {
for (let i = 0; i < RESOLVERS_NUMBER; i++) {
resolvers[i] = await deployContract('ResolverMock', [settlement.address, swap.address]);
}
const FeeBank = await ethers.getContractFactory('FeeBank');
const feeBank = FeeBank.attach(await settlement.feeBank());
await inch.approve(feeBank.address, ether('100'));
await feeBank.depositFor(resolvers[0].address, ether('100'));

return { dai, weth, swap, settlement, feeBank, resolvers };
return {
contracts: { dai, weth, swap, settlement, feeBank, resolvers },
accounts: { owner, alice },
others: { chainId, abiCoder },
};
}

async function initContractsAndApproves() {
const { dai, weth, swap, settlement, feeBank, resolvers } = await initContracts();
const data = await initContracts();
const { contracts: { dai, weth, swap }, accounts: { alice } } = data;
await dai.approve(swap.address, ether('100'));
await dai.connect(addrs[1]).approve(swap.address, ether('100'));
await dai.connect(alice).approve(swap.address, ether('100'));
await weth.approve(swap.address, ether('1'));
await weth.connect(addrs[1]).approve(swap.address, ether('1'));
return { dai, weth, swap, settlement, feeBank, resolvers };
await weth.connect(alice).approve(swap.address, ether('1'));
return data;
}

it('1 fill for 1 order', async function () {
const { dai, weth, swap, settlement, resolvers } = await loadFixture(initContractsAndApproves);
const { contracts: { dai, weth, swap, settlement, resolvers }, accounts: { owner, alice }, others: { chainId, abiCoder } } = await loadFixture(initContractsAndApproves);

const { fusions: [fusionDetails], hashes: [fusionDetailsHash], resolvers: fusionResolvers } = await buildFusions([
{ resolvers: [resolvers[0].address] },
]);
const order = buildOrder({
maker: addrs[1].address,
maker: alice.address,
makerAsset: dai.address,
takerAsset: weth.address,
makingAmount: ether('100'),
takingAmount: ether('0.1'),
makerTraits: buildMakerTraits({ allowedSender: settlement.address }),
});
order.salt = fusionDetailsHash;
const { r, vs } = compactSignature(await signOrder(order, chainId, swap.address, addrs[1]));
const { r, vs } = compactSignature(await signOrder(order, chainId, swap.address, alice));

const resolverCalldata = abiCoder.encode(
['address[]', 'bytes[]'],
[
[weth.address],
[
weth.interface.encodeFunctionData('transferFrom', [
addrs[0].address,
owner.address,
resolvers[0].address,
ether('0.1'),
]),
Expand All @@ -93,12 +96,12 @@ describe('MeasureGas', function () {

const tx = await resolvers[0].settleOrders(fillOrderToData);
console.log(`1 fill for 1 order gasUsed: ${(await tx.wait()).gasUsed}`);
await expect(tx).to.changeTokenBalances(dai, [resolvers[0], addrs[1]], [ether('100'), ether('-100')]);
await expect(tx).to.changeTokenBalances(weth, [addrs[0], addrs[1]], [ether('-0.1'), ether('0.1')]);
await expect(tx).to.changeTokenBalances(dai, [resolvers[0], alice], [ether('100'), ether('-100')]);
await expect(tx).to.changeTokenBalances(weth, [owner, alice], [ether('-0.1'), ether('0.1')]);
});

it('1 fill for 5 orders in a batch', async function () {
const { dai, weth, swap, settlement, resolvers } = await loadFixture(initContractsAndApproves);
const { contracts: { dai, weth, swap, settlement, resolvers }, accounts: { alice, owner }, others: { chainId } } = await loadFixture(initContractsAndApproves);

const resolverAddresses = resolvers.map(r => r.address);
const { fusions: fusionDetails, hashes: fusionHashes, resolvers: fusionResolvers } = await buildFusions([
Expand All @@ -114,27 +117,27 @@ describe('MeasureGas', function () {
const signatures = [];
for (let i = 0; i < 4; i++) {
orders[i] = buildOrder({
maker: addrs[1].address,
maker: alice.address,
makerAsset: dai.address,
takerAsset: weth.address,
makingAmount: ether((i + 1).toString()),
takingAmount: ether(((i + 1) / 100).toString()),
makerTraits: buildMakerTraits({ allowedSender: settlement.address }),
});
orders[i].salt = fusionHashes[i];
const { r, vs } = compactSignature(await signOrder(orders[i], chainId, swap.address, addrs[1]));
const { r, vs } = compactSignature(await signOrder(orders[i], chainId, swap.address, alice));
signatures[i] = { r, vs };
}
orders[4] = buildOrder({
maker: addrs[0].address,
maker: owner.address,
makerAsset: weth.address,
takerAsset: dai.address,
makingAmount: ether('0.1'), // takingAmount/100
takingAmount: ether('10'), // (max_i - 1) * max_i / 2
makerTraits: settlement.address,
});
orders[4].salt = fusionHashes[4];
const { r, vs } = compactSignature(await signOrder(orders[4], chainId, swap.address, addrs[0]));
const { r, vs } = compactSignature(await signOrder(orders[4], chainId, swap.address, owner));
signatures[4] = { r, vs };

// Encode data for fillingg orders
Expand Down Expand Up @@ -163,7 +166,7 @@ describe('MeasureGas', function () {

const tx = await resolvers[0].settleOrders(fillOrdersToData[0]);
console.log(`1 fill for 5 orders in a batch gasUsed: ${(await tx.wait()).gasUsed}`);
await expect(tx).to.changeTokenBalances(weth, [addrs[0], addrs[1]], [ether('-0.1'), ether('0.1')]);
await expect(tx).to.changeTokenBalances(dai, [addrs[0], addrs[1]], [ether('10'), ether('-10')]);
await expect(tx).to.changeTokenBalances(weth, [owner, alice], [ether('-0.1'), ether('0.1')]);
await expect(tx).to.changeTokenBalances(dai, [owner, alice], [ether('10'), ether('-10')]);
});
});
12 changes: 6 additions & 6 deletions test/PowerPod.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ const { ethers } = require('hardhat');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { expBase } = require('./helpers/utils');

describe('PowerPod', function () {
const COMMON_LOCK_DURATION = time.duration.days('40');
const MAX_WHITELISTED = 3;
const BALANCE_THRESHOLD = 1000; // 10%
const COMMON_LOCK_DURATION = time.duration.days('40');
const MAX_WHITELISTED = 3;
const BALANCE_THRESHOLD = 1000; // 10%

describe('PowerPod', function () {
const stakeAndRegisterInDelegation = async (st1inch, delegation, user, amount, userIndex) => {
await st1inch.connect(user).deposit(0, COMMON_LOCK_DURATION);
await st1inch.depositFor(user.address, amount);
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('PowerPod', function () {
whitelistRegistry,
'BalanceLessThanThreshold',
);
// create other stake and delegate to owner
// create others stake and delegate to owner
ZumZoom marked this conversation as resolved.
Show resolved Hide resolved
await depositAndDelegateTo(st1inch, delegation, alice, owner.address, ether('2'));
// register owner into whitelistRegistry and chack that
await whitelistRegistry.register();
Expand Down Expand Up @@ -131,7 +131,7 @@ describe('PowerPod', function () {
expect(await whitelistRegistry.getWhitelist()).to.not.contain(owner.address);
});

it('should decrease delegatee balance, if delegator delegate to other account', async function () {
it('should decrease delegatee balance, if delegator delegate to others account', async function () {
ZumZoom marked this conversation as resolved.
Show resolved Hide resolved
const {
contracts: { st1inch, delegation, whitelistRegistry },
accounts: { owner, alice, whitelistedUser1 },
Expand Down
Loading