Skip to content

Commit 9fdcc53

Browse files
committed
feat: remove collateral swap approve process & add eth support & calculate the exact price after fee
Signed-off-by: GopherJ <alex_cj96@foxmail.com>
1 parent dd9f1ca commit 9fdcc53

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

contracts/misc/marketplaces/LooksRareAdapter.sol

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ contract LooksRareAdapter is IMarketplace {
6565
itemType = ItemType.NATIVE;
6666
token = address(0);
6767
}
68+
uint256 minPrice = (makerAsk.price * makerAsk.minPercentageToAsk) /
69+
10000;
6870
consideration[0] = ConsiderationItem(
6971
itemType,
7072
token,
7173
0,
72-
makerAsk.price, // TODO: take minPercentageToAsk into account
73-
makerAsk.price,
74+
minPrice,
75+
minPrice,
7476
payable(takerBid.taker)
7577
);
7678
orderInfo.id = abi.encodePacked(makerAsk.r, makerAsk.s, makerAsk.v);

contracts/misc/marketplaces/X2Y2Adapter.sol

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {IPoolAddressesProvider} from "../../interfaces/IPoolAddressesProvider.so
1818
contract X2Y2Adapter is IMarketplace {
1919
IPoolAddressesProvider public immutable ADDRESSES_PROVIDER;
2020

21+
uint256 public constant RATE_BASE = 1e6;
22+
2123
constructor(IPoolAddressesProvider provider) {
2224
ADDRESSES_PROVIDER = provider;
2325
}
@@ -66,6 +68,14 @@ contract X2Y2Adapter is IMarketplace {
6668

6769
ConsiderationItem[] memory consideration = new ConsiderationItem[](1);
6870

71+
uint256 totalFee;
72+
for (uint256 i = 0; i < detail.fees.length; i++) {
73+
IX2Y2.Fee memory fee = detail.fees[i];
74+
uint256 amount = (detail.price * fee.percentage) / RATE_BASE;
75+
totalFee += amount;
76+
}
77+
detail.price -= totalFee;
78+
6979
ItemType itemType = ItemType.ERC20;
7080
address token = order.currency;
7181
consideration[0] = ConsiderationItem(
@@ -76,6 +86,7 @@ contract X2Y2Adapter is IMarketplace {
7686
detail.price,
7787
payable(order.user)
7888
);
89+
7990
orderInfo.id = abi.encodePacked(order.r, order.s, order.v);
8091
orderInfo.consideration = consideration;
8192
}

contracts/protocol/libraries/logic/MarketplaceLogic.sol

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ library MarketplaceLogic {
5050
uint16 indexed referralCode
5151
);
5252

53-
/**
54-
* @dev Default percentage of listing price to be supplied on behalf of the seller in a marketplace exchange.
55-
* Expressed in bps, a value of 0.9e4 results in 90.00%
56-
*/
57-
uint256 internal constant DEFAULT_SUPPLY_RATIO = 0.9e4;
58-
5953
event BuyWithCredit(
6054
bytes32 indexed marketplaceId,
6155
DataTypes.OrderInfo orderInfo,
@@ -158,7 +152,7 @@ library MarketplaceLogic {
158152
)
159153
);
160154

161-
_handleFlashSupplyRepayment(vars, params.orderInfo.maker);
155+
_handleFlashSupplyRepayment(params, vars, address(this));
162156
_handleFlashLoanRepayment(ps, params, vars, params.orderInfo.taker);
163157

164158
emit BuyWithCredit(
@@ -356,7 +350,7 @@ library MarketplaceLogic {
356350
)
357351
);
358352

359-
_handleFlashSupplyRepayment(vars, params.orderInfo.taker);
353+
_handleFlashSupplyRepayment(params, vars, address(this));
360354
_handleFlashLoanRepayment(ps, params, vars, params.orderInfo.maker);
361355

362356
emit AcceptBidWithCredit(
@@ -446,11 +440,11 @@ library MarketplaceLogic {
446440
MarketplaceLocalVars memory vars,
447441
address seller
448442
) internal {
449-
if (vars.isETH) {
450-
return; // impossible to supply ETH on behalf of the
443+
DataTypes.ReserveData storage reserve = ps._reserves[vars.creditToken];
444+
if (reserve.xTokenAddress == address(0)) {
445+
return;
451446
}
452447

453-
DataTypes.ReserveData storage reserve = ps._reserves[vars.creditToken];
454448
DataTypes.UserConfigurationMap storage sellerConfig = ps._usersConfig[
455449
seller
456450
];
@@ -459,31 +453,25 @@ library MarketplaceLogic {
459453

460454
reserve.updateState(reserveCache);
461455

462-
uint256 supplyAmount = Math.min(
463-
IERC20(vars.creditToken).allowance(seller, address(this)),
464-
vars.price.percentMul(DEFAULT_SUPPLY_RATIO)
465-
);
466-
if (supplyAmount == 0) {
467-
return;
468-
}
456+
vars.supplyAmount = vars.price;
469457

470458
ValidationLogic.validateSupply(
471459
reserveCache,
472-
supplyAmount,
460+
vars.supplyAmount,
473461
DataTypes.AssetType.ERC20
474462
);
475463

476464
reserve.updateInterestRates(
477465
reserveCache,
478466
vars.creditToken,
479-
supplyAmount,
467+
vars.supplyAmount,
480468
0
481469
);
482470

483471
bool isFirstSupply = IPToken(reserveCache.xTokenAddress).mint(
484472
msg.sender,
485473
seller,
486-
supplyAmount,
474+
vars.supplyAmount,
487475
reserveCache.nextLiquidityIndex
488476
);
489477

@@ -496,12 +484,9 @@ library MarketplaceLogic {
496484
vars.creditToken,
497485
msg.sender,
498486
seller,
499-
supplyAmount,
487+
vars.supplyAmount,
500488
params.referralCode
501489
);
502-
503-
// set supplyAmount for future repayment
504-
vars.supplyAmount = supplyAmount;
505490
}
506491

507492
/**
@@ -608,19 +593,25 @@ library MarketplaceLogic {
608593
/**
609594
* @notice "Repay" minted pToken by transferring funds from the seller to xTokenAddress
610595
* @dev
596+
* @param params The additional parameters needed to execute the buyWithCredit/acceptBidWithCredit function
611597
* @param vars The marketplace local vars for caching storage values for future reads
612-
* @param seller The NFT seller
598+
* @param recipient The ERC20 recipient
613599
*/
614600
function _handleFlashSupplyRepayment(
601+
DataTypes.ExecuteMarketplaceParams memory params,
615602
MarketplaceLocalVars memory vars,
616-
address seller
603+
address recipient
617604
) internal {
618-
if (vars.isETH || vars.supplyAmount == 0) {
605+
if (vars.supplyAmount == 0) {
619606
return;
620607
}
621608

609+
if (vars.isETH) {
610+
IWETH(params.weth).deposit{value: vars.supplyAmount}();
611+
}
612+
622613
IERC20(vars.creditToken).safeTransferFrom(
623-
seller,
614+
recipient,
624615
vars.creditXTokenAddress,
625616
vars.supplyAmount
626617
);
@@ -702,7 +693,7 @@ library MarketplaceLogic {
702693
function _validateAndGetPrice(
703694
DataTypes.ExecuteMarketplaceParams memory params,
704695
MarketplaceLocalVars memory vars
705-
) internal pure returns (uint256 price) {
696+
) internal view returns (uint256 price) {
706697
for (uint256 i = 0; i < params.orderInfo.consideration.length; i++) {
707698
ConsiderationItem memory item = params.orderInfo.consideration[i];
708699
require(
@@ -718,7 +709,7 @@ library MarketplaceLogic {
718709
item.token == params.credit.token,
719710
Errors.CREDIT_DOES_NOT_MATCH_ORDER
720711
);
721-
price += item.startAmount;
712+
if (item.recipient == address(this)) price += item.startAmount;
722713
}
723714
}
724715
}

0 commit comments

Comments
 (0)