-
Notifications
You must be signed in to change notification settings - Fork 3
enable swaps for buy orders #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
7a3e087
f8128bc
3492ab2
08877fa
1cd82b4
cc3083f
b50fba4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity 0.8.1; | ||
|
|
||
| import {LibAppStorage, AppStorage} from "../libraries/LibAppStorage.sol"; | ||
| import {LibTokenSwap} from "../libraries/LibTokenSwap.sol"; | ||
| import {LibBuyOrder} from "../libraries/LibBuyOrder.sol"; | ||
| import {IERC20} from "../../shared/interfaces/IERC20.sol"; | ||
| import {Modifiers} from "../libraries/LibAppStorage.sol"; | ||
|
|
||
| contract BuyOrderSwapFacet is Modifiers { | ||
| struct ERC1155SwapParams { | ||
| address tokenIn; | ||
| uint256 swapAmount; | ||
| uint256 minGhstOut; | ||
| uint256 swapDeadline; | ||
| uint256 buyOrderId; | ||
| address erc1155TokenAddress; | ||
| uint256 erc1155TokenId; | ||
| uint256 category; | ||
| uint256 priceInWei; | ||
| uint256 quantity; | ||
| uint256 duration; | ||
| address recipient; | ||
| } | ||
|
|
||
| struct ERC721SwapParams { | ||
| address tokenIn; | ||
| uint256 swapAmount; | ||
| uint256 minGhstOut; | ||
| uint256 swapDeadline; | ||
| uint256 buyOrderId; | ||
| address erc721TokenAddress; | ||
| uint256 erc721TokenId; | ||
| uint256 category; | ||
| uint256 priceInWei; | ||
| uint256 duration; | ||
| address recipient; | ||
| } | ||
|
|
||
| event SwapAndPlaceERC1155BuyOrder(address indexed buyer, uint256 indexed buyOrderId, address indexed tokenIn, uint256 ghstReceived); | ||
| event SwapAndPlaceERC721BuyOrder(address indexed buyer, uint256 indexed buyOrderId, address indexed tokenIn, uint256 ghstReceived); | ||
|
|
||
| /** | ||
| * @notice Swap tokens for GHST and immediately place a new ERC1155 buy order | ||
| * @param params Packed swap and order parameters | ||
| */ | ||
| function swapAndPlaceERC1155BuyOrder(ERC1155SwapParams calldata params) external payable whenNotPaused { | ||
| LibTokenSwap.validateSwapParams(params.tokenIn, params.swapAmount, params.minGhstOut, params.swapDeadline); | ||
| uint256 totalCost = LibBuyOrder.validateERC1155Params(params.erc1155TokenAddress, params.erc1155TokenId, params.priceInWei, params.quantity); | ||
|
|
||
| //assert ghst to be swapped to is enough | ||
| require(params.minGhstOut >= totalCost, "ERC1155BuyOrderSwap: minGhstOut must cover total cost"); | ||
|
|
||
| uint256 initialBalance = IERC20(s.ghstContract).balanceOf(address(this)); | ||
|
|
||
| //perform swap | ||
| uint256 ghstReceived = LibTokenSwap.swapForGHST(params.tokenIn, params.swapAmount, params.minGhstOut, params.swapDeadline, address(this)); | ||
|
|
||
| //make sure we have received enough ghst | ||
| require(ghstReceived >= totalCost, "ERC1155BuyOrderSwap: Insufficient GHST for purchase"); | ||
|
|
||
| //place buy order | ||
| LibBuyOrder._placeERC1155BuyOrder( | ||
| params.erc1155TokenAddress, | ||
| params.erc1155TokenId, | ||
| params.category, | ||
| params.priceInWei, | ||
| params.quantity, | ||
| params.duration | ||
| ); | ||
|
|
||
| // Refund any excess GHST to the recipient using shared library, leave totalCost in the diamond | ||
| LibTokenSwap.refundExcessGHST(params.recipient, initialBalance + totalCost); | ||
|
|
||
| emit SwapAndPlaceERC1155BuyOrder(msg.sender, params.buyOrderId, params.tokenIn, ghstReceived); | ||
| } | ||
|
|
||
| /** | ||
| * @notice Swap tokens for GHST and immediately place a new ERC721 buy order | ||
| * @param params Packed swap and order parameters | ||
| * @param validationOptions Flags that determine additional validation logic | ||
| */ | ||
| function swapAndPlaceERC721BuyOrder(ERC721SwapParams calldata params, bool[] calldata validationOptions) external payable whenNotPaused { | ||
| LibTokenSwap.validateSwapParams(params.tokenIn, params.swapAmount, params.minGhstOut, params.swapDeadline); | ||
| uint256 totalCost = LibBuyOrder.validateERC721Params( | ||
| params.erc721TokenAddress, | ||
| params.erc721TokenId, | ||
| params.category, | ||
| params.priceInWei, | ||
| validationOptions | ||
| ); | ||
|
|
||
| require(params.minGhstOut >= totalCost, "ERC721BuyOrderSwap: minGhstOut must cover total cost"); | ||
|
|
||
| uint256 initialBalance = IERC20(s.ghstContract).balanceOf(address(this)); | ||
|
|
||
| //perform swap | ||
| uint256 ghstReceived = LibTokenSwap.swapForGHST(params.tokenIn, params.swapAmount, params.minGhstOut, params.swapDeadline, address(this)); | ||
|
|
||
| //make sure we have received enough ghst | ||
| require(ghstReceived >= totalCost, "ERC721BuyOrderSwap: Insufficient GHST for purchase"); | ||
|
|
||
| //place buy order | ||
| LibBuyOrder._placeERC721BuyOrder( | ||
| params.erc721TokenAddress, | ||
| params.erc721TokenId, | ||
| params.category, | ||
| params.priceInWei, | ||
| params.duration, | ||
| validationOptions | ||
| ); | ||
|
|
||
| // Refund any excess GHST to the recipient using shared library, leave totalCost in the diamond | ||
| LibTokenSwap.refundExcessGHST(params.recipient, initialBalance + totalCost); | ||
|
|
||
| emit SwapAndPlaceERC721BuyOrder(msg.sender, params.buyOrderId, params.tokenIn, ghstReceived); | ||
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -15,18 +15,6 @@ import {BaazaarSplit, LibSharedMarketplace, SplitAddresses} from "../libraries/L | |||
| import {CollateralEscrow} from "../CollateralEscrow.sol"; | ||||
|
|
||||
| contract ERC721BuyOrderFacet is Modifiers { | ||||
| event ERC721BuyOrderAdded( | ||||
| uint256 indexed buyOrderId, | ||||
| address indexed buyer, | ||||
| address erc721TokenAddress, | ||||
| uint256 erc721TokenId, | ||||
| uint256 indexed category, | ||||
| uint256 priceInWei, | ||||
| uint256 duration, | ||||
| bytes32 validationHash, | ||||
| uint256 time | ||||
| ); | ||||
| event ERC721BuyOrderCanceled(uint256 indexed buyOrderId, uint256 time); | ||||
| event ERC721BuyOrderExecuted( | ||||
| uint256 indexed buyOrderId, | ||||
| address indexed buyer, | ||||
|
|
@@ -92,71 +80,19 @@ contract ERC721BuyOrderFacet is Modifiers { | |||
| uint256 _duration, | ||||
| bool[] calldata _validationOptions // 0: BRS, 1: GHST, 2: skill points | ||||
| ) external whenNotPaused { | ||||
| require(_priceInWei >= 1e18, "ERC721BuyOrder: price should be 1 GHST or larger"); | ||||
| // require(_priceInWei >= 1e18, "ERC721BuyOrder: price should be 1 GHST or larger"); | ||||
|
|
||||
|
||||
| // require(_priceInWei >= 1e18, "ERC721BuyOrder: price should be 1 GHST or larger"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The buyOrderId parameter is used in the event emission but it's not set during the function execution. The actual buyOrderId is generated inside LibBuyOrder._placeERC1155BuyOrder and LibBuyOrder._placeERC721BuyOrder functions, but those values are not returned or captured here.