Skip to content

Commit f410574

Browse files
authored
Added the base proceeds of opening a long to Hyperdrive (#760)
1 parent 34b562e commit f410574

3 files changed

Lines changed: 24 additions & 18 deletions

File tree

contracts/src/interfaces/IHyperdriveEvents.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ interface IHyperdriveEvents is IMultiTokenEvents {
6363
uint256 baseAmount,
6464
uint256 vaultShareAmount,
6565
bool asBase,
66+
uint256 baseProceeds,
6667
uint256 bondAmount
6768
);
6869

contracts/src/internal/HyperdriveShort.sol

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,18 @@ abstract contract HyperdriveShort is IHyperdriveEvents, HyperdriveLP {
6161
// backdate the bonds sold to the beginning of the checkpoint.
6262
// Note: All state deltas are derived from the external function input.
6363
uint256 maturityTime = latestCheckpoint + _positionDuration;
64-
uint256 shareReservesDelta;
65-
uint256 baseDeposit;
66-
{
67-
uint256 totalGovernanceFee;
68-
(
69-
baseDeposit,
70-
shareReservesDelta,
71-
totalGovernanceFee
72-
) = _calculateOpenShort(
64+
(
65+
uint256 baseDeposit,
66+
uint256 shareReservesDelta,
67+
uint256 totalGovernanceFee
68+
) = _calculateOpenShort(
7369
_bondAmount,
7470
vaultSharePrice,
7571
openVaultSharePrice
7672
);
7773

78-
// Attribute the governance fees.
79-
_governanceFeesAccrued += totalGovernanceFee;
80-
}
74+
// Attribute the governance fees.
75+
_governanceFeesAccrued += totalGovernanceFee;
8176

8277
// Take custody of the trader's deposit and ensure that the trader
8378
// doesn't pay more than their max deposit. The trader's deposit is
@@ -87,15 +82,15 @@ abstract contract HyperdriveShort is IHyperdriveEvents, HyperdriveLP {
8782
// Note: We don't check the maxDeposit against the output of deposit
8883
// because slippage from a deposit could cause a larger deposit taken
8984
// from the user to pass due to the shares being worth less after deposit.
90-
uint256 traderDeposit = _convertToOptionFromBase(
85+
uint256 shareDeposit = _convertToOptionFromBase(
9186
baseDeposit,
9287
vaultSharePrice,
9388
_options
9489
);
95-
if (_maxDeposit < traderDeposit) {
90+
if (_maxDeposit < shareDeposit) {
9691
revert IHyperdrive.OutputLimit();
9792
}
98-
_deposit(traderDeposit, _options);
93+
_deposit(shareDeposit, _options);
9994

10095
// Apply the state updates caused by opening the short.
10196
// Note: Updating the state using the result using the
@@ -117,18 +112,26 @@ abstract contract HyperdriveShort is IHyperdriveEvents, HyperdriveLP {
117112
_mint(assetId, _options.destination, bondAmount);
118113

119114
// Emit an OpenShort event.
115+
uint256 vaultSharePrice_ = vaultSharePrice;
120116
IHyperdrive.Options calldata options = _options;
121117
emit OpenShort(
122118
options.destination,
123119
assetId,
124120
maturityTime,
125121
baseDeposit,
126-
baseDeposit.divDown(vaultSharePrice),
122+
baseDeposit.divDown(vaultSharePrice_),
127123
options.asBase,
124+
_convertToBaseFromOption(
125+
// We add the governance fee to the share reserves delta since
126+
// the user is responsible for paying the governance fee.
127+
shareReservesDelta + totalGovernanceFee,
128+
vaultSharePrice_,
129+
options
130+
),
128131
bondAmount
129132
);
130133

131-
return (maturityTime, traderDeposit);
134+
return (maturityTime, shareDeposit);
132135
}
133136

134137
/// @dev Closes a short position with a specified maturity time.

test/units/hyperdrive/OpenShortTest.t.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,11 @@ contract OpenShortTest is HyperdriveTest {
394394
uint256 eventBaseAmount,
395395
uint256 eventVaultShareAmount,
396396
bool eventAsBase,
397+
uint256 eventBaseProceeds,
397398
uint256 eventBondAmount
398399
) = abi.decode(
399400
log.data,
400-
(uint256, uint256, uint256, bool, uint256)
401+
(uint256, uint256, uint256, bool, uint256, uint256)
401402
);
402403
assertEq(eventMaturityTime, maturityTime);
403404
assertEq(eventBaseAmount, basePaid);
@@ -406,6 +407,7 @@ contract OpenShortTest is HyperdriveTest {
406407
basePaid.divDown(hyperdrive.getPoolInfo().vaultSharePrice)
407408
);
408409
assertEq(eventAsBase, true);
410+
assertEq(eventBaseProceeds, shortAmount - basePaid);
409411
assertEq(eventBondAmount, shortAmount);
410412
}
411413

0 commit comments

Comments
 (0)