Skip to content

Instantly share code, notes, and snippets.

@RiccardoBiosas
Created September 23, 2021 18:14
Show Gist options
  • Save RiccardoBiosas/297eef4ff2579777fb911deb4688c001 to your computer and use it in GitHub Desktop.
Save RiccardoBiosas/297eef4ff2579777fb911deb4688c001 to your computer and use it in GitHub Desktop.
it("should execute the full margin option with the underlying's market price less than the strike price", async () => {
const { buyer, oracle } = namedSigners;
/**
* @dev time-travel after the maturity of the derivative
*/
await timeTravel(SECONDS_40_MINS + 100);
/**
* @dev manually pushes the current underlying's market price to the oracle contract
*/
await adminOracleController.connect(oracle).__callback(fullMarginOption.derivative.endTime, fullMarginOption.price);
/**
* @dev seller and buyer approve the execution of the derivative from a third-party (optionController)
*/
await optionCallMock.connect(optionSeller).allowThirdpartyExecution(true);
await optionCallMock.connect(buyer).allowThirdpartyExecution(true);
const buyerBalanceBefore = await dai.balanceOf(buyer.address);
const sellerBalanceBefore = await dai.balanceOf(daiRichEOA);
/**
* @dev calculates the buyer/seller payout for a given underlying's market price
*/
const [buyerPayout, sellerPayout] = await optionCallMock.getExecutionPayout(
fullMarginOption.derivative,
fullMarginOption.price,
);
/**
* @dev buyer and seller execute their LONG/SHORT positions
*/
await optionController.connect(optionSeller).executeShort(fullMarginOption.amount);
await optionController.connect(buyer).executeLong(fullMarginOption.amount);
const buyerBalanceAfter = await dai.balanceOf(buyer.address);
const sellerBalanceAfter = await dai.balanceOf(daiRichEOA);
/**
* @dev underlying's market price is less than the strike price so the seller receives the calculated payout
*/
expect(buyerPayout, "wrong buyer payout").to.be.equal(0);
expect(sellerPayout.mul(fullMarginOption.amount), "wrong seller payout").to.be.equal(
fullMarginOption.derivative.margin.mul(fullMarginOption.amount),
);
expect(sellerBalanceAfter, "wrong seller balance").to.be.equal(
sellerBalanceBefore.add(sellerPayout.mul(fullMarginOption.amount)),
);
expect(buyerBalanceAfter, "wrong buyer balance").to.be.equal(
buyerBalanceBefore.add(buyerPayout.mul(fullMarginOption.amount)),
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment