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

Staking limits #68

Merged
merged 50 commits into from
Dec 29, 2023
Merged

Staking limits #68

merged 50 commits into from
Dec 29, 2023

Conversation

r-czajkowski
Copy link
Contributor

@r-czajkowski r-czajkowski commented Dec 7, 2023

Closes: #59

This PR implements staking limits. Here we add staking parameters that define the maximum total assets(tBTC) that the Acre contract can hold and the minimum deposit amount for a single deposit operation. Each staking parameter can be updated by the owner.

Store staking-related parameters in struct such as minimum amount for a
single deposit operation and maximum total amount og tBTC token held by
Acre. In the future, we should add other staking-related parametrs here.
Only deposits where the amount is greater than or equal to the minimum
amount for a single deposit should be possible.
`apporval` -> `approval`
We want to control how many tBTC tokens the Acre contract can hold. Here
we override the `maxDeposit` function that takes into account the
staking param, maximum total assets, which determines the total amount
of tBTC token held by Acre.
In ERC4626 standard users can deposit using 2 functions `mint` and
`deposit` so we need to take into account staking limits in `mint`
function to not allow deposits that break staking limts rules.
@r-czajkowski r-czajkowski added the 🔗 Solidity Solidity contracts label Dec 7, 2023
@r-czajkowski r-czajkowski requested review from dimpar and nkuba December 7, 2023 17:08
@r-czajkowski r-czajkowski self-assigned this Dec 7, 2023
Add function that updates parameters of staking. Only owner can update
these parameters so here we also use the `Ownable` contract from `OZ`
library to use the `onlyOwner` modifier. Requirements:
- Minimum deposit amount must be greater than zero,
- Minimum deposit amount must be greater than or equal to the minimum
  deposit amount in the tBTC system,
- Maximum total assets must be greater than zero.
We added a new role `owner` to the test fixture and we need to make sure
we call functions with correct staker account.
Fix too many digits error by using `ether` suffix as recomended in
slither docs.
Since the `Acre` contract inherits from `Ownable` contract from OZ lib
we need to pass `initialOwner` to the Acre contract constructor.
core/contracts/Acre.sol Outdated Show resolved Hide resolved
core/contracts/Acre.sol Outdated Show resolved Hide resolved
core/contracts/Acre.sol Outdated Show resolved Hide resolved
core/contracts/Acre.sol Outdated Show resolved Hide resolved
There is no need to assign value from `super.deposit` to the new
variable. We can return directly.
core/contracts/Acre.sol Outdated Show resolved Hide resolved
core/contracts/Acre.sol Outdated Show resolved Hide resolved
core/contracts/Acre.sol Outdated Show resolved Hide resolved
core/contracts/Acre.sol Outdated Show resolved Hide resolved
core/contracts/Acre.sol Outdated Show resolved Hide resolved
core/contracts/Acre.sol Outdated Show resolved Hide resolved
We can pass `msg.sender` to the `Ownable` constructor and later in the
deployment scripts transfer the ownership at the end of the deployment
scenario.
Cover a case where `totalAssets()` value is greater than
`stakingParameters.maxiumumTotalAssets`. This can happen when vaults
generated yield or `maximumTotalAssets` has been updated to a lower
value.
Remove unnecessary requirement. `0.01` BTC is tBTC minting process
limitation which is expected to be invoked before `Acre.stake` function.
Leave a TODO to remember about introducing a governable parameters
update process.
The ERC4626 already uses custom errors, so it would be nice to have them
here for consistency.
@r-czajkowski r-czajkowski marked this pull request as ready for review December 12, 2023 11:36
core/contracts/Acre.sol Outdated Show resolved Hide resolved
core/contracts/Acre.sol Show resolved Hide resolved
core/contracts/Acre.sol Outdated Show resolved Hide resolved
core/contracts/Acre.sol Show resolved Hide resolved
core/test/Acre.test.ts Outdated Show resolved Hide resolved
core/test/Acre.test.ts Outdated Show resolved Hide resolved
core/test/Acre.test.ts Outdated Show resolved Hide resolved
Use `convertToShares` instead of `previewDeposit` in tests -
`convertToShares` does not take fees into account.
For the first staker, shares and stkaed amount will be the same number,
we don't need to call `previewDeposit`.
We should allow to set the maximum total assets value to `0` so the
`maxDeposit` function can return `0` when, for example, deposits are
disabled.
We need a way to remove the limit for deposits. In such cases, we will
set the value of `maximumTotalAssets` to the maximum
(`type(uint256).max`).
We need to handle a case where there is no maximum limit for deposits.
We overridden `deposit` function and now it takes into account the
minimum deposit amount parameter - here we add unit test for this
function to cover a cases where the deposit amount is less than minimum
and equal to the minimum amount. We indirectly cover this check's test
in `stake` tests but we want to add an explicit `deposit` function test
that covers it as well.
@r-czajkowski r-czajkowski requested a review from nkuba December 28, 2023 14:33
@r-czajkowski r-czajkowski force-pushed the staking-limits branch 3 times, most recently from 4dcefe9 to 43074ab Compare December 29, 2023 08:39
core/contracts/Acre.sol Outdated Show resolved Hide resolved
core/test/Acre.test.ts Show resolved Hide resolved
core/contracts/Acre.sol Outdated Show resolved Hide resolved
r-czajkowski and others added 2 commits December 29, 2023 12:14
`parametrs` -> `parameters`

Co-authored-by: Jakub Nowakowski <[email protected]>
Add a case where total tBTC amount after staking would be equal the max
amount, to confirm the transaction will succeed.
@r-czajkowski r-czajkowski requested a review from nkuba December 29, 2023 11:30
@nkuba nkuba enabled auto-merge December 29, 2023 11:33
@nkuba nkuba merged commit 8cd6745 into main Dec 29, 2023
11 checks passed
@nkuba nkuba deleted the staking-limits branch December 29, 2023 11:36
/// Acre.
/// @return The maximum amount of the tBTC token.
function maxDeposit(address) public view override returns (uint256) {
if (maximumTotalAssets == type(uint256).max) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It says that

// Maximum total amount of tBTC token held by Acre.
maximumTotalAssets

tBTC is pegged to BTC. As of now, max total supply of BTC is capped to 21M. No way BTC will reach max of uint256, unless something really ground breaking will happen to the whole crypto space. I don't think this check is necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The max uint256 value is used to disable the limit. If the maximumTotalAssets is set to the maxuint256, the function won't bother with further calculations and immediately returns the max uint256 value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, but this is a view function that doesn't cost anything even if we do the calculation. I think it's a bit confusing and I know that uint256 is a big number, but logically it's not 100% correct, because if there are already assets in Acre, then maxDeposit should return maxuint256 - totalAssets, not just maxuint256. I don't think it can bite us though. Up to you guys.

nkuba added a commit that referenced this pull request Dec 29, 2023
Ref: #68 (comment)

Add missing snapshot restorer to `deposit` tests according to the
existing pattern.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔗 Solidity Solidity contracts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement Staking Limits
3 participants