[marketplace] extend lock expiry

Add ability to extend lock expiry once all slots are filled.

Tests to support lock expiry.
This commit is contained in:
Eric Mastro 2022-08-09 21:19:42 +10:00 committed by Eric Mastro
parent 37004e0e1f
commit 3a97330e72
4 changed files with 59 additions and 1 deletions

View File

@ -42,6 +42,18 @@ contract AccountLocks {
lock.unlocked = true;
}
/// Extends the locks expiry time. Lock must not have already expired.
/// NOTE: We do not need to check that msg.sender is the lock.owner because
/// this function is internal, and is only called after all checks have been
/// performed in Marketplace.fillSlot.
function _extendLockExpiry(bytes32 lockId, uint256 duration) internal {
Lock storage lock = locks[lockId];
require(lock.owner != address(0), "Lock does not exist");
// require(lock.owner == msg.sender, "Only lock creator can extend expiry");
require(lock.expiry >= block.timestamp, "Lock already expired");
lock.expiry += duration;
}
/// Unlocks an account. This will fail if there are any active locks attached
/// to this account.
/// Calling this function triggers a cleanup of inactive locks, making this

View File

@ -76,6 +76,7 @@ contract Marketplace is Collateral, Proofs {
emit SlotFilled(requestId, slotIndex, slotId);
if (context.slotsFilled == request.ask.slots) {
context.state = RequestState.Started;
_extendLockExpiry(requestId, block.timestamp + request.ask.duration);
emit RequestFulfilled(requestId);
}
}

View File

@ -20,4 +20,8 @@ contract TestAccountLocks is AccountLocks {
function unlockAccount() public {
_unlockAccount();
}
function extendLockExpiry(bytes32 lockId, uint256 expiry) public {
_extendLockExpiry(lockId, expiry);
}
}

View File

@ -1,7 +1,10 @@
const { ethers } = require("hardhat")
const { expect } = require("chai")
const { hexlify, randomBytes, toHexString } = ethers.utils
const { advanceTimeTo, snapshot, revert } = require("./evm")
const { exampleLock } = require("./examples")
const { now } = require("./time")
const { now, hours } = require("./time")
const { waitUntilExpired } = require("./marketplace")
describe("Account Locks", function () {
let locks
@ -162,4 +165,42 @@ describe("Account Locks", function () {
await locks.unlockAccount()
})
})
describe("extend lock expiry", function () {
let expiry
let id
beforeEach(async function () {
await snapshot()
let lock = exampleLock()
id = lock.id
expiry = lock.expiry
await locks.createLock(id, expiry)
let [account] = await ethers.getSigners()
await locks.lock(account.address, id)
})
afterEach(async function () {
await revert()
})
it("fails when lock id doesn't exist", async function () {
let other = exampleLock()
await expect(
locks.extendLockExpiry(other.id, hours(1))
).to.be.revertedWith("Lock does not exist")
})
it("fails when lock is already expired", async function () {
waitUntilExpired(expiry + hours(1))
await expect(locks.extendLockExpiry(id, hours(1))).to.be.revertedWith(
"Lock already expired"
)
})
it("successfully updates lock expiry", async function () {
await expect(locks.extendLockExpiry(id, hours(1))).not.to.be.reverted
})
})
})