Skip to content

Instantly share code, notes, and snippets.

@bertil291utn
Created June 22, 2022 03:37
Show Gist options
  • Save bertil291utn/65516e387a669b4a08be301cff4326e6 to your computer and use it in GitHub Desktop.
Save bertil291utn/65516e387a669b4a08be301cff4326e6 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.12+commit.f00d7308.js&optimize=false&runs=200&gist=
// SPDX-License-Identifer: MIT
pragma solidity ^0.8.3;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
library PriceConverter{
// contract address ETH/USD price feed on rinkeby testnet
//0x8A753747A1Fa494EC906cE90E9f37563A8AF630e
//https://docs.chain.link/docs/ethereum-addresses/
function getVersion() public view returns(uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
return priceFeed.version();
}
function getPrice() public view returns(uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
(,int256 answer,,,)=priceFeed.latestRoundData();
return uint256(answer);
}
function convert(uint256 fundedAmount) internal view returns(uint256){
//funded amount as WEI (1e18)
uint256 ethPrice=getPrice();
uint256 inUSD=(ethPrice/1e8*fundedAmount)/1e18;
// divide by 1e8 (GWEI->ETH) and the total divide by 1e18(WEI->ETH) to get price in USD
return inUSD;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment