Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow call option writers to bid on spread #3

Merged
Merged
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
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