From c1cb4c97fab935951b0c92595f23874908f23b3f Mon Sep 17 00:00:00 2001 From: Jakub Nowakowski Date: Tue, 9 Apr 2024 17:34:36 +0200 Subject: [PATCH] Update BitcoinDepositor tests after stake rename --- core/test/BitcoinDepositor.test.ts | 210 +++++++++++---------- core/test/BitcoinDepositor.upgrade.test.ts | 14 +- core/types/index.ts | 2 +- 3 files changed, 114 insertions(+), 112 deletions(-) diff --git a/core/test/BitcoinDepositor.test.ts b/core/test/BitcoinDepositor.test.ts index 2c0fd02f7..93956e837 100644 --- a/core/test/BitcoinDepositor.test.ts +++ b/core/test/BitcoinDepositor.test.ts @@ -6,7 +6,7 @@ import { expect } from "chai" import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers" import { ContractTransactionResponse, MaxUint256, ZeroAddress } from "ethers" -import { StakeRequestState } from "../types" +import { DepositState } from "../types" import type { StBTC, @@ -87,24 +87,27 @@ describe("BitcoinDepositor", () => { .updateDepositorFeeDivisor(defaultDepositorFeeDivisor) }) - describe("initializeStake", () => { + describe("initializeDeposit", () => { beforeAfterSnapshotWrapper() - describe("when staker is zero address", () => { + describe("when depositOwner is zero address", () => { it("should revert", async () => { await expect( - bitcoinDepositor.initializeStake( + bitcoinDepositor.initializeDeposit( tbtcDepositData.fundingTxInfo, tbtcDepositData.reveal, ZeroAddress, 0, ), - ).to.be.revertedWithCustomError(bitcoinDepositor, "StakerIsZeroAddress") + ).to.be.revertedWithCustomError( + bitcoinDepositor, + "DepositOwnerIsZeroAddress", + ) }) }) - describe("when staker is non zero address", () => { - describe("when stake is not in progress", () => { + describe("when depositOwner is non zero address", () => { + describe("when deposit is not in progress", () => { describe("when tbtc vault address is incorrect", () => { beforeAfterSnapshotWrapper() @@ -115,10 +118,10 @@ describe("BitcoinDepositor", () => { await expect( bitcoinDepositor .connect(thirdParty) - .initializeStake( + .initializeDeposit( tbtcDepositData.fundingTxInfo, { ...tbtcDepositData.reveal, vault: invalidTbtcVault }, - tbtcDepositData.staker, + tbtcDepositData.depositOwner, tbtcDepositData.referral, ), ).to.be.revertedWith("Vault address mismatch") @@ -134,31 +137,31 @@ describe("BitcoinDepositor", () => { before(async () => { tx = await bitcoinDepositor .connect(thirdParty) - .initializeStake( + .initializeDeposit( tbtcDepositData.fundingTxInfo, tbtcDepositData.reveal, - tbtcDepositData.staker, + tbtcDepositData.depositOwner, tbtcDepositData.referral, ) }) - it("should emit StakeRequestInitialized event", async () => { + it("should emit DepositInitialized event", async () => { await expect(tx) - .to.emit(bitcoinDepositor, "StakeRequestInitialized") + .to.emit(bitcoinDepositor, "DepositInitialized") .withArgs( tbtcDepositData.depositKey, thirdParty.address, - tbtcDepositData.staker, + tbtcDepositData.depositOwner, initialDepositAmount, ) }) - it("should update stake state", async () => { - const stakeRequest = await bitcoinDepositor.stakeRequests( + it("should update deposit state", async () => { + const deposit = await bitcoinDepositor.deposits( tbtcDepositData.depositKey, ) - expect(stakeRequest).to.be.equal(StakeRequestState.Initialized) + expect(deposit).to.be.equal(DepositState.Initialized) }) it("should reveal the deposit to the bridge contract with extra data", async () => { @@ -185,10 +188,10 @@ describe("BitcoinDepositor", () => { await expect( bitcoinDepositor .connect(thirdParty) - .initializeStake( + .initializeDeposit( tbtcDepositData.fundingTxInfo, tbtcDepositData.reveal, - tbtcDepositData.staker, + tbtcDepositData.depositOwner, 0, ), ).to.be.not.reverted @@ -197,49 +200,49 @@ describe("BitcoinDepositor", () => { }) }) - describe("when stake is already in progress", () => { + describe("when deposit is already in progress", () => { beforeAfterSnapshotWrapper() before(async () => { - await initializeStake() + await initializeDeposit() }) it("should revert", async () => { await expect( bitcoinDepositor .connect(thirdParty) - .initializeStake( + .initializeDeposit( tbtcDepositData.fundingTxInfo, tbtcDepositData.reveal, - tbtcDepositData.staker, + tbtcDepositData.depositOwner, tbtcDepositData.referral, ), ).to.be.revertedWith("Deposit already revealed") }) }) - describe("when stake is already finalized", () => { + describe("when deposit is already finalized", () => { beforeAfterSnapshotWrapper() before(async () => { - await initializeStake() + await initializeDeposit() // Simulate deposit request finalization. await finalizeMinting(tbtcDepositData.depositKey) await bitcoinDepositor .connect(thirdParty) - .finalizeStake(tbtcDepositData.depositKey) + .finalizeDeposit(tbtcDepositData.depositKey) }) it("should revert", async () => { await expect( bitcoinDepositor .connect(thirdParty) - .initializeStake( + .initializeDeposit( tbtcDepositData.fundingTxInfo, tbtcDepositData.reveal, - tbtcDepositData.staker, + tbtcDepositData.depositOwner, tbtcDepositData.referral, ), ).to.be.revertedWith("Deposit already revealed") @@ -248,29 +251,29 @@ describe("BitcoinDepositor", () => { }) }) - describe("finalizeStake", () => { + describe("finalizeDeposit", () => { beforeAfterSnapshotWrapper() - describe("when stake has not been initialized", () => { + describe("when deposit has not been initialized", () => { it("should revert", async () => { await expect( bitcoinDepositor .connect(thirdParty) - .finalizeStake(tbtcDepositData.depositKey), + .finalizeDeposit(tbtcDepositData.depositKey), ) .to.be.revertedWithCustomError( bitcoinDepositor, - "UnexpectedStakeRequestState", + "UnexpectedDepositState", ) - .withArgs(StakeRequestState.Unknown, StakeRequestState.Initialized) + .withArgs(DepositState.Unknown, DepositState.Initialized) }) }) - describe("when stake has been initialized", () => { + describe("when deposit has been initialized", () => { beforeAfterSnapshotWrapper() before(async () => { - await initializeStake() + await initializeDeposit() }) describe("when deposit was not bridged", () => { @@ -278,13 +281,13 @@ describe("BitcoinDepositor", () => { await expect( bitcoinDepositor .connect(thirdParty) - .finalizeStake(tbtcDepositData.depositKey), + .finalizeDeposit(tbtcDepositData.depositKey), ).to.be.revertedWith("Deposit not finalized by the bridge") }) }) describe("when deposit was bridged", () => { - describe("when stake has not been finalized", () => { + describe("when deposit has not been finalized", () => { describe("when depositor contract balance is lower than bridged amount", () => { beforeAfterSnapshotWrapper() @@ -300,7 +303,7 @@ describe("BitcoinDepositor", () => { await expect( bitcoinDepositor .connect(thirdParty) - .finalizeStake(tbtcDepositData.depositKey), + .finalizeDeposit(tbtcDepositData.depositKey), ) .to.be.revertedWithCustomError( stbtc, @@ -333,7 +336,7 @@ describe("BitcoinDepositor", () => { before(async () => { tx = await bitcoinDepositor .connect(thirdParty) - .finalizeStake(tbtcDepositData.depositKey) + .finalizeDeposit(tbtcDepositData.depositKey) }) it("should transfer depositor fee", async () => { @@ -344,17 +347,17 @@ describe("BitcoinDepositor", () => { ) }) - it("should update stake state", async () => { - const stakeRequest = await bitcoinDepositor.stakeRequests( + it("should update deposit state", async () => { + const depositState = await bitcoinDepositor.deposits( tbtcDepositData.depositKey, ) - expect(stakeRequest).to.be.equal(StakeRequestState.Finalized) + expect(depositState).to.be.equal(DepositState.Finalized) }) - it("should emit StakeRequestFinalized event", async () => { + it("should emit DepositFinalized event", async () => { await expect(tx) - .to.emit(bitcoinDepositor, "StakeRequestFinalized") + .to.emit(bitcoinDepositor, "DepositFinalized") .withArgs( tbtcDepositData.depositKey, thirdParty.address, @@ -370,25 +373,25 @@ describe("BitcoinDepositor", () => { .to.emit(stbtc, "Deposit") .withArgs( await bitcoinDepositor.getAddress(), - tbtcDepositData.staker, + tbtcDepositData.depositOwner, expectedAssetsAmount, expectedReceivedSharesAmount, ) }) - it("should stake in Acre contract", async () => { + it("should deposit in Acre contract", async () => { await expect( tx, "invalid minted stBTC amount", ).to.changeTokenBalances( stbtc, - [tbtcDepositData.staker], + [tbtcDepositData.depositOwner], [expectedReceivedSharesAmount], ) await expect( tx, - "invalid staked tBTC amount", + "invalid deposited tBTC amount", ).to.changeTokenBalances(tbtc, [stbtc], [expectedAssetsAmount]) }) }) @@ -408,24 +411,24 @@ describe("BitcoinDepositor", () => { tx = await bitcoinDepositor .connect(thirdParty) - .finalizeStake(tbtcDepositData.depositKey) + .finalizeDeposit(tbtcDepositData.depositKey) }) it("should not transfer depositor fee", async () => { await expect(tx).to.changeTokenBalances(tbtc, [treasury], [0]) }) - it("should update stake state", async () => { - const stakeRequest = await bitcoinDepositor.stakeRequests( + it("should update deposit state", async () => { + const deposit = await bitcoinDepositor.deposits( tbtcDepositData.depositKey, ) - expect(stakeRequest).to.be.equal(StakeRequestState.Finalized) + expect(deposit).to.be.equal(DepositState.Finalized) }) - it("should emit StakeRequestFinalized event", async () => { + it("should emit DepositFinalized event", async () => { await expect(tx) - .to.emit(bitcoinDepositor, "StakeRequestFinalized") + .to.emit(bitcoinDepositor, "DepositFinalized") .withArgs( tbtcDepositData.depositKey, thirdParty.address, @@ -441,25 +444,25 @@ describe("BitcoinDepositor", () => { .to.emit(stbtc, "Deposit") .withArgs( await bitcoinDepositor.getAddress(), - tbtcDepositData.staker, + tbtcDepositData.depositOwner, expectedAssetsAmount, expectedReceivedSharesAmount, ) }) - it("should stake in Acre contract", async () => { + it("should deposit in Acre contract", async () => { await expect( tx, "invalid minted stBTC amount", ).to.changeTokenBalances( stbtc, - [tbtcDepositData.staker], + [tbtcDepositData.depositOwner], [expectedReceivedSharesAmount], ) await expect( tx, - "invalid staked tBTC amount", + "invalid deposited tBTC amount", ).to.changeTokenBalances(tbtc, [stbtc], [expectedAssetsAmount]) }) }) @@ -477,7 +480,7 @@ describe("BitcoinDepositor", () => { await expect( bitcoinDepositor .connect(thirdParty) - .finalizeStake(tbtcDepositData.depositKey), + .finalizeDeposit(tbtcDepositData.depositKey), ) .to.be.revertedWithCustomError( bitcoinDepositor, @@ -489,40 +492,37 @@ describe("BitcoinDepositor", () => { }) }) - describe("when stake has been finalized", () => { + describe("when deposit has been finalized", () => { beforeAfterSnapshotWrapper() before(async () => { // Simulate deposit request finalization. await finalizeMinting(tbtcDepositData.depositKey) - // Finalize stake. + // Finalize deposit. await bitcoinDepositor .connect(thirdParty) - .finalizeStake(tbtcDepositData.depositKey) + .finalizeDeposit(tbtcDepositData.depositKey) }) it("should revert", async () => { await expect( bitcoinDepositor .connect(thirdParty) - .finalizeStake(tbtcDepositData.depositKey), + .finalizeDeposit(tbtcDepositData.depositKey), ) .to.be.revertedWithCustomError( bitcoinDepositor, - "UnexpectedStakeRequestState", - ) - .withArgs( - StakeRequestState.Finalized, - StakeRequestState.Initialized, + "UnexpectedDepositState", ) + .withArgs(DepositState.Finalized, DepositState.Initialized) }) }) }) }) }) - describe("updateMinStakeAmount", () => { + describe("updateMinDepositAmount", () => { beforeAfterSnapshotWrapper() describe("when caller is not governance", () => { @@ -530,7 +530,7 @@ describe("BitcoinDepositor", () => { it("should revert", async () => { await expect( - bitcoinDepositor.connect(thirdParty).updateMinStakeAmount(1234), + bitcoinDepositor.connect(thirdParty).updateMinDepositAmount(1234), ) .to.be.revertedWithCustomError( bitcoinDepositor, @@ -541,7 +541,7 @@ describe("BitcoinDepositor", () => { }) describe("when caller is governance", () => { - const testUpdateMinStakeAmount = (newValue: bigint) => + const testupdateMinDepositAmount = (newValue: bigint) => function () { beforeAfterSnapshotWrapper() @@ -550,17 +550,17 @@ describe("BitcoinDepositor", () => { before(async () => { tx = await bitcoinDepositor .connect(governance) - .updateMinStakeAmount(newValue) + .updateMinDepositAmount(newValue) }) - it("should emit MinStakeAmountUpdated event", async () => { + it("should emit MinDepositAmountUpdated event", async () => { await expect(tx) - .to.emit(bitcoinDepositor, "MinStakeAmountUpdated") + .to.emit(bitcoinDepositor, "MinDepositAmountUpdated") .withArgs(newValue) }) it("should update value correctly", async () => { - expect(await bitcoinDepositor.minStakeAmount()).to.be.eq(newValue) + expect(await bitcoinDepositor.minDepositAmount()).to.be.eq(newValue) }) } @@ -568,44 +568,44 @@ describe("BitcoinDepositor", () => { // Deposit dust threshold: 1000000 satoshi = 0.01 BTC // tBTC Bridge stores the dust threshold in satoshi precision, - // we need to convert it to the tBTC token precision as `updateMinStakeAmount` + // we need to convert it to the tBTC token precision as `updateMinDepositAmount` // function expects this precision. const bridgeDepositDustThreshold = to1ePrecision( defaultDepositDustThreshold, 10, ) - describe("when new stake amount is less than bridge deposit dust threshold", () => { + describe("when new deposit amount is less than bridge deposit dust threshold", () => { beforeAfterSnapshotWrapper() - const newMinStakeAmount = bridgeDepositDustThreshold - 1n + const newMinDepositAmount = bridgeDepositDustThreshold - 1n it("should revert", async () => { await expect( bitcoinDepositor .connect(governance) - .updateMinStakeAmount(newMinStakeAmount), + .updateMinDepositAmount(newMinDepositAmount), ) .to.be.revertedWithCustomError( bitcoinDepositor, - "MinStakeAmountLowerThanBridgeMinDeposit", + "MinDepositAmountLowerThanBridgeMinDeposit", ) - .withArgs(newMinStakeAmount, bridgeDepositDustThreshold) + .withArgs(newMinDepositAmount, bridgeDepositDustThreshold) }) }) describe( - "when new stake amount is equal to bridge deposit dust threshold", - testUpdateMinStakeAmount(bridgeDepositDustThreshold), + "when new deposit amount is equal to bridge deposit dust threshold", + testupdateMinDepositAmount(bridgeDepositDustThreshold), ) describe( - "when new stake amount is greater than bridge deposit dust threshold", - testUpdateMinStakeAmount(bridgeDepositDustThreshold + 1n), + "when new deposit amount is greater than bridge deposit dust threshold", + testupdateMinDepositAmount(bridgeDepositDustThreshold + 1n), ) describe( - "when new stake amount is equal max uint256", - testUpdateMinStakeAmount(MaxUint256), + "when new deposit amount is equal max uint256", + testupdateMinDepositAmount(MaxUint256), ) }) }) @@ -669,24 +669,24 @@ describe("BitcoinDepositor", () => { const extraDataValidTestData = new Map< string, { - staker: string + depositOwner: string referral: number extraData: string } >([ [ - "staker has leading zeros", + "depositOwner has leading zeros", { - staker: "0x000055d85E80A49B5930C4a77975d44f012D86C1", + depositOwner: "0x000055d85E80A49B5930C4a77975d44f012D86C1", referral: 6851, // hex: 0x1ac3 extraData: "0x000055d85e80a49b5930c4a77975d44f012d86c11ac300000000000000000000", }, ], [ - "staker has trailing zeros", + "depositOwner has trailing zeros", { - staker: "0x2d2F8BC7923F7F806Dc9bb2e17F950b42CfE0000", + depositOwner: "0x2d2F8BC7923F7F806Dc9bb2e17F950b42CfE0000", referral: 6851, // hex: 0x1ac3 extraData: "0x2d2f8bc7923f7f806dc9bb2e17f950b42cfe00001ac300000000000000000000", @@ -695,7 +695,7 @@ describe("BitcoinDepositor", () => { [ "referral is zero", { - staker: "0xeb098d6cDE6A202981316b24B19e64D82721e89E", + depositOwner: "0xeb098d6cDE6A202981316b24B19e64D82721e89E", referral: 0, extraData: "0xeb098d6cde6a202981316b24b19e64d82721e89e000000000000000000000000", @@ -704,7 +704,7 @@ describe("BitcoinDepositor", () => { [ "referral has leading zeros", { - staker: "0xeb098d6cDE6A202981316b24B19e64D82721e89E", + depositOwner: "0xeb098d6cDE6A202981316b24B19e64D82721e89E", referral: 31, // hex: 0x001f extraData: "0xeb098d6cde6a202981316b24b19e64d82721e89e001f00000000000000000000", @@ -713,7 +713,7 @@ describe("BitcoinDepositor", () => { [ "referral has trailing zeros", { - staker: "0xeb098d6cDE6A202981316b24B19e64D82721e89E", + depositOwner: "0xeb098d6cDE6A202981316b24B19e64D82721e89E", referral: 19712, // hex: 0x4d00 extraData: "0xeb098d6cde6a202981316b24b19e64d82721e89e4d0000000000000000000000", @@ -722,7 +722,7 @@ describe("BitcoinDepositor", () => { [ "referral is maximum value", { - staker: "0xeb098d6cDE6A202981316b24B19e64D82721e89E", + depositOwner: "0xeb098d6cDE6A202981316b24B19e64D82721e89E", referral: 65535, // max uint16 extraData: "0xeb098d6cde6a202981316b24b19e64d82721e89effff00000000000000000000", @@ -733,10 +733,10 @@ describe("BitcoinDepositor", () => { describe("encodeExtraData", () => { extraDataValidTestData.forEach( // eslint-disable-next-line @typescript-eslint/no-shadow - ({ staker, referral, extraData: expectedExtraData }, testName) => { + ({ depositOwner, referral, extraData: expectedExtraData }, testName) => { it(testName, async () => { expect( - await bitcoinDepositor.encodeExtraData(staker, referral), + await bitcoinDepositor.encodeExtraData(depositOwner, referral), ).to.be.equal(expectedExtraData) }) }, @@ -746,14 +746,16 @@ describe("BitcoinDepositor", () => { describe("decodeExtraData", () => { extraDataValidTestData.forEach( ( - { staker: expectedStaker, referral: expectedReferral, extraData }, + { depositOwner: expectedStaker, referral: expectedReferral, extraData }, testName, ) => { it(testName, async () => { const [actualStaker, actualReferral] = await bitcoinDepositor.decodeExtraData(extraData) - expect(actualStaker, "invalid staker").to.be.equal(expectedStaker) + expect(actualStaker, "invalid depositOwner").to.be.equal( + expectedStaker, + ) expect(actualReferral, "invalid referral").to.be.equal( expectedReferral, ) @@ -773,18 +775,18 @@ describe("BitcoinDepositor", () => { const [actualStaker, actualReferral] = await bitcoinDepositor.decodeExtraData(extraData) - expect(actualStaker, "invalid staker").to.be.equal(expectedStaker) + expect(actualStaker, "invalid depositOwner").to.be.equal(expectedStaker) expect(actualReferral, "invalid referral").to.be.equal(expectedReferral) }) }) - async function initializeStake() { + async function initializeDeposit() { await bitcoinDepositor .connect(thirdParty) - .initializeStake( + .initializeDeposit( tbtcDepositData.fundingTxInfo, tbtcDepositData.reveal, - tbtcDepositData.staker, + tbtcDepositData.depositOwner, tbtcDepositData.referral, ) } diff --git a/core/test/BitcoinDepositor.upgrade.test.ts b/core/test/BitcoinDepositor.upgrade.test.ts index 46ea0155c..ef58f3cf1 100644 --- a/core/test/BitcoinDepositor.upgrade.test.ts +++ b/core/test/BitcoinDepositor.upgrade.test.ts @@ -42,19 +42,19 @@ describe("BitcoinDepositor contract upgrade", () => { const newVariable = 1n let bitcoinDepositorV2: BitcoinDepositorV2 let v1InitialParameters: { - minStakeAmount: bigint + minDepositAmount: bigint depositorFeeDivisor: bigint } beforeAfterSnapshotWrapper() before(async () => { - const minStakeAmount = await bitcoinDepositor.minStakeAmount() + const minDepositAmount = await bitcoinDepositor.minDepositAmount() const depositorFeeDivisor = await bitcoinDepositor.depositorFeeDivisor() v1InitialParameters = { - minStakeAmount, + minDepositAmount, depositorFeeDivisor, } @@ -98,8 +98,8 @@ describe("BitcoinDepositor contract upgrade", () => { ) expect(await bitcoinDepositorV2.stbtc()).to.eq(await stbtc.getAddress()) - expect(await bitcoinDepositorV2.minStakeAmount()).to.eq( - v1InitialParameters.minStakeAmount, + expect(await bitcoinDepositorV2.minDepositAmount()).to.eq( + v1InitialParameters.minDepositAmount, ) expect(await bitcoinDepositorV2.depositorFeeDivisor()).to.eq( v1InitialParameters.depositorFeeDivisor, @@ -107,14 +107,14 @@ describe("BitcoinDepositor contract upgrade", () => { }) }) - describe("upgraded `updateMinStakeAmount` function", () => { + describe("upgraded `updateMinDepositAmount` function", () => { const newMinStakeAmount: bigint = to1e18(1000) let tx: ContractTransactionResponse before(async () => { tx = await bitcoinDepositorV2 .connect(governance) - .updateMinStakeAmount(newMinStakeAmount) + .updateMinDepositAmount(newMinStakeAmount) }) it("should emit `NewEvent` event", async () => { diff --git a/core/types/index.ts b/core/types/index.ts index e84cb597b..19e2cf0f5 100644 --- a/core/types/index.ts +++ b/core/types/index.ts @@ -1,5 +1,5 @@ /* eslint-disable import/prefer-default-export */ -export enum StakeRequestState { +export enum DepositState { Unknown, Initialized, Finalized,