@anywalletkit/protocol-lending-kamino-solana 0.3.0 → 0.3.1

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.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 account (either token program's derivation). */
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: {
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 account (either token program's derivation). */
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: {
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
- return BigInt(scaled.replace(/^0+(?=\d)/, ""));
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);
@@ -685,7 +696,8 @@ function vaultRules(op, a, opts) {
685
696
  const redeem = { 0: me, 1: vault, 5: tokenAta, 6: mint, 7: sharesAtas, 8: sharesMint };
686
697
  rules.push(
687
698
  { program: KVAULT_PROGRAM, data: IX.vaultWithdrawFromAvailable, dataLength: 16, accounts: redeem },
688
- { program: KVAULT_PROGRAM, data: IX.vaultWithdraw, dataLength: 16, accounts: redeem }
699
+ { program: KVAULT_PROGRAM, data: IX.vaultWithdraw, dataLength: 16, accounts: redeem },
700
+ ...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
701
  );
690
702
  if (farm) {
691
703
  rules.push(