Skip to content

Commit

Permalink
fix: tokenPriceForCaller
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Nov 30, 2024
1 parent 8b5a12c commit 5cc6f91
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
61 changes: 50 additions & 11 deletions ethereum/contracts/Marketplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,7 @@ contract Marketplace is Ownable {
// get token buyer
address tokenBuyer = msg.sender;
// get whether the buyer is a contract buyer
bool isContractBuyer = false;
for (uint256 i = 0; i < sellContract.buyers.length; i++) {
if (sellContract.buyers[i] == tokenBuyer) {
isContractBuyer = true;
break;
}
}
bool _isContractBuyer = isContractBuyer(sellContract);
// get token seller
address tokenSeller = deferredContract.ownerOf(_tokenId);
// check whether we need to send reward
Expand All @@ -107,9 +101,10 @@ contract Marketplace is Ownable {
ERC20 currency = ERC20(usdErc20);

// get the required allowance
uint256 requiredAllowance = isContractBuyer
? sellContract.tokenPriceUsd + interests(sellContract.tokenPriceUsd)
: sellContract.tokenPriceUsd;
uint256 requiredAllowance = calcTokenPriceWithInterests(
sellContract,
_isContractBuyer
);
// check allowance on the currency token
require(
currency.allowance(msg.sender, address(this)) >= requiredAllowance,
Expand All @@ -122,7 +117,7 @@ contract Marketplace is Ownable {
sellContract.tokenPriceUsd
);
// if the buyer is a contract buyer, transfer the interests to the marketplace
if (isContractBuyer) {
if (_isContractBuyer) {
currency.transferFrom(
tokenBuyer,
address(this),
Expand Down Expand Up @@ -155,6 +150,50 @@ contract Marketplace is Ownable {
);
}

/// @notice Get the price of the token for the caller with the interests if the caller is a contract buyer
/// @param _tokenId The ID of the deferred NFT
/// @return _price The price of the token
function tokenPriceForCaller(
uint256 _tokenId
) external view returns (uint256) {
Deferred deferredContract = Deferred(deferred);
Deferred.SellContract memory sellContract = deferredContract
.tokenContract(_tokenId);

bool _isContractBuyer = isContractBuyer(sellContract);

return calcTokenPriceWithInterests(sellContract, _isContractBuyer);
}

/// @notice Get whether the caller is a contract buyer
/// @param _contract The contract to check
/// @return _isContractBuyer Whether the caller is a contract buyer
function isContractBuyer(
Deferred.SellContract memory _contract
) internal view returns (bool) {
for (uint256 i = 0; i < _contract.buyers.length; i++) {
if (_contract.buyers[i] == msg.sender) {
return true;
}
}

return false;
}

/// @notice Calculate the price of the token with the interests if the buyer is a contract buyer
/// @param _contract The contract to calculate the price for
/// @param _isContractBuyer Whether the buyer is a contract buyer
/// @return _price The price of the token
function calcTokenPriceWithInterests(
Deferred.SellContract memory _contract,
bool _isContractBuyer
) internal view returns (uint256) {
return
_isContractBuyer
? _contract.tokenPriceUsd + interests(_contract.tokenPriceUsd)
: _contract.tokenPriceUsd;
}

/// @notice Get the interests to pay for the token
/// @param _usdPrice The price of the token in USD
/// @return _interests The interests to pay
Expand Down
21 changes: 21 additions & 0 deletions ethereum/test/Marketplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,27 @@ describe("RewardPool", () => {
expect(await ekoke.balanceOf(buyer.address)).to.equal(EKOKE_REWARD);
});

it("Should get token price as contract buyer", async () => {
const { buyer, marketplace } = deploy;

const tokenId = 0;
const interest = 10;
// give allowance to marketplace
expect(
await marketplace.connect(buyer).tokenPriceForCaller(tokenId)
).to.equal(USD_PRICE + (USD_PRICE * interest) / 100);
});

it("Should get token price as third-party", async () => {
const { marketplace, thirdParty } = deploy;

const tokenId = 0;
// give allowance to marketplace
expect(
await marketplace.connect(thirdParty).tokenPriceForCaller(tokenId)
).to.equal(USD_PRICE);
});

it("Should set interest rate", async () => {
const { marketplace } = deploy;

Expand Down

0 comments on commit 5cc6f91

Please sign in to comment.