For The 1337 Devs
Smart Contract Overview: Delta
The Delta
contract, an ERC20 token, is the core of the Hedge protocol. It enables users to mint, manage, and redeem options fully onchain with collateral management.
State Variables
morpheus
: An array of oracle addresses to fetch asset prices.IDs
: An array of IDs corresponding to the oracles.collateralToken
: Address of the ERC20 token used as collateral.expiry
: Expiry time of the option contract.strikePrice
: The price at which option can be exercised.priceDec
: Decimals places for the price.currentPrice
: Current price of the asset fetched from the oracle.timestamp
: Latest timestamp of the price update.collateral
: Mapping of address to collateral amount.name
: Name of the token.symbol
: Symbol of the token.
Events
Initialized
: Emitted when the contract is initialized.Minted
: Emitted when new options are minted.Delta
: Emitted when profit is calculated.Unlocked
: Emitted when collateral is unlocked.Redeemed
: Emitted when options are redeemed.PriceUpdated
: Emitted when asset price is updated.
Core Functions
Initialization
function init(
string memory name_,
string memory symbol_,
address[] memory _morpheus,
address _collateralToken,
uint _expiry,
uint _strikePrice,
string memory APIendpoint,
string memory APIendpointPath,
uint256 dec
) public payable
Initialize the contract with specified parameters.
Inputs:
name_
: Name of the option token.symbol_
: Symbol of the option token._morpheus
: Addresses of oracles._collateralToken
: Address of the collateral token._expiry
: Expiry time of options._strikePrice
: Strike price for the option.APIendpoint
: API endpoint for fetching price.APIendpointPath
: Specific path for price data in the API response.dec
: Decimal places for price.
Revert: The collateral token must not have been set previously.
Effects: Sets state variables and requests feed from oracles to begin setup.
Minting Options
function mint(address to, uint amount) public
Allows a user to mint options by depositing collateral.
Inputs:
to
: Address receiving the minted options.amount
: Amount of options to be minted.
Revert: Must be called before the expiry time.
Effects: Transfers collateral, updates collateral mapping, and mints the new options and assigns them to the to address.
Calculating Profit
function getDelta() public view returns (uint profit)
Calculate the profit obtainable if the option is exercised for 1 option token.
Outputs:
profit
: The difference between strike price and current price, if profitable.
Unlocking Collateral
function unlock(uint amount) public
Allows the holder of an option thats also minter to unlock collateral by burning options. Allows for liquidity, minimization of opportunity cost risk.
Inputs:
amount
: Amount of collateral to unlock.
Effects: Reduces collateral, burns options for corresponding amount, and transfers collateral back to the user.
Redeeming Options
function redeem() public
The redeem function allows a holder of the options to redeem their options after the expiry date. The amount of collateral returned depends on the strike price, the current price, and the leverage. Also refunds collateral back to minters for strike - delta.
Requirements: Must be called after the expiry time.
Effects: Calculates payoff, burns options, and transfers payoff and/or collateral back to the holder.
Updating Price
function updatePrice() external returns (uint value)
Update the current price of the asset using oracle.
Outputs:
value
: The updated price.
Requirements: Must be called before expiry and reach a quorum.
Effects: Updates
currentPrice
andtimestamp
state variables.updateFeeds
: Allow updating of oracle feeds.sort
: Sorts an array of integers.quickSort
: A helper function for sorting.decimals
: Overrides ERC20 decimals to return collateral token decimals.
Updating Oracle Feeds
function updateFeeds() external payable
The updateFeeds
function does not take any explicit inputs and must be sent Ether to operate, which is then distributed amongst the oracles in morpheus
as bounties. The function cycles through each oracle in the morpheus
array, assigning each oracle a task with an equal share of the total sent Ether as a bounty, ensuring that the oracles are incentivized to return the requested data. This is crucial for obtaining fresh and accurate data for the updatePrice
function.
Effects:
Iterates through the
morpheus
array, distributing the sent value (msg.value
) evenly as bounties across the oracles.Invokes the
supportFeeds
function on each oracle, supplying the requisite ID and bounty.
Conclusion
Hedge allows the decentralized creation and management of PUT options, providing a mechanism for hedging assets and earning premiums in the DeFi space.
Disclaimer
Engage with smart contracts and platforms understanding the associated risks.x
Last updated