From d2a3cc4a893a0ccb642a5e547bd7756052e8323a Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 4 Nov 2021 11:40:03 +0100 Subject: [PATCH] Client pays price when creating contract --- contracts/Stakes.sol | 8 ++++++-- contracts/Storage.sol | 1 + test/Storage.test.js | 28 +++++++++++++++++++++++++--- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/contracts/Stakes.sol b/contracts/Stakes.sol index 00352a3..1613419 100644 --- a/contracts/Stakes.sol +++ b/contracts/Stakes.sol @@ -9,8 +9,12 @@ contract Stakes { mapping(address=>uint) private stakes; mapping(address=>uint) private locks; - constructor(IERC20 _token) { - token = _token; + constructor(IERC20 __token) { + token = __token; + } + + function _token() internal view returns (IERC20) { + return token; } function _stake(address account) internal view returns (uint) { diff --git a/contracts/Storage.sol b/contracts/Storage.sol index 959f914..e528390 100644 --- a/contracts/Storage.sol +++ b/contracts/Storage.sol @@ -32,6 +32,7 @@ contract Storage is Contracts, Proofs, Stakes { { require(_stake(_host) >= stakeAmount, "Insufficient stake"); _lockStake(_host); + _token().transferFrom(msg.sender, address(this), _price); _newContract( _duration, _size, diff --git a/test/Storage.test.js b/test/Storage.test.js index 8bd6b15..bc044ac 100644 --- a/test/Storage.test.js +++ b/test/Storage.test.js @@ -27,7 +27,8 @@ describe("Storage", function () { let id beforeEach(async function () { - await token.approve(storage.address, stakeAmount) + await token.connect(host).approve(storage.address, stakeAmount) + await token.connect(client).approve(storage.address, bid.price) await storage.connect(host).increaseStake(stakeAmount) let requestHash = hashRequest(request) let bidHash = hashBid({...bid, requestHash}) @@ -116,7 +117,8 @@ describe("Storage", function () { }) it("doesn't create contract with insufficient stake", async function () { - await token.approve(storage.address, stakeAmount - 1) + await token.connect(host).approve(storage.address, stakeAmount - 1) + await token.connect(client).approve(storage.address, bid.price) await storage.connect(host).increaseStake(stakeAmount - 1) let requestHash = hashRequest(request) let bidHash = hashBid({...bid, requestHash}) @@ -134,9 +136,29 @@ describe("Storage", function () { await sign(host, bidHash) )).to.be.revertedWith("Insufficient stake") }) + + it("doesn't create contract without payment of price", async function () { + await token.connect(host).approve(storage.address, stakeAmount) + await token.connect(client).approve(storage.address, bid.price - 1) + await storage.connect(host).increaseStake(stakeAmount) + let requestHash = hashRequest(request) + let bidHash = hashBid({...bid, requestHash}) + await expect(storage.newContract( + request.duration, + request.size, + request.contentHash, + request.proofPeriod, + request.proofTimeout, + request.nonce, + bid.price, + await host.getAddress(), + bid.bidExpiry, + await sign(client, requestHash), + await sign(host, bidHash) + )).to.be.revertedWith("ERC20: transfer amount exceeds allowance") + }) }) -// TODO: payment when new contract // 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