GithubHelp home page GithubHelp logo

freedreamer-crypto / ethereumoptions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from samm-source/ethereumoptions

0.0 1.0 0.0 23.53 MB

Decentralized option contracts on Ethereum

JavaScript 100.00%

ethereumoptions's Introduction

ETH Options

Table of Contents

Overview

ETH options are decentralized option contracts for Ether run on Ethereum.

Suppose Alice wants to write a put for ETH at a 300 DAI strike and an expiration of one month. Put options are simply an obligation to buy an asset at a predefined price, so she deposits 300 DAI into the ETH option contract to enforce her obligation. Alice has her freshly written put option, so she sells it to a buyer for a premium. Alice got x from the put premium, of course, with the caveat that she must buy ETH at 300 DAI at any point before expiration.

The buyer of the put option, Bob, can sell 1 ETH for 300 DAI to the put option contract at any point before expiration (aka exercising the put option). The put option that Bob bought is an ERC20 token, with all of its implied functionality.

After expiration, Alice claims her assets back from the options contract. She either gets DAI, ETH, or a mix of the two depending on buyers exercise behavior.

Factory

The factory contract is used to originate option contracts for a specific token (just ETH right now).

Each option contract created by the factory is listed by its expiration timestamp and strike. The expiration timestamp maps to a map of strikes and their respective option contract addresses. This is purposely designed to be similar to clicking on an expiration and seeing the listed strikes for a given stock which traders should be used to.

mapping(uint256 => mapping(uint256 => address)) private _call_option_contracts;
mapping(uint256 => mapping(uint256 => address)) private _put_option_contracts;

function callOptionContract(uint256 expiration_timestamp, uint256 strike) 
  public 
  view 
  returns (address) 
{
  return _call_option_contracts[expiration_timestamp][strike];
}
    
function putOptionContract(uint256 expiration_timestamp, uint256 strike) 
  public 
  view 
  returns (address) 
{
  return _put_option_contracts[expiration_timestamp][strike];
}

Creating an Option Contract

To create an option contract simply call the respective function in the Factory contract and specify the expiration timestamp and strike price (always in DAI). On success, the option will be listed in the Factory's respective map.

function createCallOptionContract(uint256 expiration_timestamp, uint256 strike) 
  public 
  returns (bool success);
  
function createPutOptionContract(uint256 expiration_timestamp, uint256 strike) 
  public 
  returns (bool success);

And that's all there is to the factory contract!

Options

The option contract controls the writing, exercise, and ERC20 functionality of each option created by the factory.

There are two types of option contracts: one for a call, and one for a put. Their interface is almost exactly the same, however, the logic is specific to the type of contract (an exercise of a put is obvously different from that of a call).

Writing an Option

When you write an option, option tokens are created on a one-to-one basis to the amount that you supply. You can then sell these option tokens on a DEX for a premium, and the buyer now has the right to exercise against your contributed collateral.

First find the correct address for the contract type, expiration timestamp, and strike you want from the factory contract. For both a call and put option you will call the writeOption function, however, there are different parameters depending on the type.

Calls

For a call contract, you will simply send along the amount of ETH (in wei) you want to write the option for.

function writeOption() public payable beforeExpiration returns (bool success);

Puts

For a put contract, you pass the amount of ETH (in wei) you want to write for. But first you must approve the contract to be able to transfer (amount * strike) from your DAI balance. Since writing a put means you have the obligation to buy ETH at the strike, the collateral is in DAI here, not ETH.

function writeOption(uint256 amount) public beforeExpiration returns (bool success);

Selling/Transfering Option Rights

As mentioned earlier, after writing an option you will have option tokens that are on a one-to-one basis with the ETH amount written. This is to make it easy for everyone to keep track of their balance. Please keep in mind when interacting with the contract that ETH is denominated in wei. These option tokens are under the ERC20 standard and as such can be sold and transferred by the writers.

contract CETHCallOption is ERC20, ERC20Detailed
contract CETHPutOption is ERC20, ERC20Detailed

The writers always have ownership of the collateral in the contract in proportion to the amount they supplied. Ownership of the collateral is currently untransferrable, but this could be changed in future versions.

Exercising Option Rights

Owners of the option tokens have the right to exercise at any point before expiration.

Calls

To exercise a call, pass along the exercisor (an approved entity of the option tokens can exercise on the owners behalf) and the amount of ETH (in wei) to exercise for. The exercisor must have approved the option contract to access (amount * strike) of its DAI balance. The contract will then transfer the DAI to itself and send the amount in ETH to the exercisor.

function exerciseOption(address payable exercisor, uint256 amount) public beforeExpiration returns (bool success);

Puts

To exercise a put, pass along the exercisor (an approved entity of the option tokens can exercise on the owners behalf) and send the amount of ETH (in wei) to exercise for. The contract will then transfer the (amount * strike) in DAI to the exercisor.

function exerciseOption(address exercisor) public payable beforeExpiration returns (bool success);

Claiming Contributed Collateral

After expiration, option writers can claim their proportion of the collateral. The writer either gets DAI, ETH, or a mix of the two depending on all buyers exercise behavior. Theoretically, the options shouldn't be exercised until the last day (at least), but they can be exercised whenever before expiration. The function is the same for both call and put option contracts.

Calls

function claimContribution() public afterExpiration returns (bool success);

Puts

function claimContribution() public afterExpiration returns (bool success);

ethereumoptions's People

Contributors

samm-source avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.