Skip to content

Commit

Permalink
Merge pull request #124 from 1inch/refactor/powerPods_tests
Browse files Browse the repository at this point in the history
[SC-906] Refactor/power pods tests
  • Loading branch information
ZumZoom authored Sep 22, 2023
2 parents 92513c1 + 3e87174 commit 7f04241
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 221 deletions.
129 changes: 0 additions & 129 deletions test/DelegationSt1inch.js

This file was deleted.

12 changes: 4 additions & 8 deletions test/FeeBank.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { expect, ether, getPermit } = require('@1inch/solidity-utils');
const { expect, ether, getPermit, deployContract } = require('@1inch/solidity-utils');
const { ethers } = require('hardhat');
const { BigNumber: BN } = require('ethers');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
Expand All @@ -15,16 +15,12 @@ describe('FeeBank', function () {
});

async function initContracts() {
const TokenPermitMock = await ethers.getContractFactory('ERC20PermitMock');
const inch = await TokenPermitMock.deploy('1INCH', '1INCH', addr.address, ether('1000'));
await inch.deployed();
const inch = await deployContract('ERC20PermitMock', ['1INCH', '1INCH', addr.address, ether('1000')]);
const { swap } = await deploySwapTokens();
const SettlementMock = await ethers.getContractFactory('SettlementMock');
const matcher = await SettlementMock.deploy(swap.address, inch.address);
await matcher.deployed();
const matcher = await deployContract('SettlementMock', [swap.address, inch.address]);

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

await inch.transfer(addr1.address, ether('100'));
await inch.approve(feeBank.address, ether('1000'));
Expand Down
11 changes: 4 additions & 7 deletions test/FeeBankCharger.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { expect, ether } = require('@1inch/solidity-utils');
const { expect, ether, deployContract } = require('@1inch/solidity-utils');
const { ethers } = require('hardhat');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');

Expand All @@ -10,12 +10,9 @@ describe('FeeBankCharger', function () {
});

async function initContracts() {
const TokenPermitMock = await ethers.getContractFactory('ERC20PermitMock');
const inch = await TokenPermitMock.deploy('1INCH', '1INCH', addr.address, ether('1000'));
await inch.deployed();
const FeeBankCharger = await ethers.getContractFactory('FeeBankCharger');
const charger = await FeeBankCharger.deploy(inch.address);
await charger.deployed();
const inch = await deployContract('ERC20PermitMock', ['1INCH', '1INCH', addr.address, ether('1000')]);

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

const FeeBank = await ethers.getContractFactory('FeeBank');
const feeBank = FeeBank.attach(await charger.feeBank());
Expand Down
7 changes: 2 additions & 5 deletions test/FusionDetailsMock.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
const { ethers } = require('hardhat');
const { BigNumber } = require('ethers');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { expect, constants, time } = require('@1inch/solidity-utils');
const { expect, constants, time, deployContract } = require('@1inch/solidity-utils');
const { buildFusions } = require('./helpers/fusionUtils');

describe('FusionDetailsMock', function () {
async function initContracts() {
const FusionDetailsMock = await ethers.getContractFactory('FusionDetailsMock');
const fusionDetailsMock = await FusionDetailsMock.deploy();
await fusionDetailsMock.deployed();
const fusionDetailsMock = await deployContract('FusionDetailsMock', []);
return { fusionDetailsMock };
}

Expand Down
141 changes: 114 additions & 27 deletions test/PowerPod.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
const { expect, time, ether } = require('@1inch/solidity-utils');
const { expect, time, ether, deployContract, constants } = require('@1inch/solidity-utils');
const { ethers } = require('hardhat');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { expBase } = require('./helpers/utils');

describe('PowerPod', function () {
let addr, addr1;
let accounts;
const commonLockDuration = time.duration.days('40');
const amountToStake = ether('1');
const COMMON_LOCK_DURATION = time.duration.days('40');
const MAX_WHITELISTED = 3;
const BALANCE_THRESHOLD = 1000; // 10%

const stakeAndRegisterInDelegation = async (st1inch, delegation, user, amount, userIndex) => {
await st1inch.connect(user).deposit(0, commonLockDuration);
await st1inch.connect(user).deposit(0, COMMON_LOCK_DURATION);
await st1inch.depositFor(user.address, amount);
await st1inch.connect(user).addPod(delegation.address);
await delegation
Expand All @@ -22,39 +21,127 @@ describe('PowerPod', function () {
await delegation.connect(user).delegate(user.address);
};

const depositAndDelegateTo = async (st1inch, delegation, from, to, amount, duration = COMMON_LOCK_DURATION) => {
await st1inch.connect(from).deposit(amount, duration);
await st1inch.connect(from).addPod(delegation.address);
await delegation.connect(from).delegate(to);
};

async function initContracts() {
const TokenPermitMock = await ethers.getContractFactory('ERC20PermitMock');
const oneInch = await TokenPermitMock.deploy('1inch', '1inch', addr.address, ether('200'));
await oneInch.deployed();
const accounts = await ethers.getSigners();
const [owner, alice, whitelistedUser1, /* whitelistedUser2 */, /* whitelistedUser3 */, clearAddress] = accounts;

const oneInch = await deployContract('ERC20PermitMock', ['1inch', '1inch', owner.address, ether('200')]);
await oneInch.transfer(alice.address, ether('100'));

const St1inch = await ethers.getContractFactory('St1inch');
const st1inch = await St1inch.deploy(oneInch.address, expBase, addr.address);
await st1inch.deployed();
const st1inch = await deployContract('St1inch', [oneInch.address, expBase, owner.address]);
await oneInch.approve(st1inch.address, ether('100'));
await oneInch.connect(alice).approve(st1inch.address, ether('100'));

const PowerPod = await ethers.getContractFactory('PowerPod');
const delegation = await PowerPod.deploy('PowerPod', 'PP', st1inch.address);
await delegation.deployed();
await stakeAndRegisterInDelegation(st1inch, delegation, addr1, amountToStake, 0);
const delegation = await deployContract('PowerPod', ['PowerPod', 'PP', st1inch.address]);

return { st1inch, delegation };
}
const whitelistRegistry = await deployContract('WhitelistRegistry', [delegation.address, BALANCE_THRESHOLD]);
// fill all whitelist into WhitelistRegistry
for (let i = 0; i < MAX_WHITELISTED; ++i) {
const userIndex = i + 2;
const user = accounts[userIndex];
await stakeAndRegisterInDelegation(st1inch, delegation, user, ether('2') * BigInt(i + 1), userIndex);
await whitelistRegistry.connect(user).register();
}
await stakeAndRegisterInDelegation(st1inch, delegation, owner, ether('1'), 0);

before(async function () {
accounts = await ethers.getSigners();
addr = accounts[0];
addr1 = accounts[1];
});
return {
contracts: { st1inch, delegation, whitelistRegistry },
accounts: { owner, alice, whitelistedUser1, clearAddress },
};
}

describe('Should calculate voting power', function () {
it('for account with 0 balance', async function () {
const { delegation } = await loadFixture(initContracts);
expect(await delegation.votingPowerOf(addr.address)).to.equal(0);
const { contracts: { delegation }, accounts: { clearAddress } } = await loadFixture(initContracts);
expect(await delegation.votingPowerOf(clearAddress.address)).to.equal(0);
});

it('for account with st1inch balance', async function () {
const { st1inch, delegation } = await loadFixture(initContracts);
expect(await delegation.votingPowerOf(addr1.address)).to.equal(await st1inch.votingPowerOf(addr1.address));
const { contracts: { st1inch, delegation }, accounts: { alice } } = await loadFixture(initContracts);
expect(await delegation.votingPowerOf(alice.address)).to.equal(await st1inch.votingPowerOf(alice.address));
});
});

describe('For add to whitelist', function () {
it('should add account, when sum stacked st1inch and deposit st1inch is sufficient', async function () {
const {
contracts: { st1inch, delegation, whitelistRegistry },
accounts: { owner, alice },
} = await loadFixture(initContracts);
// owner shouldn't register becouse his st1inch balance less that all of the whitelisted accounts
await expect(whitelistRegistry.register()).to.be.revertedWithCustomError(
whitelistRegistry,
'BalanceLessThanThreshold',
);
// create other stake and delegate to owner
await depositAndDelegateTo(st1inch, delegation, alice, owner.address, ether('2'));
// register owner into whitelistRegistry and chack that
await whitelistRegistry.register();
expect(await whitelistRegistry.getWhitelist()).to.contain(owner.address);
});

it('should add account, when sum stacked st1inch and deposit st1inch is sufficient (delegate before deposit)', async function () {
const {
contracts: { st1inch, delegation, whitelistRegistry },
accounts: { owner, alice },
} = await loadFixture(initContracts);
// delegate to owner and deposit 1inch
await st1inch.connect(alice).addPod(delegation.address);
await delegation.connect(alice).delegate(owner.address);
await st1inch.connect(alice).deposit(ether('2'), COMMON_LOCK_DURATION);

await whitelistRegistry.register();
});

it('should accrue DelegatedShare token after delegate', async function () {
const {
contracts: { st1inch, delegation },
accounts: { owner, alice },
} = await loadFixture(initContracts);
const DelegatedShare = await ethers.getContractFactory('DelegatedShare');
const delegatedShare = DelegatedShare.attach(await delegation.registration(owner.address));
const balanceDelegated = await delegatedShare.balanceOf(owner.address);
expect(await delegatedShare.totalSupply()).to.equal(balanceDelegated);
expect(await st1inch.balanceOf(owner.address)).to.equal(balanceDelegated);

await depositAndDelegateTo(st1inch, delegation, alice, owner.address, ether('2'));

const balanceDelegator = await delegatedShare.balanceOf(alice.address);
expect(balanceDelegated.mul(await st1inch.balanceOf(alice.address))).to.equal(
balanceDelegator.mul(await st1inch.balanceOf(owner.address)),
);
});

it('should decrease delegatee balance, if delegator undelegate stake', async function () {
const {
contracts: { st1inch, delegation, whitelistRegistry },
accounts: { owner, alice, whitelistedUser1 },
} = await loadFixture(initContracts);
await depositAndDelegateTo(st1inch, delegation, alice, owner.address, ether('8'));
await whitelistRegistry.register();

await delegation.connect(alice).delegate(constants.ZERO_ADDRESS);
await whitelistRegistry.connect(whitelistedUser1).register();
expect(await whitelistRegistry.getWhitelist()).to.not.contain(owner.address);
});

it('should decrease delegatee balance, if delegator delegate to other account', async function () {
const {
contracts: { st1inch, delegation, whitelistRegistry },
accounts: { owner, alice, whitelistedUser1 },
} = await loadFixture(initContracts);
await depositAndDelegateTo(st1inch, delegation, alice, owner.address, ether('8'));
await whitelistRegistry.register();

await delegation.connect(alice).delegate(whitelistedUser1.address);
await whitelistRegistry.connect(whitelistedUser1).register();
expect(await whitelistRegistry.getWhitelist()).to.not.contain(owner.address);
});
});
});
Loading

0 comments on commit 7f04241

Please sign in to comment.