Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
noisekit committed Apr 11, 2024
1 parent 3cff504 commit b62d973
Showing 1 changed file with 131 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'node:assert';
import assertBn from '@synthetixio/core-utils/utils/assertions/assert-bignumber';
import assertRevert from '@synthetixio/core-utils/utils/assertions/assert-revert';
import { snapshotCheckpoint } from '@synthetixio/core-utils/utils/mocha/snapshot';
import { ethers } from 'ethers';
import hre from 'hardhat';
import { bootstrapWithMockMarketAndPool } from '../../bootstrap';
Expand All @@ -27,13 +26,27 @@ describe('PoolModule Admin setPoolConfiguration()', function () {
const One = ethers.utils.parseEther('1');
const Hundred = ethers.utils.parseEther('100');

before('identify signers', async () => {
const marketId2 = 2;

before(async () => {
[owner, user1, user2] = signers();
});

const marketId2 = 2;
// give user1 permission to create pool
await systems()
.Core.connect(owner)
.addToFeatureFlagAllowlist(
ethers.utils.formatBytes32String('createPool'),
await user1.getAddress()
);

before('set dummy markets', async () => {
// create a pool
await (
await systems()
.Core.connect(user1)
.createPool(secondPoolId, await user1.getAddress())
).wait();

// set dummy markets
const factory = await hre.ethers.getContractFactory('MockMarket');
const MockMarket2 = await factory.connect(owner).deploy();
const MockMarket3 = await factory.connect(owner).deploy();
Expand All @@ -43,8 +56,6 @@ describe('PoolModule Admin setPoolConfiguration()', function () {
await (await systems().Core.connect(owner).registerMarket(MockMarket3.address)).wait();
});

const restore = snapshotCheckpoint(provider);

it('reverts when pool does not exist', async () => {
await assertRevert(
systems()
Expand Down Expand Up @@ -115,7 +126,13 @@ describe('PoolModule Admin setPoolConfiguration()', function () {
});

describe('repeat pool sets position', async () => {
before(restore);
let snapshotId: number;
before(async () => {
snapshotId = await provider().send('evm_snapshot', []);
});
after(async () => {
await provider().send('evm_revert', [snapshotId]);
});

before('set pool position', async () => {
await systems()
Expand All @@ -133,11 +150,14 @@ describe('PoolModule Admin setPoolConfiguration()', function () {
});

describe('if one of the markets has a min delegation time', () => {
const restore = snapshotCheckpoint(provider);

let snapshotId: number;
before('set market min delegation time to something high', async () => {
snapshotId = await provider().send('evm_snapshot', []);
await MockMarket().setMinDelegationTime(86400);
});
after(async () => {
await provider().send('evm_revert', [snapshotId]);
});

it('fails when min delegation timeout not elapsed', async () => {
await assertRevert(
Expand All @@ -152,23 +172,17 @@ describe('PoolModule Admin setPoolConfiguration()', function () {
);
});

describe('after time passes', () => {
before('fast forward', async () => {
// for some reason `fastForward` doesn't seem to work with anvil
await fastForwardTo((await getTime(provider())) + 86400, provider());
});
it('works after time passes', async () => {
// for some reason `fastForward` doesn't seem to work with anvil
await fastForwardTo((await getTime(provider())) + 86400, provider());

it('works', async () => {
await systems()
.Core.connect(owner)
.setPoolConfiguration(poolId, [
{ marketId: marketId(), weightD18: 1, maxDebtShareValueD18: One },
{ marketId: marketId2, weightD18: 3, maxDebtShareValueD18: One },
]);
});
await systems()
.Core.connect(owner)
.setPoolConfiguration(poolId, [
{ marketId: marketId(), weightD18: 1, maxDebtShareValueD18: One },
{ marketId: marketId2, weightD18: 3, maxDebtShareValueD18: One },
]);
});

after(restore);
});

describe('pool changes staking position to add another market', async () => {
Expand Down Expand Up @@ -266,100 +280,100 @@ describe('PoolModule Admin setPoolConfiguration()', function () {
await MockMarket().setLocked(ethers.utils.parseEther('0'));
});

describe('exit first market', () => {
const restore = snapshotCheckpoint(provider);
it('exit first market', async () => {
const snapshotId = await provider().send('evm_snapshot', []);

before('set pool position', async () => {
await systems()
.Core.connect(owner)
.setPoolConfiguration(poolId, [
{ marketId: marketId2, weightD18: 3, maxDebtShareValueD18: One },
]);
});
// set pool position
await systems()
.Core.connect(owner)
.setPoolConfiguration(poolId, [
{ marketId: marketId2, weightD18: 3, maxDebtShareValueD18: One },
]);

it('returns pool position correctly', async () => {
const distributions = await systems().Core.getPoolConfiguration(poolId);
// returns pool position correctly
const distributions = await systems().Core.getPoolConfiguration(poolId);

assertBn.equal(distributions[0].marketId, marketId2);
assertBn.equal(distributions[0].weightD18, 3);
assertBn.equal(distributions[0].maxDebtShareValueD18, One);
});
assertBn.equal(distributions[0].marketId, marketId2);
assertBn.equal(distributions[0].weightD18, 3);
assertBn.equal(distributions[0].maxDebtShareValueD18, One);

it('markets have same available liquidity', async () => {
// marketId() gets to keep its available liquidity because when
// the market exited when it did it "committed"
assertBn.equal(
await systems().Core.connect(owner).getWithdrawableMarketUsd(marketId()),
debtAmount
);
// markets have same available liquidity
// marketId() gets to keep its available liquidity because when
// the market exited when it did it "committed"
assertBn.equal(
await systems().Core.connect(owner).getWithdrawableMarketUsd(marketId()),
debtAmount
);

assertBn.equal(await systems().Core.connect(owner).getMarketCollateral(marketId()), 0);
});
assertBn.equal(await systems().Core.connect(owner).getMarketCollateral(marketId()), 0);

after(restore);
await provider().send('evm_revert', [snapshotId]);
});

describe('exit second market', () => {
const restore = snapshotCheckpoint(provider);

before('set pool position', async () => {
await systems()
.Core.connect(owner)
.setPoolConfiguration(poolId, [
{ marketId: marketId(), weightD18: 2, maxDebtShareValueD18: One.mul(2) },
]);
});
it('exit second market', async () => {
const snapshotId = await provider().send('evm_snapshot', []);
// set pool position
await systems()
.Core.connect(owner)
.setPoolConfiguration(poolId, [
{ marketId: marketId(), weightD18: 2, maxDebtShareValueD18: One.mul(2) },
]);

it('returns pool position correctly', async () => {
const distributions = await systems().Core.getPoolConfiguration(poolId);
// returns pool position correctly
const distributions = await systems().Core.getPoolConfiguration(poolId);

assertBn.equal(distributions[0].marketId, marketId());
assertBn.equal(distributions[0].weightD18, 2);
assertBn.equal(distributions[0].maxDebtShareValueD18, One.mul(2));
});
assertBn.equal(distributions[0].marketId, marketId());
assertBn.equal(distributions[0].weightD18, 2);
assertBn.equal(distributions[0].maxDebtShareValueD18, One.mul(2));

it('available liquidity taken away from second market', async () => {
// marketId2 never reported an increased balance so its liquidity is 0 as ever
assertBn.equal(await systems().Core.connect(owner).getMarketCollateral(marketId2), 0);
});
// available liquidity taken away from second market
// marketId2 never reported an increased balance so its liquidity is 0 as ever
assertBn.equal(await systems().Core.connect(owner).getMarketCollateral(marketId2), 0);

after(restore);
await provider().send('evm_revert', [snapshotId]);
});

describe('exit both market', () => {
before('set pool position', async () => {
await systems().Core.connect(owner).setPoolConfiguration(poolId, []);
});
it('exit both market', async () => {
const snapshotId = await provider().send('evm_snapshot', []);

it('returns pool position correctly', async () => {
assert.deepEqual(await systems().Core.getPoolConfiguration(poolId), []);
});
// set pool position
await systems().Core.connect(owner).setPoolConfiguration(poolId, []);

it('debt is still assigned', async () => {
assertBn.equal(
await systems().Core.callStatic.getVaultDebt(poolId, collateralAddress()),
debtAmount
);
});
// returns pool position correctly
assert.deepEqual(await systems().Core.getPoolConfiguration(poolId), []);

it('markets have same available liquidity', async () => {
// marketId() gets to keep its available liquidity because when
// the market exited when it did it "committed"
assertBn.equal(
await systems().Core.connect(owner).getWithdrawableMarketUsd(marketId()),
debtAmount
);
// debt is still assigned
assertBn.equal(
await systems().Core.callStatic.getVaultDebt(poolId, collateralAddress()),
debtAmount
);

// marketId2 never reported an increased balance so its liquidity is 0 as ever
assertBn.equal(await systems().Core.connect(owner).getMarketCollateral(marketId2), 0);
});
// markets have same available liquidity

// marketId() gets to keep its available liquidity because when
// the market exited when it did it "committed"
assertBn.equal(
await systems().Core.connect(owner).getWithdrawableMarketUsd(marketId()),
debtAmount
);

// marketId2 never reported an increased balance so its liquidity is 0 as ever
assertBn.equal(await systems().Core.connect(owner).getMarketCollateral(marketId2), 0);

await provider().send('evm_revert', [snapshotId]);
});
});
});
});

describe('sets max debt below current debt share', async () => {
before(restore);
let snapshotId: number;
before(async () => {
snapshotId = await provider().send('evm_snapshot', []);
});
after(async () => {
await provider().send('evm_revert', [snapshotId]);
});

before('raise maxLiquidityRatio', async () => {
const value = ethers.utils.parseEther('0.2');
Expand Down Expand Up @@ -492,10 +506,15 @@ describe('PoolModule Admin setPoolConfiguration()', function () {
);
});

const restore = snapshotCheckpoint(provider);

describe('and then the market reports balance above limit again', () => {
before(restore);
let snapshotId: number;
before(async () => {
snapshotId = await provider().send('evm_snapshot', []);
});
after(async () => {
await provider().send('evm_revert', [snapshotId]);
});

// testing the "soft" limit
before('set market', async () => {
await MockMarket().connect(user1).setReportedDebt(Hundred.add(One));
Expand Down Expand Up @@ -534,7 +553,13 @@ describe('PoolModule Admin setPoolConfiguration()', function () {
});

describe('and then the market reports balance above both pools limits', () => {
before(restore);
let snapshotId: number;
before(async () => {
snapshotId = await provider().send('evm_snapshot', []);
});
after(async () => {
await provider().send('evm_revert', [snapshotId]);
});
// testing the "soft" limit
before('set market', async () => {
await MockMarket().connect(user1).setReportedDebt(Hundred.mul(1234));
Expand Down Expand Up @@ -575,7 +600,13 @@ describe('PoolModule Admin setPoolConfiguration()', function () {
});

describe('when limit is higher than minLiquidityRatio', async () => {
before(restore);
let snapshotId: number;
before(async () => {
snapshotId = await provider().send('evm_snapshot', []);
});
after(async () => {
await provider().send('evm_revert', [snapshotId]);
});

before('set minLiquidityRatio', async () => {
const value = ethers.utils.parseEther('2');
Expand Down

0 comments on commit b62d973

Please sign in to comment.