Skip to content

Commit

Permalink
[Periphery] add plugin init data to createAndInitializePoolIfNecessary
Browse files Browse the repository at this point in the history
  • Loading branch information
IliaAzhel committed Sep 18, 2024
1 parent 05909cf commit 43f761e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 47 deletions.
5 changes: 3 additions & 2 deletions src/periphery/contracts/base/PoolInitializer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ abstract contract PoolInitializer is IPoolInitializer, PeripheryImmutableState {
address token0,
address token1,
address deployer,
uint160 sqrtPriceX96
uint160 sqrtPriceX96,
bytes calldata data
) external payable override returns (address pool) {
require(token0 < token1, 'Invalid order of tokens');

Expand All @@ -32,7 +33,7 @@ abstract contract PoolInitializer is IPoolInitializer, PeripheryImmutableState {

if (pool == address(0)) {
if (deployer == address(0)) {
pool = _factory.createPool(token0, token1, '');
pool = _factory.createPool(token0, token1, data);

_initializePool(pool, sqrtPriceX96);
}
Expand Down
4 changes: 3 additions & 1 deletion src/periphery/contracts/interfaces/IPoolInitializer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ interface IPoolInitializer {
/// @param token0 The contract address of token0 of the pool
/// @param token1 The contract address of token1 of the pool
/// @param sqrtPriceX96 The initial square root price of the pool as a Q64.96 value
/// @param data Data for plugin initialization
/// @return pool Returns the pool address based on the pair of tokens and fee, will return the newly created pool address if necessary
function createAndInitializePoolIfNecessary(
address token0,
address token1,
address deployer,
uint160 sqrtPriceX96
uint160 sqrtPriceX96,
bytes calldata data
) external payable returns (address pool);
}
70 changes: 42 additions & 28 deletions src/periphery/test/NonfungiblePositionManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ describe('NonfungiblePositionManager', () => {
]);
const code = await wallet.provider.getCode(expectedAddress);
expect(code).to.eq('0x');
await nft.createAndInitializePoolIfNecessary(tokens[0], tokens[1], ZERO_ADDRESS, encodePriceSqrt(1, 1));
await nft.createAndInitializePoolIfNecessary(tokens[0], tokens[1], ZERO_ADDRESS, encodePriceSqrt(1, 1), '0x');
const codeAfter = await wallet.provider.getCode(expectedAddress);
expect(codeAfter).to.not.eq('0x');
});

it('is payable', async () => {
await nft.createAndInitializePoolIfNecessary(tokens[0], tokens[1], ZERO_ADDRESS, encodePriceSqrt(1, 1), { value: 1 });
await nft.createAndInitializePoolIfNecessary(tokens[0], tokens[1], ZERO_ADDRESS, encodePriceSqrt(1, 1), '0x', { value: 1 });
});

it('works if pool is created but not initialized', async () => {
Expand All @@ -105,7 +105,7 @@ describe('NonfungiblePositionManager', () => {
await factory.createPool(tokens[0], tokens[1], '0x');
const code = await wallet.provider.getCode(expectedAddress);
expect(code).to.not.eq('0x');
await nft.createAndInitializePoolIfNecessary(tokens[0], tokens[1], ZERO_ADDRESS, encodePriceSqrt(2, 1));
await nft.createAndInitializePoolIfNecessary(tokens[0], tokens[1], ZERO_ADDRESS, encodePriceSqrt(2, 1), '0x');
});

it('works if pool is created and initialized', async () => {
Expand All @@ -121,22 +121,22 @@ describe('NonfungiblePositionManager', () => {
if (!wallet.provider) throw new Error('No provider');
const code = await wallet.provider.getCode(expectedAddress);
expect(code).to.not.eq('0x');
await nft.createAndInitializePoolIfNecessary(tokens[0], tokens[1], ZERO_ADDRESS, encodePriceSqrt(4, 1));
await nft.createAndInitializePoolIfNecessary(tokens[0], tokens[1], ZERO_ADDRESS, encodePriceSqrt(4, 1), '0x');
});

it('could theoretically use eth via multicall', async () => {
const [token0, token1] = await sortedTokens(wnative, tokens[0]);

const createAndInitializePoolIfNecessaryData = nft.interface.encodeFunctionData(
'createAndInitializePoolIfNecessary',
[await token0.getAddress(), await token1.getAddress(), ZERO_ADDRESS, encodePriceSqrt(1, 1)]
[await token0.getAddress(), await token1.getAddress(), ZERO_ADDRESS, encodePriceSqrt(1, 1), '0x']
);

await nft.multicall([createAndInitializePoolIfNecessaryData], { value: expandTo18Decimals(1) });
});

it('gas [ @skip-on-coverage ]', async () => {
await snapshotGasCost(nft.createAndInitializePoolIfNecessary(tokens[0], tokens[1], ZERO_ADDRESS, encodePriceSqrt(1, 1)));
await snapshotGasCost(nft.createAndInitializePoolIfNecessary(tokens[0], tokens[1], ZERO_ADDRESS, encodePriceSqrt(1, 1), '0x'));
});
});

Expand All @@ -160,7 +160,7 @@ describe('NonfungiblePositionManager', () => {
});

it('fails if cannot transfer', async () => {
await nft.createAndInitializePoolIfNecessary(tokens[0], tokens[1], ZERO_ADDRESS, encodePriceSqrt(1, 1));
await nft.createAndInitializePoolIfNecessary(tokens[0], tokens[1], ZERO_ADDRESS, encodePriceSqrt(1, 1), '0x');
await tokens[0].approve(nft, 0);
await expect(
nft.mint({
Expand All @@ -180,7 +180,7 @@ describe('NonfungiblePositionManager', () => {
});

it('fails if deadline passed', async () => {
await nft.createAndInitializePoolIfNecessary(tokens[0], tokens[1], ZERO_ADDRESS, encodePriceSqrt(1, 1));
await nft.createAndInitializePoolIfNecessary(tokens[0], tokens[1], ZERO_ADDRESS, encodePriceSqrt(1, 1), '0x');
await nft.setTime(2);
await expect(
nft.mint({
Expand All @@ -204,7 +204,8 @@ describe('NonfungiblePositionManager', () => {
tokens[0].getAddress(),
tokens[1].getAddress(),
ZERO_ADDRESS,
encodePriceSqrt(1, 1)
encodePriceSqrt(1, 1),
'0x'
);
await nft.mint({
token0: tokens[0].getAddress(),
Expand Down Expand Up @@ -253,7 +254,8 @@ describe('NonfungiblePositionManager', () => {
await token0.getAddress(),
await token1.getAddress(),
ZERO_ADDRESS,
encodePriceSqrt(1, 1),
encodePriceSqrt(1, 1),
'0x'
]);

const mintData = nft.interface.encodeFunctionData('mint', [
Expand Down Expand Up @@ -293,7 +295,8 @@ describe('NonfungiblePositionManager', () => {
tokens[0].getAddress(),
tokens[1].getAddress(),
ZERO_ADDRESS,
encodePriceSqrt(1, 1)
encodePriceSqrt(1, 1),
'0x'
);

await snapshotGasCost(
Expand All @@ -315,7 +318,7 @@ describe('NonfungiblePositionManager', () => {

it('gas first mint for pool using eth with zero refund [ @skip-on-coverage ]', async () => {
const [token0, token1] = await sortedTokens(wnative, tokens[0]);
await nft.createAndInitializePoolIfNecessary(token0, token1, ZERO_ADDRESS, encodePriceSqrt(1, 1));
await nft.createAndInitializePoolIfNecessary(token0, token1, ZERO_ADDRESS, encodePriceSqrt(1, 1), '0x');

await snapshotGasCost(
nft.multicall(
Expand Down Expand Up @@ -344,7 +347,7 @@ describe('NonfungiblePositionManager', () => {

it('gas first mint for pool using eth with non-zero refund [ @skip-on-coverage ]', async () => {
const [token0, token1] = await sortedTokens(wnative, tokens[0]);
await nft.createAndInitializePoolIfNecessary(token0, token1, ZERO_ADDRESS, encodePriceSqrt(1, 1));
await nft.createAndInitializePoolIfNecessary(token0, token1, ZERO_ADDRESS, encodePriceSqrt(1, 1), '0x');

await snapshotGasCost(
nft.multicall(
Expand Down Expand Up @@ -372,7 +375,7 @@ describe('NonfungiblePositionManager', () => {
});

it('gas mint on same ticks [ @skip-on-coverage ]', async () => {
await nft.createAndInitializePoolIfNecessary(tokens[0], tokens[1], ZERO_ADDRESS, encodePriceSqrt(1, 1));
await nft.createAndInitializePoolIfNecessary(tokens[0], tokens[1], ZERO_ADDRESS, encodePriceSqrt(1, 1), '0x');

await nft.mint({
token0: await tokens[0].getAddress(),
Expand Down Expand Up @@ -410,7 +413,8 @@ describe('NonfungiblePositionManager', () => {
tokens[0].getAddress(),
tokens[1].getAddress(),
ZERO_ADDRESS,
encodePriceSqrt(1, 1)
encodePriceSqrt(1, 1),
'0x'
);

await nft.mint({
Expand Down Expand Up @@ -452,7 +456,8 @@ describe('NonfungiblePositionManager', () => {
tokens[0].getAddress(),
tokens[1].getAddress(),
ZERO_ADDRESS,
encodePriceSqrt(1, 1)
encodePriceSqrt(1, 1),
'0x'
);

await nft.mint({
Expand Down Expand Up @@ -523,7 +528,7 @@ describe('NonfungiblePositionManager', () => {

const tokenId = 1;

await nft.createAndInitializePoolIfNecessary(token0, token1, ZERO_ADDRESS, encodePriceSqrt(1, 1));
await nft.createAndInitializePoolIfNecessary(token0, token1, ZERO_ADDRESS, encodePriceSqrt(1, 1), '0x');

const mintData = nft.interface.encodeFunctionData('mint', [
{
Expand Down Expand Up @@ -577,7 +582,8 @@ describe('NonfungiblePositionManager', () => {
tokens[0].getAddress(),
tokens[1].getAddress(),
ZERO_ADDRESS,
encodePriceSqrt(1, 1)
encodePriceSqrt(1, 1),
'0x'
);

await nft.mint({
Expand Down Expand Up @@ -701,7 +707,8 @@ describe('NonfungiblePositionManager', () => {
tokens[0].getAddress(),
tokens[1].getAddress(),
ZERO_ADDRESS,
encodePriceSqrt(1, 1)
encodePriceSqrt(1, 1),
'0x'
);

await nft.mint({
Expand Down Expand Up @@ -849,7 +856,8 @@ describe('NonfungiblePositionManager', () => {
tokens[0].getAddress(),
tokens[1].getAddress(),
ZERO_ADDRESS,
encodePriceSqrt(1, 1)
encodePriceSqrt(1, 1),
'0x'
);

await nft.mint({
Expand Down Expand Up @@ -930,7 +938,8 @@ describe('NonfungiblePositionManager', () => {
tokens[0].getAddress(),
tokens[1].getAddress(),
ZERO_ADDRESS,
encodePriceSqrt(1, 1)
encodePriceSqrt(1, 1),
'0x'
);

await nft.mint({
Expand Down Expand Up @@ -986,7 +995,8 @@ describe('NonfungiblePositionManager', () => {
tokens[0].getAddress(),
tokens[1].getAddress(),
ZERO_ADDRESS,
encodePriceSqrt(1, 1)
encodePriceSqrt(1, 1),
'0x'
);

await nft.mint({
Expand Down Expand Up @@ -1051,7 +1061,8 @@ describe('NonfungiblePositionManager', () => {
tokens[0].getAddress(),
tokens[1].getAddress(),
ZERO_ADDRESS,
encodePriceSqrt(1, 1)
encodePriceSqrt(1, 1),
'0x'
);

await nft.mint({
Expand Down Expand Up @@ -1111,7 +1122,8 @@ describe('NonfungiblePositionManager', () => {
tokens[0].getAddress(),
tokens[1].getAddress(),
ZERO_ADDRESS,
encodePriceSqrt(1, 1)
encodePriceSqrt(1, 1),
'0x'
);

await nft.mint({
Expand Down Expand Up @@ -1200,7 +1212,8 @@ describe('NonfungiblePositionManager', () => {
tokens[0].getAddress(),
tokens[1].getAddress(),
ZERO_ADDRESS,
encodePriceSqrt(1, 1)
encodePriceSqrt(1, 1),
'0x'
);

await nft.mint({
Expand Down Expand Up @@ -1240,7 +1253,8 @@ describe('NonfungiblePositionManager', () => {
tokens[0].getAddress(),
tokens[1].getAddress(),
ZERO_ADDRESS,
encodePriceSqrt(1, 1)
encodePriceSqrt(1, 1),
'0x'
);
// nft 1 earns 25% of fees
await nft.mint({
Expand Down Expand Up @@ -1297,7 +1311,6 @@ describe('NonfungiblePositionManager', () => {
amount0Max: MaxUint128,
amount1Max: MaxUint128,
});
console.log(nft1Amount0.toString(), nft1Amount1.toString(), nft2Amount0.toString(), nft2Amount1.toString());
expect(nft1Amount0).to.eq(416);
expect(nft1Amount1).to.eq(0);
expect(nft2Amount0).to.eq(1250);
Expand Down Expand Up @@ -1343,7 +1356,8 @@ describe('NonfungiblePositionManager', () => {
tokens[0].getAddress(),
tokens[1].getAddress(),
ZERO_ADDRESS,
encodePriceSqrt(1, 1)
encodePriceSqrt(1, 1),
'0x'
);

await nft.mint({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,38 @@

exports[`NonfungiblePositionManager #burn gas [ @skip-on-coverage ] 1`] = `62302`;

exports[`NonfungiblePositionManager #collect gas transfers both [ @skip-on-coverage ] 1`] = `125983`;
exports[`NonfungiblePositionManager #collect gas transfers both [ @skip-on-coverage ] 1`] = `126407`;

exports[`NonfungiblePositionManager #collect gas transfers token0 only [ @skip-on-coverage ] 1`] = `122321`;
exports[`NonfungiblePositionManager #collect gas transfers token0 only [ @skip-on-coverage ] 1`] = `122745`;

exports[`NonfungiblePositionManager #collect gas transfers token1 only [ @skip-on-coverage ] 1`] = `122512`;
exports[`NonfungiblePositionManager #collect gas transfers token1 only [ @skip-on-coverage ] 1`] = `122936`;

exports[`NonfungiblePositionManager #createAndInitializePoolIfNecessary gas [ @skip-on-coverage ] 1`] = `5277310`;
exports[`NonfungiblePositionManager #createAndInitializePoolIfNecessary gas [ @skip-on-coverage ] 1`] = `5231043`;

exports[`NonfungiblePositionManager #decreaseLiquidity gas complete decrease [ @skip-on-coverage ] 1`] = `171239`;
exports[`NonfungiblePositionManager #decreaseLiquidity gas complete decrease [ @skip-on-coverage ] 1`] = `171665`;

exports[`NonfungiblePositionManager #decreaseLiquidity gas partial decrease [ @skip-on-coverage ] 1`] = `176207`;
exports[`NonfungiblePositionManager #decreaseLiquidity gas partial decrease [ @skip-on-coverage ] 1`] = `176589`;

exports[`NonfungiblePositionManager #increaseLiquidity gas [ @skip-on-coverage ] 1`] = `182683`;
exports[`NonfungiblePositionManager #increaseLiquidity gas [ @skip-on-coverage ] 1`] = `184149`;

exports[`NonfungiblePositionManager #mint gas first mint for pool [ @skip-on-coverage ] 1`] = `635168`;
exports[`NonfungiblePositionManager #mint gas first mint for pool [ @skip-on-coverage ] 1`] = `636773`;

exports[`NonfungiblePositionManager #mint gas first mint for pool using eth with non-zero refund [ @skip-on-coverage ] 1`] = `649531`;
exports[`NonfungiblePositionManager #mint gas first mint for pool using eth with non-zero refund [ @skip-on-coverage ] 1`] = `651121`;

exports[`NonfungiblePositionManager #mint gas first mint for pool using eth with zero refund [ @skip-on-coverage ] 1`] = `642364`;
exports[`NonfungiblePositionManager #mint gas first mint for pool using eth with zero refund [ @skip-on-coverage ] 1`] = `643954`;

exports[`NonfungiblePositionManager #mint gas mint for same pool, different ticks [ @skip-on-coverage ] 1`] = `440868`;
exports[`NonfungiblePositionManager #mint gas mint for same pool, different ticks [ @skip-on-coverage ] 1`] = `442385`;

exports[`NonfungiblePositionManager #mint gas mint on same ticks [ @skip-on-coverage ] 1`] = `330678`;
exports[`NonfungiblePositionManager #mint gas mint on same ticks [ @skip-on-coverage ] 1`] = `332156`;

exports[`NonfungiblePositionManager #permit owned by eoa gas [ @skip-on-coverage ] 1`] = `60014`;
exports[`NonfungiblePositionManager #permit owned by eoa gas [ @skip-on-coverage ] 1`] = `60003`;

exports[`NonfungiblePositionManager #permit owned by verifying contract gas [ @skip-on-coverage ] 1`] = `63880`;
exports[`NonfungiblePositionManager #permit owned by verifying contract gas [ @skip-on-coverage ] 1`] = `63869`;

exports[`NonfungiblePositionManager #transferFrom gas [ @skip-on-coverage ] 1`] = `86323`;

exports[`NonfungiblePositionManager #transferFrom gas comes from approved [ @skip-on-coverage ] 1`] = `87247`;

exports[`NonfungiblePositionManager bytecode size [ @skip-on-coverage ] 1`] = `21901`;
exports[`NonfungiblePositionManager bytecode size [ @skip-on-coverage ] 1`] = `22015`;

exports[`NonfungiblePositionManager multicall exit gas [ @skip-on-coverage ] 1`] = `246963`;
exports[`NonfungiblePositionManager multicall exit gas [ @skip-on-coverage ] 1`] = `247432`;

0 comments on commit 43f761e

Please sign in to comment.