Skip to content

Commit

Permalink
[Core] add pluginFee to swap and burn events
Browse files Browse the repository at this point in the history
  • Loading branch information
IliaAzhel committed Sep 27, 2024
1 parent d6e1812 commit 897b7df
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 98 deletions.
2 changes: 1 addition & 1 deletion src/core/contracts/AlgebraFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ contract AlgebraFactory is IAlgebraFactory, Ownable2Step, AccessControlEnumerabl

/// @inheritdoc IAlgebraFactory
/// @dev keccak256 of AlgebraPool init bytecode. Used to compute pool address deterministically
bytes32 public constant POOL_INIT_CODE_HASH = 0xc5ee3168bec63cbec40c1187858f314e9c2a303e86e16005213d885cc87d4305;
bytes32 public constant POOL_INIT_CODE_HASH = 0x19c31d355e8d7bc982508cd49b44c879eaa0dad74e13aa4f42f1a07accb8cd1c;

constructor(address _poolDeployer) {
require(_poolDeployer != address(0));
Expand Down
37 changes: 32 additions & 5 deletions src/core/contracts/AlgebraPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ contract AlgebraPool is AlgebraPoolBase, TickStructure, ReentrancyGuard, Positio
}
}

if (amount | amount0 | amount1 != 0) emit Burn(msg.sender, bottomTick, topTick, amount, amount0, amount1);
if (amount | amount0 | amount1 != 0) emit Burn(msg.sender, bottomTick, topTick, amount, amount0, amount1, pluginFee);

_unlock();
_afterModifyPos(msg.sender, bottomTick, topTick, liquidityDelta, amount0, amount1, data);
Expand Down Expand Up @@ -278,7 +278,16 @@ contract AlgebraPool is AlgebraPoolBase, TickStructure, ReentrancyGuard, Positio
_changeReserves(amount0, amount1, 0, fees.communityFeeAmount, 0, fees.pluginFeeAmount); // reflect reserve change and pay communityFee
}

_emitSwapEvent(recipient, amount0, amount1, eventParams.currentPrice, eventParams.currentLiquidity, eventParams.currentTick);
_emitSwapEvent(
recipient,
amount0,
amount1,
eventParams.currentPrice,
eventParams.currentLiquidity,
eventParams.currentTick,
overrideFee,
pluginFee
);
}

_unlock();
Expand Down Expand Up @@ -350,15 +359,33 @@ contract AlgebraPool is AlgebraPoolBase, TickStructure, ReentrancyGuard, Positio
}
}

_emitSwapEvent(recipient, amount0, amount1, eventParams.currentPrice, eventParams.currentLiquidity, eventParams.currentTick);
_emitSwapEvent(
recipient,
amount0,
amount1,
eventParams.currentPrice,
eventParams.currentLiquidity,
eventParams.currentTick,
overrideFee,
pluginFee
);

_unlock();
_afterSwap(recipient, zeroToOne, amountToSell, limitSqrtPrice, amount0, amount1, data);
}

/// @dev internal function to reduce bytecode size
function _emitSwapEvent(address recipient, int256 amount0, int256 amount1, uint160 newPrice, uint128 newLiquidity, int24 newTick) private {
emit Swap(msg.sender, recipient, amount0, amount1, newPrice, newLiquidity, newTick);
function _emitSwapEvent(
address recipient,
int256 amount0,
int256 amount1,
uint160 newPrice,
uint128 newLiquidity,
int24 newTick,
uint24 overrideFee,
uint24 pluginFee
) private {
emit Swap(msg.sender, recipient, amount0, amount1, newPrice, newLiquidity, newTick, overrideFee, pluginFee);
}

function _beforeSwap(
Expand Down
25 changes: 23 additions & 2 deletions src/core/contracts/interfaces/pool/IAlgebraPoolEvents.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,16 @@ interface IAlgebraPoolEvents {
/// @param liquidityAmount The amount of liquidity to remove
/// @param amount0 The amount of token0 withdrawn
/// @param amount1 The amount of token1 withdrawn
event Burn(address indexed owner, int24 indexed bottomTick, int24 indexed topTick, uint128 liquidityAmount, uint256 amount0, uint256 amount1);
/// @param pluginFee The fee to be sent to the plugin
event Burn(
address indexed owner,
int24 indexed bottomTick,
int24 indexed topTick,
uint128 liquidityAmount,
uint256 amount0,
uint256 amount1,
uint24 pluginFee
);

/// @notice Emitted by the pool for any swaps between token0 and token1
/// @param sender The address that initiated the swap call, and that received the callback
Expand All @@ -56,7 +65,19 @@ interface IAlgebraPoolEvents {
/// @param price The sqrt(price) of the pool after the swap, as a Q64.96
/// @param liquidity The liquidity of the pool after the swap
/// @param tick The log base 1.0001 of price of the pool after the swap
event Swap(address indexed sender, address indexed recipient, int256 amount0, int256 amount1, uint160 price, uint128 liquidity, int24 tick);
/// @param overrideFee The fee to be applied to the trade
/// @param pluginFee The fee to be sent to the plugin
event Swap(
address indexed sender,
address indexed recipient,
int256 amount0,
int256 amount1,
uint160 price,
uint128 liquidity,
int24 tick,
uint24 overrideFee,
uint24 pluginFee
);

/// @notice Emitted by the pool for any flashes of token0/token1
/// @param sender The address that initiated the swap call, and that received the callback
Expand Down
14 changes: 7 additions & 7 deletions src/core/test/AlgebraPool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ describe('AlgebraPool', () => {
await swapExact1For0(expandTo18Decimals(2), other.address);
await expect(pool.burn(0, 120, expandTo18Decimals(1), '0x'))
.to.emit(pool, 'Burn')
.withArgs(wallet.address, 0, 120, expandTo18Decimals(1), 0, '6017734268818165')
.withArgs(wallet.address, 0, 120, expandTo18Decimals(1), 0, '6017734268818165', 0)
.to.not.emit(token0, 'Transfer')
.to.not.emit(token1, 'Transfer');
await expect(pool.collect(wallet.address, 0, 120, MaxUint128, MaxUint128))
Expand All @@ -1148,7 +1148,7 @@ describe('AlgebraPool', () => {
await swapExact0For1(expandTo18Decimals(2), other.address);
await expect(pool.burn(-120, 0, expandTo18Decimals(1), '0x'))
.to.emit(pool, 'Burn')
.withArgs(wallet.address, -120, 0, expandTo18Decimals(1), '6017734268818165', 0)
.withArgs(wallet.address, -120, 0, expandTo18Decimals(1), '6017734268818165', 0, 0)
.to.not.emit(token0, 'Transfer')
.to.not.emit(token1, 'Transfer');
await expect(pool.collect(wallet.address, -120, 0, MaxUint128, MaxUint128))
Expand All @@ -1168,7 +1168,7 @@ describe('AlgebraPool', () => {
await swapExact1For0(expandTo18Decimals(2), other.address);
await expect(pool.burn(0, 120, expandTo18Decimals(1), '0x'))
.to.emit(pool, 'Burn')
.withArgs(wallet.address, 0, 120, expandTo18Decimals(1), 0, '6017734268818165')
.withArgs(wallet.address, 0, 120, expandTo18Decimals(1), 0, '6017734268818165', 0)
.to.not.emit(token0, 'Transfer')
.to.not.emit(token1, 'Transfer');
await expect(pool.collect(wallet.address, 0, 120, MaxUint128, MaxUint128))
Expand Down Expand Up @@ -2054,7 +2054,7 @@ describe('AlgebraPool', () => {
await swapExact1For0(expandTo18Decimals(1), wallet.address);
await expect(pool.burn(120000, 121200, liquidityAmount, '0x'))
.to.emit(pool, 'Burn')
.withArgs(wallet.address, 120000, 121200, liquidityAmount, '30012388425661', '999499999999999999')
.withArgs(wallet.address, 120000, 121200, liquidityAmount, '30012388425661', '999499999999999999', 0)
.to.not.emit(token0, 'Transfer')
.to.not.emit(token1, 'Transfer');
expect((await pool.globalState()).tick).to.eq(120197);
Expand All @@ -2065,7 +2065,7 @@ describe('AlgebraPool', () => {
await swapExact0For1(expandTo18Decimals(1), wallet.address);
await expect(pool.burn(-121200, -120000, liquidityAmount, '0x'))
.to.emit(pool, 'Burn')
.withArgs(wallet.address, -121200, -120000, liquidityAmount, '999499999999999999', '30012388425661')
.withArgs(wallet.address, -121200, -120000, liquidityAmount, '999499999999999999', '30012388425661', 0)
.to.not.emit(token0, 'Transfer')
.to.not.emit(token1, 'Transfer');
expect((await pool.globalState()).tick).to.eq(-120198);
Expand Down Expand Up @@ -2528,7 +2528,7 @@ describe('AlgebraPool', () => {
expect(pluginFees[0]).to.be.eq(4n * 10n**15n);
})

it.only('sends pluginFee when communityFee overflows', async () => {
it('sends pluginFee when communityFee overflows', async () => {
await poolPlugin.setPluginFees(100000, 1);
await pool.setCommunityFee(800);
await swapExact0For1(expandTo18Decimals(1), wallet.address);
Expand All @@ -2542,7 +2542,7 @@ describe('AlgebraPool', () => {
expect(pluginFees[0]).to.be.eq(0);
})

it.only('sends pluginFee when communityFee overflows but plugin fee is zero', async () => {
it('sends pluginFee when communityFee overflows but plugin fee is zero', async () => {
await poolPlugin.setPluginFees(100000, 100);
await pool.setCommunityFee(800);
await swapExact0For1(expandTo18Decimals(1), wallet.address);
Expand Down
4 changes: 3 additions & 1 deletion src/core/test/AlgebraPool.swaps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,9 @@ describe('AlgebraPool swap tests', () => {
poolBalance1Delta,
globalStateAfter.price,
liquidityAfter,
globalStateAfter.tick
globalStateAfter.tick,
0,
0
);

const executionPrice = new Decimal(poolBalance1Delta.toString()).div(poolBalance0Delta.toString()).mul(-1);
Expand Down
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`] = `4745061`;
exports[`AlgebraFactory #createCustomPool gas [ @skip-on-coverage ] 1`] = `4756512`;

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

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

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

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

exports[`AlgebraFactory pool bytecode size [ @skip-on-coverage ] 1`] = `22399`;
exports[`AlgebraFactory pool bytecode size [ @skip-on-coverage ] 1`] = `22456`;
Loading

0 comments on commit 897b7df

Please sign in to comment.