vault: do not expose Lock internals on public api

This commit is contained in:
Mark Spanbroek 2025-02-06 14:34:48 +01:00
parent dbaaae8c22
commit 0915fa33f1
4 changed files with 37 additions and 16 deletions

View File

@ -25,9 +25,14 @@ contract Vault is VaultBase {
return balance.designated; return balance.designated;
} }
function getLock(Fund fund) public view returns (Lock memory) { function getLockStatus(Fund fund) public view returns (LockStatus) {
Controller controller = Controller.wrap(msg.sender); Controller controller = Controller.wrap(msg.sender);
return _getLock(controller, fund); return _getLockStatus(controller, fund);
}
function getLockExpiry(Fund fund) public view returns (Timestamp) {
Controller controller = Controller.wrap(msg.sender);
return _getLockExpiry(controller, fund);
} }
function lock(Fund fund, Timestamp expiry, Timestamp maximum) public { function lock(Fund fund, Timestamp expiry, Timestamp maximum) public {

View File

@ -28,11 +28,18 @@ abstract contract VaultBase {
_token = token; _token = token;
} }
function _getLock( function _getLockStatus(
Controller controller, Controller controller,
Fund fund Fund fund
) internal view returns (Lock memory) { ) internal view returns (LockStatus) {
return _locks[controller][fund]; return _locks[controller][fund].status();
}
function _getLockExpiry(
Controller controller,
Fund fund
) internal view returns (Timestamp) {
return _locks[controller][fund].expiry;
} }
function _getBalance( function _getBalance(

View File

@ -10,6 +10,7 @@ const {
snapshot, snapshot,
revert, revert,
} = require("./evm") } = require("./evm")
const { LockStatus } = require("./vault")
describe("Vault", function () { describe("Vault", function () {
const fund = randomBytes(32) const fund = randomBytes(32)
@ -46,8 +47,8 @@ describe("Vault", function () {
expiry = (await currentTime()) + 80 expiry = (await currentTime()) + 80
maximum = (await currentTime()) + 100 maximum = (await currentTime()) + 100
await vault.lock(fund, expiry, maximum) await vault.lock(fund, expiry, maximum)
expect((await vault.getLock(fund))[0]).to.equal(expiry) expect(await vault.getLockStatus(fund)).to.equal(LockStatus.Locked)
expect((await vault.getLock(fund))[1]).to.equal(maximum) expect(await vault.getLockExpiry(fund)).to.equal(expiry)
}) })
it("does not allow a lock with expiry past maximum", async function () { it("does not allow a lock with expiry past maximum", async function () {
@ -87,9 +88,9 @@ describe("Vault", function () {
it("can extend a lock expiry up to its maximum", async function () { it("can extend a lock expiry up to its maximum", async function () {
await vault.extendLock(fund, expiry + 1) await vault.extendLock(fund, expiry + 1)
expect((await vault.getLock(fund))[0]).to.equal(expiry + 1) expect(await vault.getLockExpiry(fund)).to.equal(expiry + 1)
await vault.extendLock(fund, maximum) await vault.extendLock(fund, maximum)
expect((await vault.getLock(fund))[0]).to.equal(maximum) expect(await vault.getLockExpiry(fund)).to.equal(maximum)
}) })
it("cannot extend a lock past its maximum", async function () { it("cannot extend a lock past its maximum", async function () {
@ -106,8 +107,8 @@ describe("Vault", function () {
await token.connect(controller).approve(vault.address, 30) await token.connect(controller).approve(vault.address, 30)
await vault.deposit(fund, account.address, 30) await vault.deposit(fund, account.address, 30)
await vault.burn(fund, account.address) await vault.burn(fund, account.address)
expect((await vault.getLock(fund))[0]).to.not.equal(0) expect(await vault.getLockStatus(fund)).to.equal(LockStatus.Locked)
expect((await vault.getLock(fund))[1]).to.not.equal(0) expect(await vault.getLockExpiry(fund)).to.not.equal(0)
}) })
}) })
@ -587,15 +588,15 @@ describe("Vault", function () {
await expire() await expire()
// some tokens are withdrawn // some tokens are withdrawn
await vault.withdraw(fund, account.address) await vault.withdraw(fund, account.address)
expect((await vault.getLock(fund))[0]).not.to.equal(0) expect(await vault.getLockStatus(fund)).to.equal(LockStatus.Unlocked)
expect((await vault.getLock(fund))[1]).not.to.equal(0) expect(await vault.getLockExpiry(fund)).not.to.equal(0)
// remainder of the tokens are withdrawn by recipient // remainder of the tokens are withdrawn by recipient
await vault await vault
.connect(account3) .connect(account3)
.withdrawByRecipient(controller.address, fund) .withdrawByRecipient(controller.address, fund)
expect((await vault.getLock(fund))[0]).to.equal(0) expect(await vault.getLockStatus(fund)).to.equal(LockStatus.NoLock)
expect((await vault.getLock(fund))[1]).to.equal(0) expect(await vault.getLockExpiry(fund)).to.equal(0)
}) })
}) })
describe("flowing", function () { describe("flowing", function () {

8
test/vault.js Normal file
View File

@ -0,0 +1,8 @@
const LockStatus = {
NoLock: 0,
Locked: 1,
Unlocked: 2,
Burned: 3
}
module.exports = { LockStatus }