Skip to content

Commit

Permalink
Fixing withdrawal functionality in stBTC (#350)
Browse files Browse the repository at this point in the history
Upon withdrawal we need to take into account fees that will be collected
by the treasury. A user will get the desired assets, but on top of that
we need to pull a bit more funds from Mezo covering withdrawal fees.

Added tests to cover scenarios with allocated funds to Mezo Portal and
then withdrawing and redeeming from Acre.
  • Loading branch information
nkuba authored Apr 23, 2024
2 parents 5256712 + 900f487 commit 9902f4c
Show file tree
Hide file tree
Showing 3 changed files with 670 additions and 202 deletions.
4 changes: 2 additions & 2 deletions solidity/contracts/lib/ERC4626Fees.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ abstract contract ERC4626Fees is ERC4626Upgradeable {
function _feeOnRaw(
uint256 assets,
uint256 feeBasisPoints
) private pure returns (uint256) {
) internal pure returns (uint256) {
return
assets.mulDiv(
feeBasisPoints,
Expand All @@ -117,7 +117,7 @@ abstract contract ERC4626Fees is ERC4626Upgradeable {
function _feeOnTotal(
uint256 assets,
uint256 feeBasisPoints
) private pure returns (uint256) {
) internal pure returns (uint256) {
return
assets.mulDiv(
feeBasisPoints,
Expand Down
7 changes: 5 additions & 2 deletions solidity/contracts/stBTC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,11 @@ contract stBTC is ERC4626Fees, PausableOwnable {
address owner
) public override whenNotPaused returns (uint256) {
uint256 currentAssetsBalance = IERC20(asset()).balanceOf(address(this));
if (assets > currentAssetsBalance) {
dispatcher.withdraw(assets - currentAssetsBalance);
// If there is not enough assets in stBTC to cover user withdrawals and
// withdrawal fees then pull the assets from the dispatcher.
uint256 assetsWithFees = assets + _feeOnRaw(assets, exitFeeBasisPoints);
if (assetsWithFees > currentAssetsBalance) {
dispatcher.withdraw(assetsWithFees - currentAssetsBalance);
}

return super.withdraw(assets, receiver, owner);
Expand Down
Loading

0 comments on commit 9902f4c

Please sign in to comment.