2022-02-15 16:01:54 +00:00
|
|
|
const { ethers } = require("hardhat")
|
|
|
|
const { expect } = require("chai")
|
2022-08-09 11:19:42 +00:00
|
|
|
const { hexlify, randomBytes, toHexString } = ethers.utils
|
2022-09-22 02:21:49 +00:00
|
|
|
const {
|
|
|
|
advanceTimeTo,
|
|
|
|
snapshot,
|
|
|
|
revert,
|
|
|
|
advanceTime,
|
|
|
|
currentTime,
|
|
|
|
} = require("./evm")
|
2022-02-15 16:01:54 +00:00
|
|
|
const { exampleLock } = require("./examples")
|
2022-09-22 02:21:49 +00:00
|
|
|
const { hours } = require("./time")
|
2022-02-15 16:01:54 +00:00
|
|
|
|
|
|
|
describe("Account Locks", function () {
|
|
|
|
let locks
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
let AccountLocks = await ethers.getContractFactory("TestAccountLocks")
|
|
|
|
locks = await AccountLocks.deploy()
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("creating a lock", function () {
|
|
|
|
it("allows creation of a lock with an expiry time", async function () {
|
2022-09-22 02:21:49 +00:00
|
|
|
let { id, expiry } = await exampleLock()
|
2022-02-15 16:01:54 +00:00
|
|
|
await locks.createLock(id, expiry)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("fails to create a lock with an existing id", async function () {
|
2022-09-22 02:21:49 +00:00
|
|
|
let { id, expiry } = await exampleLock()
|
2022-02-15 16:01:54 +00:00
|
|
|
await locks.createLock(id, expiry)
|
|
|
|
await expect(locks.createLock(id, expiry + 1)).to.be.revertedWith(
|
|
|
|
"Lock already exists"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("locking an account", function () {
|
|
|
|
let lock
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
2022-09-22 02:21:49 +00:00
|
|
|
lock = await exampleLock()
|
2022-02-15 16:01:54 +00:00
|
|
|
await locks.createLock(lock.id, lock.expiry)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("locks an account", async function () {
|
|
|
|
let [account] = await ethers.getSigners()
|
|
|
|
await locks.lock(account.address, lock.id)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("fails to lock account when lock does not exist", async function () {
|
|
|
|
let [account] = await ethers.getSigners()
|
2022-09-22 02:21:49 +00:00
|
|
|
let nonexistent = (await exampleLock()).id
|
2022-02-15 16:01:54 +00:00
|
|
|
await expect(locks.lock(account.address, nonexistent)).to.be.revertedWith(
|
|
|
|
"Lock does not exist"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("unlocking a lock", function () {
|
|
|
|
let lock
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
2022-09-22 02:21:49 +00:00
|
|
|
lock = await exampleLock()
|
2022-02-15 16:01:54 +00:00
|
|
|
await locks.createLock(lock.id, lock.expiry)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("unlocks a lock", async function () {
|
|
|
|
await locks.unlock(lock.id)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("fails to unlock a lock that does not exist", async function () {
|
2022-09-22 02:21:49 +00:00
|
|
|
let nonexistent = (await exampleLock()).id
|
2022-02-15 16:01:54 +00:00
|
|
|
await expect(locks.unlock(nonexistent)).to.be.revertedWith(
|
|
|
|
"Lock does not exist"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("fails to unlock by someone other than the creator", async function () {
|
|
|
|
let [_, other] = await ethers.getSigners()
|
|
|
|
await expect(locks.connect(other).unlock(lock.id)).to.be.revertedWith(
|
|
|
|
"Only lock creator can unlock"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("unlocking an account", function () {
|
|
|
|
it("unlocks an account that has not been locked", async function () {
|
|
|
|
await locks.unlockAccount()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("unlocks an account whose locks have been unlocked", async function () {
|
|
|
|
let [account] = await ethers.getSigners()
|
2022-09-22 02:21:49 +00:00
|
|
|
let lock = await exampleLock()
|
2022-02-15 16:01:54 +00:00
|
|
|
await locks.createLock(lock.id, lock.expiry)
|
|
|
|
await locks.lock(account.address, lock.id)
|
|
|
|
await locks.unlock(lock.id)
|
|
|
|
await locks.unlockAccount()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("unlocks an account whose locks have expired", async function () {
|
|
|
|
let [account] = await ethers.getSigners()
|
2022-09-22 02:21:49 +00:00
|
|
|
let lock = { ...(await exampleLock()), expiry: currentTime() }
|
2022-02-15 16:01:54 +00:00
|
|
|
await locks.createLock(lock.id, lock.expiry)
|
|
|
|
await locks.lock(account.address, lock.id)
|
|
|
|
await locks.unlockAccount()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("unlocks multiple accounts tied to the same lock", async function () {
|
|
|
|
let [account0, account1] = await ethers.getSigners()
|
2022-09-22 02:21:49 +00:00
|
|
|
let lock = await exampleLock()
|
2022-02-15 16:01:54 +00:00
|
|
|
await locks.createLock(lock.id, lock.expiry)
|
|
|
|
await locks.lock(account0.address, lock.id)
|
|
|
|
await locks.lock(account1.address, lock.id)
|
|
|
|
await locks.unlock(lock.id)
|
|
|
|
await locks.connect(account0).unlockAccount()
|
|
|
|
await locks.connect(account1).unlockAccount()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("fails to unlock when some locks are still locked", async function () {
|
|
|
|
let [account] = await ethers.getSigners()
|
2022-09-22 02:21:49 +00:00
|
|
|
let [lock1, lock2] = [await exampleLock(), await exampleLock()]
|
2022-02-15 16:01:54 +00:00
|
|
|
await locks.createLock(lock1.id, lock1.expiry)
|
|
|
|
await locks.createLock(lock2.id, lock2.expiry)
|
|
|
|
await locks.lock(account.address, lock1.id)
|
|
|
|
await locks.lock(account.address, lock2.id)
|
|
|
|
await locks.unlock(lock1.id)
|
|
|
|
await expect(locks.unlockAccount()).to.be.revertedWith("Account locked")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("limits", function () {
|
|
|
|
let maxlocks
|
|
|
|
let account
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
maxlocks = await locks.MAX_LOCKS_PER_ACCOUNT()
|
|
|
|
;[account] = await ethers.getSigners()
|
|
|
|
})
|
|
|
|
|
|
|
|
async function addLock() {
|
2022-09-22 02:21:49 +00:00
|
|
|
let { id, expiry } = await exampleLock()
|
2022-02-15 16:01:54 +00:00
|
|
|
await locks.createLock(id, expiry)
|
|
|
|
await locks.lock(account.address, id)
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
it("supports a limited amount of locks per account", async function () {
|
|
|
|
for (let i = 0; i < maxlocks; i++) {
|
|
|
|
await addLock()
|
|
|
|
}
|
|
|
|
await expect(addLock()).to.be.revertedWith("Max locks reached")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("doesn't count unlocked locks towards the limit", async function () {
|
|
|
|
for (let i = 0; i < maxlocks; i++) {
|
|
|
|
let id = await addLock()
|
|
|
|
await locks.unlock(id)
|
|
|
|
}
|
|
|
|
await expect(addLock()).not.to.be.reverted
|
|
|
|
})
|
|
|
|
|
|
|
|
it("handles maximum amount of locks within gas limit", async function () {
|
|
|
|
let ids = []
|
|
|
|
for (let i = 0; i < maxlocks; i++) {
|
|
|
|
ids.push(await addLock())
|
|
|
|
}
|
|
|
|
for (let id of ids) {
|
|
|
|
await locks.unlock(id)
|
|
|
|
}
|
|
|
|
await locks.unlockAccount()
|
|
|
|
})
|
|
|
|
})
|
2022-08-09 11:19:42 +00:00
|
|
|
|
|
|
|
describe("extend lock expiry", function () {
|
|
|
|
let expiry
|
2022-09-22 02:21:49 +00:00
|
|
|
let newExpiry
|
2022-08-09 11:19:42 +00:00
|
|
|
let id
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
await snapshot()
|
|
|
|
|
2022-09-22 02:21:49 +00:00
|
|
|
let lock = await exampleLock()
|
2022-08-09 11:19:42 +00:00
|
|
|
id = lock.id
|
|
|
|
expiry = lock.expiry
|
|
|
|
await locks.createLock(id, expiry)
|
2022-09-22 02:21:49 +00:00
|
|
|
newExpiry = (await currentTime()) + hours(1)
|
|
|
|
|
2022-08-09 11:19:42 +00:00
|
|
|
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 () {
|
2022-09-22 02:21:49 +00:00
|
|
|
let other = await exampleLock()
|
2022-08-09 11:19:42 +00:00
|
|
|
await expect(
|
2022-09-22 02:21:49 +00:00
|
|
|
locks.extendLockExpiryTo(other.id, newExpiry)
|
2022-08-09 11:19:42 +00:00
|
|
|
).to.be.revertedWith("Lock does not exist")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("fails when lock is already expired", async function () {
|
2022-09-23 02:33:39 +00:00
|
|
|
await advanceTimeTo(expiry)
|
2022-09-22 02:21:49 +00:00
|
|
|
await expect(locks.extendLockExpiryTo(id, newExpiry)).to.be.revertedWith(
|
2022-08-09 11:19:42 +00:00
|
|
|
"Lock already expired"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("successfully updates lock expiry", async function () {
|
2022-09-22 02:21:49 +00:00
|
|
|
await expect(locks.extendLockExpiryTo(id, newExpiry)).not.to.be.reverted
|
2022-08-09 11:19:42 +00:00
|
|
|
})
|
|
|
|
})
|
2022-02-15 16:01:54 +00:00
|
|
|
})
|