Skip to content

Commit

Permalink
Return min and max stake values in tBTC precision
Browse files Browse the repository at this point in the history
To avoid confusion of mixing precisions in the contract, we decidwed to
use tBTC precision consistently across the contract and its' API.
It will be dApp/SDK responsibility to convert to satoshi precision in
staking flow.
  • Loading branch information
nkuba committed Feb 27, 2024
1 parent 9fee52e commit 0787a6a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 67 deletions.
14 changes: 6 additions & 8 deletions core/contracts/AcreBitcoinDepositor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -466,16 +466,16 @@ contract AcreBitcoinDepositor is AbstractTBTCDepositor, Ownable2Step {
tbtcToken.safeTransfer(staker, amount);
}

/// @notice Minimum stake amount in satoshi precision.
/// @notice Minimum stake amount (in tBTC token precision).
/// @dev This function should be used by dApp to check the minimum amount
/// for the stake request.
/// @dev It is not enforced in the `initializeStakeRequest` function, as
/// it is intended to be used in the dApp staking form.
function minStakeInSatoshi() external view returns (uint256) {
return minStakeAmount / SATOSHI_MULTIPLIER;
function minStake() external view returns (uint256) {
return minStakeAmount;
}

/// @notice Maximum stake amount in satoshi precision.
/// @notice Maximum stake amount (in tBTC token precision).
/// @dev It takes into consideration the maximum total assets soft limit (soft
/// cap), that is expected to be set below the stBTC maximum total assets
/// limit (hard cap).
Expand All @@ -486,7 +486,7 @@ contract AcreBitcoinDepositor is AbstractTBTCDepositor, Ownable2Step {
/// submission of Bitcoin funding transaction and stake request
/// initialization.
/// @return Maximum allowed stake amount.
function maxStakeInSatoshi() external view returns (uint256) {
function maxStake() external view returns (uint256) {
uint256 currentTotalAssets = stbtc.totalAssets();

if (currentTotalAssets >= maxTotalAssetsSoftLimit) {
Expand All @@ -500,9 +500,7 @@ contract AcreBitcoinDepositor is AbstractTBTCDepositor, Ownable2Step {
}
availableLimit -= queuedStakesBalance;

uint256 result = Math.min(availableLimit, maxSingleStakeAmount);

return result / SATOSHI_MULTIPLIER;
return Math.min(availableLimit, maxSingleStakeAmount);
}

/// @notice Updates the minimum stake amount.
Expand Down
67 changes: 8 additions & 59 deletions core/test/AcreBitcoinDepositor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ const { lastBlockTime } = helpers.time
const { getNamedSigners, getUnnamedSigners } = helpers.signers

describe("AcreBitcoinDepositor", () => {
const satoshiMultiplier = 10n ** 10n

const defaultDepositDustThreshold = 1000000 // 1000000 satoshi = 0.01 BTC
const defaultDepositTreasuryFeeDivisor = 2000 // 1/2000 = 0.05% = 0.0005
const defaultDepositTxMaxFee = 1000 // 1000 satoshi = 0.00001 BTC
Expand Down Expand Up @@ -1409,7 +1407,7 @@ describe("AcreBitcoinDepositor", () => {
})
})

describe("maxStakeInSatoshi", () => {
describe("maxStake", () => {
beforeAfterSnapshotWrapper()

const maxTotalAssetsSoftLimit = to1ePrecision(100000000, 10) // 100000000 satoshi = 1 BTC
Expand All @@ -1431,7 +1429,7 @@ describe("AcreBitcoinDepositor", () => {
})

it("should return 0", async () => {
expect(await bitcoinDepositor.maxStakeInSatoshi()).to.be.equal(0)
expect(await bitcoinDepositor.maxStake()).to.be.equal(0)
})
})

Expand All @@ -1458,7 +1456,7 @@ describe("AcreBitcoinDepositor", () => {
})

it("should return 0", async () => {
expect(await bitcoinDepositor.maxStakeInSatoshi()).to.be.equal(0)
expect(await bitcoinDepositor.maxStake()).to.be.equal(0)
})
})

Expand All @@ -1484,15 +1482,15 @@ describe("AcreBitcoinDepositor", () => {
})

it("should return zero", async () => {
expect(await bitcoinDepositor.maxStakeInSatoshi()).to.be.equal(0)
expect(await bitcoinDepositor.maxStake()).to.be.equal(0)
})
})

describe("max single stake amount is less than the available limit", () => {
beforeAfterSnapshotWrapper()

const maxSingleStakeAmount = to1ePrecision(20000000, 10) // 20000000 satoshi = 0.2 BTC
const expectedResult = toSatoshi(maxSingleStakeAmount)
const expectedResult = maxSingleStakeAmount

before(async () => {
await bitcoinDepositor
Expand All @@ -1501,7 +1499,7 @@ describe("AcreBitcoinDepositor", () => {
})

it("should return max single stake amount", async () => {
expect(await bitcoinDepositor.maxStakeInSatoshi()).to.be.equal(
expect(await bitcoinDepositor.maxStake()).to.be.equal(
expectedResult,
)
})
Expand All @@ -1511,7 +1509,7 @@ describe("AcreBitcoinDepositor", () => {
beforeAfterSnapshotWrapper()

const maxSingleStakeAmount = to1ePrecision(35000000, 10) // 35000000 satoshi = 0.35 BTC
const expectedResult = toSatoshi(availableSoftLimitMinusQueue)
const expectedResult = availableSoftLimitMinusQueue

before(async () => {
await bitcoinDepositor
Expand All @@ -1520,57 +1518,12 @@ describe("AcreBitcoinDepositor", () => {
})

it("should return available limit", async () => {
expect(await bitcoinDepositor.maxStakeInSatoshi()).to.be.equal(
expect(await bitcoinDepositor.maxStake()).to.be.equal(
expectedResult,
)
})
})
})

describe("when available limit is equal to the satoshi multiplier", () => {
beforeAfterSnapshotWrapper()

before(async () => {
await tbtc.mint(
await stbtc.getAddress(),
availableSoftLimit - satoshiMultiplier,
)
})

it("should return 1", async () => {
expect(await bitcoinDepositor.maxStakeInSatoshi()).to.be.equal(1)
})
})

describe("when available limit is less than the satoshi multiplier", () => {
beforeAfterSnapshotWrapper()

before(async () => {
await tbtc.mint(
await stbtc.getAddress(),
availableSoftLimit - satoshiMultiplier + 1n,
)
})

it("should return 0", async () => {
expect(await bitcoinDepositor.maxStakeInSatoshi()).to.be.equal(0)
})
})

describe("when available limit is greater than the satoshi multiplier", () => {
beforeAfterSnapshotWrapper()

before(async () => {
await tbtc.mint(
await stbtc.getAddress(),
availableSoftLimit - satoshiMultiplier - 1n,
)
})

it("should return 1", async () => {
expect(await bitcoinDepositor.maxStakeInSatoshi()).to.be.equal(1)
})
})
})
})

Expand Down Expand Up @@ -1968,8 +1921,4 @@ describe("AcreBitcoinDepositor", () => {
await tbtcVault.finalizeOptimisticMintingRequest(depositKey)
}
}

function toSatoshi(tbtcAmount: bigint) {
return tbtcAmount / satoshiMultiplier
}
})

0 comments on commit 0787a6a

Please sign in to comment.