Skip to content

Commit

Permalink
Merge pull request #4 from pancakeswap/feat/update-core-periphery
Browse files Browse the repository at this point in the history
feat: Update to latest v4-core/periphery
  • Loading branch information
chef-omelette authored Jul 2, 2024
2 parents 6e60c58 + d130eef commit ddf50fb
Show file tree
Hide file tree
Showing 22 changed files with 260 additions and 171 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ jobs:
with:
version: nightly

# - name: Run Forge build
# run: |
# forge --version
# forge build --sizes
# id: build
- name: Run Forge build
run: |
forge --version
forge build --sizes
id: build

- name: Run Forge tests
run: |
forge test -vvv
forge test --isolate -vvv
id: test
17 changes: 14 additions & 3 deletions foundry.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
[rpc_endpoints]
bsc_testnet = "${RPC_BSC_TESTNET}"
bsc_mainnet = "${RPC_BSC_MAINNET}"

[profile.default]
src = "src"
out = "out"
libs = ["lib"]
solc_version = '0.8.24'
optimizer_runs = 800
via_ir = false
solc_version = "0.8.26"
optimizer_runs = 1000
via_ir = true
evm_version = 'cancun'
ffi = true
fs_permissions = [{ access = "read-write", path = ".forge-snapshots/"}]
remappings = [
"@pancakeswap/v4-core/=lib/pancake-v4-core/",
"@pancakeswap/v4-periphery/=lib/pancake-v4-periphery/",
"@openzeppelin/=lib/pancake-v4-periphery/lib/openzeppelin-contracts/"
]

[profile.default.fuzz]
runs = 1000
Expand Down
2 changes: 1 addition & 1 deletion lib/pancake-v4-core
Submodule pancake-v4-core updated 275 files
2 changes: 1 addition & 1 deletion lib/pancake-v4-periphery
32 changes: 23 additions & 9 deletions src/pool-bin/BinBaseHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ import {
HOOKS_AFTER_SWAP_OFFSET,
HOOKS_BEFORE_DONATE_OFFSET,
HOOKS_AFTER_DONATE_OFFSET,
HOOKS_NO_OP_OFFSET
HOOKS_BEFORE_SWAP_RETURNS_DELTA_OFFSET,
HOOKS_AFTER_SWAP_RETURNS_DELTA_OFFSET,
HOOKS_AFTER_MINT_RETURNS_DELTA_OFFSET,
HOOKS_AFTER_BURN_RETURNS_DELTA_OFFSET
} from "@pancakeswap/v4-core/src/pool-bin/interfaces/IBinHooks.sol";
import {PoolKey} from "@pancakeswap/v4-core/src/types/PoolKey.sol";
import {BalanceDelta} from "@pancakeswap/v4-core/src/types/BalanceDelta.sol";
import {BeforeSwapDelta} from "@pancakeswap/v4-core/src/types/BeforeSwapDelta.sol";
import {IHooks} from "@pancakeswap/v4-core/src/interfaces/IHooks.sol";
import {IVault} from "@pancakeswap/v4-core/src/interfaces/IVault.sol";
import {IBinHooks} from "@pancakeswap/v4-core/src/pool-bin/interfaces/IBinHooks.sol";
Expand Down Expand Up @@ -53,7 +57,10 @@ abstract contract BinBaseHook is IBinHooks {
bool afterSwap;
bool beforeDonate;
bool afterDonate;
bool noOp;
bool beforeSwapReturnDelta;
bool afterSwapReturnDelta;
bool afterMintReturnDelta;
bool afterBurnReturnDelta;
}

/// @notice The address of the pool manager
Expand Down Expand Up @@ -126,7 +133,7 @@ abstract contract BinBaseHook is IBinHooks {
function afterMint(address, PoolKey calldata, IBinPoolManager.MintParams calldata, BalanceDelta, bytes calldata)
external
virtual
returns (bytes4)
returns (bytes4, BalanceDelta)
{
revert HookNotImplemented();
}
Expand All @@ -144,21 +151,25 @@ abstract contract BinBaseHook is IBinHooks {
function afterBurn(address, PoolKey calldata, IBinPoolManager.BurnParams calldata, BalanceDelta, bytes calldata)
external
virtual
returns (bytes4)
returns (bytes4, BalanceDelta)
{
revert HookNotImplemented();
}

/// @inheritdoc IBinHooks
function beforeSwap(address, PoolKey calldata, bool, uint128, bytes calldata) external virtual returns (bytes4) {
function beforeSwap(address, PoolKey calldata, bool, int128, bytes calldata)
external
virtual
returns (bytes4, BeforeSwapDelta, uint24)
{
revert HookNotImplemented();
}

/// @inheritdoc IBinHooks
function afterSwap(address, PoolKey calldata, bool, uint128, BalanceDelta, bytes calldata)
function afterSwap(address, PoolKey calldata, bool, int128, BalanceDelta, bytes calldata)
external
virtual
returns (bytes4)
returns (bytes4, int128)
{
revert HookNotImplemented();
}
Expand All @@ -181,7 +192,7 @@ abstract contract BinBaseHook is IBinHooks {
revert HookNotImplemented();
}

/// @dev A convenience method to construct the hook registration map.
/// @dev Helper function to construct the hook registration map
function _hooksRegistrationBitmapFrom(Permissions memory permissions) internal pure returns (uint16) {
return uint16(
(permissions.beforeInitialize ? 1 << HOOKS_BEFORE_INITIALIZE_OFFSET : 0)
Expand All @@ -194,7 +205,10 @@ abstract contract BinBaseHook is IBinHooks {
| (permissions.afterSwap ? 1 << HOOKS_AFTER_SWAP_OFFSET : 0)
| (permissions.beforeDonate ? 1 << HOOKS_BEFORE_DONATE_OFFSET : 0)
| (permissions.afterDonate ? 1 << HOOKS_AFTER_DONATE_OFFSET : 0)
| (permissions.noOp ? 1 << HOOKS_NO_OP_OFFSET : 0)
| (permissions.beforeSwapReturnDelta ? 1 << HOOKS_BEFORE_SWAP_RETURNS_DELTA_OFFSET : 0)
| (permissions.afterSwapReturnDelta ? 1 << HOOKS_AFTER_SWAP_RETURNS_DELTA_OFFSET : 0)
| (permissions.afterMintReturnDelta ? 1 << HOOKS_AFTER_MINT_RETURNS_DELTA_OFFSET : 0)
| (permissions.afterBurnReturnDelta ? 1 << HOOKS_AFTER_BURN_RETURNS_DELTA_OFFSET : 0)
);
}
}
22 changes: 14 additions & 8 deletions src/pool-bin/geomean-oracle/BinGeomeanOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
import {IPoolManager} from "@pancakeswap/v4-core/src/interfaces/IPoolManager.sol";
import {PoolId, PoolIdLibrary} from "@pancakeswap/v4-core/src/types/PoolId.sol";
import {PoolKey} from "@pancakeswap/v4-core/src/types/PoolKey.sol";
import {BalanceDelta, BalanceDeltaLibrary} from "@pancakeswap/v4-core/src/types/BalanceDelta.sol";
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "@pancakeswap/v4-core/src/types/BeforeSwapDelta.sol";
import {Hooks} from "@pancakeswap/v4-core/src/libraries/Hooks.sol";

import {BinBaseHook} from "../BinBaseHook.sol";
Expand Down Expand Up @@ -56,7 +58,10 @@ contract BinGeomeanOracle is BinBaseHook {
afterSwap: false,
beforeDonate: false,
afterDonate: false,
noOp: false
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterMintReturnDelta: false,
afterBurnReturnDelta: false
})
);
}
Expand Down Expand Up @@ -101,14 +106,15 @@ contract BinGeomeanOracle is BinBaseHook {
revert OraclePoolMustLockLiquidity();
}

function beforeSwap(address sender, PoolKey calldata key, bool swapForY, uint128 amountIn, bytes calldata hookData)
external
override
poolManagerOnly
returns (bytes4)
{
function beforeSwap(
address sender,
PoolKey calldata key,
bool swapForY,
int128 amountSpecified,
bytes calldata hookData
) external override poolManagerOnly returns (bytes4, BeforeSwapDelta, uint24) {
_updatePool(key);
return this.beforeSwap.selector;
return (this.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, 0);
}

/**
Expand Down
61 changes: 36 additions & 25 deletions src/pool-bin/limit-order/BinLimitOrder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {LiquidityConfigurations} from "@pancakeswap/v4-core/src/pool-bin/librari
import {PackedUint128Math} from "@pancakeswap/v4-core/src/pool-bin/libraries/math/PackedUint128Math.sol";
import {BinPool} from "@pancakeswap/v4-core/src/pool-bin/libraries/BinPool.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

import {BinBaseHook} from "../BinBaseHook.sol";

Expand All @@ -56,6 +57,7 @@ contract BinLimitOrder is BinBaseHook {
using CurrencyLibrary for Currency;
using BinPoolParametersHelper for bytes32;
using PackedUint128Math for uint128;
using SafeERC20 for IERC20;

/// @notice Place zero token amount
error ZeroAmount();
Expand Down Expand Up @@ -127,7 +129,10 @@ contract BinLimitOrder is BinBaseHook {
afterSwap: true,
beforeDonate: false,
afterDonate: false,
noOp: false
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterMintReturnDelta: false,
afterBurnReturnDelta: false
})
);
}
Expand Down Expand Up @@ -167,12 +172,12 @@ contract BinLimitOrder is BinBaseHook {
address sender,
PoolKey calldata key,
bool swapForY,
uint128 amountIn,
int128 amountSpecified,
BalanceDelta delta,
bytes calldata hookData
) external override returns (bytes4) {
) external override returns (bytes4, int128) {
(uint24 activeId, uint24 lower, uint24 upper) = _getCrossedBins(key.toId());
if (lower > upper) return this.afterSwap.selector;
if (lower > upper) return (this.afterSwap.selector, 0);

// note that a swapForY swap means that the pool is actually gaining token0, so limit
// order fills are the opposite of swap fills, hence the inversion below
Expand All @@ -181,7 +186,7 @@ contract BinLimitOrder is BinBaseHook {
}

setActiveIdLast(key.toId(), activeId);
return this.afterSwap.selector;
return (this.afterSwap.selector, 0);
}

function _fillEpoch(PoolKey calldata key, uint24 binId, bool swapForY) internal {
Expand All @@ -195,16 +200,17 @@ contract BinLimitOrder is BinBaseHook {
ids[0] = binId;
uint256[] memory amounts = new uint256[](1);
amounts[0] = uint256(epochInfo.liquidityTotal);
BalanceDelta delta =
poolManager.burn(key, IBinPoolManager.BurnParams({ids: ids, amountsToBurn: amounts}), ZERO_BYTES);
BalanceDelta delta = poolManager.burn(
key, IBinPoolManager.BurnParams({ids: ids, amountsToBurn: amounts, salt: bytes32(0)}), ZERO_BYTES
);

uint256 amount0;
uint256 amount1;
if (delta.amount0() < 0) {
vault.mint(key.currency0, address(this), amount0 = uint128(-delta.amount0()));
if (delta.amount0() > 0) {
vault.mint(address(this), key.currency0, amount0 = uint128(delta.amount0()));
}
if (delta.amount1() < 0) {
vault.mint(key.currency1, address(this), amount1 = uint128(-delta.amount1()));
if (delta.amount1() > 0) {
vault.mint(address(this), key.currency1, amount1 = uint128(delta.amount1()));
}

unchecked {
Expand Down Expand Up @@ -297,19 +303,23 @@ contract BinLimitOrder is BinBaseHook {
liquidityConfigs[0] = LiquidityConfigurations.encodeParams(distributionX, distributionY, binId);

(BalanceDelta delta, BinPool.MintArrays memory mintArray) = poolManager.mint(
key, IBinPoolManager.MintParams({liquidityConfigs: liquidityConfigs, amountIn: amountIn}), ZERO_BYTES
key,
IBinPoolManager.MintParams({liquidityConfigs: liquidityConfigs, amountIn: amountIn, salt: bytes32(0)}),
ZERO_BYTES
);

liquidity = mintArray.liquidityMinted[0];

if (delta.amount0() > 0) {
IERC20(Currency.unwrap(key.currency0)).transferFrom(
owner, address(vault), uint256(uint128(delta.amount0()))
if (delta.amount0() < 0) {
vault.sync(key.currency0);
IERC20(Currency.unwrap(key.currency0)).safeTransferFrom(
owner, address(vault), uint256(uint128(-delta.amount0()))
);
vault.settle(key.currency0);
} else {
IERC20(Currency.unwrap(key.currency1)).transferFrom(
owner, address(vault), uint256(uint128(delta.amount1()))
vault.sync(key.currency1);
IERC20(Currency.unwrap(key.currency1)).safeTransferFrom(
owner, address(vault), uint256(uint128(-delta.amount1()))
);
vault.settle(key.currency1);
}
Expand Down Expand Up @@ -363,14 +373,15 @@ contract BinLimitOrder is BinBaseHook {
ids[0] = binId;
uint256[] memory amounts = new uint256[](1);
amounts[0] = liquidityDelta;
BalanceDelta delta =
poolManager.burn(key, IBinPoolManager.BurnParams({ids: ids, amountsToBurn: amounts}), ZERO_BYTES);
BalanceDelta delta = poolManager.burn(
key, IBinPoolManager.BurnParams({ids: ids, amountsToBurn: amounts, salt: bytes32(0)}), ZERO_BYTES
);

if (delta.amount0() < 0) {
vault.take(key.currency0, to, amount0 = uint128(-delta.amount0()));
if (delta.amount0() > 0) {
vault.take(key.currency0, to, amount0 = uint128(delta.amount0()));
}
if (delta.amount1() < 0) {
vault.take(key.currency1, to, amount1 = uint128(-delta.amount1()));
if (delta.amount1() > 0) {
vault.take(key.currency1, to, amount1 = uint128(delta.amount1()));
}
}

Expand Down Expand Up @@ -414,11 +425,11 @@ contract BinLimitOrder is BinBaseHook {
address to
) external selfOnly {
if (token0Amount > 0) {
vault.burn(currency0, token0Amount);
vault.burn(address(this), currency0, token0Amount);
vault.take(currency0, to, token0Amount);
}
if (token1Amount > 0) {
vault.burn(currency1, token1Amount);
vault.burn(address(this), currency1, token1Amount);
vault.take(currency1, to, token1Amount);
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/pool-bin/vecake-exclusive/BinVeCakeExclusiveHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity ^0.8.24;

import {PoolKey} from "@pancakeswap/v4-core/src/types/PoolKey.sol";
import {PoolId, PoolIdLibrary} from "@pancakeswap/v4-core/src/types/PoolId.sol";
import {FeeLibrary} from "@pancakeswap/v4-core/src/libraries/FeeLibrary.sol";
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "@pancakeswap/v4-core/src/types/BeforeSwapDelta.sol";
import {IBinPoolManager} from "@pancakeswap/v4-core/src/pool-bin/interfaces/IBinPoolManager.sol";

import {BinBaseHook} from "../BinBaseHook.sol";
Expand All @@ -17,7 +17,6 @@ interface IVeCake {
/// you a qualified holder
contract BinVeCakeExclusiveHook is BinBaseHook {
using PoolIdLibrary for PoolKey;
using FeeLibrary for uint24;

IVeCake veCake;

Expand All @@ -40,22 +39,25 @@ contract BinVeCakeExclusiveHook is BinBaseHook {
afterSwap: false,
beforeDonate: false,
afterDonate: false,
noOp: false
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterMintReturnDelta: false,
afterBurnReturnDelta: false
})
);
}

/// @dev Using tx.origin is a workaround for now. Do NOT use this in
/// production
function beforeSwap(address, PoolKey calldata, bool, uint128, bytes calldata)
function beforeSwap(address, PoolKey calldata, bool, int128, bytes calldata)
external
override
poolManagerOnly
returns (bytes4)
returns (bytes4, BeforeSwapDelta, uint24)
{
if (veCake.balanceOf(tx.origin) < 1 ether) {
revert NotVeCakeHolder();
}
return this.beforeSwap.selector;
return (this.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, 0);
}
}
Loading

0 comments on commit ddf50fb

Please sign in to comment.