Skip to content

Commit

Permalink
Add structs for module parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJem committed Jan 17, 2024
1 parent 7f5b4cc commit 35292ee
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 19 deletions.
42 changes: 35 additions & 7 deletions src/AuctionHouse.sol
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ contract AuctionHouse is Derivatizer, Auctioneer, Router {
/// @notice Sends the payout token to the recipient
/// @dev This function handles the following:
/// 1. Sends the payout token from the router to the recipient
/// 1a. If the lot is a derivative, mints the derivative token to the recipient
/// 2. Calls the post hook on the hooks contract (if provided)
///
/// This function assumes that:
Expand All @@ -494,15 +495,42 @@ contract AuctionHouse is Derivatizer, Auctioneer, Router {
Routing memory routingParams_,
bytes memory auctionOutput_
) internal {
// Get the pre-transfer balance
uint256 balanceBefore = routingParams_.baseToken.balanceOf(recipient_);
// If no derivative, then the payout is sent directly to the recipient
if (fromVeecode(routingParams_.derivativeReference) == bytes7("")) {
// Get the pre-transfer balance
uint256 balanceBefore = routingParams_.baseToken.balanceOf(recipient_);

// Send payout token to recipient
routingParams_.baseToken.safeTransfer(recipient_, payoutAmount_);
// Send payout token to recipient
routingParams_.baseToken.safeTransfer(recipient_, payoutAmount_);

// Check that the recipient received the expected amount of payout tokens
if (routingParams_.baseToken.balanceOf(recipient_) < balanceBefore + payoutAmount_) {
revert UnsupportedToken(address(routingParams_.baseToken));
// Check that the recipient received the expected amount of payout tokens
if (routingParams_.baseToken.balanceOf(recipient_) < balanceBefore + payoutAmount_) {
revert UnsupportedToken(address(routingParams_.baseToken));
}
}
// Otherwise, send parameters and payout to the derivative to mint to recipient
else {
// Get the module for the derivative type
// We assume that the module type has been checked when the lot was created
DerivativeModule module =
DerivativeModule(_getModuleIfInstalled(routingParams_.derivativeReference));

bytes memory derivativeParams = routingParams_.derivativeParams;

// Lookup condensor module from combination of auction and derivative types
// If condenser specified, condense auction output and derivative params before sending to derivative module
Veecode condenserRef =
condensers[routingParams_.auctionReference][routingParams_.derivativeReference];
if (fromVeecode(condenserRef) != bytes7("")) {
// Get condenser module
CondenserModule condenser = CondenserModule(_getModuleIfInstalled(condenserRef));

// Condense auction output and derivative params
derivativeParams = condenser.condense(auctionOutput_, derivativeParams);
}

// Call the module to mint derivative tokens to the recipient
module.mint(recipient_, derivativeParams, payoutAmount_, routingParams_.wrapDerivative);
}

// Call post hook on hooks contract if provided
Expand Down
13 changes: 7 additions & 6 deletions test/AuctionHouse/sendPayout.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ contract SendPayoutTest is Test, Permit2User {
mockAuctionModule = new MockAtomicAuctionModule(address(auctionHouse));
mockDerivativeModule = new MockDerivativeModule(address(auctionHouse));
mockCondenserModule = new MockCondenserModule(address(auctionHouse));
auctionHouse.installModule(mockAuctionModule);

quoteToken = new MockFeeOnTransferERC20("Quote Token", "QUOTE", 18);
quoteToken.setTransferFee(0);
Expand All @@ -68,7 +69,8 @@ contract SendPayoutTest is Test, Permit2User {
derivativeParams = bytes("");
wrapDerivative = false;
auctionOutputMultiplier = 2;
auctionOutput = abi.encode(auctionOutputMultiplier); // Does nothing unless the condenser is set
auctionOutput =
abi.encode(MockAtomicAuctionModule.Output({multiplier: auctionOutputMultiplier})); // Does nothing unless the condenser is set

routingParams = Auctioneer.Routing({
auctionReference: mockAuctionModule.VEECODE(),
Expand Down Expand Up @@ -265,7 +267,8 @@ contract SendPayoutTest is Test, Permit2User {
// Update parameters
derivativeReference = mockDerivativeModule.VEECODE();
derivativeTokenId = 20;
derivativeParams = abi.encode(derivativeTokenId, 0);
derivativeParams =
abi.encode(MockDerivativeModule.Params({tokenId: derivativeTokenId, multiplier: 0}));
routingParams.derivativeReference = derivativeReference;
routingParams.derivativeParams = derivativeParams;
_;
Expand Down Expand Up @@ -301,10 +304,8 @@ contract SendPayoutTest is Test, Permit2User {
givenAuctionHasDerivative
givenDerivativeParamsAreInvalid
{
// Expect revert
bytes memory err =
abi.encodeWithSelector(MockDerivativeModule.InvalidDerivativeParams.selector);
vm.expectRevert(err);
// Expect revert while decoding parameters
vm.expectRevert();

// Call
vm.prank(USER);
Expand Down
8 changes: 7 additions & 1 deletion test/modules/Auction/MockAtomicAuctionModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ contract MockAtomicAuctionModule is AuctionModule {
mapping(uint256 => uint256) public payoutData;
bool public purchaseReverts;

struct Output {
uint256 multiplier;
}

mapping(uint256 lotId => bool isCancelled) public cancelled;

constructor(address _owner) AuctionModule(_owner) {
Expand Down Expand Up @@ -52,7 +56,9 @@ contract MockAtomicAuctionModule is AuctionModule {
payout = payoutData[id_] * amount_ / 1e5;
}

auctionOutput = auctionData_;
Output memory output = Output({multiplier: 1});

auctionOutput = abi.encode(output);
}

function setPayoutMultiplier(uint256 id_, uint256 multiplier_) external virtual {
Expand Down
13 changes: 11 additions & 2 deletions test/modules/Condenser/MockCondenserModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ pragma solidity 0.8.19;
// Modules
import {Module, Veecode, toKeycode, wrapVeecode} from "src/modules/Modules.sol";

import {MockAtomicAuctionModule} from "test/modules/Auction/MockAtomicAuctionModule.sol";
import {MockDerivativeModule} from "test/modules/Derivative/MockDerivativeModule.sol";

// Condenser
import {CondenserModule} from "src/modules/Condenser.sol";

Expand All @@ -23,12 +26,18 @@ contract MockCondenserModule is CondenserModule {
bytes memory derivativeConfig_
) external pure virtual override returns (bytes memory) {
// Get auction output
(uint256 auctionMultiplier) = abi.decode(auctionOutput_, (uint256));
MockAtomicAuctionModule.Output memory auctionOutput =
abi.decode(auctionOutput_, (MockAtomicAuctionModule.Output));

// Get derivative params
(uint256 derivativeTokenId) = abi.decode(derivativeConfig_, (uint256));

MockDerivativeModule.Params memory derivativeParams = MockDerivativeModule.Params({
tokenId: derivativeTokenId,
multiplier: auctionOutput.multiplier
});

// Return condensed output
return abi.encode(derivativeTokenId, auctionMultiplier);
return abi.encode(derivativeParams);
}
}
11 changes: 8 additions & 3 deletions test/modules/Derivative/MockDerivativeModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ contract MockDerivativeModule is DerivativeModule {

error InvalidDerivativeParams();

struct Params {
uint256 tokenId;
uint256 multiplier;
}

constructor(address _owner) Module(_owner) {}

function VEECODE() public pure virtual override returns (Veecode) {
Expand All @@ -37,11 +42,11 @@ contract MockDerivativeModule is DerivativeModule {
bool wrapped_
) external virtual override returns (uint256, address, uint256) {
// TODO wrapping
(uint256 tokenId, uint256 multiplier) = abi.decode(params_, (uint256, uint256));
Params memory params = abi.decode(params_, (Params));

uint256 outputAmount = multiplier == 0 ? amount_ : amount_ * multiplier;
uint256 outputAmount = params.multiplier == 0 ? amount_ : amount_ * params.multiplier;

derivativeToken.mint(to_, tokenId, outputAmount);
derivativeToken.mint(to_, params.tokenId, outputAmount);
}

function mint(
Expand Down

0 comments on commit 35292ee

Please sign in to comment.