Skip to content

Commit

Permalink
Add tests for Baseline Allowlist
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJem committed Jun 21, 2024
1 parent 46fc158 commit d570ca1
Show file tree
Hide file tree
Showing 9 changed files with 319 additions and 22 deletions.
5 changes: 2 additions & 3 deletions script/salts/test/TestSalts.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,8 @@ contract TestSalts is Script, WithEnvironment, Permit2User, WithSalts, TestConst
// Get the salt
bytes memory callbackArgs =
abi.encode(_AUCTION_HOUSE, _BASELINE_KERNEL, _BASELINE_QUOTE_TOKEN, _OWNER);
(string memory callbackBytecodePath, bytes32 callbackBytecodeHash) = _writeBytecode(
"BaselineAllowlist", type(BALwithAllowlist).creationCode, callbackArgs
);
(string memory callbackBytecodePath, bytes32 callbackBytecodeHash) =
_writeBytecode("BaselineAllowlist", type(BALwithAllowlist).creationCode, callbackArgs);
_setTestSalt(callbackBytecodePath, "EF", "BaselineAllowlist", callbackBytecodeHash);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.19;

// Test scaffolding
import {BaselineAxisLaunchTest} from
"test/callbacks/liquidity/BaselineV2/BaselineAxisLaunchTest.sol";

// Axis
import {BALwithAllowlist} from "src/callbacks/liquidity/BaselineV2/BALwithAllowlist.sol";

contract BaselineAllowlistTest is BaselineAxisLaunchTest {
uint256 internal constant _BUYER_LIMIT = 5e18;

// ========== MODIFIERS ========== //

modifier givenCallbackIsCreated() override {
// Get the salt
bytes memory args =
abi.encode(address(_auctionHouse), _BASELINE_KERNEL, _BASELINE_QUOTE_TOKEN, _OWNER);
bytes32 salt = _getTestSalt("BaselineAllowlist", type(BALwithAllowlist).creationCode, args);

// Required for CREATE2 address to work correctly. doesn't do anything in a test
// Source: https://github.com/foundry-rs/foundry/issues/6402
vm.startBroadcast();
_dtl = new BALwithAllowlist{salt: salt}(
address(_auctionHouse), _BASELINE_KERNEL, _BASELINE_QUOTE_TOKEN, _OWNER
);
vm.stopBroadcast();

_dtlAddress = address(_dtl);

// Call configureDependencies to set everything that's needed
_mockBaselineGetModuleForKeycode();
_dtl.configureDependencies();
_;
}

modifier givenAllowlistParams(bytes32 merkleRoot_) {
_createData.allowlistParams = abi.encode(merkleRoot_);
_;
}
}
118 changes: 118 additions & 0 deletions test/callbacks/liquidity/BaselineV2/Allowlist/onBid.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.19;

import {BaselineAllowlistTest} from
"test/callbacks/liquidity/BaselineV2/Allowlist/BaselineAllowlistTest.sol";

import {BaseCallback} from "src/callbacks/BaseCallback.sol";
import {BALwithAllowlist} from "src/callbacks/liquidity/BaselineV2/BALwithAllowlist.sol";

contract BaselineAllowlistOnBidTest is BaselineAllowlistTest {
// Use the @openzeppelin/merkle-tree package or the scripts in axis-utils to generate the merkle tree

// Values:
// 0x0000000000000000000000000000000000000004
// 0x0000000000000000000000000000000000000005
bytes32 internal constant _MERKLE_ROOT =
0xc92348ba87c65979cc4f264810321a35efa64e795075908af2c507a22d4da472;
bytes32 internal constant _BUYER_MERKLE_PROOF =
0x16db2e4b9f8dc120de98f8491964203ba76de27b27b29c2d25f85a325cd37477;
bytes32 internal constant _NOT_SELLER_MERKLE_PROOF =
0xc167b0e3c82238f4f2d1a50a8b3a44f96311d77b148c30dc0ef863e1a060dcb6;

bytes32[] internal _proof;

uint64 internal constant _BID_ID = 1;

// ========== MODIFIER ========== //

modifier givenMerkleProof(bytes32 merkleProof_) {
bytes32[] memory proof = new bytes32[](1);
proof[0] = merkleProof_;

_proof = proof;
_;
}

function _onBid(uint256 bidAmount_) internal {
// Call the callback
vm.prank(address(_auctionHouse));
_dtl.onBid(_lotId, _BID_ID, _BUYER, bidAmount_, abi.encode(_proof));
}

// ========== TESTS ========== //

// [X] when the allowlist parameters are in an incorrect format
// [X] it reverts
// [X] when the merkle proof is invalid
// [X] it reverts
// [X] when the buyer is not in the merkle tree
// [X] it reverts
// [X] it succeeds

function test_parametersInvalid_reverts()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
givenOnCreate
{
// Expect revert
vm.expectRevert();

// Call the callback with an invalid parameter format
vm.prank(address(_auctionHouse));
_dtl.onBid(_lotId, _BID_ID, _BUYER, 5e18, abi.encode(uint256(20), bytes("something")));
}

function test_merkleProofInvalid_reverts()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
givenOnCreate
givenMerkleProof(_NOT_SELLER_MERKLE_PROOF)
{
// Expect revert
bytes memory err = abi.encodeWithSelector(BaseCallback.Callback_NotAuthorized.selector);
vm.expectRevert(err);

// Call the callback with an invalid merkle proof
_onBid(5e18);
}

function test_buyerNotInMerkleTree_reverts()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
givenOnCreate
givenMerkleProof(_BUYER_MERKLE_PROOF)
{
// Expect revert
bytes memory err = abi.encodeWithSelector(BaseCallback.Callback_NotAuthorized.selector);
vm.expectRevert(err);

// Call the callback
vm.prank(address(_auctionHouse));
_dtl.onBid(_lotId, _BID_ID, address(0x55), 5e18, abi.encode(_proof));
}

function test_success(uint256 bidAmount_)
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
givenOnCreate
givenMerkleProof(_BUYER_MERKLE_PROOF)
{
// Call the callback
_onBid(bidAmount_);

// Does not revert
}
}
51 changes: 51 additions & 0 deletions test/callbacks/liquidity/BaselineV2/Allowlist/onCreate.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.19;

import {BaselineAllowlistTest} from
"test/callbacks/liquidity/BaselineV2/Allowlist/BaselineAllowlistTest.sol";

import {BaseCallback} from "src/callbacks/BaseCallback.sol";
import {BALwithAllowlist} from "src/callbacks/liquidity/BaselineV2/BALwithAllowlist.sol";

contract BaselineAllowlistOnCreateTest is BaselineAllowlistTest {
/// @dev This doesn't need to be valid at the moment
bytes32 internal constant _MERKLE_ROOT =
0x1234567890123456789012345678901234567890123456789012345678901234;

// ========== TESTS ========== //

// [X] when the allowlist parameters are in an incorrect format
// [X] it reverts
// [X] it decodes and stores the merkle root

function test_allowlistParamsIncorrect_reverts()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
{
// Set the allowlist parameters to be an incorrect format
_createData.allowlistParams = abi.encode(uint256(20), bytes32("hello"), uint256(10));

// Expect revert
bytes memory err = abi.encodeWithSelector(BaseCallback.Callback_InvalidParams.selector);
vm.expectRevert(err);

// Call the callback
_onCreate();
}

function test_success()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
{
// Call the callback
_onCreate();

// Check the merkle root is stored
assertEq(BALwithAllowlist(address(_dtl)).merkleRoot(), _MERKLE_ROOT, "merkle root");
}
}
95 changes: 95 additions & 0 deletions test/callbacks/liquidity/BaselineV2/Allowlist/setMerkleRoot.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.19;

import {BaselineAllowlistTest} from
"test/callbacks/liquidity/BaselineV2/Allowlist/BaselineAllowlistTest.sol";

import {BaseCallback} from "src/callbacks/BaseCallback.sol";
import {BALwithAllowlist} from "src/callbacks/liquidity/BaselineV2/BALwithAllowlist.sol";

contract BaselineAllowlistSetMerkleRootTest is BaselineAllowlistTest {
/// @dev This doesn't need to be valid at the moment
bytes32 internal constant _MERKLE_ROOT =
0x1234567890123456789012345678901234567890123456789012345678901234;
bytes32 internal constant _NEW_MERKLE_ROOT =
0x1234567890123456789012345678901234567890123456789012345678901234;

function _setMerkleRoot() internal {
vm.prank(_OWNER);
BALwithAllowlist(address(_dtl)).setMerkleRoot(_NEW_MERKLE_ROOT);
}

// ========== TESTS ========== //

// [X] when the caller is not the owner
// [X] it reverts
// [X] when the auction has not been registered
// [X] it reverts
// [X] when the auction has been completed
// [X] it reverts
// [X] the merkle root is updated and an event is emitted

function test_notOwner_reverts()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
givenOnCreate
{
// Expect revert
vm.expectRevert("UNAUTHORIZED");

// Call the callback
BALwithAllowlist(address(_dtl)).setMerkleRoot(_NEW_MERKLE_ROOT);
}

function test_auctionNotRegistered_reverts()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
{
// Expect revert
bytes memory err = abi.encodeWithSelector(BALwithAllowlist.Callback_InvalidState.selector);
vm.expectRevert(err);

// Call the callback
_setMerkleRoot();
}

function test_auctionCompleted_reverts()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
givenOnCreate
givenAddressHasQuoteTokenBalance(_dtlAddress, _PROCEEDS_AMOUNT)
givenAddressHasBaseTokenBalance(_dtlAddress, _REFUND_AMOUNT)
givenOnSettle
{
// Expect revert
bytes memory err = abi.encodeWithSelector(BALwithAllowlist.Callback_InvalidState.selector);
vm.expectRevert(err);

// Call the callback
_setMerkleRoot();
}

function test_success()
public
givenBPoolIsCreated
givenCallbackIsCreated
givenAuctionIsCreated
givenAllowlistParams(_MERKLE_ROOT)
givenOnCreate
{
// Call the callback
_setMerkleRoot();

// Check the merkle root is updated
assertEq(BALwithAllowlist(address(_dtl)).merkleRoot(), _NEW_MERKLE_ROOT, "merkle root");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {BaselineAxisLaunchTest} from
"test/callbacks/liquidity/BaselineV2/BaselineAxisLaunchTest.sol";

// Axis
import {BALwithCappedAllowlist} from
"src/callbacks/liquidity/BaselineV2/BALwithCappedAllowlist.sol";
import {BALwithCappedAllowlist} from "src/callbacks/liquidity/BaselineV2/BALwithCappedAllowlist.sol";

contract BaselineCappedAllowlistTest is BaselineAxisLaunchTest {
uint256 internal constant _BUYER_LIMIT = 5e18;
Expand All @@ -18,9 +17,8 @@ contract BaselineCappedAllowlistTest is BaselineAxisLaunchTest {
// Get the salt
bytes memory args =
abi.encode(address(_auctionHouse), _BASELINE_KERNEL, _BASELINE_QUOTE_TOKEN, _OWNER);
bytes32 salt = _getTestSalt(
"BaselineCappedAllowlist", type(BALwithCappedAllowlist).creationCode, args
);
bytes32 salt =
_getTestSalt("BaselineCappedAllowlist", type(BALwithCappedAllowlist).creationCode, args);

// Required for CREATE2 address to work correctly. doesn't do anything in a test
// Source: https://github.com/foundry-rs/foundry/issues/6402
Expand Down
10 changes: 4 additions & 6 deletions test/callbacks/liquidity/BaselineV2/CappedAllowlist/onBid.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import {BaselineCappedAllowlistTest} from
"test/callbacks/liquidity/BaselineV2/CappedAllowlist/BaselineCappedAllowlistTest.sol";

import {BaseCallback} from "src/callbacks/BaseCallback.sol";
import {BALwithCappedAllowlist} from
"src/callbacks/liquidity/BaselineV2/BALwithCappedAllowlist.sol";
import {BALwithCappedAllowlist} from "src/callbacks/liquidity/BaselineV2/BALwithCappedAllowlist.sol";

contract BaselineCappedAllowlistOnBidTest is BaselineCappedAllowlistTest {
// Use the @openzeppelin/merkle-tree package or the scripts in axis-utils to generate the merkle tree

// Values:
// 0x0000000000000000000000000000000000000004, 5e18
// 0x0000000000000000000000000000000000000020, 0
// 0x0000000000000000000000000000000000000004
// 0x0000000000000000000000000000000000000005
bytes32 internal constant _MERKLE_ROOT =
0xc92348ba87c65979cc4f264810321a35efa64e795075908af2c507a22d4da472;
bytes32 internal constant _BUYER_MERKLE_PROOF =
Expand All @@ -22,7 +21,6 @@ contract BaselineCappedAllowlistOnBidTest is BaselineCappedAllowlistTest {
0xc167b0e3c82238f4f2d1a50a8b3a44f96311d77b148c30dc0ef863e1a060dcb6;

bytes32[] internal _proof;
uint256 internal _allocatedAmount;

uint64 internal constant _BID_ID = 1;

Expand Down Expand Up @@ -108,7 +106,7 @@ contract BaselineCappedAllowlistOnBidTest is BaselineCappedAllowlistTest {

// Call the callback
vm.prank(address(_auctionHouse));
_dtl.onBid(_lotId, _BID_ID, address(0x55), 5e18, abi.encode(_proof, _allocatedAmount));
_dtl.onBid(_lotId, _BID_ID, address(0x55), 5e18, abi.encode(_proof));
}

function test_buyerLimitSpent_reverts()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {BaselineCappedAllowlistTest} from
"test/callbacks/liquidity/BaselineV2/CappedAllowlist/BaselineCappedAllowlistTest.sol";

import {BaseCallback} from "src/callbacks/BaseCallback.sol";
import {BALwithCappedAllowlist} from
"src/callbacks/liquidity/BaselineV2/BALwithCappedAllowlist.sol";
import {BALwithCappedAllowlist} from "src/callbacks/liquidity/BaselineV2/BALwithCappedAllowlist.sol";

contract BaselineCappedAllowlistOnCreateTest is BaselineCappedAllowlistTest {
/// @dev This doesn't need to be valid at the moment
Expand Down
Loading

0 comments on commit d570ca1

Please sign in to comment.