-
Notifications
You must be signed in to change notification settings - Fork 4
/
SharesMath.sol
35 lines (31 loc) · 920 Bytes
/
SharesMath.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity =0.8.18;
import '@openzeppelin/contracts/utils/math/Math.sol';
/**
* @title SharesMath
* @author Swapscanner
* @dev This library provides functions to calculate shares and amount.
*/
library SharesMath {
uint256 public constant PRECISION_MULTIPLIER = 1e27;
function calculateShares(
uint256 amount,
uint256 totalSupply,
uint256 totalShares
) internal pure returns (uint256 shares) {
return
totalSupply < 1 || totalShares < 1
? amount * PRECISION_MULTIPLIER
: Math.mulDiv(amount, totalShares, totalSupply);
}
function calculateAmount(
uint256 shares,
uint256 totalSupply,
uint256 totalShares
) internal pure returns (uint256 amount) {
return
totalSupply < 1 || totalShares < 1
? shares / PRECISION_MULTIPLIER
: Math.mulDiv(totalSupply, shares, totalShares);
}
}