@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.
@@ -1,4 +1,4 @@
1
- import { createSolanaRpc, createSolanaRpcSubscriptions, address, getBase64EncodedWireTransaction, compileTransaction, partiallySignTransactionMessageWithSigners, appendTransactionMessageInstruction, setTransactionMessageLifetimeUsingBlockhash, isTransactionSendingSigner, getMessagePackerInstructionPlanFromInstructions, signature, AccountRole, pipe, createTransactionMessage, setTransactionMessageFeePayer, singleTransactionPlan, signAndSendTransactionMessageWithSigners, createTransactionPlanExecutor, signTransactionMessageWithSigners, isSolanaError, SOLANA_ERROR__TRANSACTION_ERROR__ALREADY_PROCESSED, createTransactionPlanner, setTransactionMessageFeePayerSigner, isTransactionPartialSigner, isInstructionForProgram, isInstructionWithData, SolanaError, SOLANA_ERROR__INSTRUCTION_ERROR__GENERIC_ERROR, airdropFactory, signatureBytes } from '@solana/kit';
1
+ import { createSolanaRpc, createSolanaRpcSubscriptions, address, getBase64EncodedWireTransaction, compileTransaction, partiallySignTransactionMessageWithSigners, appendTransactionMessageInstruction, setTransactionMessageLifetimeUsingBlockhash, isTransactionSendingSigner, getMessagePackerInstructionPlanFromInstructions, signature, AccountRole, pipe, createTransactionMessage, setTransactionMessageFeePayer, singleTransactionPlan, signAndSendTransactionMessageWithSigners, createTransactionPlanExecutor, signTransactionMessageWithSigners, isSolanaError, SOLANA_ERROR__TRANSACTION_ERROR__ALREADY_PROCESSED, createTransactionPlanner, setTransactionMessageFeePayerSigner, isTransactionPartialSigner, SOLANA_ERROR__INSTRUCTION_ERROR__COMPUTATIONAL_BUDGET_EXCEEDED, isInstructionForProgram, isInstructionWithData, SolanaError, SOLANA_ERROR__INSTRUCTION_ERROR__GENERIC_ERROR, airdropFactory, signatureBytes } from '@solana/kit';
2
2
  import { createBlockHeightExceedencePromiseFactory, createRecentSignatureConfirmationPromiseFactory, waitForRecentTransactionConfirmation } from '@solana/transaction-confirmation';
3
3
  import { getBase58Decoder, getBase58Encoder } from '@solana/codecs-strings';
4
4
  import { getTransferSolInstruction } from '@solana-program/system';
@@ -1356,10 +1356,23 @@ __name(transactionToBase64WithSigners, "transactionToBase64WithSigners");
1356
1356
 
1357
1357
  // src/transactions/prepareTransaction.ts
1358
1358
  var DEFAULT_COMPUTE_UNIT_LIMIT_MULTIPLIER = 1.1;
1359
+ var DEFAULT_COMPUTE_UNIT_LIMIT = 2e5;
1360
+ var MAX_COMPUTE_UNIT_LIMIT = 14e5;
1359
1361
  function isComputeUnitLimitInstruction(instruction) {
1360
1362
  return instruction.programAddress === COMPUTE_BUDGET_PROGRAM_ADDRESS && instruction.data?.[0] === 2;
1361
1363
  }
1362
1364
  __name(isComputeUnitLimitInstruction, "isComputeUnitLimitInstruction");
1365
+ function didExceedComputeBudget(error) {
1366
+ let current = error;
1367
+ while (isSolanaError(current)) {
1368
+ if (isSolanaError(current, SOLANA_ERROR__INSTRUCTION_ERROR__COMPUTATIONAL_BUDGET_EXCEEDED)) {
1369
+ return true;
1370
+ }
1371
+ current = current.cause;
1372
+ }
1373
+ return false;
1374
+ }
1375
+ __name(didExceedComputeBudget, "didExceedComputeBudget");
1363
1376
  async function estimateComputeUnits(rpc, transaction) {
1364
1377
  let target = transaction;
1365
1378
  const hasLifetime = transaction.lifetimeConstraint !== void 0;
@@ -1371,12 +1384,19 @@ async function estimateComputeUnits(rpc, transaction) {
1371
1384
  );
1372
1385
  }
1373
1386
  const base64Transaction = transactionToBase64(target);
1374
- const { value } = await rpc.simulateTransaction(base64Transaction, {
1375
- encoding: "base64",
1376
- replaceRecentBlockhash: false,
1377
- sigVerify: false
1378
- }).send();
1379
- return Number(value.unitsConsumed ?? 0) || 0;
1387
+ try {
1388
+ const { value } = await rpc.simulateTransaction(base64Transaction, {
1389
+ encoding: "base64",
1390
+ replaceRecentBlockhash: false,
1391
+ sigVerify: false
1392
+ }).send();
1393
+ return Number(value.unitsConsumed ?? 0) || 0;
1394
+ } catch (error) {
1395
+ if (didExceedComputeBudget(error)) {
1396
+ return MAX_COMPUTE_UNIT_LIMIT;
1397
+ }
1398
+ throw error;
1399
+ }
1380
1400
  }
1381
1401
  __name(estimateComputeUnits, "estimateComputeUnits");
1382
1402
  async function prepareTransaction(config) {
@@ -1387,7 +1407,11 @@ async function prepareTransaction(config) {
1387
1407
  const computeLimitIndex = transaction.instructions.findIndex(isComputeUnitLimitInstruction);
1388
1408
  if (computeLimitIndex === -1 || shouldResetComputeUnits) {
1389
1409
  const unitsFromSimulation = await estimateComputeUnits(config.rpc, transaction);
1390
- const units = Math.max(1, unitsFromSimulation ? Math.ceil(unitsFromSimulation * multiplier) : 2e5);
1410
+ const estimatedUnits = unitsFromSimulation ? Math.ceil(unitsFromSimulation * multiplier) : DEFAULT_COMPUTE_UNIT_LIMIT;
1411
+ const units = Math.min(
1412
+ MAX_COMPUTE_UNIT_LIMIT,
1413
+ Math.max(DEFAULT_COMPUTE_UNIT_LIMIT, Math.max(1, estimatedUnits))
1414
+ );
1391
1415
  const instruction = getSetComputeUnitLimitInstruction({ units });
1392
1416
  if (computeLimitIndex === -1) {
1393
1417
  transaction = appendTransactionMessageInstruction(instruction, transaction);