Skip to content

Commit

Permalink
Merge pull request #126 from cryptoalgebra/feature/base-plugin-data
Browse files Browse the repository at this point in the history
Base plugin data
  • Loading branch information
debych authored Jul 22, 2024
2 parents 755774f + a62495e commit f0312b2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/core/contracts/AlgebraFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ contract AlgebraFactory is IAlgebraFactory, Ownable2Step, AccessControlEnumerabl
}

/// @inheritdoc IAlgebraFactory
function createPool(address tokenA, address tokenB) external override nonReentrant returns (address pool) {
return _createPool(address(0), msg.sender, tokenA, tokenB, '');
function createPool(address tokenA, address tokenB, bytes calldata data) external override nonReentrant returns (address pool) {
return _createPool(address(0), msg.sender, tokenA, tokenB, data);
}

/// @inheritdoc IAlgebraFactory
Expand All @@ -124,7 +124,7 @@ contract AlgebraFactory is IAlgebraFactory, Ownable2Step, AccessControlEnumerabl
address plugin;
if (deployer == address(0)) {
if (address(defaultPluginFactory) != address(0)) {
plugin = defaultPluginFactory.beforeCreatePoolHook(computePoolAddress(token0, token1), creator, address(0), token0, token1, '');
plugin = defaultPluginFactory.beforeCreatePoolHook(computePoolAddress(token0, token1), creator, address(0), token0, token1, data);
}
} else {
plugin = IAlgebraPluginFactory(msg.sender).beforeCreatePoolHook(
Expand Down
3 changes: 2 additions & 1 deletion src/core/contracts/interfaces/IAlgebraFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,11 @@ interface IAlgebraFactory {
/// @notice Creates a pool for the given two tokens
/// @param tokenA One of the two tokens in the desired pool
/// @param tokenB The other of the two tokens in the desired pool
/// @param data Data for plugin creation
/// @dev tokenA and tokenB may be passed in either order: token0/token1 or token1/token0.
/// The call will revert if the pool already exists or the token arguments are invalid.
/// @return pool The address of the newly created pool
function createPool(address tokenA, address tokenB) external returns (address pool);
function createPool(address tokenA, address tokenB, bytes calldata data) external returns (address pool);

/// @notice Creates a custom pool for the given two tokens using `deployer` contract
/// @param deployer The address of plugin deployer, also used for custom pool address calculation
Expand Down
22 changes: 11 additions & 11 deletions src/core/test/AlgebraFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('AlgebraFactory', () => {
});

it('pool bytecode size [ @skip-on-coverage ]', async () => {
await factory.createPool(TEST_ADDRESSES[0], TEST_ADDRESSES[1]);
await factory.createPool(TEST_ADDRESSES[0], TEST_ADDRESSES[1], '0x');
const poolAddress = getCreate2Address(
await poolDeployer.getAddress(),
[TEST_ADDRESSES[0], TEST_ADDRESSES[1]],
Expand All @@ -108,12 +108,12 @@ describe('AlgebraFactory', () => {

async function createAndCheckPool(tokens: [string, string]) {
const create2Address = getCreate2Address(await poolDeployer.getAddress(), tokens, poolBytecode);
const create = factory.createPool(tokens[0], tokens[1]);
const create = factory.createPool(tokens[0], tokens[1], '0x');

await expect(create).to.emit(factory, 'Pool');

await expect(factory.createPool(tokens[0], tokens[1])).to.be.reverted;
await expect(factory.createPool(tokens[1], tokens[0])).to.be.reverted;
await expect(factory.createPool(tokens[0], tokens[1], '0x')).to.be.reverted;
await expect(factory.createPool(tokens[1], tokens[0], '0x')).to.be.reverted;
expect(await factory.poolByPair(tokens[0], tokens[1]), 'getPool in order').to.eq(create2Address);
expect(await factory.poolByPair(tokens[1], tokens[0]), 'getPool in reverse').to.eq(create2Address);

Expand Down Expand Up @@ -198,22 +198,22 @@ describe('AlgebraFactory', () => {
});

it('fails if token a == token b', async () => {
await expect(factory.createPool(TEST_ADDRESSES[0], TEST_ADDRESSES[0])).to.be.reverted;
await expect(factory.createPool(TEST_ADDRESSES[0], TEST_ADDRESSES[0], '0x')).to.be.reverted;
});

it('fails if token a is 0 or token b is 0', async () => {
await expect(factory.createPool(TEST_ADDRESSES[0], ZeroAddress)).to.be.reverted;
await expect(factory.createPool(ZeroAddress, TEST_ADDRESSES[0])).to.be.reverted;
expect(factory.createPool(ZeroAddress, ZeroAddress)).to.be.revertedWithoutReason;
await expect(factory.createPool(TEST_ADDRESSES[0], ZeroAddress, '0x')).to.be.reverted;
await expect(factory.createPool(ZeroAddress, TEST_ADDRESSES[0], '0x')).to.be.reverted;
expect(factory.createPool(ZeroAddress, ZeroAddress, '0x')).to.be.revertedWithoutReason;
});

it('gas [ @skip-on-coverage ]', async () => {
await snapshotGasCost(factory.createPool(TEST_ADDRESSES[0], TEST_ADDRESSES[1]));
await snapshotGasCost(factory.createPool(TEST_ADDRESSES[0], TEST_ADDRESSES[1], '0x'));
});

it('gas for second pool [ @skip-on-coverage ]', async () => {
await factory.createPool(TEST_ADDRESSES[0], TEST_ADDRESSES[1]);
await snapshotGasCost(factory.createPool(TEST_ADDRESSES[0], TEST_ADDRESSES[2]));
await factory.createPool(TEST_ADDRESSES[0], TEST_ADDRESSES[1], '0x');
await snapshotGasCost(factory.createPool(TEST_ADDRESSES[0], TEST_ADDRESSES[2], '0x'));
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/periphery/contracts/base/PoolInitializer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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, '');

_initializePool(pool, sqrtPriceX96);
}
Expand Down
4 changes: 2 additions & 2 deletions src/periphery/test/NonfungiblePositionManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('NonfungiblePositionManager', () => {
await tokens[0].getAddress(),
await tokens[1].getAddress(),
]);
await factory.createPool(tokens[0], tokens[1]);
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));
Expand All @@ -113,7 +113,7 @@ describe('NonfungiblePositionManager', () => {
await tokens[0].getAddress(),
await tokens[1].getAddress(),
]);
await factory.createPool(tokens[0], tokens[1]);
await factory.createPool(tokens[0], tokens[1], '0x');
const pool = new ethers.Contract(expectedAddress, IAlgebraPoolABI, wallet);

await pool.initialize(encodePriceSqrt(3, 1));
Expand Down

0 comments on commit f0312b2

Please sign in to comment.