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

Removing native #1176

Merged
merged 5 commits into from
Sep 29, 2023
Merged
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
8 changes: 2 additions & 6 deletions packages/marketplace/contracts/exchange/AssetMatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,8 @@ contract AssetMatcher is Ownable, IAssetMatcher {
) private view returns (LibAsset.AssetType memory) {
bytes4 classLeft = leftAssetType.assetClass;
bytes4 classRight = rightAssetType.assetClass;
if (classLeft == LibAsset.ETH_ASSET_CLASS) {
if (classRight == LibAsset.ETH_ASSET_CLASS) {
return leftAssetType;
}
return LibAsset.AssetType(0, EMPTY);
}
require(classLeft != LibAsset.ETH_ASSET_CLASS, "maker cannot transfer native token");
require(classRight != LibAsset.ETH_ASSET_CLASS, "taker cannot transfer native token");
if (classLeft == LibAsset.ERC20_ASSET_CLASS) {
if (classRight == LibAsset.ERC20_ASSET_CLASS) {
return simpleMatch(leftAssetType, rightAssetType);
Expand Down
16 changes: 2 additions & 14 deletions packages/marketplace/contracts/exchange/Exchange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ contract Exchange is Initializable, AccessControlUpgradeable, ExchangeCore, Tran
/// @param newRoyaltiesProvider registry for the different types of royalties
/// @param orderValidatorAddress new OrderValidator contract address
/// @param newAssetMatcher new AssetMatcher contract address
/// @param newNativeOrder bool to indicate of the contract accepts or doesn't native tokens, i.e. ETH or Matic
/// @param newMetaNative same as =nativeOrder but for metaTransactions
// solhint-disable-next-line func-name-mixedcase
function __Exchange_init(
address admin,
Expand All @@ -47,9 +45,7 @@ contract Exchange is Initializable, AccessControlUpgradeable, ExchangeCore, Tran
address newDefaultFeeReceiver,
IRoyaltiesProvider newRoyaltiesProvider,
IOrderValidator orderValidatorAddress,
IAssetMatcher newAssetMatcher,
bool newNativeOrder,
bool newMetaNative
IAssetMatcher newAssetMatcher
) external initializer {
__ERC2771Handler_init(newTrustedForwarder);
__AccessControl_init();
Expand All @@ -59,7 +55,7 @@ contract Exchange is Initializable, AccessControlUpgradeable, ExchangeCore, Tran
newDefaultFeeReceiver,
newRoyaltiesProvider
);
__ExchangeCoreInitialize(newNativeOrder, newMetaNative, orderValidatorAddress, newAssetMatcher);
__ExchangeCoreInitialize(orderValidatorAddress, newAssetMatcher);
_grantRole(DEFAULT_ADMIN_ROLE, admin);
}

Expand Down Expand Up @@ -140,14 +136,6 @@ contract Exchange is Initializable, AccessControlUpgradeable, ExchangeCore, Tran
_setOrderValidatorContract(contractAddress);
}

/// @notice update permissions for native orders
/// @param newNativeOrder for orders with native token
/// @param newMetaNative for meta orders with native token
/// @dev setter for permissions for native token exchange
function updateNative(bool newNativeOrder, bool newMetaNative) external onlyRole(DEFAULT_ADMIN_ROLE) {
_updateNative(newNativeOrder, newMetaNative);
}

/// @notice Change the address of the trusted forwarder for meta-transactions
/// @param newTrustedForwarder The new trustedForwarder
function setTrustedForwarder(address newTrustedForwarder) external virtual onlyRole(DEFAULT_ADMIN_ROLE) {
Expand Down
48 changes: 2 additions & 46 deletions packages/marketplace/contracts/exchange/ExchangeCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ abstract contract ExchangeCore is Initializable, TransferExecutor, ITransferMana

uint256 private constant UINT256_MAX = type(uint256).max;

/// @notice boolean to indicate if native tokens are accepted for meta transactions
/// @return true if native tokens are accepted for meta tx, false otherwise
bool public nativeMeta;

/// @notice boolean to indicate if native tokens are accepted
/// @return true if native tokens are accepted, false otherwise
bool public nativeOrder;

/// @notice stores the fills for orders
/// @return order fill
mapping(bytes32 => uint256) public fills;
Expand Down Expand Up @@ -64,22 +56,16 @@ abstract contract ExchangeCore is Initializable, TransferExecutor, ITransferMana
);
event AssetMatcherSet(IAssetMatcher indexed contractAddress);
event OrderValidatorSet(IOrderValidator indexed contractAddress);
event NativeUpdated(bool nativeOrder, bool metaNative);

/// @notice initializer for ExchangeCore
/// @param newNativeOrder for orders with native token
/// @param newMetaNative for meta orders with native token
/// @param newOrderValidatorAddress new OrderValidator contract address
/// @param newAssetMatcher new AssetMatcher contract address
/// @dev initialize permissions for native token exchange
// solhint-disable-next-line func-name-mixedcase
function __ExchangeCoreInitialize(
bool newNativeOrder,
bool newMetaNative,
IOrderValidator newOrderValidatorAddress,
IAssetMatcher newAssetMatcher
) internal {
_updateNative(newMetaNative, newNativeOrder);
_setOrderValidatorContract(newOrderValidatorAddress);
_setAssetMatcherContract(newAssetMatcher);
}
Expand All @@ -101,16 +87,6 @@ abstract contract ExchangeCore is Initializable, TransferExecutor, ITransferMana
emit OrderValidatorSet(contractAddress);
}

/// @notice update permissions for native orders
/// @param newNativeOrder for orders with native token
/// @param newMetaNative for meta orders with native token
/// @dev setter for permissions for native token exchange
function _updateNative(bool newNativeOrder, bool newMetaNative) internal {
nativeMeta = newMetaNative;
nativeOrder = newNativeOrder;
emit NativeUpdated(newNativeOrder, newMetaNative);
}

/// @notice cancel order
/// @param order to be canceled
/// @param orderHash used as a checksum to avoid mistakes in the values of order
Expand Down Expand Up @@ -238,7 +214,7 @@ abstract contract ExchangeCore is Initializable, TransferExecutor, ITransferMana
LibFill.FillResult memory newFill
) = _parseOrdersSetFillEmitMatch(from, orderLeft, orderRight);

(uint256 totalMakeValue, uint256 totalTakeValue) = doTransfers(
doTransfers(
LibDeal.DealSide({
asset: LibAsset.Asset({assetType: makeMatch, value: newFill.leftValue}),
payouts: leftOrderData.payouts,
Expand All @@ -252,28 +228,8 @@ abstract contract ExchangeCore is Initializable, TransferExecutor, ITransferMana
LibFeeSide.getFeeSide(makeMatch.assetClass, takeMatch.assetClass)
);

uint256 takeBuyAmount = newFill.rightValue;
uint256 makeBuyAmount = newFill.leftValue;

// TODO: this force me to pass from, do we want it ?
if (((from != msg.sender) && !nativeMeta) || ((from == msg.sender) && !nativeOrder)) {
require(makeMatch.assetClass != LibAsset.ETH_ASSET_CLASS, "maker cannot transfer native token");
require(takeMatch.assetClass != LibAsset.ETH_ASSET_CLASS, "taker cannot transfer native token");
}
if (makeMatch.assetClass == LibAsset.ETH_ASSET_CLASS) {
require(takeMatch.assetClass != LibAsset.ETH_ASSET_CLASS, "taker cannot transfer native token");
require(makeBuyAmount >= totalMakeValue, "not enough eth");
if (makeBuyAmount > totalMakeValue) {
// TODO: from ?
payable(msg.sender).transferEth(makeBuyAmount - totalMakeValue);
}
} else if (takeMatch.assetClass == LibAsset.ETH_ASSET_CLASS) {
require(takeBuyAmount >= totalTakeValue, "not enough eth");
if (takeBuyAmount > totalTakeValue) {
// TODO: from ?
payable(msg.sender).transferEth(takeBuyAmount - totalTakeValue);
}
}
// answer it was cut out with native orders
mvanmeerbeck marked this conversation as resolved.
Show resolved Hide resolved
}

/// @notice parse orders with LibOrderDataGeneric parse() to get the order data, then create a new fill with setFillEmitMatch()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ abstract contract SimpleTransferManager is ITransferManager {
LibDeal.DealSide memory left,
LibDeal.DealSide memory right,
LibFeeSide.FeeSide /* feeSide */
) internal override returns (uint256 totalMakeValue, uint256 totalTakeValue) {
) internal override {
transfer(left.asset, left.from, right.from);
transfer(right.asset, right.from, left.from);
totalMakeValue = left.asset.value;
totalTakeValue = right.asset.value;
}

uint256[50] private __gap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,16 @@ abstract contract TransferManager is ERC165Upgradeable, ITransferManager {
/// @notice executes transfers for 2 matched orders
/// @param left DealSide from the left order (see LibDeal.sol)
/// @param right DealSide from the right order (see LibDeal.sol)
/// @return totalLeftValue - total amount for the left order
/// @return totalRightValue - total amount for the right order
function doTransfers(
LibDeal.DealSide memory left,
LibDeal.DealSide memory right,
LibFeeSide.FeeSide feeSide
) internal override returns (uint256 totalLeftValue, uint256 totalRightValue) {
totalLeftValue = left.asset.value;
totalRightValue = right.asset.value;
) internal override {
if (feeSide == LibFeeSide.FeeSide.LEFT) {
totalLeftValue = doTransfersWithRoyalties(left, right);
doTransfersWithRoyalties(left, right);
transferPayouts(right.asset.assetType, right.asset.value, right.from, left.payouts);
} else if (feeSide == LibFeeSide.FeeSide.RIGHT) {
totalRightValue = doTransfersWithRoyalties(right, left);
doTransfersWithRoyalties(right, left);
transferPayouts(left.asset.assetType, left.asset.value, left.from, right.payouts);
} else {
transferPayouts(left.asset.assetType, left.asset.value, left.from, right.payouts);
Expand All @@ -124,11 +120,7 @@ abstract contract TransferManager is ERC165Upgradeable, ITransferManager {
/// @notice executes the fee-side transfers (payment + fees)
/// @param paymentSide DealSide of the fee-side order
/// @param nftSide DealSide of the nft-side order
/// @return totalAmount of fee-side asset
function doTransfersWithRoyalties(
LibDeal.DealSide memory paymentSide,
LibDeal.DealSide memory nftSide
) internal returns (uint256 totalAmount) {
function doTransfersWithRoyalties(LibDeal.DealSide memory paymentSide, LibDeal.DealSide memory nftSide) internal {
uint256 rest = paymentSide.asset.value;

rest = transferRoyalties(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ abstract contract ITransferManager is ITransferExecutor {
LibDeal.DealSide memory left,
LibDeal.DealSide memory right,
LibFeeSide.FeeSide feeSide
) internal virtual returns (uint256 totalMakeValue, uint256 totalTakeValue);
) internal virtual;
}
21 changes: 0 additions & 21 deletions packages/marketplace/test/exchange/ExchangeSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ describe('Exchange.sol settings', function () {
expect(await ExchangeContractAsAdmin.orderValidator()).to.be.equal(
await OrderValidatorAsDeployer.getAddress()
);
expect(await ExchangeContractAsAdmin.nativeMeta()).to.be.equal(true);
expect(await ExchangeContractAsAdmin.nativeOrder()).to.be.equal(true);
expect(await ExchangeContractAsAdmin.getTrustedForwarder()).to.be.equal(
await TrustedForwarder.getAddress()
);
Expand All @@ -49,25 +47,6 @@ describe('Exchange.sol settings', function () {
'setOrderValidatorContract',
'OrderValidatorSet'
);
it('should update native order', async function () {
const {ExchangeContractAsAdmin} = await loadFixture(deployFixtures);
expect(await ExchangeContractAsAdmin.nativeMeta()).to.be.equal(true);
expect(await ExchangeContractAsAdmin.nativeOrder()).to.be.equal(true);
await expect(ExchangeContractAsAdmin.updateNative(false, false))
.to.emit(ExchangeContractAsAdmin, 'NativeUpdated')
.withArgs(false, false);
expect(await ExchangeContractAsAdmin.nativeMeta()).to.be.equal(false);
expect(await ExchangeContractAsAdmin.nativeOrder()).to.be.equal(false);
});
it('should not update native order if caller is not owner', async function () {
const {DEFAULT_ADMIN_ROLE, ExchangeContractAsUser, user} =
await loadFixture(deployFixtures);
await expect(
ExchangeContractAsUser.updateNative(false, false)
).to.be.revertedWith(
`AccessControl: account ${user.address.toLowerCase()} is missing role ${DEFAULT_ADMIN_ROLE}`
);
});
it('should not set trusted forwarder if caller is not owner', async function () {
const {DEFAULT_ADMIN_ROLE, ExchangeContractAsUser, user} =
await loadFixture(deployFixtures);
Expand Down
2 changes: 0 additions & 2 deletions packages/marketplace/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ async function deploy() {
await RoyaltyRegistry.getAddress(),
await OrderValidatorAsDeployer.getAddress(),
await assetMatcherAsDeployer.getAddress(),
true,
true,
],
{
initializer: '__Exchange_init',
Expand Down
Loading