@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 CHANGED
@@ -92,19 +92,29 @@ function toAmount(amount) {
92
92
  if (value <= 0n) throw new KaminoError("'amount' should be greater than zero.");
93
93
  return value;
94
94
  }
95
+ var MAX_DECIMALS = 255;
96
+ var MAX_EXPONENT = 400;
97
+ var MAX_DIGITS = 1e3;
98
+ function checkDecimals(decimals) {
99
+ if (!Number.isInteger(decimals) || decimals < 0 || decimals > MAX_DECIMALS) throw new KaminoError(`Decimals must be an integer from 0 to ${MAX_DECIMALS}, not ${decimals}.`);
100
+ }
95
101
  function toDecimalString(amount, decimals) {
102
+ checkDecimals(decimals);
96
103
  const s = amount.toString().padStart(decimals + 1, "0");
97
104
  const whole = s.slice(0, s.length - decimals), frac = s.slice(s.length - decimals).replace(/0+$/, "");
98
105
  return frac ? `${whole}.${frac}` : whole;
99
106
  }
100
107
  function fromDecimalString(value, decimals) {
108
+ checkDecimals(decimals);
101
109
  const s = String(value).trim();
110
+ if (s.length > MAX_DIGITS) throw new KaminoError(`Kamino reported a figure of ${s.length} characters; no amount or rate is that long.`);
102
111
  if (/^[-+]/.test(s)) throw new KaminoError(`Kamino reported a signed figure (${s.slice(0, 40)}); every amount and rate it reports is positive.`);
103
112
  const m = /^(\d*)(?:\.(\d*))?(?:[eE]([+-]?\d+))?$/.exec(s);
104
113
  if (!m) throw new KaminoError(`Not a decimal number: ${s.slice(0, 40)}`);
105
114
  let [, whole = "", frac = "", exp] = m;
106
115
  if (exp) {
107
116
  const e = Number(exp);
117
+ 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.`);
108
118
  const digits = whole + frac;
109
119
  let point = whole.length + e;
110
120
  if (point <= 0) {
@@ -118,8 +128,9 @@ function fromDecimalString(value, decimals) {
118
128
  frac = digits.slice(point);
119
129
  }
120
130
  }
121
- const scaled = (whole || "0") + (frac + "0".repeat(decimals)).slice(0, decimals);
122
- return BigInt(scaled.replace(/^0+(?=\d)/, ""));
131
+ const scaled = ((whole || "0") + (frac + "0".repeat(decimals)).slice(0, decimals)).replace(/^0+(?=\d)/, "");
132
+ 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.`);
133
+ return BigInt(scaled);
123
134
  }
124
135
  var WAD = 10n ** 18n;
125
136
  var toWad = (value) => fromDecimalString(value, 18);
@@ -588,6 +599,11 @@ function collateralToLiquiditySf(r, ctokens) {
588
599
  if (r.collateralSupply === 0n || r.totalLiquiditySf === 0n) return ctokens * SF;
589
600
  return ctokens * r.totalLiquiditySf / r.collateralSupply;
590
601
  }
602
+ function oneCtokenLiquidityUp(r) {
603
+ const sf = r.collateralSupply === 0n || r.totalLiquiditySf === 0n ? SF : (r.totalLiquiditySf + r.collateralSupply - 1n) / r.collateralSupply;
604
+ const units = (sf + SF - 1n) / SF;
605
+ return units > 0n ? units : 1n;
606
+ }
591
607
  function decodeObligation(address, acc) {
592
608
  const d = bytesOf("Obligation", address, acc);
593
609
  const deposits = [], borrows = [];
@@ -757,7 +773,8 @@ function vaultRules(op, a, opts) {
757
773
  const redeem = { 0: me, 1: vault, 5: tokenAta, 6: mint, 7: sharesAtas, 8: sharesMint };
758
774
  rules.push(
759
775
  { program: KVAULT_PROGRAM, data: IX.vaultWithdrawFromAvailable, dataLength: 16, accounts: redeem },
760
- { program: KVAULT_PROGRAM, data: IX.vaultWithdraw, dataLength: 16, accounts: redeem }
776
+ { program: KVAULT_PROGRAM, data: IX.vaultWithdraw, dataLength: 16, accounts: redeem },
777
+ ...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 } }))
761
778
  );
762
779
  if (farm) {
763
780
  rules.push(
@@ -1153,11 +1170,21 @@ var KaminoMarketProtocolSolana = class extends KaminoBase {
1153
1170
  const base64 = await this.build("deposit", me, p);
1154
1171
  return this.sendBuilt(base64, this.spendExpectation(o.token, p.amount, this.firstDepositExtra(p)), p.rules, "supply");
1155
1172
  }
1173
+ /**
1174
+ * A withdrawal pays out about what was asked: at least the amount less the slippage tolerance, at
1175
+ * most the amount plus that tolerance (interest accrued before the block, on a 'max'), one cToken's
1176
+ * worth at the reserve's on-chain rate (klend redeems whole cTokens) and a unit of rounding. A bigger
1177
+ * payout is collateral the user did not choose to take out of the obligation.
1178
+ */
1179
+ withdrawExpectation(mint, p) {
1180
+ const max = p.amount + (p.amount - lessBps(p.amount, this.slippageBps)) + oneCtokenLiquidityUp(p.chain.reserve) + 1n;
1181
+ return this.receiveExpectation(mint, p.amount, 0n, max);
1182
+ }
1156
1183
  async quoteWithdraw(o) {
1157
1184
  const me = await this.me();
1158
1185
  const p = await this.prepare("withdraw", me, o.token, o.amount);
1159
1186
  const base64 = await this.build("withdraw", me, p);
1160
- const expect = this.receiveExpectation(o.token, p.amount);
1187
+ const expect = this.withdrawExpectation(o.token, p);
1161
1188
  const { fee } = await this.quoteBuilt(base64, expect, p.rules);
1162
1189
  return { fee, reserve: p.reserve.reserve, minReceived: expect.tokenIn.minAmount };
1163
1190
  }
@@ -1166,7 +1193,7 @@ var KaminoMarketProtocolSolana = class extends KaminoBase {
1166
1193
  const me = await this.me();
1167
1194
  const p = await this.prepare("withdraw", me, o.token, o.amount);
1168
1195
  const base64 = await this.build("withdraw", me, p);
1169
- return this.sendBuilt(base64, this.receiveExpectation(o.token, p.amount), p.rules, "withdraw");
1196
+ return this.sendBuilt(base64, this.withdrawExpectation(o.token, p), p.rules, "withdraw");
1170
1197
  }
1171
1198
  /** A borrow pays out at most what was asked: a bigger one is debt the user did not choose. */
1172
1199
  borrowExpectation(mint, amount) {