Skip to content

Commit 21547fd

Browse files
committed
simplified formula implementations
1 parent 1a1ddec commit 21547fd

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/Solaxy.sol

+12-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {ISolaxy} from "./interfaces/ISolaxy.sol";
55
import {ERC20} from "solady/tokens/ERC20.sol";
66
import {ReentrancyGuard} from "solady/utils/ReentrancyGuard.sol";
77
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
8-
import {UD60x18, ud60x18} from "@prb/[email protected]/src/UD60x18.sol";
8+
import {UD60x18, ud} from "@prb/[email protected]/src/UD60x18.sol";
99

1010
/**
1111
* @title Solaxy
@@ -66,11 +66,11 @@ contract Solaxy is ISolaxy, ERC20, ReentrancyGuard {
6666
}
6767

6868
function convertToShares(uint256 assets) external view returns (uint256 shares) {
69-
shares = ud60x18(assets).div(ud60x18(2 * totalSupply()).mul(SEMI_SLOPE)).intoUint256();
69+
shares = ud(assets).div(ud(2 * totalSupply()).mul(SEMI_SLOPE)).unwrap();
7070
}
7171

7272
function convertToAssets(uint256 shares) external view returns (uint256 assets) {
73-
assets = ud60x18(shares).mul(ud60x18(2 * totalSupply()).mul(SEMI_SLOPE)).intoUint256();
73+
assets = ud(shares).mul(ud(2 * totalSupply()).mul(SEMI_SLOPE)).unwrap();
7474
}
7575

7676
function maxWithdraw(address owner) external view returns (uint256 maxAssets) {
@@ -132,8 +132,8 @@ contract Solaxy is ISolaxy, ERC20, ReentrancyGuard {
132132
* @return shares The calculated number of shares minted for the deposited assets.
133133
*/
134134
function previewDeposit(uint256 assets) public view returns (uint256 shares) {
135-
UD60x18 totalShares = ud60x18(totalSupply());
136-
shares = totalShares.powu(2).add(ud60x18(assets).div(SEMI_SLOPE)).sqrt().sub(totalShares).intoUint256();
135+
UD60x18 totalShares = ud(totalSupply());
136+
shares = (totalShares.powu(2) + ud(assets).div(SEMI_SLOPE).sqrt() - totalShares).unwrap();
137137
}
138138

139139
/**
@@ -143,8 +143,8 @@ contract Solaxy is ISolaxy, ERC20, ReentrancyGuard {
143143
* @return shares The calculated number of shares to be burned in the withdrawal.
144144
*/
145145
function previewWithdraw(uint256 assets) public view returns (uint256 shares) {
146-
UD60x18 totalShares = ud60x18(totalSupply());
147-
shares = totalShares.sub(totalShares.powu(2).sub(ud60x18(assets).div(SEMI_SLOPE)).sqrt()).intoUint256();
146+
UD60x18 totalShares = ud(totalSupply());
147+
shares = (totalShares - (totalShares.powu(2) - (ud(assets).div(SEMI_SLOPE))).sqrt()).unwrap();
148148
}
149149

150150
/**
@@ -156,8 +156,8 @@ contract Solaxy is ISolaxy, ERC20, ReentrancyGuard {
156156
* @return assets The computed assets.
157157
*/
158158
function previewMint(uint256 shares) public view returns (uint256 assets) {
159-
UD60x18 totalShares = ud60x18(totalSupply());
160-
assets = SEMI_SLOPE.mul((totalShares + ud60x18(shares)).powu(2).sub(totalShares.powu(2))).intoUint256();
159+
UD60x18 totalShares = ud(totalSupply());
160+
assets = SEMI_SLOPE.mul((totalShares + ud(shares)).powu(2) - totalShares.powu(2)).unwrap();
161161
}
162162

163163
/**
@@ -169,8 +169,8 @@ contract Solaxy is ISolaxy, ERC20, ReentrancyGuard {
169169
* @return assets The computed assets.
170170
*/
171171
function previewRedeem(uint256 shares) public view returns (uint256 assets) {
172-
UD60x18 totalShares = ud60x18(totalSupply());
173-
assets = SEMI_SLOPE.mul(totalShares.powu(2).sub(totalShares.sub(ud60x18(shares)).powu(2))).intoUint256();
172+
UD60x18 totalShares = ud(totalSupply());
173+
assets = SEMI_SLOPE.mul(totalShares.powu(2) - (totalShares - ud(shares)).powu(2)).unwrap();
174174
}
175175

176176
/**
@@ -200,10 +200,7 @@ contract Solaxy is ISolaxy, ERC20, ReentrancyGuard {
200200
*/
201201
function _dump(address receiver, address owner, uint256 assets, uint256 shares) private nonReentrant {
202202
(uint256 initialAssets, uint256 initialShares) = (totalAssets(), totalSupply());
203-
204-
uint256 tip = ud60x18(7).div(ud60x18(186)).mul(ud60x18(assets)).div(
205-
SEMI_SLOPE.mul(ud60x18(initialShares).sub(ud60x18(shares)))
206-
).intoUint256();
203+
uint256 tip = ud(7).div(ud(186)).mul(ud(assets)).div(SEMI_SLOPE.mul(ud(initialShares) - ud(shares))).unwrap();
207204

208205
if (assets == 0) revert CannotBeZero();
209206
if (shares == 0) revert CannotBeZero();

0 commit comments

Comments
 (0)