@kamino-finance/klend-sdk 6.0.5-beta.13 → 6.0.5-beta.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ import { KaminoMarket, KaminoObligation } from '../classes';
2
+ import { Commitment, Connection, PublicKey } from '@solana/web3.js';
3
+ import { PubkeyHashMap } from './pubkey';
4
+ export declare function getUserObligationsInMarkets(connection: Connection, user: PublicKey, markets: PubkeyHashMap<PublicKey, KaminoMarket>, slot: number, commitment?: Commitment, programId?: PublicKey): Promise<KaminoObligation[]>;
5
+ //# sourceMappingURL=obligations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"obligations.d.ts","sourceRoot":"","sources":["../../src/utils/obligations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE5D,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACpE,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAKzC,wBAAsB,2BAA2B,CAC/C,UAAU,EAAE,UAAU,EACtB,IAAI,EAAE,SAAS,EACf,OAAO,EAAE,aAAa,CAAC,SAAS,EAAE,YAAY,CAAC,EAC/C,IAAI,EAAE,MAAM,EACZ,UAAU,GAAE,UAAwB,EACpC,SAAS,GAAE,SAAsB,GAChC,OAAO,CAAC,gBAAgB,EAAE,CAAC,CAqD7B"}
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getUserObligationsInMarkets = getUserObligationsInMarkets;
7
+ const classes_1 = require("../classes");
8
+ const pubkey_1 = require("./pubkey");
9
+ const bs58_1 = __importDefault(require("bs58"));
10
+ const programId_1 = require("../idl_codegen/programId");
11
+ const accounts_1 = require("../idl_codegen/accounts");
12
+ async function getUserObligationsInMarkets(connection, user, markets, slot, commitment = 'processed', programId = programId_1.PROGRAM_ID) {
13
+ const obligations = await connection.getProgramAccounts(programId, {
14
+ filters: [
15
+ {
16
+ dataSize: accounts_1.Obligation.layout.span + 8,
17
+ },
18
+ {
19
+ memcmp: {
20
+ offset: 0,
21
+ bytes: bs58_1.default.encode(accounts_1.Obligation.discriminator),
22
+ },
23
+ },
24
+ {
25
+ memcmp: {
26
+ offset: 64,
27
+ bytes: user.toBase58(),
28
+ },
29
+ },
30
+ ],
31
+ commitment,
32
+ });
33
+ const collateralExchangeRates = new pubkey_1.PubkeyHashMap();
34
+ const cumulativeBorrowRates = new pubkey_1.PubkeyHashMap();
35
+ const kaminoObligations = [];
36
+ for (const obligation of obligations) {
37
+ if (!obligation.account.owner.equals(programId)) {
38
+ throw new Error(`Account ${obligation.account.owner} doesn't belong to this program ${programId}`);
39
+ }
40
+ const obligationAccount = accounts_1.Obligation.decode(obligation.account.data);
41
+ if (!obligationAccount) {
42
+ throw Error('Could not parse obligation.');
43
+ }
44
+ const market = markets.get(obligationAccount.lendingMarket);
45
+ if (!market) {
46
+ continue;
47
+ }
48
+ classes_1.KaminoObligation.addRatesForObligation(market, obligationAccount, collateralExchangeRates, cumulativeBorrowRates, slot);
49
+ kaminoObligations.push(new classes_1.KaminoObligation(market, obligation.pubkey, obligationAccount, collateralExchangeRates, cumulativeBorrowRates));
50
+ }
51
+ return kaminoObligations;
52
+ }
53
+ //# sourceMappingURL=obligations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"obligations.js","sourceRoot":"","sources":["../../src/utils/obligations.ts"],"names":[],"mappings":";;;;;AAQA,kEA4DC;AApED,wCAA4D;AAG5D,qCAAyC;AACzC,gDAAwB;AACxB,wDAAsD;AACtD,sDAAqD;AAE9C,KAAK,UAAU,2BAA2B,CAC/C,UAAsB,EACtB,IAAe,EACf,OAA+C,EAC/C,IAAY,EACZ,a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EACjB,uBAAuB,EACvB,qBAAqB,EACrB,IAAI,CACL,CAAC;QACF,iBAAiB,CAAC,IAAI,CACpB,IAAI,0BAAgB,CAAC,MAAM,EAAE,UAAU,CAAC,MAAM,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,qBAAqB,CAAC,CACnH,CAAC;IACJ,CAAC;IACD,OAAO,iBAAiB,CAAC;AAC3B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kamino-finance/klend-sdk",
3
- "version": "6.0.5-beta.13",
3
+ "version": "6.0.5-beta.15",
4
4
  "description": "Typescript SDK for interacting with the Kamino Lending (klend) protocol",
5
5
  "repository": {
6
6
  "type": "git",
@@ -0,0 +1,69 @@
1
+ import { KaminoMarket, KaminoObligation } from '../classes';
2
+ import Decimal from 'decimal.js';
3
+ import { Commitment, Connection, PublicKey } from '@solana/web3.js';
4
+ import { PubkeyHashMap } from './pubkey';
5
+ import bs58 from 'bs58';
6
+ import { PROGRAM_ID } from '../idl_codegen/programId';
7
+ import { Obligation } from '../idl_codegen/accounts';
8
+
9
+ export async function getUserObligationsInMarkets(
10
+ connection: Connection,
11
+ user: PublicKey,
12
+ markets: PubkeyHashMap<PublicKey, KaminoMarket>,
13
+ slot: number,
14
+ commitment: Commitment = 'processed',
15
+ programId: PublicKey = PROGRAM_ID
16
+ ): Promise<KaminoObligation[]> {
17
+ const obligations = await connection.getProgramAccounts(programId, {
18
+ filters: [
19
+ {
20
+ dataSize: Obligation.layout.span + 8,
21
+ },
22
+ {
23
+ memcmp: {
24
+ offset: 0,
25
+ bytes: bs58.encode(Obligation.discriminator),
26
+ },
27
+ },
28
+ {
29
+ memcmp: {
30
+ offset: 64,
31
+ bytes: user.toBase58(),
32
+ },
33
+ },
34
+ ],
35
+ commitment,
36
+ });
37
+
38
+ const collateralExchangeRates = new PubkeyHashMap<PublicKey, Decimal>();
39
+ const cumulativeBorrowRates = new PubkeyHashMap<PublicKey, Decimal>();
40
+ const kaminoObligations: KaminoObligation[] = [];
41
+ for (const obligation of obligations) {
42
+ if (!obligation.account.owner.equals(programId)) {
43
+ throw new Error(`Account ${obligation.account.owner} doesn't belong to this program ${programId}`);
44
+ }
45
+
46
+ const obligationAccount = Obligation.decode(obligation.account.data);
47
+
48
+ if (!obligationAccount) {
49
+ throw Error('Could not parse obligation.');
50
+ }
51
+
52
+ const market = markets.get(obligationAccount.lendingMarket);
53
+ if (!market) {
54
+ continue;
55
+ }
56
+
57
+ KaminoObligation.addRatesForObligation(
58
+ market,
59
+ obligationAccount,
60
+ collateralExchangeRates,
61
+ cumulativeBorrowRates,
62
+ slot
63
+ );
64
+ kaminoObligations.push(
65
+ new KaminoObligation(market, obligation.pubkey, obligationAccount, collateralExchangeRates, cumulativeBorrowRates)
66
+ );
67
+ }
68
+ return kaminoObligations;
69
+ }