-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add Marketplace contract #33
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
Open
MichaelGrinstead
wants to merge
1
commit into
master
Choose a base branch
from
mg/marketplace
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule forge-std
updated
6 files
| +1 −0 | .gitattributes | |
| +1 −1 | package.json | |
| +666 −0 | scripts/vm.py | |
| +734 −472 | src/Vm.sol | |
| +3 −1 | test/StdError.t.sol | |
| +2 −2 | test/Vm.t.sol |
Submodule openzeppelin-contracts-upgradeable
updated
140 files
Submodule openzeppelin-foundry-upgrades
updated
12 files
| +2 −3 | .github/workflows/checks.yml | |
| +50 −0 | DEFENDER.md | |
| +4 −0 | README.md | |
| +20 −0 | src/Defender.sol | |
| +24 −51 | src/Upgrades.sol | |
| +75 −0 | src/internal/DefenderDeploy.sol | |
| +186 −0 | src/internal/Utils.sol | |
| +2 −0 | src/internal/Versions.sol | |
| +16 −0 | test/Defender.s.sol | |
| +37 −0 | test/DefenderDeploy.t.sol | |
| +120 −0 | test/Utils.t.sol | |
| +8 −0 | test/contracts/MyContractFile.sol |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,248 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity 0.8.21; | ||
|
|
||
| import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
| import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; | ||
| import "@openzeppelin/contracts/access/AccessControl.sol"; | ||
| import "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; | ||
| import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol"; | ||
| import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol"; | ||
| import "../src/interfaces/IMarketplace.sol"; | ||
| import "../src/interfaces/Releases/IReleasesRegistration.sol"; | ||
| import "../src/interfaces/Releases/IReleases.sol"; | ||
|
|
||
| /** | ||
| * @title Marketplace | ||
| * @dev This contract allows buying and selling of Releases and charges a fee on each sale. | ||
| */ | ||
| contract Marketplace is IMarketplace, ERC1155Holder, ReentrancyGuard, AccessControl { | ||
| using SafeERC20 for IERC20; | ||
|
|
||
| // State Variables | ||
|
|
||
| IERC20 _token; | ||
| IReleasesRegistration _catalog; | ||
| address payable public treasury; | ||
| uint256 public treasuryFee; | ||
|
|
||
| /// @dev releaseOwner => saleId => Sale | ||
| mapping(address => Sale[]) private sales; | ||
|
|
||
| // Errors | ||
| error CannotBeZeroAddress(); | ||
| error TreasuryFeeCannotBeZero(); | ||
| error TokenAmountCannotBeZero(); | ||
| error MaxCountCannotBeZero(); | ||
| error StartCannotBeAfterEnd(uint256 startTime, uint256 endTime); | ||
| error InsufficientSupply(uint256 remainingSupply); | ||
| error MaxSupplyReached(uint256 maxSupplyPerWallet); | ||
| error SaleNotStarted(uint256 startTime); | ||
| error SaleHasEnded(uint256 endTime, uint256 currentTime); | ||
| error ReleasesIsNotRegistered(); | ||
| error OnlySellerCanWithdraw(); | ||
|
|
||
| /** | ||
| * @dev Constructor | ||
| * @param treasury_ - The address of the organizations treasury | ||
| * @param treasuryFee_ - The percentage that will be transferred | ||
| * to the treasury on each sale. Based on a denominator of 10_000 e.g. 1000 = 10% | ||
| * @param token - A token that implements an IERC20 interface that will be used for payments | ||
| * @param catalog - A contract that implements the IReleasesRegistration interface | ||
| */ | ||
| constructor( | ||
| address payable treasury_, | ||
| uint256 treasuryFee_, | ||
| IERC20 token, | ||
| IReleasesRegistration catalog | ||
| ) { | ||
| if (address(token) == address(0)) revert CannotBeZeroAddress(); | ||
| if (treasuryFee_ == 0) revert TreasuryFeeCannotBeZero(); | ||
| if (treasury_ == address(0)) revert CannotBeZeroAddress(); | ||
| treasury = treasury_; | ||
| treasuryFee = treasuryFee_; | ||
| _token = token; | ||
| _catalog = catalog; | ||
| _grantRole(DEFAULT_ADMIN_ROLE, msg.sender); | ||
| } | ||
|
|
||
| // External Functions | ||
|
|
||
| /** | ||
| * @inheritdoc IMarketplace | ||
| */ | ||
| function createSale( | ||
| address releaseOwner, | ||
| address payable beneficiary, | ||
| IReleases releases, | ||
| uint256 tokenId, | ||
| uint256 amountTotal, | ||
| uint256 pricePerToken, | ||
| uint256 startAt, | ||
| uint256 endAt, | ||
| uint256 maxCountPerWallet | ||
| ) external { | ||
| if (IReleasesRegistration(_catalog).getReleasesOwner(address(releases)) == address(0)) { | ||
| revert ReleasesIsNotRegistered(); | ||
| } | ||
| if (beneficiary == address(0)) revert CannotBeZeroAddress(); | ||
| if (amountTotal == 0) revert TokenAmountCannotBeZero(); | ||
| if (endAt != 0 && startAt > endAt) { | ||
| revert StartCannotBeAfterEnd(startAt, endAt); | ||
| } | ||
| if (maxCountPerWallet == 0) revert MaxCountCannotBeZero(); | ||
| IERC1155(address(releases)).safeTransferFrom( | ||
| _msgSender(), address(this), tokenId, amountTotal, "" | ||
| ); | ||
|
|
||
| sales[releaseOwner].push( | ||
| Sale({ | ||
| seller: _msgSender(), | ||
| releaseOwner: releaseOwner, | ||
| beneficiary: beneficiary, | ||
| releases: address(releases), | ||
| tokenId: tokenId, | ||
| amountRemaining: amountTotal, | ||
| amountTotal: amountTotal, | ||
| pricePerToken: pricePerToken, | ||
| startAt: startAt, | ||
| endAt: endAt, | ||
| maxCountPerWallet: maxCountPerWallet | ||
| }) | ||
| ); | ||
|
|
||
| emit SaleCreated(releaseOwner, sales[releaseOwner].length - 1); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc IMarketplace | ||
| */ | ||
| function purchase( | ||
| address releaseOwner, | ||
| uint256 saleId, | ||
| uint256 tokenAmount, | ||
| address recipient | ||
| ) external nonReentrant { | ||
| Sale storage sale = _getSaleForPurchase(releaseOwner, saleId, tokenAmount); | ||
|
|
||
| uint256 totalPrice = sale.pricePerToken * tokenAmount; | ||
| uint256 fee = (treasuryFee * totalPrice) / 10_000; | ||
| _token.safeTransferFrom(_msgSender(), address(this), totalPrice); | ||
| _token.transfer(treasury, fee); | ||
| _token.transfer(sale.beneficiary, totalPrice - fee); | ||
|
|
||
| _transferTokens(sale.releases, sale.tokenId, tokenAmount, recipient); | ||
|
|
||
| sale.amountRemaining -= tokenAmount; | ||
|
|
||
| emit Purchase( | ||
| sale.releases, sale.tokenId, recipient, releaseOwner, saleId, tokenAmount, block.timestamp | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc IMarketplace | ||
| */ | ||
| function withdraw(address releaseOwner, uint256 saleId, uint256 tokenAmount) external nonReentrant { | ||
| if (tokenAmount == 0) revert TokenAmountCannotBeZero(); | ||
| Sale storage sale = sales[releaseOwner][saleId]; | ||
| if (_msgSender() != sale.seller) revert OnlySellerCanWithdraw(); | ||
| if (tokenAmount > sale.amountRemaining) { | ||
| revert InsufficientSupply(sale.amountRemaining); | ||
| } | ||
| _transferTokens(sale.releases, sale.tokenId, tokenAmount, _msgSender()); | ||
|
|
||
| sale.amountRemaining -= tokenAmount; | ||
|
|
||
| emit Withdraw(_msgSender(), saleId, tokenAmount); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc IMarketplace | ||
| */ | ||
| function getSale(address releaseOwner, uint256 saleId) external view returns (Sale memory) { | ||
| return sales[releaseOwner][saleId]; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc IMarketplace | ||
| */ | ||
| function saleCount(address releaseOwner) external view returns (uint256) { | ||
| return sales[releaseOwner].length; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc IMarketplace | ||
| */ | ||
| function setTreasuryFee(uint256 newFee) external onlyRole(DEFAULT_ADMIN_ROLE) { | ||
| treasuryFee = newFee; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc IMarketplace | ||
| */ | ||
| function setTreasury(address payable newTreasury) external onlyRole(DEFAULT_ADMIN_ROLE) { | ||
| treasury = newTreasury; | ||
| } | ||
|
|
||
| // Public Functions | ||
|
|
||
| /** | ||
| * @inheritdoc ERC165 | ||
| */ | ||
| function supportsInterface(bytes4 interfaceId) | ||
| public | ||
| view | ||
| override(AccessControl, ERC1155Holder) | ||
| returns (bool) | ||
| { | ||
| return super.supportsInterface(interfaceId); | ||
| } | ||
|
|
||
| // Internal Functions | ||
|
|
||
| /** | ||
| * @dev Verifies the purchase process for a sale | ||
| * @param releaseOwner - The address of the releaseOwner | ||
| * @param saleId - The id of the sale | ||
| * @param tokenAmount - The amount of tokens to purchase | ||
| */ | ||
| function _getSaleForPurchase( | ||
| address releaseOwner, | ||
| uint256 saleId, | ||
| uint256 tokenAmount | ||
| ) internal view returns (Sale storage) { | ||
| Sale storage sale = sales[releaseOwner][saleId]; | ||
| if (sale.startAt > block.timestamp) { | ||
| revert SaleNotStarted(sale.startAt); | ||
| } | ||
| if (sale.endAt != 0 && sale.endAt < block.timestamp) { | ||
| revert SaleHasEnded(sale.endAt, block.timestamp); | ||
| } | ||
| if (tokenAmount == 0) revert TokenAmountCannotBeZero(); | ||
| if (tokenAmount > sale.amountRemaining) { | ||
| revert InsufficientSupply(sale.amountRemaining); | ||
| } | ||
|
|
||
| uint256 buyerBalance = IERC1155(sale.releases).balanceOf(_msgSender(), sale.tokenId); | ||
| if ((buyerBalance + tokenAmount) > sale.maxCountPerWallet) { | ||
| revert MaxSupplyReached(sale.maxCountPerWallet); | ||
| } | ||
| return sale; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Transfers Release tokens from the contract to the recipient | ||
| * @param releases - The address of the Releases contract | ||
| * @param tokenId - The id of the token | ||
| * @param tokenAmount - The amount of tokens to transfer | ||
| * @param recipient - The address that will receive the Tokens | ||
| */ | ||
| function _transferTokens( | ||
| address releases, | ||
| uint256 tokenId, | ||
| uint256 tokenAmount, | ||
| address recipient | ||
| ) internal { | ||
| IERC1155(releases).safeTransferFrom(address(this), recipient, tokenId, tokenAmount, ""); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.