# 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();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.defipie.com/uniswappriceoracle.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
