Skip to content

Commit

Permalink
Amend derivative test for purchase()
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJem committed Jan 17, 2024
1 parent b7a394c commit a681c44
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions test/AuctionHouse/purchase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ pragma solidity 0.8.19;

// Libraries
import {Test} from "forge-std/Test.sol";
import {ERC20} from "lib/solmate/src/tokens/ERC20.sol";
import {ERC20} from "solmate/tokens/ERC20.sol";
import {IPermit2} from "src/lib/permit2/interfaces/IPermit2.sol";

// Mocks
import {MockERC20} from "lib/solmate/src/test/utils/mocks/MockERC20.sol";
import {MockERC20} from "solmate/test/utils/mocks/MockERC20.sol";
import {MockERC6909} from "solmate/test/utils/mocks/MockERC6909.sol";
import {MockAtomicAuctionModule} from "test/modules/Auction/MockAtomicAuctionModule.sol";
import {MockBatchAuctionModule} from "test/modules/Auction/MockBatchAuctionModule.sol";
import {MockDerivativeModule} from "test/modules/Derivative/MockDerivativeModule.sol";
Expand Down Expand Up @@ -35,6 +36,7 @@ import {
contract PurchaseTest is Test, Permit2User {
MockERC20 internal baseToken;
MockERC20 internal quoteToken;
MockERC6909 internal derivativeToken;
MockAtomicAuctionModule internal mockAuctionModule;
MockDerivativeModule internal mockDerivativeModule;
MockCondenserModule internal mockCondenserModule;
Expand Down Expand Up @@ -70,13 +72,15 @@ contract PurchaseTest is Test, Permit2User {
uint256 internal approvalNonce;
bytes internal approvalSignature;
uint48 internal approvalDeadline;
uint256 internal derivativeTokenId;

function setUp() external {
aliceKey = _getRandomUint256();
alice = vm.addr(aliceKey);

baseToken = new MockERC20("Base Token", "BASE", 18);
quoteToken = new MockERC20("Quote Token", "QUOTE", 18);
derivativeToken = new MockERC6909();

auctionHouse = new AuctionHouse(protocol, _PERMIT2_ADDRESS);
mockAuctionModule = new MockAtomicAuctionModule(address(auctionHouse));
Expand All @@ -85,8 +89,6 @@ contract PurchaseTest is Test, Permit2User {
mockAllowlist = new MockAllowlist();
mockHook = new MockHook(address(quoteToken), address(baseToken));

// mockDerivativeModule.setDerivativeToken(baseToken);

auctionParams = Auction.AuctionParams({
start: uint48(block.timestamp),
duration: uint48(1 days),
Expand Down Expand Up @@ -149,12 +151,11 @@ contract PurchaseTest is Test, Permit2User {
_;
}

modifier whenCondenserModuleIsInstalled() {
modifier givenDerivativeHasCondenser() {
// Install the condenser module
auctionHouse.installModule(mockCondenserModule);
_;
}

modifier whenCondenserIsMapped() {
// Set the condenser
auctionHouse.setCondenser(
mockAuctionModule.VEECODE(),
mockDerivativeModule.VEECODE(),
Expand Down Expand Up @@ -539,11 +540,17 @@ contract PurchaseTest is Test, Permit2User {
// ======== Derivative flow ======== //

modifier givenAuctionHasDerivative() {
// Assumes the derivative module is already installed
// Install the derivative module
auctionHouse.installModule(mockDerivativeModule);

mockDerivativeModule.setDerivativeToken(derivativeToken);

// Set up a new auction with a derivative
derivativeTokenId = 20;
routingParams.derivativeType = toKeycode("DERV");
routingParams.derivativeParams = abi.encode("");
routingParams.derivativeParams =
abi.encode(MockDerivativeModule.Params({tokenId: derivativeTokenId, multiplier: 0}));

vm.prank(auctionOwner);
lotId = auctionHouse.auction(routingParams, auctionParams);

Expand All @@ -557,28 +564,40 @@ contract PurchaseTest is Test, Permit2User {

function test_derivative()
public
givenDerivativeModuleIsInstalled
givenAuctionHasDerivative
givenUserHasQuoteTokenBalance(AMOUNT_IN)
givenOwnerHasBaseTokenBalance(AMOUNT_OUT)
givenQuoteTokenSpendingIsApproved
givenBaseTokenSpendingIsApproved
{
// Call
vm.prank(alice);
auctionHouse.purchase(purchaseParams);

// Check balances
// Check balances of the quote token
assertEq(quoteToken.balanceOf(alice), 0);
assertEq(quoteToken.balanceOf(recipient), 0);
assertEq(quoteToken.balanceOf(address(mockHook)), 0);
assertEq(
quoteToken.balanceOf(address(auctionHouse)), amountInProtocolFee + amountInReferrerFee
);
assertEq(quoteToken.balanceOf(auctionOwner), amountInLessFee);
assertEq(quoteToken.balanceOf(address(mockDerivativeModule)), 0);

// Check balances of the base token
assertEq(baseToken.balanceOf(alice), 0);
assertEq(baseToken.balanceOf(recipient), AMOUNT_OUT);
assertEq(baseToken.balanceOf(recipient), 0);
assertEq(baseToken.balanceOf(address(mockHook)), 0);
assertEq(baseToken.balanceOf(address(auctionHouse)), 0);
assertEq(baseToken.balanceOf(auctionOwner), 0);
assertEq(baseToken.balanceOf(address(mockDerivativeModule)), AMOUNT_OUT);

// Check balances of the derivative token
assertEq(derivativeToken.balanceOf(alice, derivativeTokenId), 0);
assertEq(derivativeToken.balanceOf(recipient, derivativeTokenId), AMOUNT_OUT);
assertEq(derivativeToken.balanceOf(address(mockHook), derivativeTokenId), 0);
assertEq(derivativeToken.balanceOf(address(auctionHouse), derivativeTokenId), 0);
assertEq(derivativeToken.balanceOf(auctionOwner, derivativeTokenId), 0);
assertEq(derivativeToken.balanceOf(address(mockDerivativeModule), derivativeTokenId), 0);
}
}

0 comments on commit a681c44

Please sign in to comment.