UniswapPriceOracle
DeFiPIE protocol uses UniswapPriceOracle to get time-weighted average prices from Uniswap v2 or other DEX (clones Uniswap v2).
Every time the user interacts with a pool, the protocol gets the current price from Uniswap and averages it with the previously stored price, thereby achieving the Time-weighted average price effect, which in turn protects the protocol from price manipulation through flash loans.
Updating the price of an asset by the pToken address.
function updateUnderlyingPrice(address pToken) public override returns (uint)
pToken
: pToken address.RETURN
: 0 on success, otherwise an Error code.
UniswapPriceOracle oracle = UniswapPriceOracle(0xABCD...);
PErc20 pToken = PErc20(0x3FDB...);
oracle.updateUnderlyingPrice(address(pToken));
const oracle = UniswapPriceOracle.at(0xABCD...);
const pToken = PErc20.at(0x3FDB...);
oracle.methods.updateUnderlyingPrice(pToken).send({from: ...});
Updating the price of an asset by the asset address.
function update(address asset) public returns (uint)
asset
: asset address.RETURN
: 0 on success, otherwise an Error code.
UniswapPriceOracle oracle = UniswapPriceOracle(0xABCD...);
Erc20 token = Erc20(0x3FDB...);
oracle.update(address(token));
const oracle = UniswapPriceOracle.at(0xABCD...);
const token = Erc20.at(0x3FDB...);
oracle.methods.update(token ).send({from: ...});
Get an average asset price in ETH with 18 decimals of precision.
function getCourseInETH(address asset) public view returns (uint)
asset
: asset address.RETURN
: asset price.
UniswapPriceOracle oracle = UniswapPriceOracle(0xABCD...);
Erc20 token = Erc20(0x3FDB...);
uint price = oracle.getCourseInETH(address(token));
const oracle = UniswapPriceOracle.at(0xABCD...);
const token = Erc20.at(0x3FDB...);
const price = oracle.methods.getCourseInETH(token).call();
Get an average asset price in USD with 18 decimals of precision.
function getPriceInUSD(address asset) public view virtual returns (uint)
asset
: asset address.RETURN
: asset price.
UniswapPriceOracle oracle = UniswapPriceOracle(0xABCD...);
Erc20 token = Erc20(0x3FDB...);
uint price = oracle.getPriceInUSD(address(token));
const oracle = UniswapPriceOracle.at(0xABCD...);
const token = Erc20.at(0x3FDB...);
const price = oracle.methods.getPriceInUSD(token).call();
Last modified 1yr ago