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

feat(shares-math): add util to get shares from amount #221

Merged
Merged
Changes from 1 commit
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
32 changes: 32 additions & 0 deletions src/libraries/SharesMath.sol
Original file line number Diff line number Diff line change
@@ -34,4 +34,36 @@ library SharesMath {
function toAssetsUp(uint256 shares, uint256 totalAssets, uint256 totalShares) internal pure returns (uint256) {
return shares.mulDivUp(totalAssets + VIRTUAL_ASSETS, totalShares + VIRTUAL_SHARES);
}

/// @dev Calculates the amount of shares corresponding to a given amount of supply.
/// Note: only works as long as totalSupplyShares >= totalSupply.
MathisGD marked this conversation as resolved.
Show resolved Hide resolved
function toSupplyShares(uint256 amount, uint256 totalSupply, uint256 totalSupplyShares)
MathisGD marked this conversation as resolved.
Show resolved Hide resolved
internal
pure
returns (uint256)
{
uint256 shares = toSharesUp(amount, totalSupply, totalSupplyShares);

if (amount != toAssetsDown(shares, totalSupply, totalSupplyShares)) {
return toSharesDown(amount + 1, totalSupply, totalSupplyShares);
}

return shares;
}

/// @dev Calculates the amount of shares corresponding to a given amount of borrow.
/// Note: only works as long as totalBorrowShares >= totalBorrow.
function toBorrowShares(uint256 amount, uint256 totalBorrow, uint256 totalBorrowShares)
MathisGD marked this conversation as resolved.
Show resolved Hide resolved
internal
pure
returns (uint256)
{
uint256 shares = toSharesDown(amount, totalBorrow, totalBorrowShares);

if (amount != 0 && amount != toAssetsUp(shares, totalBorrow, totalBorrowShares)) {
return toSharesUp(amount - 1, totalBorrow, totalBorrowShares);
}

return shares;
}
}
41 changes: 41 additions & 0 deletions test/forge/SharesMath.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import "forge-std/Test.sol";
import "forge-std/console2.sol";

import {SharesMath} from "src/libraries/SharesMath.sol";

contract SharesMathTest is Test {
using SharesMath for uint256;

function testToSupplyShares(uint256 amount, uint256 supplyShares, uint256 totalSupply, uint256 totalSupplyShares)
public
{
totalSupplyShares = bound(totalSupplyShares, SharesMath.VIRTUAL_SHARES, type(uint128).max);
totalSupply = bound(totalSupply, 0, totalSupplyShares);

supplyShares = bound(supplyShares, 0, totalSupplyShares);

amount = bound(amount, 0, supplyShares.toAssetsDown(totalSupply, totalSupplyShares));

assertEq(
amount, amount.toSupplyShares(totalSupply, totalSupplyShares).toAssetsDown(totalSupply, totalSupplyShares)
);
}

function testToBorrowShares(uint256 amount, uint256 borrowShares, uint256 totalBorrow, uint256 totalBorrowShares)
public
{
totalBorrowShares = bound(totalBorrowShares, SharesMath.VIRTUAL_SHARES, type(uint128).max);
totalBorrow = bound(totalBorrow, 0, totalBorrowShares);

borrowShares = bound(borrowShares, 0, totalBorrowShares);

amount = bound(amount, 0, borrowShares.toAssetsDown(totalBorrow, totalBorrowShares));

assertEq(
amount, amount.toBorrowShares(totalBorrow, totalBorrowShares).toAssetsUp(totalBorrow, totalBorrowShares)
);
}
}