@exponent-labs/kamino-reserve-deserializer 0.0.3

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.
Files changed (71) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/build/fraction.d.ts +23 -0
  3. package/build/fraction.js +61 -0
  4. package/build/fraction.js.map +1 -0
  5. package/build/index.d.ts +102 -0
  6. package/build/index.js +214 -0
  7. package/build/index.js.map +1 -0
  8. package/build/types/BigFractionBytes.d.ts +26 -0
  9. package/build/types/BigFractionBytes.js +72 -0
  10. package/build/types/BigFractionBytes.js.map +1 -0
  11. package/build/types/BorrowRateCurve.d.ts +27 -0
  12. package/build/types/BorrowRateCurve.js +63 -0
  13. package/build/types/BorrowRateCurve.js.map +1 -0
  14. package/build/types/CurvePoint.d.ts +25 -0
  15. package/build/types/CurvePoint.js +68 -0
  16. package/build/types/CurvePoint.js.map +1 -0
  17. package/build/types/LastUpdate.d.ts +46 -0
  18. package/build/types/LastUpdate.js +88 -0
  19. package/build/types/LastUpdate.js.map +1 -0
  20. package/build/types/PriceHeuristic.d.ts +40 -0
  21. package/build/types/PriceHeuristic.js +81 -0
  22. package/build/types/PriceHeuristic.js.map +1 -0
  23. package/build/types/PythConfiguration.d.ts +24 -0
  24. package/build/types/PythConfiguration.js +64 -0
  25. package/build/types/PythConfiguration.js.map +1 -0
  26. package/build/types/ReserveCollateral.d.ts +52 -0
  27. package/build/types/ReserveCollateral.js +101 -0
  28. package/build/types/ReserveCollateral.js.map +1 -0
  29. package/build/types/ReserveConfig.d.ts +366 -0
  30. package/build/types/ReserveConfig.js +307 -0
  31. package/build/types/ReserveConfig.js.map +1 -0
  32. package/build/types/ReserveFees.d.ts +77 -0
  33. package/build/types/ReserveFees.js +98 -0
  34. package/build/types/ReserveFees.js.map +1 -0
  35. package/build/types/ReserveLiquidity.d.ts +181 -0
  36. package/build/types/ReserveLiquidity.js +214 -0
  37. package/build/types/ReserveLiquidity.js.map +1 -0
  38. package/build/types/ScopeConfiguration.d.ts +40 -0
  39. package/build/types/ScopeConfiguration.js +82 -0
  40. package/build/types/ScopeConfiguration.js.map +1 -0
  41. package/build/types/SwitchboardConfiguration.d.ts +29 -0
  42. package/build/types/SwitchboardConfiguration.js +70 -0
  43. package/build/types/SwitchboardConfiguration.js.map +1 -0
  44. package/build/types/TokenInfo.d.ts +121 -0
  45. package/build/types/TokenInfo.js +154 -0
  46. package/build/types/TokenInfo.js.map +1 -0
  47. package/build/types/WithdrawalCaps.d.ts +37 -0
  48. package/build/types/WithdrawalCaps.js +90 -0
  49. package/build/types/WithdrawalCaps.js.map +1 -0
  50. package/build/types/index.d.ts +46 -0
  51. package/build/types/index.js +88 -0
  52. package/build/types/index.js.map +1 -0
  53. package/package.json +20 -0
  54. package/src/fraction.ts +70 -0
  55. package/src/index.ts +269 -0
  56. package/src/types/BigFractionBytes.ts +59 -0
  57. package/src/types/BorrowRateCurve.ts +53 -0
  58. package/src/types/CurvePoint.ts +58 -0
  59. package/src/types/LastUpdate.ts +88 -0
  60. package/src/types/PriceHeuristic.ts +76 -0
  61. package/src/types/PythConfiguration.ts +54 -0
  62. package/src/types/ReserveCollateral.ts +103 -0
  63. package/src/types/ReserveConfig.ts +431 -0
  64. package/src/types/ReserveFees.ts +116 -0
  65. package/src/types/ReserveLiquidity.ts +280 -0
  66. package/src/types/ScopeConfiguration.ts +83 -0
  67. package/src/types/SwitchboardConfiguration.ts +62 -0
  68. package/src/types/TokenInfo.ts +178 -0
  69. package/src/types/WithdrawalCaps.ts +84 -0
  70. package/src/types/index.ts +88 -0
  71. package/tsconfig.json +18 -0
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.LastUpdate = void 0;
30
+ const bn_js_1 = __importDefault(require("bn.js")); // eslint-disable-line @typescript-eslint/no-unused-vars
31
+ const borsh = __importStar(require("@coral-xyz/borsh"));
32
+ /** Last update state */
33
+ class LastUpdate {
34
+ /** Last slot when updated */
35
+ slot;
36
+ /** True when marked stale, false when slot updated */
37
+ stale;
38
+ /** Status of the prices used to calculate the last update */
39
+ priceStatus;
40
+ placeholder;
41
+ constructor(fields) {
42
+ this.slot = fields.slot;
43
+ this.stale = fields.stale;
44
+ this.priceStatus = fields.priceStatus;
45
+ this.placeholder = fields.placeholder;
46
+ }
47
+ static layout(property) {
48
+ return borsh.struct([borsh.u64("slot"), borsh.u8("stale"), borsh.u8("priceStatus"), borsh.array(borsh.u8(), 6, "placeholder")], property);
49
+ }
50
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
51
+ static fromDecoded(obj) {
52
+ return new LastUpdate({
53
+ slot: obj.slot,
54
+ stale: obj.stale,
55
+ priceStatus: obj.priceStatus,
56
+ placeholder: obj.placeholder,
57
+ });
58
+ }
59
+ static toEncodable(fields) {
60
+ return {
61
+ slot: fields.slot,
62
+ stale: fields.stale,
63
+ priceStatus: fields.priceStatus,
64
+ placeholder: fields.placeholder,
65
+ };
66
+ }
67
+ toJSON() {
68
+ return {
69
+ slot: this.slot.toString(),
70
+ stale: this.stale,
71
+ priceStatus: this.priceStatus,
72
+ placeholder: this.placeholder,
73
+ };
74
+ }
75
+ static fromJSON(obj) {
76
+ return new LastUpdate({
77
+ slot: new bn_js_1.default(obj.slot),
78
+ stale: obj.stale,
79
+ priceStatus: obj.priceStatus,
80
+ placeholder: obj.placeholder,
81
+ });
82
+ }
83
+ toEncodable() {
84
+ return LastUpdate.toEncodable(this);
85
+ }
86
+ }
87
+ exports.LastUpdate = LastUpdate;
88
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAAsB,CAAC,wDAAwD;AAC/E,wDAAyC;AAsBzC,wBAAwB;AACxB,MAAa,UAAU;IACrB,6BAA6B;IACpB,IAAI,CAAI;IACjB,sDAAsD;IAC7C,KAAK,CAAQ;IACtB,6DAA6D;IACpD,WAAW,CAAQ;IACnB,WAAW,CAAe;IAEnC,YAAY,MAAwB;QAClC,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAA;QACvB,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAA;QACzB,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,CAAA;QACrC,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,CAAA;IACvC,CAAC;IAED,MAAM,CAAC,MAAM,CAAC,QAAiB;QAC7B,OAAO,KAAK,CAAC,MAAM,CACjB,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,KAAK,CAAC,EAAE,CAAC,OAAO,CAAC,EAAE,KAAK,CAAC,EAAE,CAAC,aAAa,CAAC,EAAE,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,aAAa,CAAC,CAAC,EAC1G,QAAQ,CACT,CAAA;IACH,CAAC;IAED,8DAA8D;IAC9D,MAAM,CAAC,WAAW,CAAC,GAAQ;QACzB,OAAO,IAAI,UAAU,CAAC;YACpB,IAAI,EAAE,GAAG,CAAC,IAAI;YACd,KAAK,EAAE,GAAG,CAAC,KAAK;YAChB,WAAW,EAAE,GAAG,CAAC,WAAW;YAC5B,WAAW,EAAE,GAAG,CAAC,WAAW;SAC7B,CAAC,CAAA;IACJ,CAAC;IAED,MAAM,CAAC,WAAW,CAAC,MAAwB;QACzC,OAAO;YACL,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,KAAK,EAAE,MAAM,CAAC,KAAK;YACnB,WAAW,EAAE,MAAM,CAAC,WAAW;YAC/B,WAAW,EAAE,MAAM,CAAC,WAAW;SAChC,CAAA;IACH,CAAC;IAED,MAAM;QACJ,OAAO;YACL,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;YAC1B,KAAK,EAAE,IAAI,CAAC,KAAK;YACjB,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,WAAW,EAAE,IAAI,CAAC,WAAW;SAC9B,CAAA;IACH,CAAC;IAED,MAAM,CAAC,QAAQ,CAAC,GAAmB;QACjC,OAAO,IAAI,UAAU,CAAC;YACpB,IAAI,EAAE,IAAI,eAAE,CAAC,GAAG,CAAC,IAAI,CAAC;YACtB,KAAK,EAAE,GAAG,CAAC,KAAK;YAChB,WAAW,EAAE,GAAG,CAAC,WAAW;YAC5B,WAAW,EAAE,GAAG,CAAC,WAAW;SAC7B,CAAC,CAAA;IACJ,CAAC;IAED,WAAW;QACT,OAAO,UAAU,CAAC,WAAW,CAAC,IAAI,CAAC,CAAA;IACrC,CAAC;CACF;AA/DD,gCA+DC"}
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@exponent-labs/kamino-reserve-deserializer",
3
+ "version": "0.0.3",
4
+ "main": "build/index.js",
5
+ "types": "build/index.d.ts",
6
+ "license": "AGPL-3.0",
7
+ "scripts": {
8
+ "build": "tsc --build"
9
+ },
10
+ "dependencies": {
11
+ "@coral-xyz/anchor": "0.28.0",
12
+ "@coral-xyz/borsh": "^0.28.0",
13
+ "bn.js": "^5.2.1",
14
+ "decimal.js": "^10.4.3"
15
+ },
16
+ "devDependencies": {
17
+ "typescript": "5.4.5"
18
+ },
19
+ "gitHead": "209b8847e9a0fadb5b5ec96b9b47f0ace4a3bf9d"
20
+ }
@@ -0,0 +1,70 @@
1
+ import BN from "bn.js"
2
+ import Decimal from "decimal.js"
3
+
4
+ export class Fraction {
5
+ static MAX_SIZE_F = 128
6
+ static MAX_SIZE_BF = 256
7
+ static FRACTIONS = 60
8
+ static MULTIPLIER_NUMBER = Math.pow(2, Fraction.FRACTIONS)
9
+
10
+ static MAX_BN = new BN(2).pow(new BN(Fraction.MAX_SIZE_BF)).sub(new BN(1))
11
+ static MIN_BN = new BN(0)
12
+
13
+ valueSf: BN
14
+
15
+ constructor(valueSf: BN) {
16
+ if (valueSf.lt(Fraction.MIN_BN) || valueSf.gt(Fraction.MAX_BN)) {
17
+ throw new Error("Number out of range")
18
+ }
19
+
20
+ this.valueSf = valueSf
21
+ }
22
+
23
+ toDecimal(): Decimal {
24
+ return new Decimal(this.valueSf.toString()).div(Fraction.MULTIPLIER_NUMBER)
25
+ }
26
+
27
+ static fromDecimal(n: Decimal): Fraction {
28
+ const MULTIPLIER_DECIMAL = new Decimal(Fraction.MULTIPLIER_NUMBER.toString())
29
+ const scaledDecimal = n.mul(MULTIPLIER_DECIMAL).round()
30
+ const scaledValue = new BN(scaledDecimal.toString())
31
+
32
+ return new Fraction(scaledValue)
33
+ }
34
+
35
+ static fromBps(n: Decimal): Fraction {
36
+ const decimal = n.div(10000)
37
+ return Fraction.fromDecimal(decimal)
38
+ }
39
+
40
+ static fromPercent(n: number): Fraction {
41
+ const decimal = new Decimal(n).div(100)
42
+ return Fraction.fromDecimal(decimal)
43
+ }
44
+
45
+ getValue(): BN {
46
+ return this.valueSf
47
+ }
48
+
49
+ gt(x: Fraction): boolean {
50
+ return this.valueSf.gt(x.getValue())
51
+ }
52
+
53
+ lt(x: Fraction): boolean {
54
+ return this.valueSf.lt(x.getValue())
55
+ }
56
+
57
+ gte(x: Fraction): boolean {
58
+ return this.valueSf.gte(x.getValue())
59
+ }
60
+
61
+ lte(x: Fraction): boolean {
62
+ return this.valueSf.lte(x.getValue())
63
+ }
64
+
65
+ eq(x: Fraction): boolean {
66
+ return this.valueSf.eq(x.getValue())
67
+ }
68
+ }
69
+
70
+ export const ZERO_FRACTION = new Fraction(new BN(0))
package/src/index.ts ADDED
@@ -0,0 +1,269 @@
1
+ import { PublicKey, Connection } from "@solana/web3.js"
2
+ import BN from "bn.js" // eslint-disable-line @typescript-eslint/no-unused-vars
3
+ import * as borsh from "@coral-xyz/borsh" // eslint-disable-line @typescript-eslint/no-unused-vars
4
+ import { LastUpdate, LastUpdateFields, LastUpdateJSON } from "./types"
5
+ import { ReserveLiquidity, ReserveLiquidityFields, ReserveLiquidityJSON } from "./types/ReserveLiquidity"
6
+ import { ReserveCollateral, ReserveCollateralFields, ReserveCollateralJSON } from "./types/ReserveCollateral"
7
+ import { ReserveConfig, ReserveConfigFields, ReserveConfigJSON } from "./types/ReserveConfig"
8
+ import Decimal from "decimal.js"
9
+ import { Fraction } from "./fraction"
10
+
11
+ const PROGRAM_ID = new PublicKey("KLend2g3cP87fffoy8q1mQqGKjrxjC8boSyAYavgmjD")
12
+
13
+ export interface ReserveFields {
14
+ /** Version of the reserve */
15
+ version: BN
16
+ /** Last slot when supply and rates updated */
17
+ lastUpdate: LastUpdateFields
18
+ /** Lending market address */
19
+ lendingMarket: PublicKey
20
+ farmCollateral: PublicKey
21
+ farmDebt: PublicKey
22
+ /** Reserve liquidity */
23
+ liquidity: ReserveLiquidityFields
24
+ reserveLiquidityPadding: Array<BN>
25
+ /** Reserve collateral */
26
+ collateral: ReserveCollateralFields
27
+ reserveCollateralPadding: Array<BN>
28
+ /** Reserve configuration values */
29
+ config: ReserveConfigFields
30
+ configPadding: Array<BN>
31
+ borrowedAmountOutsideElevationGroup: BN
32
+ /**
33
+ * Amount of token borrowed in lamport of debt asset in the given
34
+ * elevation group when this reserve is part of the collaterals.
35
+ */
36
+ borrowedAmountsAgainstThisReserveInElevationGroups: Array<BN>
37
+ padding: Array<BN>
38
+ }
39
+
40
+ export interface ReserveJSON {
41
+ /** Version of the reserve */
42
+ version: string
43
+ /** Last slot when supply and rates updated */
44
+ lastUpdate: LastUpdateJSON
45
+ /** Lending market address */
46
+ lendingMarket: string
47
+ farmCollateral: string
48
+ farmDebt: string
49
+ /** Reserve liquidity */
50
+ liquidity: ReserveLiquidityJSON
51
+ reserveLiquidityPadding: Array<string>
52
+ /** Reserve collateral */
53
+ collateral: ReserveCollateralJSON
54
+ reserveCollateralPadding: Array<string>
55
+ /** Reserve configuration values */
56
+ config: ReserveConfigJSON
57
+ configPadding: Array<string>
58
+ borrowedAmountOutsideElevationGroup: string
59
+ /**
60
+ * Amount of token borrowed in lamport of debt asset in the given
61
+ * elevation group when this reserve is part of the collaterals.
62
+ */
63
+ borrowedAmountsAgainstThisReserveInElevationGroups: Array<string>
64
+ padding: Array<string>
65
+ }
66
+
67
+ export class Reserve {
68
+ /** Version of the reserve */
69
+ readonly version: BN
70
+ /** Last slot when supply and rates updated */
71
+ readonly lastUpdate: LastUpdate
72
+ /** Lending market address */
73
+ readonly lendingMarket: PublicKey
74
+ readonly farmCollateral: PublicKey
75
+ readonly farmDebt: PublicKey
76
+ /** Reserve liquidity */
77
+ readonly liquidity: ReserveLiquidity
78
+ readonly reserveLiquidityPadding: Array<BN>
79
+ /** Reserve collateral */
80
+ readonly collateral: ReserveCollateral
81
+ readonly reserveCollateralPadding: Array<BN>
82
+ /** Reserve configuration values */
83
+ readonly config: ReserveConfig
84
+ readonly configPadding: Array<BN>
85
+ readonly borrowedAmountOutsideElevationGroup: BN
86
+ /**
87
+ * Amount of token borrowed in lamport of debt asset in the given
88
+ * elevation group when this reserve is part of the collaterals.
89
+ */
90
+ readonly borrowedAmountsAgainstThisReserveInElevationGroups: Array<BN>
91
+ readonly padding: Array<BN>
92
+
93
+ static readonly discriminator = Buffer.from([43, 242, 204, 202, 26, 247, 59, 127])
94
+
95
+ static readonly layout = borsh.struct([
96
+ borsh.u64("version"),
97
+ LastUpdate.layout("lastUpdate"),
98
+ borsh.publicKey("lendingMarket"),
99
+ borsh.publicKey("farmCollateral"),
100
+ borsh.publicKey("farmDebt"),
101
+ ReserveLiquidity.layout("liquidity"),
102
+ borsh.array(borsh.u64(), 150, "reserveLiquidityPadding"),
103
+ ReserveCollateral.layout("collateral"),
104
+ borsh.array(borsh.u64(), 150, "reserveCollateralPadding"),
105
+ ReserveConfig.layout("config"),
106
+ borsh.array(borsh.u64(), 117, "configPadding"),
107
+ borsh.u64("borrowedAmountOutsideElevationGroup"),
108
+ borsh.array(borsh.u64(), 32, "borrowedAmountsAgainstThisReserveInElevationGroups"),
109
+ borsh.array(borsh.u64(), 207, "padding"),
110
+ ])
111
+
112
+ constructor(fields: ReserveFields) {
113
+ this.version = fields.version
114
+ this.lastUpdate = new LastUpdate({ ...fields.lastUpdate })
115
+ this.lendingMarket = fields.lendingMarket
116
+ this.farmCollateral = fields.farmCollateral
117
+ this.farmDebt = fields.farmDebt
118
+ this.liquidity = new ReserveLiquidity({ ...fields.liquidity })
119
+ this.reserveLiquidityPadding = fields.reserveLiquidityPadding
120
+ this.collateral = new ReserveCollateral({ ...fields.collateral })
121
+ this.reserveCollateralPadding = fields.reserveCollateralPadding
122
+ this.config = new ReserveConfig({ ...fields.config })
123
+ this.configPadding = fields.configPadding
124
+ this.borrowedAmountOutsideElevationGroup = fields.borrowedAmountOutsideElevationGroup
125
+ this.borrowedAmountsAgainstThisReserveInElevationGroups = fields.borrowedAmountsAgainstThisReserveInElevationGroups
126
+ this.padding = fields.padding
127
+ }
128
+
129
+ static async fetch(c: Connection, address: PublicKey, programId: PublicKey = PROGRAM_ID): Promise<Reserve | null> {
130
+ const info = await c.getAccountInfo(address)
131
+
132
+ if (info === null) {
133
+ return null
134
+ }
135
+ if (!info.owner.equals(programId)) {
136
+ throw new Error("account doesn't belong to this program")
137
+ }
138
+
139
+ return this.decode(info.data)
140
+ }
141
+
142
+ static async fetchMultiple(
143
+ c: Connection,
144
+ addresses: PublicKey[],
145
+ programId: PublicKey = PROGRAM_ID,
146
+ ): Promise<Array<Reserve | null>> {
147
+ const infos = await c.getMultipleAccountsInfo(addresses)
148
+
149
+ return infos.map((info) => {
150
+ if (info === null) {
151
+ return null
152
+ }
153
+ if (!info.owner.equals(programId)) {
154
+ throw new Error("account doesn't belong to this program")
155
+ }
156
+
157
+ return this.decode(info.data)
158
+ })
159
+ }
160
+
161
+ static decode(data: Buffer): Reserve {
162
+ if (!data.slice(0, 8).equals(Reserve.discriminator)) {
163
+ throw new Error("invalid account discriminator")
164
+ }
165
+
166
+ const dec = Reserve.layout.decode(data.slice(8))
167
+
168
+ return new Reserve({
169
+ version: dec.version,
170
+ lastUpdate: LastUpdate.fromDecoded(dec.lastUpdate),
171
+ lendingMarket: dec.lendingMarket,
172
+ farmCollateral: dec.farmCollateral,
173
+ farmDebt: dec.farmDebt,
174
+ liquidity: ReserveLiquidity.fromDecoded(dec.liquidity),
175
+ reserveLiquidityPadding: dec.reserveLiquidityPadding,
176
+ collateral: ReserveCollateral.fromDecoded(dec.collateral),
177
+ reserveCollateralPadding: dec.reserveCollateralPadding,
178
+ config: ReserveConfig.fromDecoded(dec.config),
179
+ configPadding: dec.configPadding,
180
+ borrowedAmountOutsideElevationGroup: dec.borrowedAmountOutsideElevationGroup,
181
+ borrowedAmountsAgainstThisReserveInElevationGroups: dec.borrowedAmountsAgainstThisReserveInElevationGroups,
182
+ padding: dec.padding,
183
+ })
184
+ }
185
+
186
+ toJSON(): ReserveJSON {
187
+ return {
188
+ version: this.version.toString(),
189
+ lastUpdate: this.lastUpdate.toJSON(),
190
+ lendingMarket: this.lendingMarket.toString(),
191
+ farmCollateral: this.farmCollateral.toString(),
192
+ farmDebt: this.farmDebt.toString(),
193
+ liquidity: this.liquidity.toJSON(),
194
+ reserveLiquidityPadding: this.reserveLiquidityPadding.map((item) => item.toString()),
195
+ collateral: this.collateral.toJSON(),
196
+ reserveCollateralPadding: this.reserveCollateralPadding.map((item) => item.toString()),
197
+ config: this.config.toJSON(),
198
+ configPadding: this.configPadding.map((item) => item.toString()),
199
+ borrowedAmountOutsideElevationGroup: this.borrowedAmountOutsideElevationGroup.toString(),
200
+ borrowedAmountsAgainstThisReserveInElevationGroups: this.borrowedAmountsAgainstThisReserveInElevationGroups.map(
201
+ (item) => item.toString(),
202
+ ),
203
+ padding: this.padding.map((item) => item.toString()),
204
+ }
205
+ }
206
+
207
+ static fromJSON(obj: ReserveJSON): Reserve {
208
+ return new Reserve({
209
+ version: new BN(obj.version),
210
+ lastUpdate: LastUpdate.fromJSON(obj.lastUpdate),
211
+ lendingMarket: new PublicKey(obj.lendingMarket),
212
+ farmCollateral: new PublicKey(obj.farmCollateral),
213
+ farmDebt: new PublicKey(obj.farmDebt),
214
+ liquidity: ReserveLiquidity.fromJSON(obj.liquidity),
215
+ reserveLiquidityPadding: obj.reserveLiquidityPadding.map((item) => new BN(item)),
216
+ collateral: ReserveCollateral.fromJSON(obj.collateral),
217
+ reserveCollateralPadding: obj.reserveCollateralPadding.map((item) => new BN(item)),
218
+ config: ReserveConfig.fromJSON(obj.config),
219
+ configPadding: obj.configPadding.map((item) => new BN(item)),
220
+ borrowedAmountOutsideElevationGroup: new BN(obj.borrowedAmountOutsideElevationGroup),
221
+ borrowedAmountsAgainstThisReserveInElevationGroups: obj.borrowedAmountsAgainstThisReserveInElevationGroups.map(
222
+ (item) => new BN(item),
223
+ ),
224
+ padding: obj.padding.map((item) => new BN(item)),
225
+ })
226
+ }
227
+
228
+ getCollateralExchangeRate(): Decimal {
229
+ const totalSupply = this.getTotalSupply()
230
+
231
+ const mintTotalSupply = this.collateral.mintTotalSupply
232
+
233
+ if (mintTotalSupply.isZero() || totalSupply.isZero()) {
234
+ return new Decimal(1)
235
+ } else {
236
+ const num = totalSupply.toString()
237
+ const den = new Decimal(mintTotalSupply.toString())
238
+ return new Decimal(num).dividedBy(den)
239
+ }
240
+ }
241
+
242
+ getTotalSupply(): Decimal {
243
+ return this.getLiquidityAvailableAmount()
244
+ .add(this.getBorrowedAmount())
245
+ .sub(this.getAccumulatedProtocolFees())
246
+ .sub(this.getAccumulatedReferrerFees())
247
+ .sub(this.getPendingReferrerFees())
248
+ }
249
+
250
+ getLiquidityAvailableAmount(): Decimal {
251
+ return new Decimal(this.liquidity.availableAmount.toString())
252
+ }
253
+
254
+ getBorrowedAmount(): Decimal {
255
+ return new Fraction(this.liquidity.borrowedAmountSf).toDecimal()
256
+ }
257
+
258
+ getAccumulatedProtocolFees(): Decimal {
259
+ return new Fraction(this.liquidity.accumulatedProtocolFeesSf).toDecimal()
260
+ }
261
+
262
+ getAccumulatedReferrerFees(): Decimal {
263
+ return new Fraction(this.liquidity.accumulatedReferrerFeesSf).toDecimal()
264
+ }
265
+
266
+ getPendingReferrerFees(): Decimal {
267
+ return new Fraction(this.liquidity.pendingReferrerFeesSf).toDecimal()
268
+ }
269
+ }
@@ -0,0 +1,59 @@
1
+ import BN from "bn.js"
2
+ import * as borsh from "@coral-xyz/borsh"
3
+
4
+ export interface BigFractionBytesFields {
5
+ value: Array<BN>
6
+ padding: Array<BN>
7
+ }
8
+
9
+ export interface BigFractionBytesJSON {
10
+ value: Array<string>
11
+ padding: Array<string>
12
+ }
13
+
14
+ export class BigFractionBytes {
15
+ readonly value: Array<BN>
16
+ readonly padding: Array<BN>
17
+
18
+ constructor(fields: BigFractionBytesFields) {
19
+ this.value = fields.value
20
+ this.padding = fields.padding
21
+ }
22
+
23
+ static layout(property?: string) {
24
+ return borsh.struct([borsh.array(borsh.u64(), 4, "value"), borsh.array(borsh.u64(), 2, "padding")], property)
25
+ }
26
+
27
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
28
+ static fromDecoded(obj: any) {
29
+ return new BigFractionBytes({
30
+ value: obj.value,
31
+ padding: obj.padding,
32
+ })
33
+ }
34
+
35
+ static toEncodable(fields: BigFractionBytesFields) {
36
+ return {
37
+ value: fields.value,
38
+ padding: fields.padding,
39
+ }
40
+ }
41
+
42
+ toJSON(): BigFractionBytesJSON {
43
+ return {
44
+ value: this.value.map((item) => item.toString()),
45
+ padding: this.padding.map((item) => item.toString()),
46
+ }
47
+ }
48
+
49
+ static fromJSON(obj: BigFractionBytesJSON): BigFractionBytes {
50
+ return new BigFractionBytes({
51
+ value: obj.value.map((item) => new BN(item)),
52
+ padding: obj.padding.map((item) => new BN(item)),
53
+ })
54
+ }
55
+
56
+ toEncodable() {
57
+ return BigFractionBytes.toEncodable(this)
58
+ }
59
+ }
@@ -0,0 +1,53 @@
1
+ import * as borsh from "@coral-xyz/borsh"
2
+ import { CurvePoint, CurvePointFields, CurvePointJSON } from "./CurvePoint"
3
+
4
+ export interface BorrowRateCurveFields {
5
+ points: Array<CurvePointFields>
6
+ }
7
+
8
+ export interface BorrowRateCurveJSON {
9
+ points: Array<CurvePointJSON>
10
+ }
11
+
12
+ export class BorrowRateCurve {
13
+ readonly points: Array<CurvePoint>
14
+
15
+ constructor(fields: BorrowRateCurveFields) {
16
+ this.points = fields.points.map((item) => new CurvePoint({ ...item }))
17
+ }
18
+
19
+ static layout(property?: string) {
20
+ return borsh.struct([borsh.array(CurvePoint.layout(), 11, "points")], property)
21
+ }
22
+
23
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
24
+ static fromDecoded(obj: any) {
25
+ return new BorrowRateCurve({
26
+ points: obj.points.map((item: any /* eslint-disable-line @typescript-eslint/no-explicit-any */) =>
27
+ CurvePoint.fromDecoded(item),
28
+ ),
29
+ })
30
+ }
31
+
32
+ static toEncodable(fields: BorrowRateCurveFields) {
33
+ return {
34
+ points: fields.points.map((item) => CurvePoint.toEncodable(item)),
35
+ }
36
+ }
37
+
38
+ toJSON(): BorrowRateCurveJSON {
39
+ return {
40
+ points: this.points.map((item) => item.toJSON()),
41
+ }
42
+ }
43
+
44
+ static fromJSON(obj: BorrowRateCurveJSON): BorrowRateCurve {
45
+ return new BorrowRateCurve({
46
+ points: obj.points.map((item) => CurvePoint.fromJSON(item)),
47
+ })
48
+ }
49
+
50
+ toEncodable() {
51
+ return BorrowRateCurve.toEncodable(this)
52
+ }
53
+ }
@@ -0,0 +1,58 @@
1
+ import * as borsh from "@coral-xyz/borsh"
2
+
3
+ export interface CurvePointFields {
4
+ utilizationRateBps: number
5
+ borrowRateBps: number
6
+ }
7
+
8
+ export interface CurvePointJSON {
9
+ utilizationRateBps: number
10
+ borrowRateBps: number
11
+ }
12
+
13
+ export class CurvePoint {
14
+ readonly utilizationRateBps: number
15
+ readonly borrowRateBps: number
16
+
17
+ constructor(fields: CurvePointFields) {
18
+ this.utilizationRateBps = fields.utilizationRateBps
19
+ this.borrowRateBps = fields.borrowRateBps
20
+ }
21
+
22
+ static layout(property?: string) {
23
+ return borsh.struct([borsh.u32("utilizationRateBps"), borsh.u32("borrowRateBps")], property)
24
+ }
25
+
26
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
+ static fromDecoded(obj: any) {
28
+ return new CurvePoint({
29
+ utilizationRateBps: obj.utilizationRateBps,
30
+ borrowRateBps: obj.borrowRateBps,
31
+ })
32
+ }
33
+
34
+ static toEncodable(fields: CurvePointFields) {
35
+ return {
36
+ utilizationRateBps: fields.utilizationRateBps,
37
+ borrowRateBps: fields.borrowRateBps,
38
+ }
39
+ }
40
+
41
+ toJSON(): CurvePointJSON {
42
+ return {
43
+ utilizationRateBps: this.utilizationRateBps,
44
+ borrowRateBps: this.borrowRateBps,
45
+ }
46
+ }
47
+
48
+ static fromJSON(obj: CurvePointJSON): CurvePoint {
49
+ return new CurvePoint({
50
+ utilizationRateBps: obj.utilizationRateBps,
51
+ borrowRateBps: obj.borrowRateBps,
52
+ })
53
+ }
54
+
55
+ toEncodable() {
56
+ return CurvePoint.toEncodable(this)
57
+ }
58
+ }