@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.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);
@@ -757,7 +768,8 @@ function vaultRules(op, a, opts) {
757
768
  const redeem = { 0: me, 1: vault, 5: tokenAta, 6: mint, 7: sharesAtas, 8: sharesMint };
758
769
  rules.push(
759
770
  { program: KVAULT_PROGRAM, data: IX.vaultWithdrawFromAvailable, dataLength: 16, accounts: redeem },
760
- { program: KVAULT_PROGRAM, data: IX.vaultWithdraw, dataLength: 16, accounts: redeem }
771
+ { program: KVAULT_PROGRAM, data: IX.vaultWithdraw, dataLength: 16, accounts: redeem },
772
+ ...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
773
  );
762
774
  if (farm) {
763
775
  rules.push(