UniswapPriceOracle

Introduction

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.

Update price by pToken

Updating the price of an asset by the pToken address.

UniswapPriceOracle

function updateUnderlyingPrice(address pToken) public override returns (uint)
  • pToken: pToken address.

  • RETURN: 0 on success, otherwise an Error code.

Solidity

UniswapPriceOracle oracle = UniswapPriceOracle(0xABCD...);
PErc20 pToken = PErc20(0x3FDB...);
oracle.updateUnderlyingPrice(address(pToken));

Web3 1.0

const oracle = UniswapPriceOracle.at(0xABCD...);
const pToken = PErc20.at(0x3FDB...);
oracle.methods.updateUnderlyingPrice(pToken).send({from: ...});

Update price by an asset.

Updating the price of an asset by the asset address.

UniswapPriceOracle

function update(address asset) public returns (uint)
  • asset: asset address.

  • RETURN: 0 on success, otherwise an Error code.

Solidity

UniswapPriceOracle oracle = UniswapPriceOracle(0xABCD...);
Erc20 token = Erc20(0x3FDB...);
oracle.update(address(token));

Web3 1.0

const oracle = UniswapPriceOracle.at(0xABCD...);
const token = Erc20.at(0x3FDB...);
oracle.methods.update(token ).send({from: ...});

Get asset price in ETH

Get an average asset price in ETH with 18 decimals of precision.

UniswapPriceOracle

function getCourseInETH(address asset) public view returns (uint)
  • asset: asset address.

  • RETURN: asset price.

Solidity

UniswapPriceOracle oracle = UniswapPriceOracle(0xABCD...);
Erc20 token = Erc20(0x3FDB...);
uint price = oracle.getCourseInETH(address(token));

Web3 1.0

const oracle = UniswapPriceOracle.at(0xABCD...);
const token = Erc20.at(0x3FDB...);
const price = oracle.methods.getCourseInETH(token).call();

Get asset price in USD

Get an average asset price in USD with 18 decimals of precision.

UniswapPriceOracle

function getPriceInUSD(address asset) public view virtual returns (uint)
  • asset: asset address.

  • RETURN: asset price.

Solidity

UniswapPriceOracle oracle = UniswapPriceOracle(0xABCD...);
Erc20 token = Erc20(0x3FDB...);
uint price = oracle.getPriceInUSD(address(token));

Web3 1.0

const oracle = UniswapPriceOracle.at(0xABCD...);
const token = Erc20.at(0x3FDB...);
const price = oracle.methods.getPriceInUSD(token).call();