@anywalletkit/protocol-lending-kamino-solana 0.3.0 → 0.3.2
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/dist/index.cjs +32 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.js +32 -5
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.d.cts
CHANGED
|
@@ -302,7 +302,7 @@ interface VaultRuleAccounts {
|
|
|
302
302
|
sharesMint: string;
|
|
303
303
|
/** The wallet's token account for the vault's asset. */
|
|
304
304
|
tokenAta: string;
|
|
305
|
-
/** The wallet's shares
|
|
305
|
+
/** The wallet's shares accounts: the Token program's derivation, then Token-2022's. */
|
|
306
306
|
sharesAtas: readonly string[];
|
|
307
307
|
/** The vault's farm and the wallet's state in it, when the vault has a farm. */
|
|
308
308
|
farm: {
|
|
@@ -630,6 +630,13 @@ declare class KaminoMarketProtocolSolana extends KaminoBase<KaminoMarketConfig>
|
|
|
630
630
|
private reported;
|
|
631
631
|
quoteSupply(o: SupplyOptions): Promise<MarketQuote>;
|
|
632
632
|
supply(o: SupplyOptions): Promise<LendingResult>;
|
|
633
|
+
/**
|
|
634
|
+
* A withdrawal pays out about what was asked: at least the amount less the slippage tolerance, at
|
|
635
|
+
* most the amount plus that tolerance (interest accrued before the block, on a 'max'), one cToken's
|
|
636
|
+
* worth at the reserve's on-chain rate (klend redeems whole cTokens) and a unit of rounding. A bigger
|
|
637
|
+
* payout is collateral the user did not choose to take out of the obligation.
|
|
638
|
+
*/
|
|
639
|
+
private withdrawExpectation;
|
|
633
640
|
quoteWithdraw(o: KaminoMarketWithdrawOptions): Promise<MarketQuote>;
|
|
634
641
|
withdraw(o: KaminoMarketWithdrawOptions): Promise<LendingResult>;
|
|
635
642
|
/** A borrow pays out at most what was asked: a bigger one is debt the user did not choose. */
|
package/dist/index.d.ts
CHANGED
|
@@ -302,7 +302,7 @@ interface VaultRuleAccounts {
|
|
|
302
302
|
sharesMint: string;
|
|
303
303
|
/** The wallet's token account for the vault's asset. */
|
|
304
304
|
tokenAta: string;
|
|
305
|
-
/** The wallet's shares
|
|
305
|
+
/** The wallet's shares accounts: the Token program's derivation, then Token-2022's. */
|
|
306
306
|
sharesAtas: readonly string[];
|
|
307
307
|
/** The vault's farm and the wallet's state in it, when the vault has a farm. */
|
|
308
308
|
farm: {
|
|
@@ -630,6 +630,13 @@ declare class KaminoMarketProtocolSolana extends KaminoBase<KaminoMarketConfig>
|
|
|
630
630
|
private reported;
|
|
631
631
|
quoteSupply(o: SupplyOptions): Promise<MarketQuote>;
|
|
632
632
|
supply(o: SupplyOptions): Promise<LendingResult>;
|
|
633
|
+
/**
|
|
634
|
+
* A withdrawal pays out about what was asked: at least the amount less the slippage tolerance, at
|
|
635
|
+
* most the amount plus that tolerance (interest accrued before the block, on a 'max'), one cToken's
|
|
636
|
+
* worth at the reserve's on-chain rate (klend redeems whole cTokens) and a unit of rounding. A bigger
|
|
637
|
+
* payout is collateral the user did not choose to take out of the obligation.
|
|
638
|
+
*/
|
|
639
|
+
private withdrawExpectation;
|
|
633
640
|
quoteWithdraw(o: KaminoMarketWithdrawOptions): Promise<MarketQuote>;
|
|
634
641
|
withdraw(o: KaminoMarketWithdrawOptions): Promise<LendingResult>;
|
|
635
642
|
/** A borrow pays out at most what was asked: a bigger one is debt the user did not choose. */
|
package/dist/index.js
CHANGED
|
@@ -20,19 +20,29 @@ function toAmount(amount) {
|
|
|
20
20
|
if (value <= 0n) throw new KaminoError("'amount' should be greater than zero.");
|
|
21
21
|
return value;
|
|
22
22
|
}
|
|
23
|
+
var MAX_DECIMALS = 255;
|
|
24
|
+
var MAX_EXPONENT = 400;
|
|
25
|
+
var MAX_DIGITS = 1e3;
|
|
26
|
+
function checkDecimals(decimals) {
|
|
27
|
+
if (!Number.isInteger(decimals) || decimals < 0 || decimals > MAX_DECIMALS) throw new KaminoError(`Decimals must be an integer from 0 to ${MAX_DECIMALS}, not ${decimals}.`);
|
|
28
|
+
}
|
|
23
29
|
function toDecimalString(amount, decimals) {
|
|
30
|
+
checkDecimals(decimals);
|
|
24
31
|
const s = amount.toString().padStart(decimals + 1, "0");
|
|
25
32
|
const whole = s.slice(0, s.length - decimals), frac = s.slice(s.length - decimals).replace(/0+$/, "");
|
|
26
33
|
return frac ? `${whole}.${frac}` : whole;
|
|
27
34
|
}
|
|
28
35
|
function fromDecimalString(value, decimals) {
|
|
36
|
+
checkDecimals(decimals);
|
|
29
37
|
const s = String(value).trim();
|
|
38
|
+
if (s.length > MAX_DIGITS) throw new KaminoError(`Kamino reported a figure of ${s.length} characters; no amount or rate is that long.`);
|
|
30
39
|
if (/^[-+]/.test(s)) throw new KaminoError(`Kamino reported a signed figure (${s.slice(0, 40)}); every amount and rate it reports is positive.`);
|
|
31
40
|
const m = /^(\d*)(?:\.(\d*))?(?:[eE]([+-]?\d+))?$/.exec(s);
|
|
32
41
|
if (!m) throw new KaminoError(`Not a decimal number: ${s.slice(0, 40)}`);
|
|
33
42
|
let [, whole = "", frac = "", exp] = m;
|
|
34
43
|
if (exp) {
|
|
35
44
|
const e = Number(exp);
|
|
45
|
+
if (!(Math.abs(e) <= MAX_EXPONENT)) throw new KaminoError(`Kamino reported a figure with exponent ${exp.slice(0, 40)}; more than ${MAX_EXPONENT} either way is no amount or rate.`);
|
|
36
46
|
const digits = whole + frac;
|
|
37
47
|
let point = whole.length + e;
|
|
38
48
|
if (point <= 0) {
|
|
@@ -46,8 +56,9 @@ function fromDecimalString(value, decimals) {
|
|
|
46
56
|
frac = digits.slice(point);
|
|
47
57
|
}
|
|
48
58
|
}
|
|
49
|
-
const scaled = (whole || "0") + (frac + "0".repeat(decimals)).slice(0, decimals);
|
|
50
|
-
|
|
59
|
+
const scaled = ((whole || "0") + (frac + "0".repeat(decimals)).slice(0, decimals)).replace(/^0+(?=\d)/, "");
|
|
60
|
+
if (scaled.length > MAX_DIGITS) throw new KaminoError(`Kamino reported a figure of ${scaled.length} digits in base units; no amount or rate is that large.`);
|
|
61
|
+
return BigInt(scaled);
|
|
51
62
|
}
|
|
52
63
|
var WAD = 10n ** 18n;
|
|
53
64
|
var toWad = (value) => fromDecimalString(value, 18);
|
|
@@ -516,6 +527,11 @@ function collateralToLiquiditySf(r, ctokens) {
|
|
|
516
527
|
if (r.collateralSupply === 0n || r.totalLiquiditySf === 0n) return ctokens * SF;
|
|
517
528
|
return ctokens * r.totalLiquiditySf / r.collateralSupply;
|
|
518
529
|
}
|
|
530
|
+
function oneCtokenLiquidityUp(r) {
|
|
531
|
+
const sf = r.collateralSupply === 0n || r.totalLiquiditySf === 0n ? SF : (r.totalLiquiditySf + r.collateralSupply - 1n) / r.collateralSupply;
|
|
532
|
+
const units = (sf + SF - 1n) / SF;
|
|
533
|
+
return units > 0n ? units : 1n;
|
|
534
|
+
}
|
|
519
535
|
function decodeObligation(address, acc) {
|
|
520
536
|
const d = bytesOf("Obligation", address, acc);
|
|
521
537
|
const deposits = [], borrows = [];
|
|
@@ -685,7 +701,8 @@ function vaultRules(op, a, opts) {
|
|
|
685
701
|
const redeem = { 0: me, 1: vault, 5: tokenAta, 6: mint, 7: sharesAtas, 8: sharesMint };
|
|
686
702
|
rules.push(
|
|
687
703
|
{ program: KVAULT_PROGRAM, data: IX.vaultWithdrawFromAvailable, dataLength: 16, accounts: redeem },
|
|
688
|
-
{ program: KVAULT_PROGRAM, data: IX.vaultWithdraw, dataLength: 16, accounts: redeem }
|
|
704
|
+
{ program: KVAULT_PROGRAM, data: IX.vaultWithdraw, dataLength: 16, accounts: redeem },
|
|
705
|
+
...sharesAtas.slice(0, 2).map((ata, i) => ({ program: i === 0 ? TOKEN_PROGRAM : TOKEN_2022_PROGRAM, data: "09", dataLength: 1, accounts: { 0: ata, 1: me, 2: me } }))
|
|
689
706
|
);
|
|
690
707
|
if (farm) {
|
|
691
708
|
rules.push(
|
|
@@ -1081,11 +1098,21 @@ var KaminoMarketProtocolSolana = class extends KaminoBase {
|
|
|
1081
1098
|
const base64 = await this.build("deposit", me, p);
|
|
1082
1099
|
return this.sendBuilt(base64, this.spendExpectation(o.token, p.amount, this.firstDepositExtra(p)), p.rules, "supply");
|
|
1083
1100
|
}
|
|
1101
|
+
/**
|
|
1102
|
+
* A withdrawal pays out about what was asked: at least the amount less the slippage tolerance, at
|
|
1103
|
+
* most the amount plus that tolerance (interest accrued before the block, on a 'max'), one cToken's
|
|
1104
|
+
* worth at the reserve's on-chain rate (klend redeems whole cTokens) and a unit of rounding. A bigger
|
|
1105
|
+
* payout is collateral the user did not choose to take out of the obligation.
|
|
1106
|
+
*/
|
|
1107
|
+
withdrawExpectation(mint, p) {
|
|
1108
|
+
const max = p.amount + (p.amount - lessBps(p.amount, this.slippageBps)) + oneCtokenLiquidityUp(p.chain.reserve) + 1n;
|
|
1109
|
+
return this.receiveExpectation(mint, p.amount, 0n, max);
|
|
1110
|
+
}
|
|
1084
1111
|
async quoteWithdraw(o) {
|
|
1085
1112
|
const me = await this.me();
|
|
1086
1113
|
const p = await this.prepare("withdraw", me, o.token, o.amount);
|
|
1087
1114
|
const base64 = await this.build("withdraw", me, p);
|
|
1088
|
-
const expect = this.
|
|
1115
|
+
const expect = this.withdrawExpectation(o.token, p);
|
|
1089
1116
|
const { fee } = await this.quoteBuilt(base64, expect, p.rules);
|
|
1090
1117
|
return { fee, reserve: p.reserve.reserve, minReceived: expect.tokenIn.minAmount };
|
|
1091
1118
|
}
|
|
@@ -1094,7 +1121,7 @@ var KaminoMarketProtocolSolana = class extends KaminoBase {
|
|
|
1094
1121
|
const me = await this.me();
|
|
1095
1122
|
const p = await this.prepare("withdraw", me, o.token, o.amount);
|
|
1096
1123
|
const base64 = await this.build("withdraw", me, p);
|
|
1097
|
-
return this.sendBuilt(base64, this.
|
|
1124
|
+
return this.sendBuilt(base64, this.withdrawExpectation(o.token, p), p.rules, "withdraw");
|
|
1098
1125
|
}
|
|
1099
1126
|
/** A borrow pays out at most what was asked: a bigger one is debt the user did not choose. */
|
|
1100
1127
|
borrowExpectation(mint, amount) {
|