[marketplace] move price calculations to Requests.sol

This commit is contained in:
Mark Spanbroek 2023-01-17 09:19:51 +01:00 committed by markspanbroek
parent 7aa162b526
commit 2dbdd0028d
2 changed files with 13 additions and 23 deletions

View File

@ -60,7 +60,7 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
addToMyRequests(request.client, id); addToMyRequests(request.client, id);
uint256 amount = price(request); uint256 amount = request.price();
funds.received += amount; funds.received += amount;
funds.balance += amount; funds.balance += amount;
transferFrom(msg.sender, amount); transferFrom(msg.sender, amount);
@ -178,7 +178,7 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
removeFromMySlots(slot.host, slotId); removeFromMySlots(slot.host, slotId);
uint256 amount = pricePerSlot(requests[requestId]); uint256 amount = requests[requestId].pricePerSlot();
funds.sent += amount; funds.sent += amount;
funds.balance -= amount; funds.balance -= amount;
slot.state = SlotState.Paid; slot.state = SlotState.Paid;
@ -205,7 +205,7 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
// TODO: To be changed once we start paying out hosts for the time they // TODO: To be changed once we start paying out hosts for the time they
// fill a slot. The amount that we paid to hosts will then have to be // fill a slot. The amount that we paid to hosts will then have to be
// deducted from the price. // deducted from the price.
uint256 amount = _price(request); uint256 amount = request.price();
funds.sent += amount; funds.sent += amount;
funds.balance -= amount; funds.balance -= amount;
require(token.transfer(msg.sender, amount), "Withdraw failed"); require(token.transfer(msg.sender, amount), "Withdraw failed");
@ -241,26 +241,6 @@ contract Marketplace is Collateral, Proofs, StateRetrieval {
} }
} }
function _price(
uint64 numSlots,
uint256 duration,
uint256 reward
) internal pure returns (uint256) {
return numSlots * duration * reward;
}
function _price(Request memory request) internal pure returns (uint256) {
return _price(request.ask.slots, request.ask.duration, request.ask.reward);
}
function price(Request calldata request) private pure returns (uint256) {
return _price(request.ask.slots, request.ask.duration, request.ask.reward);
}
function pricePerSlot(Request memory request) private pure returns (uint256) {
return request.ask.duration * request.ask.reward;
}
function requestState( function requestState(
RequestId requestId RequestId requestId
) public view requestIsKnown(requestId) returns (RequestState) { ) public view requestIsKnown(requestId) returns (RequestState) {

View File

@ -82,4 +82,14 @@ library Requests {
result := ids result := ids
} }
} }
function pricePerSlot(
Request memory request
) internal pure returns (uint256) {
return request.ask.duration * request.ask.reward;
}
function price(Request memory request) internal pure returns (uint256) {
return request.ask.slots * pricePerSlot(request);
}
} }