2025-01-13 12:04:03 +01:00
|
|
|
const { expect } = require("chai")
|
|
|
|
const { ethers } = require("hardhat")
|
|
|
|
const { randomBytes } = ethers.utils
|
2025-01-27 16:06:00 +01:00
|
|
|
const {
|
|
|
|
currentTime,
|
2025-01-30 09:34:42 +01:00
|
|
|
advanceTimeTo,
|
2025-01-27 16:06:00 +01:00
|
|
|
mine,
|
2025-01-28 10:31:39 +01:00
|
|
|
setAutomine,
|
2025-01-30 13:34:25 +01:00
|
|
|
setNextBlockTimestamp,
|
2025-01-27 16:06:00 +01:00
|
|
|
snapshot,
|
|
|
|
revert,
|
|
|
|
} = require("./evm")
|
2025-01-13 12:04:03 +01:00
|
|
|
|
|
|
|
describe("Vault", function () {
|
|
|
|
let token
|
|
|
|
let vault
|
2025-01-22 11:32:22 +01:00
|
|
|
let controller
|
2025-01-21 14:05:04 +01:00
|
|
|
let account, account2, account3
|
2025-01-13 12:04:03 +01:00
|
|
|
|
|
|
|
beforeEach(async function () {
|
2025-01-27 16:06:00 +01:00
|
|
|
await snapshot()
|
2025-01-13 12:04:03 +01:00
|
|
|
const TestToken = await ethers.getContractFactory("TestToken")
|
|
|
|
token = await TestToken.deploy()
|
|
|
|
const Vault = await ethers.getContractFactory("Vault")
|
|
|
|
vault = await Vault.deploy(token.address)
|
2025-01-22 11:32:22 +01:00
|
|
|
;[controller, account, account2, account3] = await ethers.getSigners()
|
2025-01-14 12:09:48 +01:00
|
|
|
await token.mint(account.address, 1_000_000)
|
2025-01-21 14:05:04 +01:00
|
|
|
await token.mint(account2.address, 1_000_000)
|
|
|
|
await token.mint(account3.address, 1_000_000)
|
2025-01-13 12:04:03 +01:00
|
|
|
})
|
|
|
|
|
2025-01-27 16:06:00 +01:00
|
|
|
afterEach(async function () {
|
|
|
|
await revert()
|
|
|
|
})
|
|
|
|
|
2025-01-13 12:04:03 +01:00
|
|
|
describe("depositing", function () {
|
2025-01-14 12:09:48 +01:00
|
|
|
const context = randomBytes(32)
|
2025-01-13 12:04:03 +01:00
|
|
|
const amount = 42
|
|
|
|
|
|
|
|
it("accepts deposits of tokens", async function () {
|
2025-01-14 12:09:48 +01:00
|
|
|
await token.connect(account).approve(vault.address, amount)
|
|
|
|
await vault.deposit(context, account.address, amount)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await vault.getBalance(context, account.address)).to.equal(amount)
|
2025-01-13 12:04:03 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("keeps custody of tokens that are deposited", async function () {
|
2025-01-14 12:09:48 +01:00
|
|
|
await token.connect(account).approve(vault.address, amount)
|
|
|
|
await vault.deposit(context, account.address, amount)
|
2025-01-13 12:04:03 +01:00
|
|
|
expect(await token.balanceOf(vault.address)).to.equal(amount)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("deposit fails when tokens cannot be transferred", async function () {
|
2025-01-14 12:09:48 +01:00
|
|
|
await token.connect(account).approve(vault.address, amount - 1)
|
|
|
|
const depositing = vault.deposit(context, account.address, amount)
|
2025-01-13 12:04:03 +01:00
|
|
|
await expect(depositing).to.be.revertedWith("insufficient allowance")
|
|
|
|
})
|
|
|
|
|
2025-01-15 11:43:18 +01:00
|
|
|
it("adds multiple deposits to the balance", async function () {
|
2025-01-14 12:09:48 +01:00
|
|
|
await token.connect(account).approve(vault.address, amount)
|
|
|
|
await vault.deposit(context, account.address, amount / 2)
|
|
|
|
await vault.deposit(context, account.address, amount / 2)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await vault.getBalance(context, account.address)).to.equal(amount)
|
2025-01-13 12:04:03 +01:00
|
|
|
})
|
|
|
|
|
2025-01-14 12:09:48 +01:00
|
|
|
it("separates deposits from different contexts", async function () {
|
|
|
|
const context1 = randomBytes(32)
|
|
|
|
const context2 = randomBytes(32)
|
|
|
|
await token.connect(account).approve(vault.address, 3)
|
|
|
|
await vault.deposit(context1, account.address, 1)
|
|
|
|
await vault.deposit(context2, account.address, 2)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await vault.getBalance(context1, account.address)).to.equal(1)
|
|
|
|
expect(await vault.getBalance(context2, account.address)).to.equal(2)
|
2025-01-14 12:09:48 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("separates deposits from different controllers", async function () {
|
|
|
|
const [, , controller1, controller2] = await ethers.getSigners()
|
|
|
|
const vault1 = vault.connect(controller1)
|
|
|
|
const vault2 = vault.connect(controller2)
|
|
|
|
await token.connect(account).approve(vault.address, 3)
|
|
|
|
await vault1.deposit(context, account.address, 1)
|
|
|
|
await vault2.deposit(context, account.address, 2)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await vault1.getBalance(context, account.address)).to.equal(1)
|
|
|
|
expect(await vault2.getBalance(context, account.address)).to.equal(2)
|
2025-01-13 12:04:03 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("withdrawing", function () {
|
2025-01-14 12:09:48 +01:00
|
|
|
const context = randomBytes(32)
|
|
|
|
const amount = 42
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
await token.connect(account).approve(vault.address, amount)
|
|
|
|
await vault.deposit(context, account.address, amount)
|
|
|
|
})
|
2025-01-13 12:04:03 +01:00
|
|
|
|
2025-01-22 11:32:22 +01:00
|
|
|
it("allows controller to withdraw for a recipient", async function () {
|
2025-01-14 12:09:48 +01:00
|
|
|
const before = await token.balanceOf(account.address)
|
|
|
|
await vault.withdraw(context, account.address)
|
|
|
|
const after = await token.balanceOf(account.address)
|
|
|
|
expect(after - before).to.equal(amount)
|
|
|
|
})
|
|
|
|
|
2025-01-22 11:32:22 +01:00
|
|
|
it("allows recipient to withdraw for itself", async function () {
|
|
|
|
const before = await token.balanceOf(account.address)
|
|
|
|
await vault
|
|
|
|
.connect(account)
|
|
|
|
.withdrawByRecipient(controller.address, context)
|
|
|
|
const after = await token.balanceOf(account.address)
|
|
|
|
expect(after - before).to.equal(amount)
|
|
|
|
})
|
|
|
|
|
2025-01-14 12:09:48 +01:00
|
|
|
it("empties the balance when withdrawing", async function () {
|
|
|
|
await vault.withdraw(context, account.address)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await vault.getBalance(context, account.address)).to.equal(0)
|
2025-01-13 12:04:03 +01:00
|
|
|
})
|
|
|
|
|
2025-01-14 12:09:48 +01:00
|
|
|
it("does not withdraw more than once", async function () {
|
|
|
|
await vault.withdraw(context, account.address)
|
|
|
|
const before = await token.balanceOf(account.address)
|
|
|
|
await vault.withdraw(context, account.address)
|
|
|
|
const after = await token.balanceOf(account.address)
|
|
|
|
expect(after).to.equal(before)
|
2025-01-13 12:04:03 +01:00
|
|
|
})
|
|
|
|
})
|
2025-01-14 14:11:53 +01:00
|
|
|
|
|
|
|
describe("burning", function () {
|
|
|
|
const context = randomBytes(32)
|
|
|
|
const amount = 42
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
await token.connect(account).approve(vault.address, amount)
|
|
|
|
await vault.deposit(context, account.address, amount)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("can burn a deposit", async function () {
|
|
|
|
await vault.burn(context, account.address)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await vault.getBalance(context, account.address)).to.equal(0)
|
2025-01-14 14:11:53 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("no longer allows withdrawal", async function () {
|
|
|
|
await vault.burn(context, account.address)
|
|
|
|
const before = await token.balanceOf(account.address)
|
|
|
|
await vault.withdraw(context, account.address)
|
|
|
|
const after = await token.balanceOf(account.address)
|
|
|
|
expect(after).to.equal(before)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("moves the tokens to address 0xdead", async function () {
|
|
|
|
const dead = "0x000000000000000000000000000000000000dead"
|
|
|
|
const before = await token.balanceOf(dead)
|
|
|
|
await vault.burn(context, account.address)
|
|
|
|
const after = await token.balanceOf(dead)
|
|
|
|
expect(after - before).to.equal(amount)
|
|
|
|
})
|
|
|
|
})
|
2025-01-15 10:08:10 +01:00
|
|
|
|
|
|
|
describe("transfering", function () {
|
|
|
|
const context = randomBytes(32)
|
|
|
|
const amount = 42
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
await token.connect(account).approve(vault.address, amount)
|
|
|
|
await vault.deposit(context, account.address, amount)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("can transfer tokens from one recipient to the other", async function () {
|
2025-01-15 11:43:18 +01:00
|
|
|
await vault.transfer(context, account.address, account2.address, amount)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await vault.getBalance(context, account.address)).to.equal(0)
|
|
|
|
expect(await vault.getBalance(context, account2.address)).to.equal(amount)
|
2025-01-15 10:08:10 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("can transfer part of a balance", async function () {
|
2025-01-15 11:43:18 +01:00
|
|
|
await vault.transfer(context, account.address, account2.address, 10)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await vault.getBalance(context, account.address)).to.equal(
|
2025-01-15 10:08:10 +01:00
|
|
|
amount - 10
|
|
|
|
)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await vault.getBalance(context, account2.address)).to.equal(10)
|
2025-01-15 10:08:10 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("does not transfer more than the balance", async function () {
|
|
|
|
await expect(
|
2025-01-15 11:43:18 +01:00
|
|
|
vault.transfer(context, account.address, account2.address, amount + 1)
|
2025-01-15 10:08:10 +01:00
|
|
|
).to.be.revertedWith("InsufficientBalance")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("can withdraw funds that were transfered in", async function () {
|
2025-01-15 11:43:18 +01:00
|
|
|
await vault.transfer(context, account.address, account2.address, amount)
|
|
|
|
const before = await token.balanceOf(account2.address)
|
|
|
|
await vault.withdraw(context, account2.address)
|
|
|
|
const after = await token.balanceOf(account2.address)
|
2025-01-15 10:08:10 +01:00
|
|
|
expect(after - before).to.equal(amount)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("cannot withdraw funds that were transfered out", async function () {
|
2025-01-15 11:43:18 +01:00
|
|
|
await vault.transfer(context, account.address, account2.address, amount)
|
2025-01-15 10:08:10 +01:00
|
|
|
const before = await token.balanceOf(account.address)
|
|
|
|
await vault.withdraw(context, account.address)
|
|
|
|
const after = await token.balanceOf(account.address)
|
|
|
|
expect(after).to.equal(before)
|
|
|
|
})
|
2025-01-15 11:43:18 +01:00
|
|
|
|
|
|
|
it("can transfer out funds that were transfered in", async function () {
|
|
|
|
await vault.transfer(context, account.address, account2.address, amount)
|
|
|
|
await vault.transfer(context, account2.address, account3.address, amount)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await vault.getBalance(context, account2.address)).to.equal(0)
|
|
|
|
expect(await vault.getBalance(context, account3.address)).to.equal(amount)
|
2025-01-15 11:43:18 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("designating", async function () {
|
|
|
|
const context = randomBytes(32)
|
|
|
|
const amount = 42
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
;[, , account2] = await ethers.getSigners()
|
|
|
|
await token.connect(account).approve(vault.address, amount)
|
|
|
|
await vault.deposit(context, account.address, amount)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("can designate tokens for a single recipient", async function () {
|
|
|
|
await vault.designate(context, account.address, amount)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(
|
|
|
|
await vault.getDesignatedBalance(context, account.address)
|
|
|
|
).to.equal(amount)
|
2025-01-15 11:43:18 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("can designate part of the balance", async function () {
|
|
|
|
await vault.designate(context, account.address, 10)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(
|
|
|
|
await vault.getDesignatedBalance(context, account.address)
|
|
|
|
).to.equal(10)
|
2025-01-15 11:43:18 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("adds up designated tokens", async function () {
|
|
|
|
await vault.designate(context, account.address, 10)
|
|
|
|
await vault.designate(context, account.address, 10)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(
|
|
|
|
await vault.getDesignatedBalance(context, account.address)
|
|
|
|
).to.equal(20)
|
2025-01-15 11:43:18 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("cannot designate more than the undesignated balance", async function () {
|
|
|
|
await vault.designate(context, account.address, amount)
|
|
|
|
await expect(
|
|
|
|
vault.designate(context, account.address, 1)
|
|
|
|
).to.be.revertedWith("InsufficientBalance")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("does not change the balance", async function () {
|
|
|
|
await vault.designate(context, account.address, 10)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await vault.getBalance(context, account.address)).to.equal(amount)
|
2025-01-15 11:43:18 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("does not allow designated tokens to be transfered", async function () {
|
|
|
|
await vault.designate(context, account.address, 1)
|
|
|
|
await expect(
|
|
|
|
vault.transfer(context, account.address, account2.address, amount)
|
|
|
|
).to.be.revertedWith("InsufficientBalance")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("allows designated tokens to be withdrawn", async function () {
|
|
|
|
await vault.designate(context, account.address, 10)
|
|
|
|
const before = await token.balanceOf(account.address)
|
|
|
|
await vault.withdraw(context, account.address)
|
|
|
|
const after = await token.balanceOf(account.address)
|
|
|
|
expect(after - before).to.equal(amount)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("does not withdraw designated tokens more than once", async function () {
|
|
|
|
await vault.designate(context, account.address, 10)
|
|
|
|
await vault.withdraw(context, account.address)
|
|
|
|
const before = await token.balanceOf(account.address)
|
|
|
|
await vault.withdraw(context, account.address)
|
|
|
|
const after = await token.balanceOf(account.address)
|
|
|
|
expect(after).to.equal(before)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("allows designated tokens to be burned", async function () {
|
|
|
|
await vault.designate(context, account.address, 10)
|
|
|
|
await vault.burn(context, account.address)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await vault.getBalance(context, account.address)).to.equal(0)
|
2025-01-15 11:43:18 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("moves burned designated tokens to address 0xdead", async function () {
|
|
|
|
const dead = "0x000000000000000000000000000000000000dead"
|
|
|
|
await vault.designate(context, account.address, 10)
|
|
|
|
const before = await token.balanceOf(dead)
|
|
|
|
await vault.burn(context, account.address)
|
|
|
|
const after = await token.balanceOf(dead)
|
|
|
|
expect(after - before).to.equal(amount)
|
|
|
|
})
|
2025-01-15 10:08:10 +01:00
|
|
|
})
|
2025-01-15 14:51:53 +01:00
|
|
|
|
|
|
|
describe("locking", async function () {
|
|
|
|
const context = randomBytes(32)
|
|
|
|
const amount = 42
|
|
|
|
|
2025-01-21 14:06:01 +01:00
|
|
|
let expiry
|
|
|
|
let maximum
|
|
|
|
|
2025-01-15 14:51:53 +01:00
|
|
|
beforeEach(async function () {
|
|
|
|
await token.connect(account).approve(vault.address, amount)
|
|
|
|
await vault.deposit(context, account.address, amount)
|
2025-01-21 14:06:01 +01:00
|
|
|
let start = await currentTime()
|
|
|
|
expiry = start + 10
|
|
|
|
maximum = start + 20
|
2025-01-15 14:51:53 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("can lock up all tokens in a context", async function () {
|
2025-01-28 10:22:17 +01:00
|
|
|
await vault.lock(context, expiry, maximum)
|
|
|
|
expect((await vault.getLock(context))[0]).to.equal(expiry)
|
|
|
|
expect((await vault.getLock(context))[1]).to.equal(maximum)
|
2025-01-15 14:51:53 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("cannot lock up when already locked", async function () {
|
2025-01-28 10:22:17 +01:00
|
|
|
await vault.lock(context, expiry, maximum)
|
|
|
|
const locking = vault.lock(context, expiry, maximum)
|
2025-01-15 14:51:53 +01:00
|
|
|
await expect(locking).to.be.revertedWith("AlreadyLocked")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("cannot lock when expiry is past maximum", async function () {
|
2025-01-28 10:22:17 +01:00
|
|
|
const locking = vault.lock(context, maximum + 1, maximum)
|
2025-01-15 14:51:53 +01:00
|
|
|
await expect(locking).to.be.revertedWith("ExpiryPastMaximum")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("does not allow withdrawal before lock expires", async function () {
|
2025-01-28 10:22:17 +01:00
|
|
|
await vault.lock(context, expiry, expiry)
|
2025-01-30 09:34:42 +01:00
|
|
|
await advanceTimeTo(expiry - 1)
|
2025-01-15 14:51:53 +01:00
|
|
|
const withdrawing = vault.withdraw(context, account.address)
|
|
|
|
await expect(withdrawing).to.be.revertedWith("Locked")
|
|
|
|
})
|
|
|
|
|
2025-01-21 14:06:59 +01:00
|
|
|
it("locks withdrawal for all recipients in a context", async function () {
|
2025-01-28 10:22:17 +01:00
|
|
|
await vault.lock(context, expiry, expiry)
|
2025-01-21 14:06:59 +01:00
|
|
|
const address1 = account.address
|
|
|
|
const address2 = account2.address
|
|
|
|
await vault.transfer(context, address1, address2, amount / 2)
|
|
|
|
let withdrawing1 = vault.withdraw(context, address1)
|
|
|
|
let withdrawing2 = vault.withdraw(context, address2)
|
|
|
|
await expect(withdrawing1).to.be.revertedWith("Locked")
|
|
|
|
await expect(withdrawing2).to.be.revertedWith("Locked")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("locks withdrawal for newly deposited tokens", async function () {
|
2025-01-28 10:22:17 +01:00
|
|
|
await vault.lock(context, expiry, expiry)
|
2025-01-21 14:06:59 +01:00
|
|
|
await token.connect(account2).approve(vault.address, amount)
|
|
|
|
await vault.deposit(context, account2.address, amount)
|
|
|
|
const withdrawing = vault.withdraw(context, account2.address)
|
|
|
|
await expect(withdrawing).to.be.revertedWith("Locked")
|
|
|
|
})
|
|
|
|
|
2025-01-15 14:51:53 +01:00
|
|
|
it("allows withdrawal after lock expires", async function () {
|
2025-01-28 10:22:17 +01:00
|
|
|
await vault.lock(context, expiry, expiry)
|
2025-01-30 09:34:42 +01:00
|
|
|
await advanceTimeTo(expiry)
|
2025-01-15 14:51:53 +01:00
|
|
|
const before = await token.balanceOf(account.address)
|
|
|
|
await vault.withdraw(context, account.address)
|
|
|
|
const after = await token.balanceOf(account.address)
|
|
|
|
expect(after - before).to.equal(amount)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("can extend a lock expiry up to its maximum", async function () {
|
2025-01-28 10:22:17 +01:00
|
|
|
await vault.lock(context, expiry, maximum)
|
|
|
|
await vault.extendLock(context, expiry + 1)
|
|
|
|
expect((await vault.getLock(context))[0]).to.equal(expiry + 1)
|
|
|
|
await vault.extendLock(context, maximum)
|
|
|
|
expect((await vault.getLock(context))[0]).to.equal(maximum)
|
2025-01-15 14:51:53 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("cannot extend a lock past its maximum", async function () {
|
2025-01-28 10:22:17 +01:00
|
|
|
await vault.lock(context, expiry, maximum)
|
|
|
|
const extending = vault.extendLock(context, maximum + 1)
|
2025-01-15 14:51:53 +01:00
|
|
|
await expect(extending).to.be.revertedWith("ExpiryPastMaximum")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("cannot move expiry forward", async function () {
|
2025-01-28 10:22:17 +01:00
|
|
|
await vault.lock(context, expiry, maximum)
|
|
|
|
const extending = vault.extendLock(context, expiry - 1)
|
2025-01-15 14:51:53 +01:00
|
|
|
await expect(extending).to.be.revertedWith("InvalidExpiry")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("cannot extend an expired lock", async function () {
|
2025-01-28 10:22:17 +01:00
|
|
|
await vault.lock(context, expiry, maximum)
|
2025-01-30 09:34:42 +01:00
|
|
|
await advanceTimeTo(expiry)
|
2025-01-28 10:22:17 +01:00
|
|
|
const extending = vault.extendLock(context, maximum)
|
2025-01-28 10:31:39 +01:00
|
|
|
await expect(extending).to.be.revertedWith("LockRequired")
|
2025-01-15 14:51:53 +01:00
|
|
|
})
|
2025-01-21 13:37:01 +01:00
|
|
|
|
2025-01-21 14:06:59 +01:00
|
|
|
it("allows locked tokens to be burned", async function () {
|
2025-01-28 10:22:17 +01:00
|
|
|
await vault.lock(context, expiry, expiry)
|
2025-01-21 14:06:59 +01:00
|
|
|
await vault.burn(context, account.address)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await vault.getBalance(context, account.address)).to.equal(0)
|
2025-01-21 14:06:59 +01:00
|
|
|
})
|
|
|
|
|
2025-01-21 13:37:01 +01:00
|
|
|
it("deletes lock when funds are withdrawn", async function () {
|
2025-01-28 10:22:17 +01:00
|
|
|
await vault.lock(context, expiry, expiry)
|
2025-01-30 09:34:42 +01:00
|
|
|
await advanceTimeTo(expiry)
|
2025-01-21 13:37:01 +01:00
|
|
|
await vault.withdraw(context, account.address)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect((await vault.getLock(context))[0]).to.equal(0)
|
|
|
|
expect((await vault.getLock(context))[1]).to.equal(0)
|
2025-01-21 13:37:01 +01:00
|
|
|
})
|
2025-01-15 14:51:53 +01:00
|
|
|
})
|
2025-01-22 15:07:51 +01:00
|
|
|
|
|
|
|
describe("flow", function () {
|
|
|
|
const context = randomBytes(32)
|
2025-01-28 10:36:52 +01:00
|
|
|
const deposit = 1000
|
2025-01-22 15:07:51 +01:00
|
|
|
|
2025-01-23 10:29:53 +01:00
|
|
|
let sender
|
|
|
|
let receiver
|
2025-01-28 10:31:39 +01:00
|
|
|
let receiver2
|
2025-01-23 10:29:53 +01:00
|
|
|
|
2025-01-22 15:07:51 +01:00
|
|
|
beforeEach(async function () {
|
2025-01-23 14:28:17 +01:00
|
|
|
await token.connect(account).approve(vault.address, deposit)
|
|
|
|
await vault.deposit(context, account.address, deposit)
|
2025-01-23 10:29:53 +01:00
|
|
|
sender = account.address
|
|
|
|
receiver = account2.address
|
2025-01-28 10:31:39 +01:00
|
|
|
receiver2 = account3.address
|
2025-01-22 15:07:51 +01:00
|
|
|
})
|
|
|
|
|
2025-01-28 10:22:17 +01:00
|
|
|
async function getBalance(recipient) {
|
|
|
|
return await vault.getBalance(context, recipient)
|
|
|
|
}
|
|
|
|
|
2025-01-23 10:29:53 +01:00
|
|
|
it("requires that a lock is set", async function () {
|
|
|
|
await expect(vault.flow(context, sender, receiver, 2)).to.be.revertedWith(
|
|
|
|
"LockRequired"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2025-01-23 15:05:07 +01:00
|
|
|
it("requires that the lock is not expired", async function () {
|
|
|
|
let expiry = (await currentTime()) + 20
|
2025-01-28 10:22:17 +01:00
|
|
|
await vault.lock(context, expiry, expiry)
|
2025-01-23 15:05:07 +01:00
|
|
|
await advanceTimeTo(expiry)
|
|
|
|
await expect(vault.flow(context, sender, receiver, 2)).to.be.revertedWith(
|
2025-01-28 10:31:39 +01:00
|
|
|
"LockRequired"
|
2025-01-23 15:05:07 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2025-01-23 10:29:53 +01:00
|
|
|
describe("when a lock is set", async function () {
|
|
|
|
let expiry
|
2025-01-23 14:28:17 +01:00
|
|
|
let maximum
|
2025-01-23 10:29:53 +01:00
|
|
|
|
|
|
|
beforeEach(async function () {
|
2025-01-30 13:34:25 +01:00
|
|
|
const beginning = (await currentTime()) + 10
|
|
|
|
expiry = beginning + 80
|
|
|
|
maximum = beginning + 100
|
|
|
|
await setAutomine(false)
|
|
|
|
await setNextBlockTimestamp(beginning)
|
2025-01-28 10:22:17 +01:00
|
|
|
await vault.lock(context, expiry, maximum)
|
2025-01-23 10:29:53 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("moves tokens over time", async function () {
|
|
|
|
await vault.flow(context, sender, receiver, 2)
|
2025-01-30 13:34:25 +01:00
|
|
|
mine()
|
2025-01-23 10:29:53 +01:00
|
|
|
const start = await currentTime()
|
|
|
|
await advanceTimeTo(start + 2)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await getBalance(sender)).to.equal(deposit - 4)
|
|
|
|
expect(await getBalance(receiver)).to.equal(4)
|
2025-01-23 10:29:53 +01:00
|
|
|
await advanceTimeTo(start + 4)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await getBalance(sender)).to.equal(deposit - 8)
|
|
|
|
expect(await getBalance(receiver)).to.equal(8)
|
2025-01-23 10:29:53 +01:00
|
|
|
})
|
|
|
|
|
2025-01-28 10:31:39 +01:00
|
|
|
it("can move tokens to several different recipients", async function () {
|
|
|
|
await vault.flow(context, sender, receiver, 1)
|
|
|
|
await vault.flow(context, sender, receiver2, 2)
|
|
|
|
await mine()
|
|
|
|
const start = await currentTime()
|
|
|
|
await advanceTimeTo(start + 2)
|
|
|
|
expect(await getBalance(sender)).to.equal(deposit - 6)
|
|
|
|
expect(await getBalance(receiver)).to.equal(2)
|
|
|
|
expect(await getBalance(receiver2)).to.equal(4)
|
|
|
|
await advanceTimeTo(start + 4)
|
|
|
|
expect(await getBalance(sender)).to.equal(deposit - 12)
|
|
|
|
expect(await getBalance(receiver)).to.equal(4)
|
|
|
|
expect(await getBalance(receiver2)).to.equal(8)
|
|
|
|
})
|
|
|
|
|
2025-01-28 15:22:47 +01:00
|
|
|
it("allows flows to be diverted to other recipient", async function () {
|
|
|
|
await vault.flow(context, sender, receiver, 3)
|
|
|
|
await vault.flow(context, receiver, receiver2, 1)
|
|
|
|
await mine()
|
|
|
|
const start = await currentTime()
|
|
|
|
await advanceTimeTo(start + 2)
|
|
|
|
expect(await getBalance(sender)).to.equal(deposit - 6)
|
|
|
|
expect(await getBalance(receiver)).to.equal(4)
|
|
|
|
expect(await getBalance(receiver2)).to.equal(2)
|
|
|
|
await advanceTimeTo(start + 4)
|
|
|
|
expect(await getBalance(sender)).to.equal(deposit - 12)
|
|
|
|
expect(await getBalance(receiver)).to.equal(8)
|
|
|
|
expect(await getBalance(receiver2)).to.equal(4)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("allows flow to be reversed back to the sender", async function () {
|
|
|
|
await vault.flow(context, sender, receiver, 3)
|
|
|
|
await vault.flow(context, receiver, sender, 3)
|
|
|
|
await mine()
|
|
|
|
const start = await currentTime()
|
|
|
|
await advanceTimeTo(start + 2)
|
|
|
|
expect(await getBalance(sender)).to.equal(deposit)
|
|
|
|
expect(await getBalance(receiver)).to.equal(0)
|
|
|
|
await advanceTimeTo(start + 4)
|
|
|
|
expect(await getBalance(sender)).to.equal(deposit)
|
|
|
|
expect(await getBalance(receiver)).to.equal(0)
|
|
|
|
})
|
|
|
|
|
2025-01-28 14:58:14 +01:00
|
|
|
it("can change flows over time", async function () {
|
|
|
|
await vault.flow(context, sender, receiver, 1)
|
|
|
|
await vault.flow(context, sender, receiver2, 2)
|
|
|
|
await mine()
|
|
|
|
const start = await currentTime()
|
2025-01-30 13:34:25 +01:00
|
|
|
setNextBlockTimestamp(start + 4)
|
2025-01-28 14:58:14 +01:00
|
|
|
await vault.flow(context, receiver2, receiver, 1)
|
|
|
|
await mine()
|
|
|
|
expect(await getBalance(sender)).to.equal(deposit - 12)
|
|
|
|
expect(await getBalance(receiver)).to.equal(4)
|
|
|
|
expect(await getBalance(receiver2)).to.equal(8)
|
|
|
|
await advanceTimeTo(start + 8)
|
|
|
|
expect(await getBalance(sender)).to.equal(deposit - 24)
|
|
|
|
expect(await getBalance(receiver)).to.equal(12)
|
|
|
|
expect(await getBalance(receiver2)).to.equal(12)
|
|
|
|
await advanceTimeTo(start + 12)
|
|
|
|
expect(await getBalance(sender)).to.equal(deposit - 36)
|
|
|
|
expect(await getBalance(receiver)).to.equal(20)
|
|
|
|
expect(await getBalance(receiver2)).to.equal(16)
|
|
|
|
})
|
|
|
|
|
2025-01-23 10:29:53 +01:00
|
|
|
it("designates tokens that flow for the recipient", async function () {
|
|
|
|
await vault.flow(context, sender, receiver, 3)
|
2025-01-30 13:34:25 +01:00
|
|
|
await mine()
|
2025-01-23 10:29:53 +01:00
|
|
|
const start = await currentTime()
|
|
|
|
await advanceTimeTo(start + 7)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await vault.getDesignatedBalance(context, receiver)).to.equal(21)
|
2025-01-23 10:29:53 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("stops flowing when lock expires", async function () {
|
|
|
|
await vault.flow(context, sender, receiver, 2)
|
2025-01-30 13:34:25 +01:00
|
|
|
await mine()
|
2025-01-23 10:29:53 +01:00
|
|
|
const start = await currentTime()
|
|
|
|
await advanceTimeTo(expiry)
|
|
|
|
const total = (expiry - start) * 2
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await getBalance(sender)).to.equal(deposit - total)
|
|
|
|
expect(await getBalance(receiver)).to.equal(total)
|
2025-01-23 10:29:53 +01:00
|
|
|
await advanceTimeTo(expiry + 10)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await getBalance(sender)).to.equal(deposit - total)
|
|
|
|
expect(await getBalance(receiver)).to.equal(total)
|
2025-01-23 10:29:53 +01:00
|
|
|
})
|
2025-01-23 14:28:17 +01:00
|
|
|
|
|
|
|
it("flows longer when lock is extended", async function () {
|
|
|
|
await vault.flow(context, sender, receiver, 2)
|
2025-01-30 13:34:25 +01:00
|
|
|
await mine()
|
2025-01-23 14:28:17 +01:00
|
|
|
const start = await currentTime()
|
2025-01-28 10:22:17 +01:00
|
|
|
await vault.extendLock(context, maximum)
|
2025-01-30 13:34:25 +01:00
|
|
|
await mine()
|
2025-01-23 14:28:17 +01:00
|
|
|
await advanceTimeTo(maximum)
|
|
|
|
const total = (maximum - start) * 2
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await getBalance(sender)).to.equal(deposit - total)
|
|
|
|
expect(await getBalance(receiver)).to.equal(total)
|
2025-01-23 14:28:17 +01:00
|
|
|
await advanceTimeTo(maximum + 10)
|
2025-01-28 10:22:17 +01:00
|
|
|
expect(await getBalance(sender)).to.equal(deposit - total)
|
|
|
|
expect(await getBalance(receiver)).to.equal(total)
|
2025-01-23 14:28:17 +01:00
|
|
|
})
|
|
|
|
|
2025-01-28 15:22:47 +01:00
|
|
|
it("rejects negative flows", async function () {
|
2025-01-30 13:34:25 +01:00
|
|
|
setAutomine(true)
|
2025-01-28 10:33:54 +01:00
|
|
|
await expect(
|
|
|
|
vault.flow(context, sender, receiver, -1)
|
|
|
|
).to.be.revertedWith("NegativeFlow")
|
|
|
|
})
|
|
|
|
|
2025-01-23 14:28:17 +01:00
|
|
|
it("rejects flow when insufficient available tokens", async function () {
|
2025-01-30 13:34:25 +01:00
|
|
|
setAutomine(true)
|
2025-01-23 14:28:17 +01:00
|
|
|
await expect(
|
2025-01-28 15:55:50 +01:00
|
|
|
vault.flow(context, sender, receiver, 11)
|
2025-01-23 14:28:17 +01:00
|
|
|
).to.be.revertedWith("InsufficientBalance")
|
|
|
|
})
|
2025-01-28 10:31:39 +01:00
|
|
|
|
|
|
|
it("rejects total flows exceeding available tokens", async function () {
|
2025-01-28 15:55:50 +01:00
|
|
|
await vault.flow(context, sender, receiver, 10)
|
2025-01-30 13:34:25 +01:00
|
|
|
setAutomine(true)
|
2025-01-28 10:31:39 +01:00
|
|
|
await expect(
|
2025-01-28 15:55:50 +01:00
|
|
|
vault.flow(context, sender, receiver, 1)
|
2025-01-28 10:31:39 +01:00
|
|
|
).to.be.revertedWith("InsufficientBalance")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("cannot flow designated tokens", async function () {
|
2025-01-28 15:55:50 +01:00
|
|
|
await vault.designate(context, sender, 500)
|
2025-01-30 13:34:25 +01:00
|
|
|
await vault.flow(context, sender, receiver, 5)
|
|
|
|
setAutomine(true)
|
2025-01-28 15:55:50 +01:00
|
|
|
await expect(
|
|
|
|
vault.flow(context, sender, receiver, 1)
|
2025-01-28 10:31:39 +01:00
|
|
|
).to.be.revertedWith("InsufficientBalance")
|
|
|
|
})
|
2025-01-28 15:56:53 +01:00
|
|
|
|
|
|
|
it("cannot transfer tokens that are flowing", async function () {
|
|
|
|
await vault.flow(context, sender, receiver, 5)
|
2025-01-30 13:34:25 +01:00
|
|
|
setAutomine(true)
|
|
|
|
await vault.transfer(context, sender, receiver, 500)
|
2025-01-28 15:56:53 +01:00
|
|
|
await expect(
|
|
|
|
vault.transfer(context, sender, receiver, 1)
|
|
|
|
).to.be.revertedWith("InsufficientBalance")
|
|
|
|
})
|
2025-01-28 16:08:11 +01:00
|
|
|
|
2025-01-30 13:36:46 +01:00
|
|
|
it("cannot designate tokens that are flowing", async function () {
|
|
|
|
await vault.flow(context, sender, receiver, 5)
|
|
|
|
setAutomine(true)
|
|
|
|
await vault.designate(context, sender, 500)
|
|
|
|
await expect(vault.designate(context, sender, 1)).to.be.revertedWith(
|
|
|
|
"InsufficientBalance"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2025-01-28 16:08:11 +01:00
|
|
|
it("cannot burn tokens that are flowing", async function () {
|
|
|
|
await vault.flow(context, sender, receiver, 5)
|
2025-01-30 13:34:25 +01:00
|
|
|
setAutomine(true)
|
|
|
|
await expect(vault.burn(context, sender)).to.be.revertedWith(
|
|
|
|
"CannotBurnFlowingTokens"
|
|
|
|
)
|
|
|
|
await expect(vault.burn(context, receiver)).to.be.revertedWith(
|
|
|
|
"CannotBurnFlowingTokens"
|
|
|
|
)
|
2025-01-28 16:08:11 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("can burn tokens that are no longer flowing", async function () {
|
2025-01-30 13:34:25 +01:00
|
|
|
setAutomine(true)
|
2025-01-28 16:08:11 +01:00
|
|
|
await vault.flow(context, sender, receiver, 5)
|
|
|
|
await vault.flow(context, receiver, sender, 5)
|
|
|
|
await expect(vault.burn(context, sender)).not.to.be.reverted
|
|
|
|
})
|
2025-01-22 15:58:34 +01:00
|
|
|
})
|
2025-01-22 15:07:51 +01:00
|
|
|
})
|
2025-01-13 12:04:03 +01:00
|
|
|
})
|