2025-01-13 12:04:03 +01:00
|
|
|
const { expect } = require("chai")
|
|
|
|
const { ethers } = require("hardhat")
|
|
|
|
const { randomBytes } = ethers.utils
|
|
|
|
|
|
|
|
describe("Vault", function () {
|
|
|
|
let token
|
|
|
|
let vault
|
2025-01-14 12:09:48 +01:00
|
|
|
let account
|
2025-01-13 12:04:03 +01:00
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
const TestToken = await ethers.getContractFactory("TestToken")
|
|
|
|
token = await TestToken.deploy()
|
|
|
|
const Vault = await ethers.getContractFactory("Vault")
|
|
|
|
vault = await Vault.deploy(token.address)
|
2025-01-14 12:09:48 +01:00
|
|
|
;[, account] = await ethers.getSigners()
|
|
|
|
await token.mint(account.address, 1_000_000)
|
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)
|
|
|
|
expect(await vault.balance(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-14 12:09:48 +01:00
|
|
|
it("multiple deposits add to the balance", async function () {
|
|
|
|
await token.connect(account).approve(vault.address, amount)
|
|
|
|
await vault.deposit(context, account.address, amount / 2)
|
|
|
|
await vault.deposit(context, account.address, amount / 2)
|
|
|
|
expect(await vault.balance(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)
|
|
|
|
expect(await vault.balance(context1, account.address)).to.equal(1)
|
|
|
|
expect(await vault.balance(context2, account.address)).to.equal(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
expect(await vault1.balance(context, account.address)).to.equal(1)
|
|
|
|
expect(await vault2.balance(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
|
|
|
|
|
|
|
it("can withdraw a deposit", 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)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("empties the balance when withdrawing", async function () {
|
|
|
|
await vault.withdraw(context, account.address)
|
|
|
|
expect(await vault.balance(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
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|