From ae92f63987d86f1fd1b1ce39ba6258cb88c19718 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 17 Feb 2022 11:06:14 +0100 Subject: [PATCH] Add host address to offers --- contracts/Marketplace.sol | 2 ++ test/Marketplace.test.js | 11 +++++++++-- test/examples.js | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/contracts/Marketplace.sol b/contracts/Marketplace.sol index d511095..51b7aaf 100644 --- a/contracts/Marketplace.sol +++ b/contracts/Marketplace.sol @@ -37,6 +37,7 @@ contract Marketplace is Collateral { Request storage request = requests[offer.requestId]; require(balanceOf(msg.sender) >= collateral, "Insufficient collateral"); require(request.size != 0, "Unknown request"); + require(offer.host == msg.sender, "Invalid host address"); require(offers[id].expiry == 0, "Offer already exists"); // solhint-disable-next-line not-rely-on-time require(offer.expiry > block.timestamp, "Offer expired"); @@ -57,6 +58,7 @@ contract Marketplace is Collateral { } struct Offer { + address host; bytes32 requestId; uint256 price; uint256 expiry; diff --git a/test/Marketplace.test.js b/test/Marketplace.test.js index 48c78c7..222d4ec 100644 --- a/test/Marketplace.test.js +++ b/test/Marketplace.test.js @@ -97,6 +97,13 @@ describe("Marketplace", function () { .withArgs(offerId(offer), offerToArray(offer)) }) + it("rejects offer with invalid host address", async function () { + let invalid = { ...offer, host: client.address } + await expect(marketplace.offerStorage(invalid)).to.be.revertedWith( + "Invalid host address" + ) + }) + it("rejects offer for unknown request", async function () { let unknown = exampleRequest() let invalid = { ...offer, requestId: requestId(unknown) } @@ -159,7 +166,7 @@ function requestId(request) { function offerId(offer) { return keccak256( defaultAbiCoder.encode( - ["bytes32", "uint256", "uint256"], + ["address", "bytes32", "uint256", "uint256"], offerToArray(offer) ) ) @@ -179,5 +186,5 @@ function requestToArray(request) { } function offerToArray(offer) { - return [offer.requestId, offer.price, offer.expiry] + return [offer.host, offer.requestId, offer.price, offer.expiry] } diff --git a/test/examples.js b/test/examples.js index d400bf4..37bf8a5 100644 --- a/test/examples.js +++ b/test/examples.js @@ -19,6 +19,7 @@ const exampleBid = () => ({ }) const exampleOffer = () => ({ + host: hexlify(randomBytes(20)), price: 42, expiry: now() + hours(1), })