Aave V4 Exchange Rates
Exchange rates in Aave are provided by Oracles using Chainlink price feeds.
AaveKit provides current conversion rates between ERC-20 tokens, native tokens, and supported currencies: USD, EUR, and GBP. For smart-contract integrations, query USD reserve prices directly from the spoke oracle.
Fetching Exchange Rates
- React: Use the
usehook for live exchange rates with automatic polling every 10 seconds, or the imperativeExchange Rate usehook for on-demand exchange rates.Exchange Rate Action - TypeScript: Use the
exchangeaction, and in GraphQL, theRate exchangequery to fetch conversion rates.Rate
import { type ExchangeRateRequest, useExchangeRate } from "@aave/react";
function ExchangeRate({ request }: { request: ExchangeRateRequest }) {
const { data, loading, error } = useExchangeRate(request);
if (loading) return <div>Loading…</div>;
if (error) return <div>Error: {error.message}</div>;
// data: ExchangeAmount
return (
<span>
{data.symbol}
{data.value.toFixed(2)}
</span>
);
}
import { type ExchangeRateRequest, useExchangeRate } from "@aave/react";
function ExchangeRate({ request }: { request: ExchangeRateRequest }) {
const { data } = useExchangeRate({
...request,
suspense: true,
});
// data: ExchangeAmount
return (
<span>
{data.symbol}
{data.value.toFixed(2)}
</span>
);
}
import { type ExchangeRateRequest, useExchangeRateAction } from "@aave/react";
// …
const [fetch, { called, data, error, loading }] = useExchangeRateAction();
const handler = async (request: ExchangeRateRequest) => {
const result = await fetch(request);
if (result.isOk()) {
console.log(result.value); // ExchangeAmount
} else {
console.error(result.error);
}
};
import { exchangeRate } from "@aave/client/actions";
import { evmAddress, chainId, Currency } from "@aave/client";
import { client } from "./client";
const result = await exchangeRate(client, {
from: {
erc20: {
chainId: chainId(1),
address: evmAddress("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
},
},
to: Currency.Usd,
});
if (result.isOk()) {
// result.value: ExchangeAmount
console.log("Exchange rate:", result.value);
} else {
console.error("Error:", result.error);
}
import { exchangeRate } from "@aave/client/actions";
import { chainId, Currency } from "@aave/client";
import { client } from "./client";
const result = await exchangeRate(client, {
from: {
native: chainId(1),
},
to: Currency.Usd,
});
if (result.isOk()) {
// result.value: ExchangeAmount
console.log("Exchange rate:", result.value);
} else {
console.error("Error:", result.error);
}
import { exchangeRate } from "@aave/client/actions";
import { Currency } from "@aave/client";
import { client } from "./client";
const result = await exchangeRate(client, {
from: {
fiat: Currency.Eur,
},
to: Currency.Usd,
});
if (result.isOk()) {
// result.value: ExchangeAmount
console.log("Exchange rate:", result.value);
} else {
console.error("Error:", result.error);
}
import { AaveClient } from "@aave/client";
export const client = AaveClient.create();
query ExchangeRate($request: ExchangeRateRequest!) {
exchangeRate(request: $request) {
value
name
symbol
}
}
Example request objects for React and GraphQL:
import { evmAddress, chainId, Currency } from "@aave/react";
const request: ExchangeRateRequest = {
from: {
erc20: {
chainId: chainId(1),
address: evmAddress("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
},
},
to: Currency.Usd,
};
import { chainId, Currency } from "@aave/react";
const request: ExchangeRateRequest = {
from: {
native: chainId(1),
},
to: Currency.Usd,
};
import { Currency } from "@aave/react";
const request: ExchangeRateRequest = {
from: {
fiat: Currency.Eur,
},
to: Currency.Usd,
};
{
"request": {
"from": {
"erc20": {
"chainId": 1,
"address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
}
},
"to": "USD"
}
}
{
"request": {
"from": {
"native": 1
},
"to": "USD"
}
}
{
"request": {
"from": {
"fiat": "EUR"
},
"to": "USD"
}
}
In Solidity, start with the spoke address:
address spokeAddress = 0x123…;
Then, get the oracle address from the spoke and query the price.
import { ISpoke } from "aave-v4/src/spoke/interfaces/ISpoke.sol";
import { IAaveOracle } from "aave-v4/src/spoke/interfaces/IAaveOracle.sol";
address oracleAddress = ISpoke(spokeAddress).ORACLE();
IAaveOracle oracle = IAaveOracle(oracleAddress);
uint256 reserveId = 1;
uint256 price = oracle.getReservePrice(reserveId);
import { ISpoke } from "aave-v4/src/spoke/interfaces/ISpoke.sol";
import { IAaveOracle } from "aave-v4/src/spoke/interfaces/IAaveOracle.sol";
address oracleAddress = ISpoke(spokeAddress).ORACLE();
IAaveOracle oracle = IAaveOracle(oracleAddress);
uint256[] memory reserveIds = new uint256[](2);
reserveIds[0] = 1;
reserveIds[1] = 2;
uint256[] memory prices = oracle.getReservesPrices(reserveIds);