From 38fee6d83a13dd0085b21da4337c04bd118a2620 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 4 Nov 2021 11:55:47 +0100 Subject: [PATCH] Pay host when contract is finished --- contracts/Storage.sol | 1 + test/Storage.test.js | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/contracts/Storage.sol b/contracts/Storage.sol index e528390..25d8717 100644 --- a/contracts/Storage.sol +++ b/contracts/Storage.sol @@ -60,6 +60,7 @@ contract Storage is Contracts, Proofs, Stakes { function finishContract(bytes32 id) public { require(block.number > proofEnd(id), "Contract has not ended yet"); require(!finished[id], "Contract already finished"); + require(_token().transfer(host(id), price(id)), "Payment failed"); _unlockStake(host(id)); finished[id] = true; } diff --git a/test/Storage.test.js b/test/Storage.test.js index bc044ac..29ab4bf 100644 --- a/test/Storage.test.js +++ b/test/Storage.test.js @@ -88,15 +88,27 @@ describe("Storage", function () { await storage.connect(host).startContract(id) }) - it("unlocks the host stake", async function () { + async function mineUntilEnd() { const end = await storage.proofEnd(id) while (await minedBlockNumber() < end) { await mineBlock() } + } + + it("unlocks the host stake", async function () { + await mineUntilEnd() await storage.finishContract(id) await expect(storage.connect(host).withdrawStake()).not.to.be.reverted }) + it("pays the host", async function () { + await mineUntilEnd() + const startBalance = await token.balanceOf(host.address) + await storage.finishContract(id) + const endBalance = await token.balanceOf(host.address) + expect(endBalance - startBalance).to.equal(bid.price) + }) + it("is only allowed when end time has passed", async function () { await expect( storage.finishContract(id) @@ -104,10 +116,7 @@ describe("Storage", function () { }) it("can only be done once", async function () { - const end = await storage.proofEnd(id) - while (await minedBlockNumber() < end) { - await mineBlock() - } + await mineUntilEnd() await storage.finishContract(id) await expect( storage.finishContract(id) @@ -162,4 +171,6 @@ describe("Storage", function () { // TODO: contract start and timeout // TODO: failure to start contract burns host and client // TODO: implement checking of actual proofs of storage, instead of dummy bool -// TODO: payout +// TODO: slash stake when too many missed proofs +// TODO: allow other host to take over contract when too many missed proofs +// TODO: small partial payouts when proofs are being submitted