@solana/client 0.0.2 → 0.1.0

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.
@@ -1358,10 +1358,23 @@ __name(transactionToBase64WithSigners, "transactionToBase64WithSigners");
1358
1358
 
1359
1359
  // src/transactions/prepareTransaction.ts
1360
1360
  var DEFAULT_COMPUTE_UNIT_LIMIT_MULTIPLIER = 1.1;
1361
+ var DEFAULT_COMPUTE_UNIT_LIMIT = 2e5;
1362
+ var MAX_COMPUTE_UNIT_LIMIT = 14e5;
1361
1363
  function isComputeUnitLimitInstruction(instruction) {
1362
1364
  return instruction.programAddress === computeBudget.COMPUTE_BUDGET_PROGRAM_ADDRESS && instruction.data?.[0] === 2;
1363
1365
  }
1364
1366
  __name(isComputeUnitLimitInstruction, "isComputeUnitLimitInstruction");
1367
+ function didExceedComputeBudget(error) {
1368
+ let current = error;
1369
+ while (kit.isSolanaError(current)) {
1370
+ if (kit.isSolanaError(current, kit.SOLANA_ERROR__INSTRUCTION_ERROR__COMPUTATIONAL_BUDGET_EXCEEDED)) {
1371
+ return true;
1372
+ }
1373
+ current = current.cause;
1374
+ }
1375
+ return false;
1376
+ }
1377
+ __name(didExceedComputeBudget, "didExceedComputeBudget");
1365
1378
  async function estimateComputeUnits(rpc, transaction) {
1366
1379
  let target = transaction;
1367
1380
  const hasLifetime = transaction.lifetimeConstraint !== void 0;
@@ -1373,12 +1386,19 @@ async function estimateComputeUnits(rpc, transaction) {
1373
1386
  );
1374
1387
  }
1375
1388
  const base64Transaction = transactionToBase64(target);
1376
- const { value } = await rpc.simulateTransaction(base64Transaction, {
1377
- encoding: "base64",
1378
- replaceRecentBlockhash: false,
1379
- sigVerify: false
1380
- }).send();
1381
- return Number(value.unitsConsumed ?? 0) || 0;
1389
+ try {
1390
+ const { value } = await rpc.simulateTransaction(base64Transaction, {
1391
+ encoding: "base64",
1392
+ replaceRecentBlockhash: false,
1393
+ sigVerify: false
1394
+ }).send();
1395
+ return Number(value.unitsConsumed ?? 0) || 0;
1396
+ } catch (error) {
1397
+ if (didExceedComputeBudget(error)) {
1398
+ return MAX_COMPUTE_UNIT_LIMIT;
1399
+ }
1400
+ throw error;
1401
+ }
1382
1402
  }
1383
1403
  __name(estimateComputeUnits, "estimateComputeUnits");
1384
1404
  async function prepareTransaction(config) {
@@ -1389,7 +1409,11 @@ async function prepareTransaction(config) {
1389
1409
  const computeLimitIndex = transaction.instructions.findIndex(isComputeUnitLimitInstruction);
1390
1410
  if (computeLimitIndex === -1 || shouldResetComputeUnits) {
1391
1411
  const unitsFromSimulation = await estimateComputeUnits(config.rpc, transaction);
1392
- const units = Math.max(1, unitsFromSimulation ? Math.ceil(unitsFromSimulation * multiplier) : 2e5);
1412
+ const estimatedUnits = unitsFromSimulation ? Math.ceil(unitsFromSimulation * multiplier) : DEFAULT_COMPUTE_UNIT_LIMIT;
1413
+ const units = Math.min(
1414
+ MAX_COMPUTE_UNIT_LIMIT,
1415
+ Math.max(DEFAULT_COMPUTE_UNIT_LIMIT, Math.max(1, estimatedUnits))
1416
+ );
1393
1417
  const instruction = computeBudget.getSetComputeUnitLimitInstruction({ units });
1394
1418
  if (computeLimitIndex === -1) {
1395
1419
  transaction = kit.appendTransactionMessageInstruction(instruction, transaction);