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

[Plugin] plugins to modules #114

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions src/core/contracts/AlgebraFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ contract AlgebraFactory is IAlgebraFactory, Ownable2Step, AccessControlEnumerabl

pool = IAlgebraPoolDeployer(poolDeployer).deploy(plugin, token0, token1, deployer);

if (deployer == address(0) && address(defaultPluginFactory) != address(0)) {
defaultPluginFactory.afterCreatePoolHook(plugin, pool, deployer);
}

_poolByPair[token0][token1] = pool;
_poolByPair[token1][token0] = pool;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ interface IAlgebraPluginFactory {
address token1,
bytes calldata data
) external returns (address);

function afterCreatePoolHook(
address modularHub,
address pool,
address deployer
) external;
}
6 changes: 6 additions & 0 deletions src/core/contracts/test/MockCustomPoolCreator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ contract MockCustomPoolCreator is IAlgebraPluginFactory {
emit BeforeCreateHook(pool, creator, deployer, token0, token1, data);
}

function afterCreatePoolHook(
address modularHub,
address pool,
address deployer
) external override {}

function createCustomPool(address factory, address tokenA, address tokenB, bytes calldata data) external returns (address pool) {
pool = IAlgebraFactory(factory).createCustomPool(address(this), msg.sender, tokenA, tokenB, data);
}
Expand Down
6 changes: 6 additions & 0 deletions src/core/contracts/test/MockDefaultPluginFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ contract MockDefaultPluginFactory is IAlgebraPluginFactory {
plugin = address(new MockPoolPlugin(pool));
pluginsForPools[pool] = plugin;
}

function afterCreatePoolHook(
address modularHub,
address pool,
address deployer
) external override {}
}
10 changes: 5 additions & 5 deletions src/core/test/__snapshots__/AlgebraFactory.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AlgebraFactory #createCustomPool gas [ @skip-on-coverage ] 1`] = `4833064`;
exports[`AlgebraFactory #createCustomPool gas [ @skip-on-coverage ] 1`] = `4833110`;

exports[`AlgebraFactory #createCustomPool gas for second pool [ @skip-on-coverage ] 1`] = `4833064`;
exports[`AlgebraFactory #createCustomPool gas for second pool [ @skip-on-coverage ] 1`] = `4833110`;

exports[`AlgebraFactory #createPool gas [ @skip-on-coverage ] 1`] = `4820775`;
exports[`AlgebraFactory #createPool gas [ @skip-on-coverage ] 1`] = `4820938`;

exports[`AlgebraFactory #createPool gas for second pool [ @skip-on-coverage ] 1`] = `4820775`;
exports[`AlgebraFactory #createPool gas for second pool [ @skip-on-coverage ] 1`] = `4820938`;

exports[`AlgebraFactory factory bytecode size [ @skip-on-coverage ] 1`] = `10254`;
exports[`AlgebraFactory factory bytecode size [ @skip-on-coverage ] 1`] = `10467`;

exports[`AlgebraFactory pool bytecode size [ @skip-on-coverage ] 1`] = `22844`;
3 changes: 2 additions & 1 deletion src/farming/contracts/FarmingCenter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import '@cryptoalgebra/integral-periphery/contracts/interfaces/INonfungiblePosit
import '@cryptoalgebra/integral-periphery/contracts/base/Multicall.sol';
import '@cryptoalgebra/integral-periphery/contracts/libraries/PoolAddress.sol';
import '@cryptoalgebra/integral-base-plugin/contracts/interfaces/plugins/IFarmingPlugin.sol';
import '@cryptoalgebra/integral-base-plugin/contracts/modules/FarmingModule.sol';

import './interfaces/IFarmingCenter.sol';
import './libraries/IncentiveId.sol';
Expand Down Expand Up @@ -139,7 +140,7 @@ contract FarmingCenter is IFarmingCenter, IPositionFollower, Multicall {
function _checkParamsForVirtualPoolToggle(address virtualPool, IFarmingPlugin plugin) internal view returns (IAlgebraPool pool) {
require(msg.sender == address(eternalFarming), 'Only farming can call this');
require(virtualPool != address(0), 'Zero address as virtual pool');
pool = IAlgebraPool(plugin.pool());
pool = IAlgebraPool(FarmingModule(address(plugin)).pool());
require(address(pool) == PoolAddress.computeAddress(algebraPoolDeployer, PoolAddress.PoolKey(pool.token0(), pool.token1())), 'Invalid pool');
}
}
16 changes: 9 additions & 7 deletions src/farming/contracts/farmings/AlgebraEternalFarming.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import '@cryptoalgebra/integral-periphery/contracts/libraries/TransferHelper.sol

import '@cryptoalgebra/integral-base-plugin/contracts/interfaces/plugins/IFarmingPlugin.sol';

import '@cryptoalgebra/algebra-modular-hub-v0.8.20/contracts/interfaces/IAlgebraModularHub.sol';

import '../interfaces/IAlgebraEternalFarming.sol';
import '../interfaces/IAlgebraEternalVirtualPool.sol';
import '../interfaces/IFarmingCenter.sol';
Expand Down Expand Up @@ -121,14 +123,14 @@ contract AlgebraEternalFarming is IAlgebraEternalFarming {
function createEternalFarming(
IncentiveKey memory key,
IncentiveParams memory params,
address plugin
address farmingModule
) external override onlyIncentiveMaker returns (address virtualPool) {
address connectedPlugin = key.pool.plugin();
if (connectedPlugin != plugin || connectedPlugin == address(0)) revert pluginNotConnected();
if (IFarmingPlugin(connectedPlugin).incentive() != address(0)) revert anotherFarmingIsActive();
address modularHubAddress = key.pool.plugin();
if (modularHubAddress == address(0) || IAlgebraModularHub(modularHubAddress).moduleAddressToIndex(farmingModule) == 0) revert pluginNotConnected();
if (IFarmingPlugin(farmingModule).incentive() != address(0)) revert anotherFarmingIsActive();

virtualPool = address(new EternalVirtualPool(address(this), connectedPlugin));
IFarmingCenter(farmingCenter).connectVirtualPoolToPlugin(virtualPool, IFarmingPlugin(connectedPlugin));
virtualPool = address(new EternalVirtualPool(address(this), farmingModule));
IFarmingCenter(farmingCenter).connectVirtualPoolToPlugin(virtualPool, IFarmingPlugin(farmingModule));

key.nonce = numOfIncentives++;
bytes32 incentiveId = IncentiveId.compute(key);
Expand All @@ -143,7 +145,7 @@ contract AlgebraEternalFarming is IAlgebraEternalFarming {
}
newIncentive.virtualPoolAddress = virtualPool;
newIncentive.minimalPositionWidth = params.minimalPositionWidth;
newIncentive.pluginAddress = connectedPlugin;
newIncentive.pluginAddress = farmingModule;

emit EternalFarmingCreated(
key.rewardToken,
Expand Down
21 changes: 21 additions & 0 deletions src/farming/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/farming/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"@openzeppelin/contracts": "4.9.3",
"@cryptoalgebra/integral-core": "1.1.0",
"@cryptoalgebra/integral-periphery": "1.1.0",
"@cryptoalgebra/integral-base-plugin": "1.1.0"
"@cryptoalgebra/integral-base-plugin": "1.1.0",
"@cryptoalgebra/algebra-modular-hub-v0.8.20": "0.1.10"
},
"devDependencies": {
"@types/lodash": "^4.14.170",
Expand Down
11 changes: 8 additions & 3 deletions src/farming/test/AlgebraFarming.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ describe('AlgebraFarming', () => {
await context.rewardToken.connect(incentiveCreator).approve(context.eternalFarming, totalReward);
await context.bonusRewardToken.connect(incentiveCreator).approve(context.eternalFarming, bonusReward);

// проблема: при замене модуля в модулар хабе, он не заменится в фабрике
// console.log('farming ', await context.farmingModuleFactory.poolToPlugin(context.pool01));

await expect(
(context.eternalFarming as AlgebraEternalFarming).connect(incentiveCreator).createEternalFarming(
{
Expand All @@ -107,7 +110,7 @@ describe('AlgebraFarming', () => {
bonusRewardRate: 10,
minimalPositionWidth: 2 ** 23 - 1 + 2 ** 23 - 1,
},
await context.poolObj.connect(incentiveCreator).plugin()
await context.farmingModuleFactory.poolToPlugin(context.pool01)
)
).to.be.revertedWithCustomError(context.eternalFarming as AlgebraEternalFarming, 'minimalPositionWidthTooWide');

Expand All @@ -126,7 +129,7 @@ describe('AlgebraFarming', () => {
bonusRewardRate: 10,
minimalPositionWidth: 887272 * 2 + 1,
},
await context.poolObj.connect(incentiveCreator).plugin()
await context.farmingModuleFactory.poolToPlugin(context.pool01)
)
).to.be.revertedWithCustomError(context.eternalFarming as AlgebraEternalFarming, 'minimalPositionWidthTooWide');

Expand All @@ -145,7 +148,7 @@ describe('AlgebraFarming', () => {
bonusRewardRate: 10,
minimalPositionWidth: (887272 - (887272 % 60)) * 2,
},
await context.poolObj.connect(incentiveCreator).plugin()
await context.farmingModuleFactory.poolToPlugin(context.pool01)
)
).to.be.not.reverted;
});
Expand Down Expand Up @@ -203,6 +206,7 @@ describe('AlgebraFarming', () => {
bonusRewardToken: context.bonusRewardToken,
minimalPositionWidth: getMaxTick(TICK_SPACINGS[FeeAmount.MEDIUM]) - getMinTick(TICK_SPACINGS[FeeAmount.MEDIUM]),
poolAddress: context.pool01,
farmingModuleFactory: context.farmingModuleFactory,
totalReward,
bonusReward,
});
Expand Down Expand Up @@ -254,6 +258,7 @@ describe('AlgebraFarming', () => {
rewardToken,
bonusRewardToken,
poolAddress: context.pool01,
farmingModuleFactory: context.farmingModuleFactory,
totalReward,
bonusReward,
});
Expand Down
7 changes: 4 additions & 3 deletions src/farming/test/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ethers } from 'hardhat';
import { ContractParams } from '../../types/contractParams';
import { TestContext } from '../types';
import Decimal from 'decimal.js';
import { IAlgebraFarmingModuleFactory } from '@cryptoalgebra/integral-base-plugin/typechain';

/***
* HelperCommands is a utility that abstracts away lower-tier ethereum details
Expand Down Expand Up @@ -104,9 +105,9 @@ export class HelperCommands {
await params.bonusRewardToken.connect(incentiveCreator).approve(this.eternalFarming, params.bonusReward);

let pluginAddres = params.plugin;

if (!pluginAddres) {
const pool = (await ethers.getContractAt('IAlgebraPool', params.poolAddress)) as any as IAlgebraPool;
pluginAddres = await pool.connect(incentiveCreator).plugin();
pluginAddres = await params.farmingModuleFactory.poolToPlugin(params.poolAddress);
}

txResult = await (this.eternalFarming as AlgebraEternalFarming).connect(incentiveCreator).createEternalFarming(
Expand All @@ -126,7 +127,7 @@ export class HelperCommands {
pluginAddres
);
// @ts-ignore
virtualPoolAddress = (await txResult.wait(1)).logs[4].args['virtualPool'];
virtualPoolAddress = (await txResult.wait(1)).logs[3].args['virtualPool'];

return {
..._.pick(params, ['poolAddress', 'totalReward', 'bonusReward', 'rewardToken', 'bonusRewardToken']),
Expand Down
4 changes: 3 additions & 1 deletion src/farming/test/helpers/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Wallet, Contract, ContractTransactionResponse } from 'ethers';
import { TestERC20 } from '../../typechain';
import { TestERC20, IAlgebraFarmingModuleFactory } from '../../typechain';
import { FeeAmount } from '../shared';

export module HelperTypes {
Expand All @@ -13,6 +13,7 @@ export module HelperTypes {
nonce: bigint;
totalReward: bigint;
bonusReward: bigint;
farmingModuleFactory: IAlgebraFarmingModuleFactory;

rewardRate?: bigint;
bonusRewardRate?: bigint;
Expand All @@ -39,6 +40,7 @@ export module HelperTypes {
amountsToFarm: [bigint, bigint];
ticks: [number, number];
createIncentiveResult: CreateIncentive.Result;
farmingModuleFactory: IAlgebraFarmingModuleFactory;
};

export type Result = {
Expand Down
Loading
Loading