Skip to content

Commit

Permalink
Allow call option writers to bid on spread (#3)
Browse files Browse the repository at this point in the history
* Bid functionality

* WIP

* Tests

* Remove debug code
  • Loading branch information
regynald authored May 3, 2022
1 parent c96f670 commit 02cd9c6
Show file tree
Hide file tree
Showing 2 changed files with 445 additions and 13 deletions.
58 changes: 45 additions & 13 deletions src/HookCoveredCallImplV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -262,26 +262,55 @@ contract HookCoveredCallImplV1 is
uint256 bidAmt = msg.value;
CallOption storage call = optionParams[optionId];

// TODO(HOOK-805) allow option writer to make a bid with an deposited amount equal
// to the spread between the their bid and the option's strike price.
if (msg.sender == call.writer) {
_writerBid(call, optionId);
} else {
_generalBid(call);
}

// the new high bidder is the beneficial owner of the asset.
// The beneficial owner must be set here instead of with a final bid
// because the ability to
IHookERC721Vault(call.vaultAddress).setBeneficialOwner(msg.sender);

// emit event
emit Bid(optionId, bidAmt, msg.sender);
}

function _writerBid(CallOption storage call, uint256 optionId) internal {
uint256 bidAmt = msg.value + call.strike;

require(bidAmt > call.bid, "bid - bid is lower than the current bid");
require(bidAmt > call.strike, "bid - bid is lower than the strike price");

// return current bidder's money
_safeTransferETHWithFallback(call.highBidder, call.bid);
_returnBidToPreviousBidder(call);

// set the new bidder
call.bid = bidAmt;
call.highBidder = msg.sender;
}

// the new high bidder is the beneficial owner of the asset.
// The beneficial owner must be set here instead of with a final bid
// because the ability to
IHookERC721Vault(call.vaultAddress).setBeneficialOwner(msg.sender);
function _generalBid(CallOption storage call) internal {
uint256 bidAmt = msg.value;

// emit event
emit Bid(optionId, bidAmt, msg.sender);
require(bidAmt > call.bid, "bid - bid is lower than the current bid");
require(bidAmt > call.strike, "bid - bid is lower than the strike price");

_returnBidToPreviousBidder(call);

// set the new bidder
call.bid = bidAmt;
call.highBidder = msg.sender;
}

function _returnBidToPreviousBidder(CallOption storage call) internal {
uint256 unnormalizedHighBid = call.bid;
if (call.highBidder == call.writer) {
unnormalizedHighBid -= call.strike;
}

// return current bidder's money
_safeTransferETHWithFallback(call.highBidder, unnormalizedHighBid);
}

/**
Expand Down Expand Up @@ -338,12 +367,15 @@ contract HookCoveredCallImplV1 is

uint256 spread = call.bid - call.strike;

// If the option writer is the high bidder they don't recieve the strike because they bid on the spread.
if (call.highBidder != call.writer) {
// send option writer the strike price
_safeTransferETHWithFallback(call.writer, call.strike);
}

// return send option holder their earnings
_safeTransferETHWithFallback(ownerOf(optionId), spread);

// send option writer the strike price
_safeTransferETHWithFallback(call.writer, call.strike);

if (returnNft) {
IHookERC721Vault(call.vaultAddress).withdrawalAsset();
}
Expand Down
Loading

0 comments on commit 02cd9c6

Please sign in to comment.