Skip to content

Commit

Permalink
Merge pull request #655 from interlay/peter/fix-qtoken-exchange-rate
Browse files Browse the repository at this point in the history
Peter/fix qtoken exchange rate
  • Loading branch information
peterslany authored Jul 11, 2023
2 parents 074278b + 640b222 commit 4d699de
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@interlay/interbtc-api",
"version": "2.3.5",
"version": "2.3.6",
"description": "JavaScript library to interact with interBTC",
"main": "build/src/index.js",
"typings": "build/src/index.d.ts",
Expand Down
21 changes: 15 additions & 6 deletions src/parachain/oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,23 @@ export class DefaultOracleAPI implements OracleAPI {

if (isLendToken(currency)) {
const lendTokenId = newCurrencyId(this.api, currency);
const underlyingCcyId = await this.api.query.loans.underlyingAssetId(lendTokenId);

const rawLendToUnderlying = (await this.api.query.loans.exchangeRate(underlyingCcyId)) as UnsignedFixedPoint;
const underlyingCurrencyIdRaw = await this.api.query.loans.underlyingAssetId(lendTokenId);
if (underlyingCurrencyIdRaw.isNone) {
throw new Error(
`No underlying asset found for lend token with id ${lendTokenId.asLendToken.toString()}`
);
}
const underlyingCurrencyId = underlyingCurrencyIdRaw.unwrap();

const rawLendToUnderlying = await this.api.query.loans.exchangeRate(underlyingCurrencyId);

// multiply this rate with lendtoken to get underlying amount
const lendToUnderRate = decodeFixedPointType(rawLendToUnderlying);

const underlyingCurrency = await currencyIdToMonetaryCurrency(this.api, underlyingCcyId.unwrap());
const btcToUnderRate = (await this.convertWrappedToCurrency(new MonetaryAmount(Bitcoin, 1), underlyingCurrency)).toBig();

const underlyingCurrency = await currencyIdToMonetaryCurrency(this.api, underlyingCurrencyIdRaw.unwrap());
const btcToUnderRate = (
await this.convertWrappedToCurrency(new MonetaryAmount(Bitcoin, 1), underlyingCurrency)
).toBig();
const btcToLendRate = btcToUnderRate.div(lendToUnderRate);

// final rate is normalized (base unit vs base unit), construct accordingly
Expand Down

0 comments on commit 4d699de

Please sign in to comment.