Skip to content

Commit

Permalink
make tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
wakamex committed Nov 10, 2023
1 parent 450a578 commit c732d10
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 27 deletions.
4 changes: 4 additions & 0 deletions contracts/src/HyperdriveBase.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.19;

import "forge-std/console2.sol";
import { HyperdriveStorage } from "./HyperdriveStorage.sol";
import { IERC20 } from "./interfaces/IERC20.sol";
import { IHyperdrive } from "./interfaces/IHyperdrive.sol";
Expand Down Expand Up @@ -270,12 +271,15 @@ abstract contract HyperdriveBase is
// Calculate the spot price after making the trade on the curve but
// before accounting for fees. Compare this to the max spot price to
// determine if the trade is negative interest.
console2.log("_isNegativeInterest:calculateSpotPrice");
uint256 endingSpotPrice = HyperdriveMath.calculateSpotPrice(
_effectiveShareReserves() + _shareCurveDelta,
_marketState.bondReserves - _bondCurveDelta,
_initialSharePrice,
_timeStretch
);
console2.log("_isNegativeInterest:endingSpotPrice = ", endingSpotPrice);
console2.log("_isNegativeInterest:_maxSpotPrice = ", _maxSpotPrice);
return endingSpotPrice > _maxSpotPrice;
}

Expand Down
16 changes: 16 additions & 0 deletions contracts/src/HyperdriveLong.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.19;

import "forge-std/console2.sol";
import { HyperdriveLP } from "./HyperdriveLP.sol";
import { IHyperdrive } from "./interfaces/IHyperdrive.sol";
import { IHyperdriveWrite } from "./interfaces/IHyperdriveWrite.sol";
Expand Down Expand Up @@ -42,6 +43,7 @@ abstract contract HyperdriveLong is IHyperdriveWrite, HyperdriveLP {
isNotPaused
returns (uint256 maturityTime, uint256 bondProceeds)
{
console2.log("HyperdriveLong::openLong");
// Check that the message value is valid.
_checkMessageValue();

Expand All @@ -60,14 +62,17 @@ abstract contract HyperdriveLong is IHyperdriveWrite, HyperdriveLP {
if (baseDeposited < _minimumTransactionAmount) {
revert IHyperdrive.MinimumTransactionAmount();
}
console2.log("MinimumSharePrice");
if (sharePrice < _minSharePrice) {
revert IHyperdrive.MinimumSharePrice();
}

console2.log("checkpoint");
// Perform a checkpoint.
uint256 latestCheckpoint = _latestCheckpoint();
_applyCheckpoint(latestCheckpoint, sharePrice);

console2.log("calculateOpenLong");
// Calculate the pool and user deltas using the trading function. We
// backdate the bonds purchased to the beginning of the checkpoint.
uint256 shareReservesDelta;
Expand All @@ -81,13 +86,17 @@ abstract contract HyperdriveLong is IHyperdriveWrite, HyperdriveLP {
) = _calculateOpenLong(sharesDeposited, sharePrice);

// Enforce min user outputs
console2.log("OutputLimit");
if (_minOutput > bondProceeds) revert IHyperdrive.OutputLimit();

// Attribute the governance fee.
console2.log("governanceFee");
_governanceFeesAccrued += totalGovernanceFee;

// Apply the open long to the state.
console2.log("maturityTime");
maturityTime = latestCheckpoint + _positionDuration;
console2.log("_applyOpenLong");
_applyOpenLong(
shareReservesDelta,
bondProceeds,
Expand Down Expand Up @@ -362,6 +371,7 @@ abstract contract HyperdriveLong is IHyperdriveWrite, HyperdriveLP {
{
// Calculate the effect that opening the long should have on the pool's
// reserves as well as the amount of bond the trader receives.
console2.log("_calculateOpenLong:calculateOpenLong");
bondReservesDelta = HyperdriveMath.calculateOpenLong(
_effectiveShareReserves(),
_marketState.bondReserves,
Expand All @@ -373,12 +383,14 @@ abstract contract HyperdriveLong is IHyperdriveWrite, HyperdriveLP {

// Ensure that the trader didn't purchase bonds at a negative interest
// rate after accounting for fees.
console2.log("_calculateOpenLong:calculateSpotPrice");
uint256 spotPrice = HyperdriveMath.calculateSpotPrice(
_effectiveShareReserves(),
_marketState.bondReserves,
_initialSharePrice,
_timeStretch
);
console2.log("_calculateOpenLong:_isNegativeInterest");
if (
_isNegativeInterest(
_shareAmount,
Expand All @@ -394,15 +406,18 @@ abstract contract HyperdriveLong is IHyperdriveWrite, HyperdriveLP {
}

// Record an oracle update if enough time has elapsed.
console2.log("_calculateOpenLong:recordPrice");
recordPrice(spotPrice);

// Calculate the fees charged to the user (curveFee) and the portion
// of those fees that are paid to governance (governanceCurveFee).
console2.log("_calculateOpenLong:_calculateFeesGivenShares");
(
uint256 curveFee, // bonds
uint256 governanceCurveFee // bonds
) = _calculateFeesGivenShares(_shareAmount, spotPrice, _sharePrice);

console2.log("_calculateOpenLong:bondProceeds");
// Calculate the number of bonds the trader receives.
// This is the amount of bonds the trader receives minus the fees.
bondProceeds = bondReservesDelta - curveFee;
Expand All @@ -418,6 +433,7 @@ abstract contract HyperdriveLong is IHyperdriveWrite, HyperdriveLP {
// conversion is needed:
//
// bonds = bonds + bonds
console2.log("_calculateOpenLong:bondReservesDelta");
bondReservesDelta = bondProceeds + governanceCurveFee;

// Calculate the fees owed to governance in shares. Open longs
Expand Down
8 changes: 6 additions & 2 deletions contracts/src/libraries/HyperdriveMath.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.19;

import "forge-std/console2.sol";
import { IHyperdrive } from "../interfaces/IHyperdrive.sol";
import { FixedPointMath, ONE } from "./FixedPointMath.sol";
import { YieldSpaceMath } from "./YieldSpaceMath.sol";
Expand Down Expand Up @@ -120,10 +121,13 @@ library HyperdriveMath {
uint256 _curveFee,
uint256 _flatFee
) internal pure returns (uint256) {
console2.log("calculateOpenLongMaxSpotPrice:flatFee", _flatFee);
return
(ONE - _flatFee).divDown(
ONE + _curveFee.mulUp(ONE.divUp(_startingSpotPrice) - ONE)
.mulUp(ONE - _flatFee)
ONE +
_curveFee.mulUp(ONE.divUp(_startingSpotPrice) - ONE).mulUp(
ONE - _flatFee
)
);
}

Expand Down
18 changes: 9 additions & 9 deletions test/integrations/hyperdrive/NegativeInterestLongFeeTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ contract NegativeInterestLongFeeTest is HyperdriveTest {
);
variableInterest = -variableInterest.normalizeToRange(0, .5e18);
uint256 curveFee = 0e18;
uint256 flatFee = 0.1e18;
uint256 flatFee = 0.01e18;
uint256 governanceFee = 1e18;
test_negative_interest_long_full_term_fees(
initialSharePrice,
Expand All @@ -231,7 +231,7 @@ contract NegativeInterestLongFeeTest is HyperdriveTest {
int256 preTradeVariableInterest = -0.05e18;
int256 variableInterest = -0.1e18;
uint256 curveFee = 0e18;
uint256 flatFee = 0.1e18;
uint256 flatFee = 0.01e18;
uint256 governanceFee = 1e18;
test_negative_interest_long_full_term_fees(
initialSharePrice,
Expand All @@ -255,7 +255,7 @@ contract NegativeInterestLongFeeTest is HyperdriveTest {
int256 preTradeVariableInterest = -0.05e18;
int256 variableInterest = -0.1e18;
uint256 curveFee = 0e18;
uint256 flatFee = 0.1e18;
uint256 flatFee = 0.01e18;
uint256 governanceFee = 1e18;
test_negative_interest_long_full_term_fees(
initialSharePrice,
Expand All @@ -279,7 +279,7 @@ contract NegativeInterestLongFeeTest is HyperdriveTest {
int256 preTradeVariableInterest = -0.05e18;
int256 variableInterest = -0.1e18;
uint256 curveFee = 0e18;
uint256 flatFee = 0.1e18;
uint256 flatFee = 0.01e18;
uint256 governanceFee = 1e18;
test_negative_interest_long_full_term_fees(
initialSharePrice,
Expand Down Expand Up @@ -392,7 +392,7 @@ contract NegativeInterestLongFeeTest is HyperdriveTest {
);
variableInterest = -variableInterest.normalizeToRange(0, .5e18);
uint256 curveFee = 0.1e18;
uint256 flatFee = 0.1e18;
uint256 flatFee = 0.01e18;
uint256 governanceFee = 1e18;
test_negative_interest_long_half_term_fees(
initialSharePrice,
Expand All @@ -418,7 +418,7 @@ contract NegativeInterestLongFeeTest is HyperdriveTest {
int256 preTradeVariableInterest = -0.05e18;
int256 variableInterest = -0.1e18;
uint256 curveFee = 0.1e18;
uint256 flatFee = 0.1e18;
uint256 flatFee = 0.01e18;
uint256 governanceFee = 1e18;
test_negative_interest_long_half_term_fees(
initialSharePrice,
Expand All @@ -444,7 +444,7 @@ contract NegativeInterestLongFeeTest is HyperdriveTest {
int256 preTradeVariableInterest = -0.05e18;
int256 variableInterest = -0.1e18;
uint256 curveFee = 0.1e18;
uint256 flatFee = 0.1e18;
uint256 flatFee = 0.01e18;
uint256 governanceFee = 1e18;
test_negative_interest_long_half_term_fees(
initialSharePrice,
Expand All @@ -470,7 +470,7 @@ contract NegativeInterestLongFeeTest is HyperdriveTest {
int256 preTradeVariableInterest = -0.05e18;
int256 variableInterest = -0.1e18;
uint256 curveFee = 0.1e18;
uint256 flatFee = 0.1e18;
uint256 flatFee = 0.01e18;
uint256 governanceFee = 1e18;
test_negative_interest_long_half_term_fees(
initialSharePrice,
Expand Down Expand Up @@ -578,7 +578,7 @@ contract NegativeInterestLongFeeTest is HyperdriveTest {
FixedPointMath.ONE_18 - normalizedTimeRemaining,
openSharePrice
)
.mulDown(0.1e18);
.mulDown(flatFee);
uint256 expectedCurve = (FixedPointMath.ONE_18 -
calculatedSpotPrice)
.mulDown(0.1e18)
Expand Down
5 changes: 5 additions & 0 deletions test/integrations/hyperdrive/RoundTripTest.t.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.19;

import "forge-std/console2.sol";
import { stdError } from "forge-std/StdError.sol";
import { IHyperdrive } from "contracts/src/interfaces/IHyperdrive.sol";
import { AssetId } from "contracts/src/libraries/AssetId.sol";
Expand Down Expand Up @@ -171,6 +172,10 @@ contract RoundTripTest is HyperdriveTest {
uint256 timeStretchApr,
uint256 basePaid
) external {
console2.log("apr = ", apr);
console2.log("timeStretchApr = ", timeStretchApr);
console2.log("basePaid = ", basePaid);
basePaid = 10e18;
_test_long_multiblock_round_trip_end_of_checkpoint(
apr,
timeStretchApr,
Expand Down
8 changes: 6 additions & 2 deletions test/units/hyperdrive/CloseLongTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ contract CloseLongTest is HyperdriveTest {
config = testConfig(fixedRate);
config.fees = IHyperdrive.Fees({
curve: 0,
flat: 1e18,
flat: 0.01e18,
governance: 1e18
});
deploy(address(deployer), config);
Expand All @@ -728,7 +728,11 @@ contract CloseLongTest is HyperdriveTest {

// 7. deploy a pool with 100% curve fees and 0% gov fees
config = testConfig(fixedRate);
config.fees = IHyperdrive.Fees({ curve: 0, flat: 1e18, governance: 0 });
config.fees = IHyperdrive.Fees({
curve: 0,
flat: 0.01e18,
governance: 0
});
// Deploy and initialize the new pool
deploy(address(deployer), config);
initialize(alice, fixedRate, contribution);
Expand Down
38 changes: 25 additions & 13 deletions test/units/hyperdrive/FeeTest.t.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.19;

import "forge-std/console2.sol";
import { stdError } from "forge-std/StdError.sol";
import { IHyperdrive } from "contracts/src/interfaces/IHyperdrive.sol";
import { AssetId } from "contracts/src/libraries/AssetId.sol";
Expand All @@ -13,12 +14,15 @@ import { Lib } from "test/utils/Lib.sol";
contract FeeTest is HyperdriveTest {
using FixedPointMath for uint256;
using Lib for *;
uint256 deployCurveFee = 0.1e18; // 10%
uint256 deployFlatFee = 0.01e18; // 0.1%
uint256 deployGovernanceFee = 0.5e18; // 50%

function test_governanceFeeAccrual_invalidFeeDestination_failure() public {
// Deploy and initialize a new pool with fees.
uint256 apr = 0.05e18;
uint256 contribution = 500_000_000e18;
deploy(alice, apr, 0.1e18, 0.1e18, 0.5e18);
deploy(alice, apr, deployCurveFee, deployFlatFee, deployGovernanceFee);
initialize(alice, apr, contribution);

// Open a long and ensure that the governance fees accrued are non-zero.
Expand All @@ -45,7 +49,7 @@ contract FeeTest is HyperdriveTest {
uint256 contribution = 500_000_000e18;

// Deploy and initialize a new pool with fees.
deploy(alice, apr, 0.1e18, 0.1e18, 0.5e18);
deploy(alice, apr, deployCurveFee, deployFlatFee, deployGovernanceFee);
initialize(alice, apr, contribution);

// Open a long, record the accrued fees x share price
Expand Down Expand Up @@ -78,9 +82,9 @@ contract FeeTest is HyperdriveTest {
function test_flat_gov_fee_close_long() public {
uint256 initialSharePrice = 1e18;
int256 variableInterest = 0.0e18;
uint256 curveFee = 0e18;
uint256 flatFee = .1e18;
uint256 governanceFee = 1e18;
uint256 curveFee = 0e18; // 0%
uint256 flatFee = 0.001e18; // 0.1%
uint256 governanceFee = 1e18; // 100%
uint256 timeElapsed = 73 days;

uint256 governanceFees = 0;
Expand Down Expand Up @@ -183,9 +187,9 @@ contract FeeTest is HyperdriveTest {
// This test demonstrates that the governance fees from curve fee are NOT included in the shareReserves.
function test_curve_gov_fee_close_long() public {
uint256 initialSharePrice = 1e18;
uint256 curveFee = 0.1e18;
uint256 flatFee = 0e18;
uint256 governanceFee = 1e18;
uint256 curveFee = 0.1e18; // 10%
uint256 flatFee = 0e18; // 0%
uint256 governanceFee = 1e18; // 100%
uint256 timeElapsed = 73 days;

uint256 governanceFeesFromCloseLong = 0;
Expand Down Expand Up @@ -256,6 +260,14 @@ contract FeeTest is HyperdriveTest {
.mulDown(normalizedTimeRemaining);

// actual curve fee from close long should equal the expected curve fee from close long
console2.log(
"governanceFeesFromCloseLong = ",
governanceFeesFromCloseLong
);
console2.log(
"expectedFeeSubtractedFromShareReserves = ",
expectedFeeSubtractedFromShareReserves
);
assertEq(
governanceFeesFromCloseLong,
expectedFeeSubtractedFromShareReserves
Expand Down Expand Up @@ -330,7 +342,7 @@ contract FeeTest is HyperdriveTest {
uint256 contribution = 500_000_000e18;

// Deploy and initialize a new pool with fees.
deploy(alice, apr, 0.1e18, 0.1e18, 0.5e18);
deploy(alice, apr, deployCurveFee, deployFlatFee, deployGovernanceFee);
initialize(alice, apr, contribution);

// Ensure that the governance initially has zero balance
Expand Down Expand Up @@ -393,7 +405,7 @@ contract FeeTest is HyperdriveTest {
uint256 contribution = 500_000_000e18;

// Deploy and initialize a new pool with fees.
deploy(alice, apr, 0.1e18, 0.1e18, 0.5e18);
deploy(alice, apr, deployCurveFee, deployFlatFee, deployGovernanceFee);
initialize(alice, apr, contribution);

// Ensure that the governance initially has zero balance
Expand Down Expand Up @@ -463,7 +475,7 @@ contract FeeTest is HyperdriveTest {
// Initialize the pool with a large amount of capital.
uint256 contribution = 500_000_000e18;
// Deploy and initialize a new pool with fees.
deploy(alice, apr, 0.1e18, 0.1e18, 0.5e18);
deploy(alice, apr, deployCurveFee, deployFlatFee, deployGovernanceFee);
initialize(alice, apr, contribution);

(uint256 curveFee, uint256 governanceCurveFee) = MockHyperdrive(
Expand All @@ -486,7 +498,7 @@ contract FeeTest is HyperdriveTest {
// Initialize the pool with a large amount of capital.
uint256 contribution = 500_000_000e18;
// Deploy and initialize a new pool with fees.
deploy(alice, apr, 0.1e18, 0.1e18, 0.5e18);
deploy(alice, apr, deployCurveFee, 0.1e18, deployGovernanceFee);
initialize(alice, apr, contribution);
(
uint256 curveFee,
Expand Down Expand Up @@ -527,7 +539,7 @@ contract FeeTest is HyperdriveTest {
// Initialize the pool with a large amount of capital.
uint256 contribution = 500_000_000e18;
// Deploy and initialize a new pool with fees.
deploy(alice, apr, 0.1e18, 0.1e18, 0.5e18);
deploy(alice, apr, deployCurveFee, 0.1e18, deployGovernanceFee);
initialize(alice, apr, contribution);
(
uint256 curveFee,
Expand Down
Loading

0 comments on commit c732d10

Please sign in to comment.