Skip to content

feat: add oracle price fallback for custom-priced tokens #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/external-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,36 @@ const _assembleTxOdosMemoized = memoize(
export async function _assembleTxOdos(this: Llamalend, pathId: string): Promise<string> {
return _assembleTxOdosMemoized(this.constants.ALIASES.leverage_zap, pathId);
}

export const _getOraclePricesFromApi = memoize(
async (network: INetworkName): Promise<IDict<number>> => {
const url = `https://prices.curve.finance/v1/lending/markets/${network}`;
const response = await fetch(url);
if (response.status !== 200) {
return {};
}

const { data } = await response.json() as { data: Array<{
controller: string,
collateral_token: {
symbol: string,
address: string
},
price_oracle: number
}> };

const oraclePrices: IDict<number> = {};

for (const market of data) {
if (market.price_oracle && market.collateral_token && market.collateral_token.address) {
oraclePrices[market.collateral_token.address.toLowerCase()] = market.price_oracle;
}
}

return oraclePrices;
},
{
promise: true,
maxAge: 60 * 1000,
}
);
19 changes: 18 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ethers, BigNumberish, Numeric } from "ethers";
import { Call } from "@curvefi/ethcall";
import BigNumber from 'bignumber.js';
import { ICurveContract, IDict, TGas } from "./interfaces.js";
import { _getUsdPricesFromApi } from "./external-api.js";
import { _getUsdPricesFromApi, _getOraclePricesFromApi } from "./external-api.js";
import type { Llamalend } from "./llamalend.js";
import { JsonFragment } from "ethers/lib.esm";
import { L2Networks } from "./constants/L2Networks.js";
Expand Down Expand Up @@ -370,6 +370,23 @@ export const _getUsdRate = async function (this: Llamalend, assetId: string): Pr
}
}

if (_usdRatesCache[assetId]['rate'] === 0) {
const originalAssetId = arguments[0];
const oraclePrices = await _getOraclePricesFromApi.call(this, this.constants.NETWORK_NAME);

if (originalAssetId.toLowerCase() in oraclePrices) {
const oraclePriceInCrvUsd = oraclePrices[originalAssetId.toLowerCase()];

if (oraclePriceInCrvUsd > 0) {
const crvUsdAddress = this.constants.ALIASES.crvUSD;
const crvUsdPrice = assetId.toLowerCase() === crvUsdAddress.toLowerCase() ? 1 :
await _getUsdRate.call(this, crvUsdAddress);

_usdRatesCache[assetId] = {'rate': oraclePriceInCrvUsd * crvUsdPrice, 'time': Date.now()};
}
}
}

return _usdRatesCache[assetId]['rate']
}

Expand Down