Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sentilesdal committed Dec 12, 2023
1 parent 099d850 commit 835fc64
Show file tree
Hide file tree
Showing 2 changed files with 289 additions and 2 deletions.
281 changes: 281 additions & 0 deletions test/units/hyperdrive/ClosePositionsPastMaturity.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.19;

import { stdError } from "forge-std/StdError.sol";
import { VmSafe } from "forge-std/Vm.sol";
import { IHyperdrive } from "contracts/src/interfaces/IHyperdrive.sol";
import { AssetId } from "contracts/src/libraries/AssetId.sol";
import { FixedPointMath, ONE } from "contracts/src/libraries/FixedPointMath.sol";
import { HyperdriveMath } from "contracts/src/libraries/HyperdriveMath.sol";
import { YieldSpaceMath } from "contracts/src/libraries/YieldSpaceMath.sol";
import { MockHyperdrive } from "contracts/test/MockHyperdrive.sol";
import { HyperdriveTest, HyperdriveUtils } from "test/utils/HyperdriveTest.sol";
import { Lib } from "test/utils/Lib.sol";
import { console2 as console } from "forge-std/console2.sol";

contract ClosingLongsAtMaturityTest is HyperdriveTest {
using FixedPointMath for uint256;
using HyperdriveUtils for *;
using Lib for *;

function setUp() public override {
super.setUp();

// Start recording event logs.
vm.recordLogs();
}

// position opened at beginning of checkpoint
function test_position_at_checkpoint_start() external {
// Initialize the pool with a large amount of capital.
int256 fixedRate = int256(0.05e18);
uint256 contribution = 500_000_000e18;
initialize(alice, uint256(fixedRate), contribution);
uint256 flatFee = hyperdrive.getPoolConfig().fees.flat;

// go to the beginning of a checkpoint and create one
uint256 timeToNextCheckpoint = block.timestamp %
hyperdrive.getPoolConfig().checkpointDuration;
advanceTime(timeToNextCheckpoint, fixedRate);
hyperdrive.checkpoint(block.timestamp);

// Open a long position a 0% of checkpoint duration
uint256 baseAmount = 10e18;
(uint256 maturityTime, uint256 bonds) = openLong(bob, baseAmount);

// Advance to maturity.
advanceTimeTo(maturityTime, fixedRate);
hyperdrive.checkpoint(maturityTime);

// Advance time again
advanceTime(POSITION_DURATION / 2, fixedRate);

uint256 actualBaseReceived = hyperdrive.closeLong(
maturityTime,
bonds,
0,
IHyperdrive.Options({
destination: bob,
asBase: true,
extraData: new bytes(0)
})
);

uint256 expectedBaseRecieved = bonds - bonds.mulDown(uint256(flatFee));

console.log("actualBaseReceived ", actualBaseReceived);
console.log("expectedBaseRecieved", expectedBaseRecieved);
int difference = int(actualBaseReceived) - int(expectedBaseRecieved);
console.log("difference", difference);
assertApproxEqAbs(actualBaseReceived, expectedBaseRecieved, 2);
}

function test_position_at_checkpoint_25_percent() external {
// Initialize the pool with a large amount of capital.
int256 fixedRate = int256(0.05e18);
uint256 contribution = 500_000_000e18;
initialize(alice, uint256(fixedRate), contribution);
uint256 flatFee = hyperdrive.getPoolConfig().fees.flat;

// go to the beginning of a checkpoint and create one
uint256 timeToNextCheckpoint = block.timestamp %
hyperdrive.getPoolConfig().checkpointDuration;
advanceTime(timeToNextCheckpoint, fixedRate);
hyperdrive.checkpoint(block.timestamp);

// Open a long position a 25% of checkpoint duration
uint256 baseAmount = 10e18;
advanceTime(CHECKPOINT_DURATION / 4, fixedRate);
(uint256 maturityTime, uint256 bonds) = openLong(bob, baseAmount);

// Advance to maturity.
advanceTimeTo(maturityTime, fixedRate);
hyperdrive.checkpoint(maturityTime);

// Advance time again
advanceTime(POSITION_DURATION / 2, fixedRate);

uint256 actualBaseReceived = hyperdrive.closeLong(
maturityTime,
bonds,
0,
IHyperdrive.Options({
destination: bob,
asBase: true,
extraData: new bytes(0)
})
);

uint256 expectedBaseRecieved = bonds - bonds.mulDown(uint256(flatFee));

console.log("actualBaseReceived ", actualBaseReceived);
console.log("expectedBaseRecieved", expectedBaseRecieved);

int difference = int(actualBaseReceived) - int(expectedBaseRecieved);
console.log("difference", difference);
assertApproxEqAbs(actualBaseReceived, expectedBaseRecieved, 2);
}

// Initialize the pool with a large amount of capital.
function test_position_at_checkpoint_50_percent() external {
int256 fixedRate = int256(0.05e18);
uint256 contribution = 500_000_000e18;
initialize(alice, uint256(fixedRate), contribution);
uint256 flatFee = hyperdrive.getPoolConfig().fees.flat;

// go to the beginning of a checkpoint and create one
uint256 timeToNextCheckpoint = block.timestamp %
hyperdrive.getPoolConfig().checkpointDuration;
advanceTime(timeToNextCheckpoint, fixedRate);
hyperdrive.checkpoint(block.timestamp);

// Open a long position at 50% of checkpoint duration
uint256 baseAmount = 10e18;
advanceTime((2 * CHECKPOINT_DURATION) / 4, fixedRate);
(uint256 maturityTime, uint256 bonds) = openLong(bob, baseAmount);

// Advance to maturity.
advanceTimeTo(maturityTime, fixedRate);
hyperdrive.checkpoint(block.timestamp);

// Advance time again.
advanceTime(POSITION_DURATION / 2, fixedRate);

uint256 actualBaseReceived = hyperdrive.closeLong(
maturityTime,
bonds,
0,
IHyperdrive.Options({
destination: bob,
asBase: true,
extraData: new bytes(0)
})
);

uint256 expectedBaseRecieved = bonds - bonds.mulDown(uint256(flatFee));

console.log("actualBaseReceived ", actualBaseReceived);
console.log("expectedBaseRecieved", expectedBaseRecieved);
int difference = int(actualBaseReceived) - int(expectedBaseRecieved);
console.log("difference", difference);
assertApproxEqAbs(actualBaseReceived, expectedBaseRecieved, 2);
}

function test_position_at_checkpoint_75_percent() external {
// Initialize the pool with a large amount of capital.
int256 fixedRate = int256(0.05e18);
uint256 contribution = 500_000_000e18;
initialize(alice, uint256(fixedRate), contribution);
uint256 flatFee = hyperdrive.getPoolConfig().fees.flat;

// go to the beginning of a checkpoint and create one
uint256 timeToNextCheckpoint = block.timestamp %
hyperdrive.getPoolConfig().checkpointDuration;
advanceTime(timeToNextCheckpoint, fixedRate);
hyperdrive.checkpoint(block.timestamp);

// Open a long position at 75% of checkpoint duration
uint256 baseAmount = 10e18;
advanceTime((3 * CHECKPOINT_DURATION) / 4, fixedRate);
(uint256 maturityTime, uint256 bonds) = openLong(bob, baseAmount);

// Advance to maturity.
advanceTimeTo(maturityTime, fixedRate);
hyperdrive.checkpoint(maturityTime);

// Advance time again
advanceTime(POSITION_DURATION / 2, fixedRate);

uint256 actualBaseReceived = hyperdrive.closeLong(
maturityTime,
bonds,
0,
IHyperdrive.Options({
destination: bob,
asBase: true,
extraData: new bytes(0)
})
);

uint256 expectedBaseRecieved = bonds - bonds.mulDown(uint256(flatFee));

console.log("actualBaseReceived ", actualBaseReceived);
console.log("expectedBaseRecieved", expectedBaseRecieved);

int difference = int(actualBaseReceived) - int(expectedBaseRecieved);
console.log("difference", difference);
assertApproxEqAbs(actualBaseReceived, expectedBaseRecieved, 2);
}

// function test_long_position_values_throughout_checkpoint() external {
// // Initialize the pool with a large amount of capital.
// int256 fixedRate = int256(0.05e18);
// uint256 contribution = 500_000_000e18;
// initialize(alice, uint256(fixedRate), contribution);
// uint256 flatFee = hyperdrive.getPoolConfig().fees.flat;

// // go to the beginning of a checkpoint
// uint256 timeToNextCheckpoint = block.timestamp %
// hyperdrive.getPoolConfig().checkpointDuration;
// advanceTime(timeToNextCheckpoint, fixedRate);

// // Open a long position a 0% of checkpoint duration
// uint256 baseAmount = 10e18;
// (uint256 maturityTime1, uint256 bobBonds) = openLong(bob, baseAmount);

// // Open a long position at 25% of checkpoint duration
// advanceTime(CHECKPOINT_DURATION / 4, fixedRate);
// (uint256 maturityTime2, uint256 celineBonds) = openLong(
// celine,
// baseAmount
// );

// // Open a long position at 50% of checkpoint duration
// advanceTime(CHECKPOINT_DURATION / 4, fixedRate);
// (uint256 maturityTime3, uint256 danBonds) = openLong(dan, baseAmount);

// // Open a long position at 75% of checkpoint duration
// advanceTime(CHECKPOINT_DURATION / 4, fixedRate);
// (uint256 maturityTime4, uint256 eveBonds) = openLong(eve, baseAmount);

// assertEq(maturityTime1, maturityTime2);
// assertEq(maturityTime2, maturityTime3);
// assertEq(maturityTime3, maturityTime4);

// // Advance to maturity.
// advanceTimeTo(maturityTime1, fixedRate);
// hyperdrive.checkpoint(maturityTime);

// // Close positions.
// address[4] memory addresses = [bob, celine, dan, eve];
// uint[4] memory bonds = [bobBonds, celineBonds, danBonds, eveBonds];

// for (uint i; i < 4; i++) {
// address user = addresses[i];
// uint256 userBonds = bonds[i];
// vm.stopPrank();
// vm.startPrank(user);

// uint256 actualBaseReceived = hyperdrive.closeLong(
// maturityTime1,
// userBonds,
// 0,
// IHyperdrive.Options({
// destination: user,
// asBase: true,
// extraData: new bytes(0)
// })
// );

// uint256 expectedBaseRecieved = userBonds -
// userBonds.mulDown(uint256(flatFee));

// int difference = int(actualBaseReceived) - int(expectedBaseRecieved);
// assertApproxEqAbs(
// actualBaseReceived,
// expectedBaseRecieved,
// 1
// );
// }
// }
}
10 changes: 8 additions & 2 deletions test/utils/HyperdriveTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ contract HyperdriveTest is BaseTest {
uint256 fixedRate
) internal view returns (IHyperdrive.PoolConfig memory) {
IHyperdrive.Fees memory fees = IHyperdrive.Fees({
curve: 0,
flat: 0,
curve: 0.005 ether,
flat: 0.005 ether,
governance: 0
});
return
Expand Down Expand Up @@ -725,6 +725,12 @@ contract HyperdriveTest is BaseTest {
vm.warp(block.timestamp + time);
}

function advanceTimeTo(uint256 timestamp, int256 apr) internal virtual {
uint256 time = timestamp - block.timestamp;
MockHyperdrive(address(hyperdrive)).accrue(time, apr);
vm.warp(timestamp);
}

function advanceTimeWithCheckpoints(
uint256 time,
int256 apr
Expand Down

0 comments on commit 835fc64

Please sign in to comment.