mirror of
https://github.com/codex-storage/codex-contracts-eth.git
synced 2025-02-18 06:16:33 +00:00
[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:
parent
37004e0e1f
commit
3a97330e72
@ -42,6 +42,18 @@ contract AccountLocks {
|
|||||||
lock.unlocked = true;
|
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
|
/// Unlocks an account. This will fail if there are any active locks attached
|
||||||
/// to this account.
|
/// to this account.
|
||||||
/// Calling this function triggers a cleanup of inactive locks, making this
|
/// Calling this function triggers a cleanup of inactive locks, making this
|
||||||
|
@ -76,6 +76,7 @@ contract Marketplace is Collateral, Proofs {
|
|||||||
emit SlotFilled(requestId, slotIndex, slotId);
|
emit SlotFilled(requestId, slotIndex, slotId);
|
||||||
if (context.slotsFilled == request.ask.slots) {
|
if (context.slotsFilled == request.ask.slots) {
|
||||||
context.state = RequestState.Started;
|
context.state = RequestState.Started;
|
||||||
|
_extendLockExpiry(requestId, block.timestamp + request.ask.duration);
|
||||||
emit RequestFulfilled(requestId);
|
emit RequestFulfilled(requestId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,4 +20,8 @@ contract TestAccountLocks is AccountLocks {
|
|||||||
function unlockAccount() public {
|
function unlockAccount() public {
|
||||||
_unlockAccount();
|
_unlockAccount();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function extendLockExpiry(bytes32 lockId, uint256 expiry) public {
|
||||||
|
_extendLockExpiry(lockId, expiry);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
const { ethers } = require("hardhat")
|
const { ethers } = require("hardhat")
|
||||||
const { expect } = require("chai")
|
const { expect } = require("chai")
|
||||||
|
const { hexlify, randomBytes, toHexString } = ethers.utils
|
||||||
|
const { advanceTimeTo, snapshot, revert } = require("./evm")
|
||||||
const { exampleLock } = require("./examples")
|
const { exampleLock } = require("./examples")
|
||||||
const { now } = require("./time")
|
const { now, hours } = require("./time")
|
||||||
|
const { waitUntilExpired } = require("./marketplace")
|
||||||
|
|
||||||
describe("Account Locks", function () {
|
describe("Account Locks", function () {
|
||||||
let locks
|
let locks
|
||||||
@ -162,4 +165,42 @@ describe("Account Locks", function () {
|
|||||||
await locks.unlockAccount()
|
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
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user