Skip to content
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

Change listing fee(SOLID-90) #173

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ update-submodules:
test-events:
@echo Running tests for wearable events
@forge test --mc PeripheryTest -vvvvv

test-listingFee:
@echo Testing listingFee changes
@forge t --mc ListingFeeTest -vvvv
29 changes: 6 additions & 23 deletions contracts/Aavegotchi/facets/ERC1155MarketplaceFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,7 @@ contract ERC1155MarketplaceFacet is Modifiers {
///@param _erc1155TypeId The identifier of the NFT to be listed
///@param _quantity The amount of NFTs to be listed
///@param _priceInWei The cost price of the NFT individually in $GHST
function setERC1155Listing(
address _erc1155TokenAddress,
uint256 _erc1155TypeId,
uint256 _quantity,
uint256 _priceInWei
) external {
function setERC1155Listing(address _erc1155TokenAddress, uint256 _erc1155TypeId, uint256 _quantity, uint256 _priceInWei) external payable {
createERC1155Listing(_erc1155TokenAddress, _erc1155TypeId, _quantity, _priceInWei, [10000, 0], address(0));
}

Expand All @@ -176,7 +171,7 @@ contract ERC1155MarketplaceFacet is Modifiers {
uint256 _priceInWei,
uint16[2] memory _principalSplit,
address _affiliate
) external {
) external payable {
createERC1155Listing(_erc1155TokenAddress, _erc1155TypeId, _quantity, _priceInWei, _principalSplit, _affiliate);
}

Expand Down Expand Up @@ -241,7 +236,7 @@ contract ERC1155MarketplaceFacet is Modifiers {

//Burn listing fee
if (s.listingFeeInWei > 0) {
LibSharedMarketplace.burnListingFee(s.listingFeeInWei, LibMeta.msgSender(), s.ghstContract);
LibSharedMarketplace.burnListingFee(s.listingFeeInWei, LibMeta.msgSender());
}
}

Expand All @@ -258,11 +253,7 @@ contract ERC1155MarketplaceFacet is Modifiers {
///@param _listingId The identifier of the listing to execute
///@param _quantity The amount of ERC1155 NFTs execute/buy
///@param _priceInWei the cost price of the ERC1155 NFTs individually
function executeERC1155Listing(
uint256 _listingId,
uint256 _quantity,
uint256 _priceInWei
) external {
function executeERC1155Listing(uint256 _listingId, uint256 _quantity, uint256 _priceInWei) external {
ERC1155Listing storage listing = s.erc1155Listings[_listingId];
handleExecuteERC1155Listing(_listingId, listing.erc1155TokenAddress, listing.erc1155TypeId, _quantity, _priceInWei, LibMeta.msgSender());
}
Expand Down Expand Up @@ -387,23 +378,15 @@ contract ERC1155MarketplaceFacet is Modifiers {
///@param _erc1155TokenAddress Contract address of the ERC1155 token
///@param _erc1155TypeId Identifier of the ERC1155 token
///@param _owner Owner of the ERC1155 token
function updateERC1155Listing(
address _erc1155TokenAddress,
uint256 _erc1155TypeId,
address _owner
) external {
function updateERC1155Listing(address _erc1155TokenAddress, uint256 _erc1155TypeId, address _owner) external {
LibERC1155Marketplace.updateERC1155Listing(_erc1155TokenAddress, _erc1155TypeId, _owner);
}

///@notice Update the ERC1155 listings of an address
///@param _erc1155TokenAddress Contract address of the ERC1155 token
///@param _erc1155TypeIds An array containing the identifiers of the ERC1155 tokens to update
///@param _owner Owner of the ERC1155 tokens
function updateBatchERC1155Listing(
address _erc1155TokenAddress,
uint256[] calldata _erc1155TypeIds,
address _owner
) external {
function updateBatchERC1155Listing(address _erc1155TokenAddress, uint256[] calldata _erc1155TypeIds, address _owner) external {
for (uint256 i; i < _erc1155TypeIds.length; i++) {
LibERC1155Marketplace.updateERC1155Listing(_erc1155TokenAddress, _erc1155TypeIds[i], _owner);
}
Expand Down
16 changes: 4 additions & 12 deletions contracts/Aavegotchi/facets/ERC721MarketplaceFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,7 @@ contract ERC721MarketplaceFacet is Modifiers {
///@param _erc721TokenId The identifier of the NFT to be listed
///@param _priceInWei The cost price of the NFT in $GHST

function addERC721Listing(
address _erc721TokenAddress,
uint256 _erc721TokenId,
uint256 _priceInWei
) external {
function addERC721Listing(address _erc721TokenAddress, uint256 _erc721TokenId, uint256 _priceInWei) external payable {
createERC721Listing(_erc721TokenAddress, _erc721TokenId, _priceInWei, [10000, 0], address(0));
}

Expand All @@ -240,7 +236,7 @@ contract ERC721MarketplaceFacet is Modifiers {
uint256 _priceInWei,
uint16[2] memory _principalSplit,
address _affiliate
) external {
) external payable {
createERC721Listing(_erc721TokenAddress, _erc721TokenId, _priceInWei, _principalSplit, _affiliate);
}

Expand Down Expand Up @@ -312,7 +308,7 @@ contract ERC721MarketplaceFacet is Modifiers {

//Burn listing fee
if (s.listingFeeInWei > 0) {
LibSharedMarketplace.burnListingFee(s.listingFeeInWei, msgSender, s.ghstContract);
LibSharedMarketplace.burnListingFee(s.listingFeeInWei, msgSender);
}
}

Expand Down Expand Up @@ -442,11 +438,7 @@ contract ERC721MarketplaceFacet is Modifiers {
///@param _erc721TokenAddress Contract address of the ERC721 token
///@param _erc721TokenId Identifier of the ERC721 token
///@param _owner Owner of the ERC721 token
function updateERC721Listing(
address _erc721TokenAddress,
uint256 _erc721TokenId,
address _owner
) external {
function updateERC721Listing(address _erc721TokenAddress, uint256 _erc721TokenId, address _owner) external {
LibERC721Marketplace.updateERC721Listing(_erc721TokenAddress, _erc721TokenId, _owner);
}

Expand Down
18 changes: 12 additions & 6 deletions contracts/Aavegotchi/libraries/LibSharedMarketplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,19 @@ library LibSharedMarketplace {
}
}

function burnListingFee(
uint256 listingFee,
address owner,
address ghstContract
) internal {
//will only habdle fee asserts and refunds(in case of >s.listingFee values)
function burnListingFee(uint256 listingFee, address owner) internal {
if (listingFee > 0) {
LibERC20.transferFrom(ghstContract, owner, address(0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF), listingFee);
assert(msg.value >= listingFee);
//send out fee to feeCollector
address to;
(bool success, ) = to.call{value: listingFee}("");
assert(success);
if (msg.value > listingFee) {
//refund extra matic sent in
(bool success2, ) = owner.call{value: msg.value - listingFee}("");
assert(success2);
}
}
}
}
46 changes: 46 additions & 0 deletions scripts/upgrades/upgrade-listingFee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { run } from "hardhat";
import {
convertFacetAndSelectorsToString,
DeployUpgradeTaskArgs,
FacetsAndAddSelectors,
} from "../../tasks/deployUpgrade";
import { maticDiamondAddress } from "../helperFunctions";

export async function upgrade() {
const diamondUpgrader = "0x35fe3df776474a7b24b3b1ec6e745a830fdad351";

const facets: FacetsAndAddSelectors[] = [
{
facetName: "ERC721MarketplaceFacet",
addSelectors: [],
removeSelectors: [],
},
{
facetName: "ERC1155MarketplaceFacet",
addSelectors: [],
removeSelectors: [],
},
];

const joined = convertFacetAndSelectorsToString(facets);

const args: DeployUpgradeTaskArgs = {
diamondUpgrader: diamondUpgrader,
diamondAddress: maticDiamondAddress,
facetsAndAddSelectors: joined,
useLedger: true,
useMultisig: false,
};

await run("deployUpgrade", args);
}

if (require.main === module) {
upgrade()
.then(() => process.exit(0))
// .then(() => console.log('upgrade completed') /* process.exit(0) */)
.catch((error) => {
console.error(error);
process.exit(1);
});
}
81 changes: 81 additions & 0 deletions test/listingFee/ListingFeeTest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.1;

import "forge-std/Test.sol";
import "../../contracts/shared/interfaces/IDiamondCut.sol";

import "../periphery/foundry/PeripheryConstants.sol";
import "../../contracts/Aavegotchi/facets/ERC1155MarketplaceFacet.sol";
import "../../contracts/Aavegotchi/facets/ERC721MarketplaceFacet.sol";
import "../../contracts/shared/facets/OwnershipFacet.sol";

contract ListingFeeTest is Test, IDiamondCut, PeripheryConstants {
ERC1155MarketplaceFacet erc1155;
ERC721MarketplaceFacet erc721;

function setUp() public {
diamondOwner = OwnershipFacet(aavegotchiDiamond).owner();

//deploy all changed aavegotchi diamond facets
erc1155 = new ERC1155MarketplaceFacet();
erc721 = new ERC721MarketplaceFacet();

cutDiamonds();
}

function cutDiamonds() internal {
//prepare cut args for aavegotchi diamond

IDiamondCut.FacetCut[] memory cut = new IDiamondCut.FacetCut[](2);

vm.startPrank(diamondOwner);
cut[0] = IDiamondCut.FacetCut({
facetAddress: address(erc1155),
action: IDiamondCut.FacetCutAction.Replace,
functionSelectors: generateSelectors("ERC1155MarketplaceFacet")
});

cut[1] = IDiamondCut.FacetCut({
facetAddress: address(erc721),
action: IDiamondCut.FacetCutAction.Replace,
functionSelectors: generateSelectors("ERC721MarketplaceFacet")
});

/////UPGRADE AAVEGOTCHI DIAMOND
IDiamondCut(aavegotchiDiamond).diamondCut(cut, address(0), "");
vm.stopPrank();
}

function testListingFee() external {
address parcelOwner = IERC721(realmAddress).ownerOf(parcelId);
//get balances before
//test ERC1155listing
uint256 ghstbalanceBefore = ERC20(ghstAddress).balanceOf(parcelOwner);
uint256 maticBalanceBefore = parcelOwner.balance;
uint256 targetAddressBalanceBefore = address(0).balance;
vm.prank(parcelOwner);
ERC721MarketplaceFacet(aavegotchiDiamond).addERC721Listing{value: 1e18}(realmAddress, parcelId, 100e18);
uint256 targetAddressBalanceAfter = address(0).balance;
uint256 ghstbalanceAfter = ERC20(ghstAddress).balanceOf(parcelOwner);
uint256 maticBalanceAfter = parcelOwner.balance;
assertEq(targetAddressBalanceAfter, targetAddressBalanceBefore + 1e17);
assertEq(maticBalanceAfter, maticBalanceBefore - 1e17);
assertEq(ghstbalanceBefore, ghstbalanceAfter);
}

//no need to test for erc1155 since internal function is shared
function diamondCut(FacetCut[] calldata _diamondCut, address _init, bytes calldata _calldata) external override {}

function generateSelectors(string memory _facetName) internal returns (bytes4[] memory selectors) {
string[] memory cmd = new string[](3);
cmd[0] = "node";
cmd[1] = "scripts/genSelectors.js";
cmd[2] = _facetName;
bytes memory res = vm.ffi(cmd);
selectors = abi.decode(res, (bytes4[]));
}
}

interface ERC20 {
function balanceOf(address _owner) external view returns (uint256);
}
17 changes: 10 additions & 7 deletions test/periphery/foundry/PeripheryConstants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ pragma solidity 0.8.1;

abstract contract PeripheryConstants {
address constant aavegotchiDiamond = 0x86935F11C86623deC8a25696E1C19a8659CbF95d;
//using constants to save test time
address constant ownerShipFacet = 0xAE7DF9f59FEc446903c64f21a76d039Bc81712ef;
address constant loupeFacet = 0x58f64b56B1e15D8C932c51287d814EDaa8d6feb9;
address constant cutFacet = 0x4f908Fa47F10bc2254dae7c74d8B797C1749A8a6;
address constant ghstAddress = 0x385Eeac5cB85A38A9a07A70c73e0a3271CfB54A7;

address constant realmAddress = 0x1D0360BaC7299C86Ec8E99d0c1C9A95FEfaF2a11;

//using constants to save test time
address constant potionOwner = 0x6bac48867BC94Ff20B4C62b21d484a44D04d342C;
uint256 constant potionId = 128;
uint256 constant potionOwnerGotchiId = 2761;

address constant wearableOwner = 0x852271883ed42CC3F9D9559b05aB4784Fc768E93;
uint256 gotchiId = 3342;
uint256 sampleWearableId = 302;

address constant ownerShipFacet = 0xAE7DF9f59FEc446903c64f21a76d039Bc81712ef;
address constant loupeFacet = 0x58f64b56B1e15D8C932c51287d814EDaa8d6feb9;
address constant cutFacet = 0x4f908Fa47F10bc2254dae7c74d8B797C1749A8a6;
address constant ghstOwner = 0x32037cabd6e86e7E9E83ab1f997E4420b8503710;
address constant listingOwner = 0xA60CA8a1400333468a736c7247BAad6350C6496d;
uint256 constant listingId = 329683;
Expand All @@ -23,5 +24,7 @@ abstract contract PeripheryConstants {

address diamondOwner;
address wearableDiamond;
}

//listingFee
uint256 parcelId = 12860;
}