Client pays price when creating contract

This commit is contained in:
Mark Spanbroek 2021-11-04 11:40:03 +01:00
parent afad0e49ec
commit d2a3cc4a89
3 changed files with 32 additions and 5 deletions

View File

@ -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) {

View File

@ -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,

View File

@ -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