Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions contracts/EverlongStrategyKeeper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ contract EverlongStrategyKeeper is Ownable {
for (uint256 i = 0; i < positionCount; i++) {
position = IEverlongStrategy(_strategy).positionAt(i);
if (hyperdrive.isMature(position)) {
// For accurate calculation of position value, ensure we're using the
// correct state as would be used in an actual closeLong call
canSpend += hyperdrive.previewCloseLong(
asBase,
poolConfig,
Expand All @@ -340,6 +342,7 @@ contract EverlongStrategyKeeper is Ownable {

// Calculate the amount of bonds that would be received if a long was
// opened with `canSpend` assets.
// For accurate preview, ensure the calculation simulates the correct state
uint256 expectedOutput = hyperdrive.previewOpenLong(
asBase,
poolConfig,
Expand Down
23 changes: 23 additions & 0 deletions contracts/libraries/HyperdriveExecution.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Packing } from "openzeppelin/utils/Packing.sol";
import { IEverlongEvents } from "../interfaces/IEverlongEvents.sol";
import { IEverlongStrategy } from "../interfaces/IEverlongStrategy.sol";
import { ONE, LEGACY_SDAI_HYPERDRIVE, LEGACY_STETH_HYPERDRIVE } from "./Constants.sol";
import { LPMath } from "./LPMath.sol";

// TODO: Extract into its own library.
uint256 constant HYPERDRIVE_SHARE_RESERVES_BOND_RESERVES_SLOT = 2;
Expand Down Expand Up @@ -290,6 +291,28 @@ library HyperdriveExecutionLibrary {
IEverlongStrategy.EverlongPosition memory _position,
bytes memory // unused extradata
) internal view returns (uint256) {
// Check if the bond amount is less than minimumTransactionAmount.
// In the actual closeLong function, this would revert.
if (_position.bondAmount < _poolConfig.minimumTransactionAmount) {
return 0;
}

// If the long hasn't matured, we checkpoint the latest checkpoint.
// Otherwise, we perform a checkpoint at the time the long matured.
// This ensures the long and all of the other positions in the
// checkpoint are closed.
if (block.timestamp < _position.maturityTime) {
self.checkpoint(
latestCheckpoint(self),
LPMath.SHARE_PROCEEDS_MAX_ITERATIONS
);
} else {
self.checkpoint(
_position.maturityTime,
LPMath.SHARE_PROCEEDS_MAX_ITERATIONS
);
}

// Read select `PoolInfo` fields directly from Hyperdrive's storage to
// save on gas costs.
uint256[] memory slots = new uint256[](2);
Expand Down
Loading
Loading