Skip to content

Commit

Permalink
Merge pull request #631 from peterslany/peter/loans-get-lend-token-ex…
Browse files Browse the repository at this point in the history
…change-rates

feat(loans): add method to get lendtoken exchange rates
  • Loading branch information
peterslany authored May 15, 2023
2 parents 5c27de9 + 1d24a7a commit 8a30b73
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
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.2.1",
"version": "2.2.2",
"description": "JavaScript library to interact with interBTC",
"main": "build/src/index.js",
"typings": "build/src/index.d.ts",
Expand Down
21 changes: 21 additions & 0 deletions src/parachain/loans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ export interface LoansAPI {
*/
getLendTokens(): Promise<Array<LendToken>>;

/**
* @return Exchange rates for underlying currency -> lend token.
* Representing amount of lend token equal to 1 of underlying currency.
*/
getLendTokenExchangeRates(): Promise<TickerToData<Big>>;

/**
* Get accrued subsidy rewards amounts for the account.
*
Expand Down Expand Up @@ -306,6 +312,21 @@ export class DefaultLoansAPI implements LoansAPI {
);
}

async getLendTokenExchangeRates(): Promise<TickerToData<Big>> {
const exchangeRateEntries = await this.api.query.loans.exchangeRate.entries();
const underlyingCurrencies = await Promise.all(
exchangeRateEntries.map(([key]) => currencyIdToMonetaryCurrency(this.api, storageKeyToNthInner(key)))
);

return exchangeRateEntries.reduce((exchangeRates, [, rate], index) => {
const underlyingCurrencyTicker = underlyingCurrencies[index].ticker;
return {
...exchangeRates,
[underlyingCurrencyTicker]: decodeFixedPointType(rate),
};
}, {});
}

async getBorrowerAccountIds(): Promise<Array<AccountId>> {
const accountBorrows = await this.api.query.loans.accountBorrows.entries();
return [
Expand Down

0 comments on commit 8a30b73

Please sign in to comment.