Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Total assets tests #374

Merged
merged 4 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions solidity/test/MezoAllocator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ describe("MezoAllocator", () => {
const totalAssets = await mezoAllocator.totalAssets()
expect(totalAssets).to.equal(to1e18(5))
})

it("should be equal to the deposit balance", async () => {
const depositBalance = await mezoAllocator.depositBalance()
expect(await mezoAllocator.totalAssets()).to.equal(depositBalance)
})
})
})

Expand Down
146 changes: 146 additions & 0 deletions solidity/test/stBTC.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1938,6 +1938,152 @@ describe("stBTC", () => {
})
})

describe("totalAssets", () => {
beforeAfterSnapshotWrapper()

const donation = to1e18(1)
const firstDeposit = to1e18(2)
const secondDeposit = to1e18(3)

context("when there are no deposits", () => {
it("should return 0", async () => {
expect(await stbtc.totalAssets()).to.be.eq(0)
})
})

context("when there are deposits", () => {
context("when there is a first deposit made", () => {
beforeAfterSnapshotWrapper()

before(async () => {
await tbtc.mint(depositor1.address, firstDeposit)
await tbtc
.connect(depositor1)
.approve(await stbtc.getAddress(), firstDeposit)
await stbtc
.connect(depositor1)
.deposit(firstDeposit, depositor1.address)
})

it("should return the total assets", async () => {
nkuba marked this conversation as resolved.
Show resolved Hide resolved
const expectedAssets =
firstDeposit - feeOnTotal(firstDeposit, entryFeeBasisPoints)
expect(await stbtc.totalAssets()).to.be.eq(expectedAssets)
})

it("should be equal to tBTC balance of the contract", async () => {
expect(await stbtc.totalAssets()).to.be.eq(
await tbtc.balanceOf(await stbtc.getAddress()),
)
})
})

context("when there are two deposits made", () => {
beforeAfterSnapshotWrapper()

before(async () => {
await tbtc.mint(depositor1.address, firstDeposit + secondDeposit)
await tbtc
.connect(depositor1)
.approve(await stbtc.getAddress(), firstDeposit + secondDeposit)
await stbtc
.connect(depositor1)
.deposit(firstDeposit, depositor1.address)
await stbtc
.connect(depositor1)
.deposit(secondDeposit, depositor1.address)
})

it("should return the total assets", async () => {
const expectedAssetsFirstDeposit =
firstDeposit - feeOnTotal(firstDeposit, entryFeeBasisPoints)
const expectedAssetsSecondDeposit =
secondDeposit - feeOnTotal(secondDeposit, entryFeeBasisPoints)
expect(await stbtc.totalAssets()).to.be.eq(
expectedAssetsFirstDeposit + expectedAssetsSecondDeposit,
)
})
})

context("when the funds were allocated after deposits", () => {
beforeAfterSnapshotWrapper()

before(async () => {
await tbtc.mint(depositor1.address, firstDeposit + secondDeposit)
await tbtc
.connect(depositor1)
.approve(await stbtc.getAddress(), firstDeposit + secondDeposit)
await stbtc
.connect(depositor1)
.deposit(firstDeposit, depositor1.address)
await stbtc
.connect(depositor1)
.deposit(secondDeposit, depositor1.address)
await mezoAllocator.connect(maintainer).allocate()
})

it("should return the total assets", async () => {
const deposits = firstDeposit + secondDeposit
const expectedAssets =
deposits - feeOnTotal(deposits, entryFeeBasisPoints)
expect(await stbtc.totalAssets()).to.be.eq(expectedAssets)
})
})

context("when there is a donation made", () => {
beforeAfterSnapshotWrapper()

let totalAssetsBeforeDonation: bigint

before(async () => {
await tbtc.mint(depositor1.address, firstDeposit)
await tbtc
.connect(depositor1)
.approve(await stbtc.getAddress(), firstDeposit)
await stbtc
.connect(depositor1)
.deposit(firstDeposit, depositor1.address)
totalAssetsBeforeDonation = await stbtc.totalAssets()
await tbtc.mint(await stbtc.getAddress(), donation)
})

it("should return the total assets", async () => {
expect(await stbtc.totalAssets()).to.be.eq(
totalAssetsBeforeDonation + donation,
)
})
})

context("when there was a withdrawal", () => {
beforeAfterSnapshotWrapper()

let totalAssetsBeforeWithdrawal: bigint

before(async () => {
await tbtc.mint(depositor1.address, firstDeposit)
await tbtc
.connect(depositor1)
.approve(await stbtc.getAddress(), firstDeposit)
await stbtc
.connect(depositor1)
.deposit(firstDeposit, depositor1.address)
totalAssetsBeforeWithdrawal = await stbtc.totalAssets()
await stbtc
.connect(depositor1)
.withdraw(to1e18(1), depositor1, depositor1)
})

it("should return the total assets", async () => {
const actualWithdrawnAssets =
to1e18(1) + feeOnRaw(to1e18(1), exitFeeBasisPoints)
expect(await stbtc.totalAssets()).to.be.eq(
totalAssetsBeforeWithdrawal - actualWithdrawnAssets,
)
})
})
})
})

describe("feeOnTotal - internal test helper", () => {
context("when the fee's modulo remainder is greater than 0", () => {
it("should add 1 to the result", () => {
Expand Down
Loading