Skip to content

Commit

Permalink
Update BaselineAxisLaunch to check the BPOOL's active tick against th…
Browse files Browse the repository at this point in the history
…e auction price. Updates tests accordingly. Remove redundant TimeslotLib.
  • Loading branch information
0xJem committed Jun 19, 2024
1 parent 91ec912 commit 0d2804e
Show file tree
Hide file tree
Showing 13 changed files with 264 additions and 115 deletions.
4 changes: 2 additions & 2 deletions script/salts/salts.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"0xeea914831cafc1587d76be464f11002411abf2a8819b9fdb690b0861a50b7013": "0xfc5a3e878c2ebbe399fd000b0874c27ddc00fccafbebc26a47062384fb792271"
},
"Test_BaselineAllocatedAllowlist": {
"0xdf4e330322b2cf16b983e30be68fdcc9f28aa9ca3ea5668db2da4680e00f7656": "0x90d7692e46b872b1176d8f108f9ffa9f37afd52ab59b2411c01b53d585a47df2"
"0x03bef2ceffa258ff6109ad6bc16d951564cde898281cb9d4d0011390a461e977": "0xbb9a94cda3e330636bc8519044b22888a2e0f5fa06f48c1203c824730d429978"
},
"Test_BaselineAxisLaunch": {
"0x93fb862631c4414f2d4108397ca06e077a3caf62374b7a6c8fb38d35c275f02a": "0xd116feb11c4c04385a9476d1f1a1da81cfedca07a1b3638abd751622fbeffb8a"
"0x5554c48c8c661389476240aa7a1f3ec5cc9eee83e418504c25ee333d29d9bf87": "0x5ed9eb6bba3f801a5c7155da7e61545c108a342439c577724bf5514a06dd54a2"
},
"Test_CappedMerkleAllowlist": {
"0xd46fcdc05daffec5dc7bd7b165f3ffef024c5074d5f5d8f096ad176a348b93e9": "0x9a505eb6b9322904cad8128403089877c62cbbfad4b91ab17f83e0e51bc519e2",
Expand Down
73 changes: 52 additions & 21 deletions src/callbacks/liquidity/BaselineV2/BaselineAxisLaunch.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
Permissions as BaselinePermissions
} from "src/callbacks/liquidity/BaselineV2/lib/Kernel.sol";
import {Range, IBPOOLv1} from "src/callbacks/liquidity/BaselineV2/lib/IBPOOL.sol";
import {TimeslotLib} from "src/callbacks/liquidity/BaselineV2/lib/TimeslotLib.sol";
import {TickMath} from "lib/uniswap-v3-core/contracts/libraries/TickMath.sol";

// Other libraries
Expand All @@ -37,14 +36,40 @@ import {SqrtPriceMath} from "src/lib/uniswap-v3/SqrtPriceMath.sol";
/// It is designed to be used with a single auction and Baseline pool
contract BaselineAxisLaunch is BaseCallback, Policy, Owned {
using FixedPointMathLib for uint256;
using TimeslotLib for uint256;

// ========== ERRORS ========== //

/// @notice The address of the base token (passed in the `onCreate` callback) does not match the address of the bAsset that the callback was initialized with
error Callback_Params_BAssetTokenMismatch(address baseToken_, address bAsset_);

/// @notice The address of the quote token (passed in the `onCreate` callback) does not match the address of the reserve that the callback was initialized with
error Callback_Params_ReserveTokenMismatch(address quoteToken_, address reserve_);

/// @notice The auction price and the pool active tick do not match
error Callback_Params_PoolTickMismatch(int24 auctionTick_, int24 poolTick_);

/// @notice The auction format is not supported
error Callback_Params_UnsupportedAuctionFormat();

/// @notice The anchor tick width is invalid
error Callback_Params_InvalidAnchorTickWidth();

/// @notice The discovery tick width is invalid
error Callback_Params_InvalidDiscoveryTickWidth();

/// @notice The floor reserves percent is invalid
error Callback_Params_InvalidFloorReservesPercent();

/// @notice The auction tied to this callbacks contract has already been completed
error Callback_AlreadyComplete();

/// @notice The required funds were not sent to this callbacks contract
error Callback_MissingFunds();

/// @notice The BPOOL reserve token does not match the configured `RESERVE` address
error InvalidModule();

/// @notice Deploying reserves and liquidity would result in the Baseline pool being insolvent
error Insolvent();

// ========== EVENTS ========== //
Expand Down Expand Up @@ -165,13 +190,12 @@ contract BaselineAxisLaunch is BaseCallback, Policy, Owned {
{
BaselineKeycode bpool = toBaselineKeycode("BPOOL");

requests = new BaselinePermissions[](6);
requests[0] = BaselinePermissions(bpool, BPOOL.initializePool.selector);
requests[1] = BaselinePermissions(bpool, BPOOL.addReservesTo.selector);
requests[2] = BaselinePermissions(bpool, BPOOL.addLiquidityTo.selector);
requests[3] = BaselinePermissions(bpool, BPOOL.burnAllBAssetsInContract.selector);
requests[4] = BaselinePermissions(bpool, BPOOL.mint.selector);
requests[5] = BaselinePermissions(bpool, BPOOL.setTicks.selector);
requests = new BaselinePermissions[](5);
requests[0] = BaselinePermissions(bpool, BPOOL.addReservesTo.selector);
requests[1] = BaselinePermissions(bpool, BPOOL.addLiquidityTo.selector);
requests[2] = BaselinePermissions(bpool, BPOOL.burnAllBAssetsInContract.selector);
requests[3] = BaselinePermissions(bpool, BPOOL.mint.selector);
requests[4] = BaselinePermissions(bpool, BPOOL.setTicks.selector);
}

// ========== CALLBACK FUNCTIONS ========== //
Expand All @@ -198,12 +222,12 @@ contract BaselineAxisLaunch is BaseCallback, Policy, Owned {
/// - `baseToken_` is not the same as `bAsset`
/// - `quoteToken_` is not the same as `RESERVE`
/// - `lotId` is already set
/// - `CreateData.percentReservesFloor` is less than 0% or greater than 100%
/// - `CreateData.floorReservesPercent` is less than 0% or greater than 100%
/// - `CreateData.anchorTickWidth` is 0
/// - `CreateData.discoveryTickWidth` is 0
/// - The auction format is not supported
/// - FPB auction format: `CreateData.initAnchorTick` is 0
/// - The auction is not prefunded
/// - The active tick of the Baseline pool (from `baseToken_`) is not the same as the tick corresponding to the auction price
function _onCreate(
uint96 lotId_,
address seller_,
Expand All @@ -215,8 +239,11 @@ contract BaselineAxisLaunch is BaseCallback, Policy, Owned {
) internal override {
// Validate the base token is the baseline token
// and the quote token is the reserve
if (baseToken_ != address(bAsset) || quoteToken_ != address(RESERVE)) {
revert Callback_InvalidParams();
if (baseToken_ != address(bAsset)) {
revert Callback_Params_BAssetTokenMismatch(baseToken_, address(bAsset));
}
if (quoteToken_ != address(RESERVE)) {
revert Callback_Params_ReserveTokenMismatch(quoteToken_, address(RESERVE));
}

// Validate that the lot ID is not already set
Expand All @@ -227,22 +254,22 @@ contract BaselineAxisLaunch is BaseCallback, Policy, Owned {

// Validate that the anchor tick width is at least 1 tick spacing
if (cbData.anchorTickWidth <= 0) {
revert Callback_InvalidParams();
revert Callback_Params_InvalidAnchorTickWidth();
}

// Validate that the discovery tick width is at least 1 tick spacing
if (cbData.discoveryTickWidth <= 0) {
revert Callback_InvalidParams();
revert Callback_Params_InvalidDiscoveryTickWidth();
}

// Validate that the floor reserves percent is between 0% and 100%
if (cbData.floorReservesPercent > ONE_HUNDRED_PERCENT) {
revert Callback_InvalidParams();
revert Callback_Params_InvalidFloorReservesPercent();
}

// Auction must be prefunded for batch auctions (which is the only type supported with this callback),
// this can't fail because it's checked in the AH as well, but including for completeness
if (!prefund_) revert Callback_InvalidParams();
if (!prefund_) revert Callback_Params_UnsupportedAuctionFormat();

// Set the lot ID
lotId = lotId_;
Expand All @@ -257,7 +284,7 @@ contract BaselineAxisLaunch is BaseCallback, Policy, Owned {

// Only supports Fixed Price Batch Auctions initially
if (fromAxisKeycode(auctionFormat) != bytes5("FPBA")) {
revert Callback_InvalidParams();
revert Callback_Params_UnsupportedAuctionFormat();
}

// This contract can be extended with an allowlist for the auction
Expand Down Expand Up @@ -286,10 +313,14 @@ contract BaselineAxisLaunch is BaseCallback, Policy, Owned {
address(RESERVE), address(bAsset), auctionPrice, 10 ** baseTokenDecimals
);
activeTick = TickMath.getTickAtSqrtRatio(sqrtPriceX96);
}

// Initialize the Baseline pool at the tick determined by the auction price
BPOOL.initializePool(activeTick);
// Check that the pool is initialized at the active tick
// This is to ensure that the pool is initialized at the auction price
(, int24 poolCurrentTick,,,,,) = BPOOL.pool().slot0();
if (poolCurrentTick != activeTick) {
revert Callback_Params_PoolTickMismatch(activeTick, poolCurrentTick);
}
}

int24 tickSpacing = BPOOL.TICK_SPACING();

Expand Down
30 changes: 0 additions & 30 deletions src/callbacks/liquidity/BaselineV2/lib/TimeslotLib.sol

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ contract BaselineAllocatedAllowlistOnBidTest is BaselineAllocatedAllowlistTest {

function test_parametersInvalid_reverts()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
Expand All @@ -84,6 +85,7 @@ contract BaselineAllocatedAllowlistOnBidTest is BaselineAllocatedAllowlistTest {

function test_merkleProofInvalid_reverts()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
Expand All @@ -101,6 +103,7 @@ contract BaselineAllocatedAllowlistOnBidTest is BaselineAllocatedAllowlistTest {

function test_buyerNotInMerkleTree_reverts()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
Expand All @@ -119,6 +122,7 @@ contract BaselineAllocatedAllowlistOnBidTest is BaselineAllocatedAllowlistTest {

function test_buyerLimitSpent_reverts()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
Expand All @@ -140,6 +144,7 @@ contract BaselineAllocatedAllowlistOnBidTest is BaselineAllocatedAllowlistTest {

function test_buyerZeroLimit_reverts()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
Expand All @@ -159,6 +164,7 @@ contract BaselineAllocatedAllowlistOnBidTest is BaselineAllocatedAllowlistTest {

function test_noBids_aboveLimit_reverts()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
Expand All @@ -177,6 +183,7 @@ contract BaselineAllocatedAllowlistOnBidTest is BaselineAllocatedAllowlistTest {

function test_noBids_belowLimit()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
Expand All @@ -193,6 +200,7 @@ contract BaselineAllocatedAllowlistOnBidTest is BaselineAllocatedAllowlistTest {

function test_remainingLimit_aboveLimit_reverts()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
Expand All @@ -214,6 +222,7 @@ contract BaselineAllocatedAllowlistOnBidTest is BaselineAllocatedAllowlistTest {

function test_remainingLimit_belowLimit()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ contract BaselineAllocatedAllowlistOnCreateTest is BaselineAllocatedAllowlistTes

function test_allowlistParamsIncorrect_reverts()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
{
Expand All @@ -37,6 +38,7 @@ contract BaselineAllocatedAllowlistOnCreateTest is BaselineAllocatedAllowlistTes

function test_success_reverts()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ contract BaselineAllocatedAllowlistSetMerkleRootTest is BaselineAllocatedAllowli

function test_notOwner_reverts()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
Expand All @@ -46,6 +47,7 @@ contract BaselineAllocatedAllowlistSetMerkleRootTest is BaselineAllocatedAllowli

function test_auctionNotRegistered_reverts()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
Expand All @@ -61,6 +63,7 @@ contract BaselineAllocatedAllowlistSetMerkleRootTest is BaselineAllocatedAllowli

function test_auctionCompleted_reverts()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
Expand All @@ -80,6 +83,7 @@ contract BaselineAllocatedAllowlistSetMerkleRootTest is BaselineAllocatedAllowli

function test_success()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
Expand Down
Loading

0 comments on commit 0d2804e

Please sign in to comment.