@kamino-finance/klend-sdk 3.2.10 → 3.2.12
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.
- package/README_KAMINO_MANAGER.md +60 -3
- package/dist/classes/manager.d.ts +15 -1
- package/dist/classes/manager.js +292 -0
- package/dist/classes/manager.js.map +1 -1
- package/dist/classes/obligation.d.ts +35 -0
- package/dist/classes/obligation.js +67 -35
- package/dist/classes/obligation.js.map +1 -1
- package/dist/classes/reserve.d.ts +7 -1
- package/dist/classes/reserve.js +74 -1
- package/dist/classes/reserve.js.map +1 -1
- package/dist/classes/shared.d.ts +11 -1
- package/dist/classes/shared.js.map +1 -1
- package/dist/classes/utils.d.ts +1 -0
- package/dist/classes/utils.js +8 -0
- package/dist/classes/utils.js.map +1 -1
- package/dist/client_kamino_manager.js +100 -0
- package/dist/client_kamino_manager.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { AccountInfo, Connection, PublicKey, TransactionInstruction } from '@solana/web3.js';
|
|
2
2
|
import Decimal from 'decimal.js';
|
|
3
3
|
import { MarketWithAddress, TokenOracleData } from '../utils';
|
|
4
|
-
import { ReserveDataType } from './shared';
|
|
4
|
+
import { ReserveDataType, ReserveRewardYield } from './shared';
|
|
5
5
|
import { Reserve } from '../idl_codegen/accounts';
|
|
6
6
|
import { BorrowRateCurve, ReserveConfig } from '../idl_codegen/types';
|
|
7
7
|
import { ActionType } from './action';
|
|
8
8
|
import { KaminoMarket } from './market';
|
|
9
|
+
import { KaminoPrices } from '@kamino-finance/kliquidity-sdk';
|
|
9
10
|
export declare const DEFAULT_RECENT_SLOT_DURATION_MS = 450;
|
|
10
11
|
export declare class KaminoReserve {
|
|
11
12
|
state: Reserve;
|
|
@@ -13,6 +14,7 @@ export declare class KaminoReserve {
|
|
|
13
14
|
symbol: string;
|
|
14
15
|
tokenOraclePrice: TokenOracleData;
|
|
15
16
|
stats: ReserveDataType;
|
|
17
|
+
private farmData;
|
|
16
18
|
private buffer;
|
|
17
19
|
private connection;
|
|
18
20
|
private readonly recentSlotDurationMs;
|
|
@@ -193,6 +195,10 @@ export declare class KaminoReserve {
|
|
|
193
195
|
load(tokenOraclePrice: TokenOracleData): Promise<void>;
|
|
194
196
|
totalSupplyAPY(currentSlot: number): number;
|
|
195
197
|
totalBorrowAPY(currentSlot: number): number;
|
|
198
|
+
loadFarmStates(): Promise<void>;
|
|
199
|
+
getRewardYields(prices: KaminoPrices): Promise<ReserveRewardYield[]>;
|
|
200
|
+
private calculateRewardYield;
|
|
201
|
+
private getRewardPerTimeUnitSecond;
|
|
196
202
|
private formatReserveData;
|
|
197
203
|
/**
|
|
198
204
|
* Compound current borrow rate over elapsed slots
|
package/dist/classes/reserve.js
CHANGED
|
@@ -56,9 +56,11 @@ const lib_1 = require("../lib");
|
|
|
56
56
|
const anchor = __importStar(require("@coral-xyz/anchor"));
|
|
57
57
|
const spl_token_1 = require("@solana/spl-token");
|
|
58
58
|
const UpdateConfigMode_1 = require("../idl_codegen/types/UpdateConfigMode");
|
|
59
|
+
const farms_sdk_1 = require("@hubbleprotocol/farms-sdk");
|
|
59
60
|
exports.DEFAULT_RECENT_SLOT_DURATION_MS = 450;
|
|
60
61
|
class KaminoReserve {
|
|
61
62
|
constructor(state, address, tokenOraclePrice, connection, recentSlotDurationMs) {
|
|
63
|
+
this.farmData = { fetched: false, farmStates: [] };
|
|
62
64
|
/**
|
|
63
65
|
*
|
|
64
66
|
* @returns the flash loan fee percentage of the reserve
|
|
@@ -558,6 +560,77 @@ class KaminoReserve {
|
|
|
558
560
|
}
|
|
559
561
|
return (0, utils_2.calculateAPYFromAPR)(this.calculateBorrowAPR(currentSlot, 0));
|
|
560
562
|
}
|
|
563
|
+
loadFarmStates() {
|
|
564
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
565
|
+
if (!this.farmData.fetched) {
|
|
566
|
+
const farmStates = yield farms_sdk_1.FarmState.fetchMultiple(this.connection, [
|
|
567
|
+
this.state.farmDebt,
|
|
568
|
+
this.state.farmCollateral,
|
|
569
|
+
]);
|
|
570
|
+
this.farmData.farmStates.push(...farmStates.filter((x) => x !== null));
|
|
571
|
+
this.farmData.fetched = true;
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
getRewardYields(prices) {
|
|
576
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
577
|
+
const { stats } = this;
|
|
578
|
+
if (!stats) {
|
|
579
|
+
throw Error('KaminoMarket must call loadReserves.');
|
|
580
|
+
}
|
|
581
|
+
const isDebtReward = this.state.farmDebt.equals(this.address);
|
|
582
|
+
yield this.loadFarmStates();
|
|
583
|
+
const yields = [];
|
|
584
|
+
for (const farmState of this.farmData.farmStates) {
|
|
585
|
+
for (const rewardInfo of farmState.rewardInfos.filter((x) => !x.token.mint.equals(web3_js_1.PublicKey.default))) {
|
|
586
|
+
const { apy, apr } = this.calculateRewardYield(prices, rewardInfo, isDebtReward);
|
|
587
|
+
yields.push({ apy, apr, rewardInfo });
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
return yields;
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
calculateRewardYield(prices, rewardInfo, isDebtReward) {
|
|
594
|
+
const { decimals } = this.stats;
|
|
595
|
+
const totalBorrows = this.getBorrowedAmount();
|
|
596
|
+
const totalSupply = this.getTotalSupply();
|
|
597
|
+
const mintAddress = this.getLiquidityMint();
|
|
598
|
+
const totalAmount = isDebtReward
|
|
599
|
+
? (0, utils_2.lamportsToNumberDecimal)(totalBorrows, decimals)
|
|
600
|
+
: (0, utils_2.lamportsToNumberDecimal)(totalSupply, decimals);
|
|
601
|
+
const totalValue = totalAmount.mul(prices.spot[mintAddress.toString()].price);
|
|
602
|
+
const rewardPerTimeUnitSecond = this.getRewardPerTimeUnitSecond(rewardInfo);
|
|
603
|
+
const rewardsInYear = rewardPerTimeUnitSecond.mul(60 * 60 * 24 * 365);
|
|
604
|
+
const rewardsInYearValue = rewardsInYear.mul(prices.spot[rewardInfo.token.mint.toString()].price);
|
|
605
|
+
const apy = rewardsInYearValue.div(totalValue);
|
|
606
|
+
return { apy, apr: (0, utils_2.calculateAPRFromAPY)(apy) };
|
|
607
|
+
}
|
|
608
|
+
getRewardPerTimeUnitSecond(reward) {
|
|
609
|
+
const now = new decimal_js_1.default(new Date().getTime()).div(1000);
|
|
610
|
+
let rewardPerTimeUnitSecond = new decimal_js_1.default(0);
|
|
611
|
+
for (let i = 0; i < reward.rewardScheduleCurve.points.length - 1; i++) {
|
|
612
|
+
const { tsStart: tsStartThisPoint, rewardPerTimeUnit } = reward.rewardScheduleCurve.points[i];
|
|
613
|
+
const { tsStart: tsStartNextPoint } = reward.rewardScheduleCurve.points[i + 1];
|
|
614
|
+
const thisPeriodStart = new decimal_js_1.default(tsStartThisPoint.toString());
|
|
615
|
+
const thisPeriodEnd = new decimal_js_1.default(tsStartNextPoint.toString());
|
|
616
|
+
const rps = new decimal_js_1.default(rewardPerTimeUnit.toString());
|
|
617
|
+
if (thisPeriodStart <= now && thisPeriodEnd >= now) {
|
|
618
|
+
rewardPerTimeUnitSecond = rps;
|
|
619
|
+
break;
|
|
620
|
+
}
|
|
621
|
+
else if (thisPeriodStart > now && thisPeriodEnd > now) {
|
|
622
|
+
rewardPerTimeUnitSecond = rps;
|
|
623
|
+
break;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
const rewardTokenDecimals = reward.token.decimals.toNumber();
|
|
627
|
+
const rewardAmountPerUnitDecimals = new decimal_js_1.default(10).pow(reward.rewardsPerSecondDecimals.toString());
|
|
628
|
+
const rewardAmountPerUnitLamports = new decimal_js_1.default(10).pow(rewardTokenDecimals.toString());
|
|
629
|
+
const rpsAdjusted = new decimal_js_1.default(rewardPerTimeUnitSecond.toString())
|
|
630
|
+
.div(rewardAmountPerUnitDecimals)
|
|
631
|
+
.div(rewardAmountPerUnitLamports);
|
|
632
|
+
return rewardPerTimeUnitSecond ? rpsAdjusted : new decimal_js_1.default(0);
|
|
633
|
+
}
|
|
561
634
|
formatReserveData(parsedData) {
|
|
562
635
|
const mintTotalSupply = new decimal_js_1.default(parsedData.collateral.mintTotalSupply.toString()).div(this.getMintFactor());
|
|
563
636
|
let reserveStatus = shared_1.ReserveStatus.Active;
|
|
@@ -577,7 +650,7 @@ class KaminoReserve {
|
|
|
577
650
|
status: reserveStatus,
|
|
578
651
|
mintAddress: parsedData.liquidity.mintPubkey,
|
|
579
652
|
borrowCurve: truncateBorrowCurve(parsedData.config.borrowRateCurve.points),
|
|
580
|
-
|
|
653
|
+
loanToValue: parsedData.config.loanToValuePct / 100,
|
|
581
654
|
maxLiquidationBonus: parsedData.config.maxLiquidationBonusBps / 10000,
|
|
582
655
|
minLiquidationBonus: parsedData.config.minLiquidationBonusBps / 10000,
|
|
583
656
|
liquidationThreshold: parsedData.config.liquidationThresholdPct / 100,
|