diff --git a/package.json b/package.json index 9b7e0a61c..195f92657 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/parachain/oracle.ts b/src/parachain/oracle.ts index 2426eab07..89d283ea7 100644 --- a/src/parachain/oracle.ts +++ b/src/parachain/oracle.ts @@ -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