Skip to content

Commit

Permalink
[Plugin] tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fourlen committed May 22, 2024
1 parent 5ad5279 commit e94c913
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 76 deletions.
2 changes: 1 addition & 1 deletion src/plugin/test/AlgebraOracleV1TWAP.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('AlgebraOracleV1TWAP', () => {
tokens = [tokensFixtureRes.token0, tokensFixtureRes.token1];

const mockPluginFactoryFactory = await ethers.getContractFactory('MockTimeDSFactory');
const _mockPluginFactory = await mockPluginFactoryFactory.deploy(ZERO_ADDRESS);
const _mockPluginFactory = await mockPluginFactoryFactory.deploy(ZERO_ADDRESS, ZERO_ADDRESS, ZERO_ADDRESS, ZERO_ADDRESS);

const algebraOracleV1TWAPFactory = await ethers.getContractFactory('AlgebraOracleV1TWAP');
const _algebraOracleV1TWAP = await algebraOracleV1TWAPFactory.deploy(_mockPluginFactory);
Expand Down
55 changes: 40 additions & 15 deletions src/plugin/test/AlgebraPool.gas.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ethers } from 'hardhat';
import { Wallet } from 'ethers';
import { loadFixture, reset as resetNetwork } from '@nomicfoundation/hardhat-network-helpers';
import { MockTimeAlgebraPool } from '../../core/typechain';
import { MockTimeAlgebraBasePluginV1, MockTimeDSFactory, MockTimeVirtualPool } from '../typechain';
import { AlgebraFactory, MockTimeAlgebraPool } from '../../core/typechain';
import { AlgebraModularHub, FarmingModule, FarmingModuleFactory, MockTimeAlgebraBasePluginV1, MockTimeDSFactory, MockTimeDynamicFeeModule, MockTimeOracleModule, MockTimeVirtualPool } from '../typechain';
import { expect } from './shared/expect';

import { algebraPoolDeployerMockFixture } from './shared/externalFixtures';
Expand Down Expand Up @@ -41,21 +41,37 @@ describe('AlgebraPool gas tests [ @skip-on-coverage ]', () => {
async function gasTestFixture() {
const fix = await algebraPoolDeployerMockFixture();
const pool = await fix.createPool();
const farmingModuleFactory = fix.farmingModuleFactory;

const mockPluginFactoryFactory = await ethers.getContractFactory('MockTimeDSFactory');
const mockPluginFactory = (await mockPluginFactoryFactory.deploy(fix.factory)) as any as MockTimeDSFactory;
const mockPluginFactory = (await mockPluginFactoryFactory.deploy(fix.factory, fix.dynamicFeeModuleFactory, fix.farmingModuleFactory, fix.mockTimeOracleModuleFactory)) as any as MockTimeDSFactory;

await fix.factory.grantRole(ethers.keccak256(ethers.toUtf8Bytes("POOLS_ADMINISTRATOR")), mockPluginFactory);
await fix.factory.grantRole(ethers.keccak256(ethers.toUtf8Bytes("ALGEBRA_BASE_PLUGIN_FACTORY_ADMINISTRATOR")), mockPluginFactory);

await mockPluginFactory.createPlugin(pool, ZERO_ADDRESS, ZERO_ADDRESS);
const pluginAddress = await mockPluginFactory.pluginByPool(pool);

const mockDSOperatorFactory = await ethers.getContractFactory('MockTimeAlgebraBasePluginV1');
const plugin = mockDSOperatorFactory.attach(pluginAddress) as any as MockTimeAlgebraBasePluginV1;

await pool.setPlugin(plugin);
const mockDSOperatorFactory = await ethers.getContractFactory('AlgebraModularHub');
const plugin = mockDSOperatorFactory.attach(pluginAddress) as any as AlgebraModularHub;

const mockTimeOracleModuleAddress = await plugin.modules(1);
const dynamicFeeModuleAddress = await plugin.modules(2);
const farmingModuleAddress = await plugin.modules(3);

const mockTimeOracleModuleFactory_ethers = await ethers.getContractFactory('MockTimeOracleModule');
const mockOracleModule = mockTimeOracleModuleFactory_ethers.attach(mockTimeOracleModuleAddress) as any as MockTimeOracleModule;

const mockTimeDynamicFeeModule_ethers = await ethers.getContractFactory('MockTimeDynamicFeeModule');
const mockDynamicFeeModule = mockTimeDynamicFeeModule_ethers.attach(dynamicFeeModuleAddress) as any as MockTimeDynamicFeeModule;

const farmingModuleFactory_ethers = await ethers.getContractFactory('FarmingModule');
const mockFarmingModule = farmingModuleFactory_ethers.attach(farmingModuleAddress) as any as FarmingModule;

const advanceTime = async (secs: any) => {
await pool.advanceTime(secs);
await plugin.advanceTime(secs);
await mockOracleModule.advanceTime(secs);
await mockDynamicFeeModule.advanceTime(secs);
};

const { swapExact0For1, swapExact1For0, swapToHigherPrice, mint, swapToLowerPrice } = createPoolFunctions({
Expand Down Expand Up @@ -86,6 +102,10 @@ describe('AlgebraPool gas tests [ @skip-on-coverage ]', () => {
plugin,
virtualPoolMock,
mockPluginFactory,
mockOracleModule,
mockDynamicFeeModule,
mockFarmingModule,
farmingModuleFactory,
swapExact0For1,
swapExact1For0,
mint,
Expand All @@ -98,9 +118,13 @@ describe('AlgebraPool gas tests [ @skip-on-coverage ]', () => {
let swapExact1For0: SwapFunction;
let swapToHigherPrice: SwapToPriceFunction;
let pool: MockTimeAlgebraPool;
let plugin: MockTimeAlgebraBasePluginV1;
let plugin: AlgebraModularHub;
let virtualPoolMock: MockTimeVirtualPool;
let mockPluginFactory: MockTimeDSFactory;
let mockOracleModule: MockTimeOracleModule;
let mockDynamicFeeModule: MockTimeDynamicFeeModule;
let mockFarmingModule: FarmingModule;
let farmingModuleFactory: FarmingModuleFactory;
let mint: MintFunction;
let advanceTime: any;

Expand All @@ -122,7 +146,8 @@ describe('AlgebraPool gas tests [ @skip-on-coverage ]', () => {
};

beforeEach('load the fixture', async () => {
({ advanceTime, swapExact0For1, swapExact1For0, pool, plugin, virtualPoolMock, mockPluginFactory, mint, swapToHigherPrice } =
({ advanceTime, swapExact0For1, swapExact1For0, pool, plugin, virtualPoolMock, mockPluginFactory, mockOracleModule, mockDynamicFeeModule,
mockFarmingModule, farmingModuleFactory, mint, swapToHigherPrice } =
await loadFixture(gasTestCommunityFeeFixture));
});

Expand Down Expand Up @@ -155,7 +180,7 @@ describe('AlgebraPool gas tests [ @skip-on-coverage ]', () => {
});

it('first swap in block with no tick movement, static fee', async () => {
await plugin.changeFeeConfiguration({
await mockDynamicFeeModule.changeFeeConfiguration({
alpha1: 0,
alpha2: 0,
beta1: 0,
Expand Down Expand Up @@ -287,8 +312,8 @@ describe('AlgebraPool gas tests [ @skip-on-coverage ]', () => {

describe('farming connected', async () => {
beforeEach('connect virtual pool', async () => {
await mockPluginFactory.setFarmingAddress(wallet);
await plugin.connect(wallet).setIncentive(virtualPoolMock);
await farmingModuleFactory.setFarmingAddress(wallet);
await mockFarmingModule.connect(wallet).setIncentive(virtualPoolMock);
});

it('first swap in block with no tick movement', async () => {
Expand Down Expand Up @@ -457,7 +482,7 @@ describe('AlgebraPool gas tests [ @skip-on-coverage ]', () => {
tick: startingTick + i - j,
});
}
await fix.plugin.batchUpdate(batch);
await fix.mockOracleModule.batchUpdate(batch);
}
await fix.pool.advanceTime(summaryTimeDelta);

Expand All @@ -475,7 +500,7 @@ describe('AlgebraPool gas tests [ @skip-on-coverage ]', () => {
describe(isDynamicFee ? 'dynamic fee' : 'static fee', async () => {
beforeEach(async () => {
if (!isDynamicFee) {
await plugin.changeFeeConfiguration({
await mockDynamicFeeModule.changeFeeConfiguration({
alpha1: 0,
alpha2: 0,
beta1: 0,
Expand Down
22 changes: 13 additions & 9 deletions src/plugin/test/BasePluginV1Factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ describe('BasePluginV1Factory', () => {
await mockPool.setPluginFactory(pluginFactoryMock);

await mockAlgebraFactory.grantRole(ethers.keccak256(ethers.toUtf8Bytes("POOLS_ADMINISTRATOR")), pluginFactoryMock);
await mockAlgebraFactory.grantRole(ethers.keccak256(ethers.toUtf8Bytes("ALGEBRA_BASE_PLUGIN_FACTORY_ADMINISTRATOR")), pluginFactoryMock);
const pluginAddress = await mockAlgebraFactory.createPlugin.staticCall(pluginFactoryMock, mockPool);

await mockAlgebraFactory.createPlugin(pluginFactoryMock, mockPool);

const pluginMock = (await ethers.getContractFactory('AlgebraModularHub')).attach(pluginAddress) as any as AlgebraModularHub;
Expand Down Expand Up @@ -81,6 +83,7 @@ describe('BasePluginV1Factory', () => {
await mockAlgebraFactory.stubPool(token0, token1, mockPool);

await mockAlgebraFactory.grantRole(ethers.keccak256(ethers.toUtf8Bytes("POOLS_ADMINISTRATOR")), pluginFactory);
await mockAlgebraFactory.grantRole(ethers.keccak256(ethers.toUtf8Bytes("ALGEBRA_BASE_PLUGIN_FACTORY_ADMINISTRATOR")), pluginFactory);

await pluginFactory.createPluginForExistingPool(token0, token1);
const pluginAddress = await pluginFactory.pluginByPool(mockPool);
Expand Down Expand Up @@ -112,6 +115,7 @@ describe('BasePluginV1Factory', () => {
await mockAlgebraFactory.stubPool(token0, token1, mockPool);

await mockAlgebraFactory.grantRole(ethers.keccak256(ethers.toUtf8Bytes("POOLS_ADMINISTRATOR")), pluginFactory);
await mockAlgebraFactory.grantRole(ethers.keccak256(ethers.toUtf8Bytes("ALGEBRA_BASE_PLUGIN_FACTORY_ADMINISTRATOR")), pluginFactory);

await pluginFactory.createPluginForExistingPool(token0, token1);

Expand All @@ -131,13 +135,13 @@ describe('BasePluginV1Factory', () => {
baseFee: 150,
};
it('fails if caller is not owner', async () => {
await expect(pluginFactory.connect(other).setDefaultFeeConfiguration(configuration)).to.be.revertedWith('Only administrator');
await expect(mockDynamicFeeModuleFactory.connect(other).setDefaultFeeConfiguration(configuration)).to.be.revertedWith('Only administrator');
});

it('updates defaultFeeConfiguration', async () => {
await pluginFactory.setDefaultFeeConfiguration(configuration);
await mockDynamicFeeModuleFactory.setDefaultFeeConfiguration(configuration);

const newConfig = await pluginFactory.defaultFeeConfiguration();
const newConfig = await mockDynamicFeeModuleFactory.defaultFeeConfiguration();

expect(newConfig.alpha1).to.eq(configuration.alpha1);
expect(newConfig.alpha2).to.eq(configuration.alpha2);
Expand All @@ -149,8 +153,8 @@ describe('BasePluginV1Factory', () => {
});

it('emits event', async () => {
await expect(pluginFactory.setDefaultFeeConfiguration(configuration))
.to.emit(pluginFactory, 'DefaultFeeConfiguration')
await expect(mockDynamicFeeModuleFactory.setDefaultFeeConfiguration(configuration))
.to.emit(mockDynamicFeeModuleFactory, 'DefaultFeeConfiguration')
.withArgs([
configuration.alpha1,
configuration.alpha2,
Expand All @@ -167,22 +171,22 @@ describe('BasePluginV1Factory', () => {
conf2.alpha1 = 30000;
conf2.alpha2 = 30000;
conf2.baseFee = 15000;
await expect(pluginFactory.setDefaultFeeConfiguration(conf2)).to.be.revertedWith('Max fee exceeded');
await expect(mockDynamicFeeModuleFactory.setDefaultFeeConfiguration(conf2)).to.be.revertedWith('Max fee exceeded');
});

it('cannot set zero gamma', async () => {
let conf2 = { ...configuration };
conf2.gamma1 = 0;
await expect(pluginFactory.setDefaultFeeConfiguration(conf2)).to.be.revertedWith('Gammas must be > 0');
await expect(mockDynamicFeeModuleFactory.setDefaultFeeConfiguration(conf2)).to.be.revertedWith('Gammas must be > 0');

conf2 = { ...configuration };
conf2.gamma2 = 0;
await expect(pluginFactory.setDefaultFeeConfiguration(conf2)).to.be.revertedWith('Gammas must be > 0');
await expect(mockDynamicFeeModuleFactory.setDefaultFeeConfiguration(conf2)).to.be.revertedWith('Gammas must be > 0');

conf2 = { ...configuration };
conf2.gamma1 = 0;
conf2.gamma2 = 0;
await expect(pluginFactory.setDefaultFeeConfiguration(conf2)).to.be.revertedWith('Gammas must be > 0');
await expect(mockDynamicFeeModuleFactory.setDefaultFeeConfiguration(conf2)).to.be.revertedWith('Gammas must be > 0');
});
});
});
Expand Down
Loading

0 comments on commit e94c913

Please sign in to comment.