Last active
March 25, 2023 03:54
-
-
Save Ashar2shahid/fe7004c8abe8277acc404c693f59c17e to your computer and use it in GitHub Desktop.
Only imports and necessary functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.8.17; | |
import "https://github.com/smartcontractkit/chainlink/blob/develop/evm-contracts/src/v0.6/interfaces/LinkTokenInterface.sol"; | |
import "https://github.com/smartcontractkit/chainlink/blob/master/evm-contracts/src/v0.6/interfaces/AggregatorV3Interface.sol"; | |
contract chainlinkOptions { | |
//Pricefeed interfaces | |
AggregatorV3Interface internal ethFeed; | |
AggregatorV3Interface internal linkFeed; | |
//Interface for LINK token functions | |
LinkTokenInterface internal LINK; | |
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() public { | |
//ETH/USD Kovan feed | |
ethFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331); | |
//LINK/USD Kovan feed | |
linkFeed = AggregatorV3Interface(0x396c5E36DD0a0F5a5D33dae44368D4193f69a1F0); | |
//LINK token address on Kovan | |
LINK = LinkTokenInterface(0xa36085F69e2889c224210F603D836748e7dC0088); | |
contractAddr = payable(address(this)); | |
} | |
//Returns the latest ETH price | |
function getEthPrice() public view returns (uint) { | |
( | |
uint80 roundID, | |
int price, | |
uint startedAt, | |
uint timeStamp, | |
uint80 answeredInRound | |
) = ethFeed.latestRoundData(); | |
// If the round is not complete yet, timestamp is 0 | |
require(timeStamp > 0, "Round not complete"); | |
//Price should never be negative thus cast int to unit is ok | |
//Price is 8 decimal places and will require 1e10 correction later to 18 places | |
return uint(price); | |
} | |
//Returns the latest LINK price | |
function getLinkPrice() public view returns (uint) { | |
( | |
uint80 roundID, | |
int price, | |
uint startedAt, | |
uint timeStamp, | |
uint80 answeredInRound | |
) = linkFeed.latestRoundData(); | |
// If the round is not complete yet, timestamp is 0 | |
require(timeStamp > 0, "Round not complete"); | |
//Price should never be negative thus cast int to unit is ok | |
//Price is 8 decimal places and will require 1e10 correction later to 18 places | |
return uint(price); | |
} | |
//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