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

sUSDS #1180

Merged
merged 9 commits into from
Sep 27, 2024
Merged

sUSDS #1180

Show file tree
Hide file tree
Changes from 5 commits
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
1 change: 1 addition & 0 deletions test/instances/aave/AaveHyperdrive.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ contract AaveHyperdriveTest is InstanceTest {
// The share test tolerances.
closeLongWithSharesTolerance: 20,
closeShortWithSharesTolerance: 100,
closeLongWithBaseTolerance: 20,
roundTripLpInstantaneousWithSharesTolerance: 1e5,
roundTripLpWithdrawalSharesWithSharesTolerance: 1e3,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 1e3,
Expand Down
1 change: 1 addition & 0 deletions test/instances/chainlink/CbETHBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ contract CbETHBaseTest is ChainlinkHyperdriveInstanceTest {
// The share test tolerances.
closeLongWithSharesTolerance: 20,
closeShortWithSharesTolerance: 100,
closeLongWithBaseTolerance: 20,
roundTripLpInstantaneousWithSharesTolerance: 1e3,
roundTripLpWithdrawalSharesWithSharesTolerance: 1e3,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 1e4,
Expand Down
1 change: 1 addition & 0 deletions test/instances/chainlink/WstETHGnosisChain.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ contract WstETHGnosisChainTest is ChainlinkHyperdriveInstanceTest {
// The share test tolerances.
closeLongWithSharesTolerance: 20,
closeShortWithSharesTolerance: 100,
closeLongWithBaseTolerance: 20,
roundTripLpInstantaneousWithSharesTolerance: 1e3,
roundTripLpWithdrawalSharesWithSharesTolerance: 1e3,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 1e3,
Expand Down
1 change: 1 addition & 0 deletions test/instances/corn/CornHyperdrive.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ contract CornHyperdriveInstanceTest is InstanceTest {
// The share test tolerances.
closeLongWithSharesTolerance: 0,
closeShortWithSharesTolerance: 0,
closeLongWithBaseTolerance: 0,
roundTripLpInstantaneousWithSharesTolerance: 0,
roundTripLpWithdrawalSharesWithSharesTolerance: 0,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 0,
Expand Down
1 change: 1 addition & 0 deletions test/instances/corn/Corn_LBTC_Hyperdrive.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ contract Corn_LBTC_Hyperdrive is CornHyperdriveInstanceTest {
// The share test tolerances.
closeLongWithSharesTolerance: 0,
closeShortWithSharesTolerance: 0,
closeLongWithBaseTolerance: 0,
roundTripLpInstantaneousWithSharesTolerance: 0,
roundTripLpWithdrawalSharesWithSharesTolerance: 0,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 0,
Expand Down
1 change: 1 addition & 0 deletions test/instances/corn/Corn_sDAI_Hyperdrive.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ contract Corn_sDAI_Hyperdrive is CornHyperdriveInstanceTest {
// The share test tolerances.
closeLongWithSharesTolerance: 0,
closeShortWithSharesTolerance: 0,
closeLongWithBaseTolerance: 0,
roundTripLpInstantaneousWithSharesTolerance: 0,
roundTripLpWithdrawalSharesWithSharesTolerance: 0,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 0,
Expand Down
1 change: 1 addition & 0 deletions test/instances/eeth/EETHHyperdrive.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ contract EETHHyperdriveTest is InstanceTest {
// The share test tolerances.
closeLongWithSharesTolerance: 20,
closeShortWithSharesTolerance: 100,
closeLongWithBaseTolerance: 20,
roundTripLpInstantaneousWithSharesTolerance: 1e5,
roundTripLpWithdrawalSharesWithSharesTolerance: 1e5,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 1e3,
Expand Down
1 change: 1 addition & 0 deletions test/instances/erc4626/SUSDe.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ contract SUSDeHyperdriveTest is ERC4626HyperdriveInstanceTest {
// The share test tolerances.
closeLongWithSharesTolerance: 20,
closeShortWithSharesTolerance: 100,
closeLongWithBaseTolerance: 20,
roundTripLpInstantaneousWithSharesTolerance: 1e8,
roundTripLpWithdrawalSharesWithSharesTolerance: 1e8,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 1e3,
Expand Down
1 change: 1 addition & 0 deletions test/instances/erc4626/StUSD.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ contract stUSDHyperdriveTest is ERC4626HyperdriveInstanceTest {
// The share test tolerances.
closeLongWithSharesTolerance: 20,
closeShortWithSharesTolerance: 100,
closeLongWithBaseTolerance: 20,
roundTripLpInstantaneousWithSharesTolerance: 1e7,
roundTripLpWithdrawalSharesWithSharesTolerance: 1e7,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 1e3,
Expand Down
195 changes: 195 additions & 0 deletions test/instances/erc4626/sUSDS.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.22;

import { stdStorage, StdStorage } from "forge-std/Test.sol";
import { IERC20 } from "../../../contracts/src/interfaces/IERC20.sol";
import { IERC4626 } from "../../../contracts/src/interfaces/IERC4626.sol";
import { IHyperdrive } from "../../../contracts/src/interfaces/IHyperdrive.sol";
import { HyperdriveUtils } from "../../utils/HyperdriveUtils.sol";
import { InstanceTest } from "../../utils/InstanceTest.sol";
import { Lib } from "../../utils/Lib.sol";
import { ERC4626HyperdriveInstanceTest } from "./ERC4626HyperdriveInstanceTest.t.sol";

interface ISUSDS {
function chi() external view returns (uint256);

function rho() external view returns (uint256);

function ssr() external view returns (uint256);

function drip() external returns (uint256);
}

contract sUSDSHyperdriveTest is ERC4626HyperdriveInstanceTest {
using HyperdriveUtils for uint256;
using HyperdriveUtils for IHyperdrive;
using Lib for *;
using stdStorage for StdStorage;

/// @dev The RAY constant from sUSDS.
uint256 internal constant RAY = 1e27;

/// @dev The USDS contract.
IERC20 internal constant USDS =
IERC20(0xdC035D45d973E3EC169d2276DDab16f1e407384F);

/// @dev The sUSDS contract.
IERC4626 internal constant SUSDS =
IERC4626(0xa3931d71877C0E7a3148CB7Eb4463524FEc27fbD);

/// @dev Whale accounts.
address internal USDS_TOKEN_WHALE =
address(0x0650CAF159C5A49f711e8169D4336ECB9b950275);
address[] internal baseTokenWhaleAccounts = [USDS_TOKEN_WHALE];
address internal SUSDS_TOKEN_WHALE =
address(0x2674341D40b445c21287b81c4Bc95EC8E358f7E8);
address[] internal vaultSharesTokenWhaleAccounts = [SUSDS_TOKEN_WHALE];

/// @notice Instantiates the instance testing suite with the configuration.
constructor()
InstanceTest(
InstanceTestConfig({
name: "Hyperdrive",
kind: "ERC4626Hyperdrive",
decimals: 18,
baseTokenWhaleAccounts: baseTokenWhaleAccounts,
vaultSharesTokenWhaleAccounts: vaultSharesTokenWhaleAccounts,
baseToken: USDS,
vaultSharesToken: SUSDS,
shareTolerance: 1e3,
minimumShareReserves: 1e15,
minimumTransactionAmount: 1e15,
positionDuration: POSITION_DURATION,
fees: IHyperdrive.Fees({
curve: 0,
flat: 0,
governanceLP: 0,
governanceZombie: 0
}),
enableBaseDeposits: true,
enableShareDeposits: true,
enableBaseWithdraws: true,
enableShareWithdraws: true,
baseWithdrawError: new bytes(0),
isRebasing: false,
shouldAccrueInterest: true,
// The base test tolerances.
roundTripLpInstantaneousWithBaseTolerance: 1e8,
roundTripLpWithdrawalSharesWithBaseTolerance: 1e8,
roundTripLongInstantaneousWithBaseUpperBoundTolerance: 1e3,
roundTripLongInstantaneousWithBaseTolerance: 1e8,
roundTripLongMaturityWithBaseUpperBoundTolerance: 1e3,
roundTripLongMaturityWithBaseTolerance: 1e5,
roundTripShortInstantaneousWithBaseUpperBoundTolerance: 1e3,
roundTripShortInstantaneousWithBaseTolerance: 1e5,
roundTripShortMaturityWithBaseTolerance: 1e5,
// The share test tolerances.
closeLongWithSharesTolerance: 1e3,
closeLongWithBaseTolerance: 1e3,
closeShortWithSharesTolerance: 100,
roundTripLpInstantaneousWithSharesTolerance: 1e7,
roundTripLpWithdrawalSharesWithSharesTolerance: 1e7,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 1e3,
roundTripLongInstantaneousWithSharesTolerance: 1e5,
roundTripLongMaturityWithSharesUpperBoundTolerance: 1e3,
roundTripLongMaturityWithSharesTolerance: 1e5,
roundTripShortInstantaneousWithSharesUpperBoundTolerance: 1e3,
roundTripShortInstantaneousWithSharesTolerance: 1e5,
roundTripShortMaturityWithSharesTolerance: 1e5,
// The verification tolerances.
verifyDepositTolerance: 5,
verifyWithdrawalTolerance: 2
})
)
{}

/// @notice Forge function that is invoked to setup the testing environment.
function setUp() public override __mainnet_fork(20_836_852) {
// Invoke the Instance testing suite setup.
super.setUp();
}

/// Helpers ///

/// @dev Advance time and accrue interest.
/// @param timeDelta The time to advance.
/// @param variableRate The variable rate.
function advanceTime(
uint256 timeDelta,
int256 variableRate
) internal override {
uint256 chi = ISUSDS(address(SUSDS)).chi();
uint256 rho = ISUSDS(address(SUSDS)).rho();
uint256 ssr = ISUSDS(address(SUSDS)).ssr();
chi = (_rpow(ssr, block.timestamp - rho) * chi) / RAY;

// Accrue interest in the sUSDS market. This amounts to manually
// updating the total supply assets.
(chi, ) = chi.calculateInterest(variableRate, timeDelta);

// Advance the time.
vm.warp(block.timestamp + timeDelta);

// Update the sUSDS market state.
vm.store(
address(SUSDS),
bytes32(uint256(5)),
bytes32((uint256(block.timestamp) << 192) | chi)
);
}

/// @dev The ray pow method from sUSDS.
/// @param x The base of the exponentiation.
/// @param n The exponent of the exponentiation.
/// @param z The result of the exponentiation.
function _rpow(uint256 x, uint256 n) internal pure returns (uint256 z) {
assembly {
switch x
case 0 {
switch n
case 0 {
z := RAY
}
default {
z := 0
}
}
default {
switch mod(n, 2)
case 0 {
z := RAY
}
default {
z := x
}
let half := div(RAY, 2) // for rounding.
for {
n := div(n, 2)
} n {
n := div(n, 2)
} {
let xx := mul(x, x)
if iszero(eq(div(xx, x), x)) {
revert(0, 0)
}
let xxRound := add(xx, half)
if lt(xxRound, xx) {
revert(0, 0)
}
x := div(xxRound, RAY)
if mod(n, 2) {
let zx := mul(z, x)
if and(iszero(iszero(x)), iszero(eq(div(zx, x), z))) {
revert(0, 0)
}
let zxRound := add(zx, half)
if lt(zxRound, zx) {
revert(0, 0)
}
z := div(zxRound, RAY)
}
}
}
}
}
}
1 change: 1 addition & 0 deletions test/instances/erc4626/sxDai.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ contract sxDaiHyperdriveTest is ERC4626HyperdriveInstanceTest {
// The share test tolerances.
closeLongWithSharesTolerance: 20,
closeShortWithSharesTolerance: 100,
closeLongWithBaseTolerance: 20,
roundTripLpInstantaneousWithSharesTolerance: 1e7,
roundTripLpWithdrawalSharesWithSharesTolerance: 1e7,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 1e3,
Expand Down
1 change: 1 addition & 0 deletions test/instances/ezETH/EzETHHyperdrive.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ contract EzETHHyperdriveTest is InstanceTest {
// The share test tolerances.
closeLongWithSharesTolerance: 1e6,
closeShortWithSharesTolerance: 1e6,
closeLongWithBaseTolerance: 20,
roundTripLpInstantaneousWithSharesTolerance: 1e7,
roundTripLpWithdrawalSharesWithSharesTolerance: 1e7,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 1e3,
Expand Down
1 change: 1 addition & 0 deletions test/instances/ezeth-linea/EzETHLineaTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ contract EzETHLineaHyperdriveTest is InstanceTest {
// The share test tolerances.
closeLongWithSharesTolerance: 20,
closeShortWithSharesTolerance: 100,
closeLongWithBaseTolerance: 20,
roundTripLpInstantaneousWithSharesTolerance: 100,
roundTripLpWithdrawalSharesWithSharesTolerance: 1e3,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 1e3,
Expand Down
1 change: 1 addition & 0 deletions test/instances/lseth/LsETHHyperdrive.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ contract LsETHHyperdriveTest is InstanceTest {
// The share test tolerances.
closeLongWithSharesTolerance: 20,
closeShortWithSharesTolerance: 100,
closeLongWithBaseTolerance: 20,
roundTripLpInstantaneousWithSharesTolerance: 1e3,
roundTripLpWithdrawalSharesWithSharesTolerance: 1e3,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 1e3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ contract MorphoBlue_USDe_DAI_HyperdriveTest is
// The share test tolerances.
closeLongWithSharesTolerance: 0,
closeShortWithSharesTolerance: 0,
closeLongWithBaseTolerance: 0,
roundTripLpInstantaneousWithSharesTolerance: 0,
roundTripLpWithdrawalSharesWithSharesTolerance: 0,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ contract MorphoBlue_cbETH_USDC_Base_HyperdriveTest is
// The share test tolerances.
closeLongWithSharesTolerance: 0,
closeShortWithSharesTolerance: 0,
closeLongWithBaseTolerance: 0,
roundTripLpInstantaneousWithSharesTolerance: 0,
roundTripLpWithdrawalSharesWithSharesTolerance: 0,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ contract MorphoBlue_sUSDe_DAI_HyperdriveTest is
// The share test tolerances.
closeLongWithSharesTolerance: 0,
closeShortWithSharesTolerance: 0,
closeLongWithBaseTolerance: 0,
roundTripLpInstantaneousWithSharesTolerance: 0,
roundTripLpWithdrawalSharesWithSharesTolerance: 0,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ contract MorphoBlue_wstETH_USDA_HyperdriveTest is
// The share test tolerances.
closeLongWithSharesTolerance: 0,
closeShortWithSharesTolerance: 0,
closeLongWithBaseTolerance: 0,
roundTripLpInstantaneousWithSharesTolerance: 0,
roundTripLpWithdrawalSharesWithSharesTolerance: 0,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ contract MorphoBlue_wstETH_USDC_HyperdriveTest is
// The share test tolerances.
closeLongWithSharesTolerance: 0,
closeShortWithSharesTolerance: 0,
closeLongWithBaseTolerance: 0,
roundTripLpInstantaneousWithSharesTolerance: 0,
roundTripLpWithdrawalSharesWithSharesTolerance: 0,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 0,
Expand Down
1 change: 1 addition & 0 deletions test/instances/reth/RETHHyperdrive.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ contract RETHHyperdriveTest is InstanceTest {
// The share test tolerances.
closeLongWithSharesTolerance: 20,
closeShortWithSharesTolerance: 100,
closeLongWithBaseTolerance: 20,
roundTripLpInstantaneousWithSharesTolerance: 2e3,
roundTripLpWithdrawalSharesWithSharesTolerance: 2e3,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 1e3,
Expand Down
1 change: 1 addition & 0 deletions test/instances/rseth-linea/RsETHLineaHyperdrive.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ contract RsETHLineaHyperdriveTest is InstanceTest {
// The share test tolerances.
closeLongWithSharesTolerance: 100,
closeShortWithSharesTolerance: 100,
closeLongWithBaseTolerance: 20,
roundTripLpInstantaneousWithSharesTolerance: 100,
roundTripLpWithdrawalSharesWithSharesTolerance: 1e4,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 1e3,
Expand Down
1 change: 1 addition & 0 deletions test/instances/steth/StETHHyperdrive.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ contract StETHHyperdriveTest is InstanceTest {
// The share test tolerances.
closeLongWithSharesTolerance: 20,
closeShortWithSharesTolerance: 100,
closeLongWithBaseTolerance: 20,
roundTripLpInstantaneousWithSharesTolerance: 1e5,
roundTripLpWithdrawalSharesWithSharesTolerance: 1e5,
roundTripLongInstantaneousWithSharesUpperBoundTolerance: 1e3,
Expand Down
Loading
Loading