Skip to content

Instantly share code, notes, and snippets.

@Ashar2shahid
Last active March 25, 2023 04:08
Show Gist options
  • Save Ashar2shahid/004ebfa339e081941524516c1392928a to your computer and use it in GitHub Desktop.
Save Ashar2shahid/004ebfa339e081941524516c1392928a to your computer and use it in GitHub Desktop.
Only imports and main functions
pragma solidity ^0.8.17;
import "@api3/contracts/v0.8/interfaces/IProxy.sol";
contract Api3Options {
//Pricefeed proxies
address public ethProxy;
address public linkProxy;
uint ethPrice;
uint linkPrice;
//Precomputing hash of strings
bytes32 ethHash = keccak256(abi.encodePacked("ETH"));
bytes32 linkHash = keccak256(abi.encodePacked("LINK"));
address payable contractAddr;
//Options stored in arrays of structs
struct option {
uint strike; //Price in USD (18 decimal places) option allows buyer to purchase tokens at
uint premium; //Fee in contract token that option writer charges
uint expiry; //Unix timestamp of expiration time
uint amount; //Amount of tokens the option contract is for
bool exercised; //Has option been exercised
bool canceled; //Has option been canceled
uint id; //Unique ID of option, also array index
uint latestCost; //Helper to show last updated cost to exercise
address payable writer; //Issuer of option
address payable buyer; //Buyer of option
}
option[] public ethOpts;
option[] public linkOpts;
//Kovan feeds: https://docs.chain.link/docs/reference-contracts
constructor(address _ethProxy, address _linkProxy) public {
//ETH/USD Proxy on Goerli
ethProxy = _ethProxy
//LINK/USD Proxy on Goerli
linkProxy = _linkProxy
contractAddr = payable(address(this));
}
//Returns the latest ETH price
function getEthPrice() public view returns (uint) {
(int224 value,uint32 timestamp) = IProxy(ethProxy).read();
// if the data feed is being updated with a one day-heartbeat
// interval, you may want to check for that.
require(
timestamp + 1 days > block.timestamp,
"Timestamp older than one day"
);
//Price should never be negative thus cast int to unit is ok
//Price is 18 decimal places
return uint(uint224(value));
}
//Returns the latest LINK price
function getLinkPrice() public view returns (uint) {
(int224 value,uint32 timestamp) = IProxy(linkProxy).read();
// if the data feed is being updated with a one day-heartbeat
// interval, you may want to check for that.
require(
timestamp + 1 days > block.timestamp,
"Timestamp older than one day"
);
//Price should never be negative thus cast int to unit is ok
//Price is 18 decimal places
return uint(uint224(value));
}
//Updates prices to latest
function updatePrices() internal {
ethPrice = getEthPrice();
linkPrice = getLinkPrice();
}
-----------------------
-----------------------
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment