@@ -14,13 +14,15 @@ import type { SolanaClient } from "gill";
1414import type { TransactionStatusEvent } from "../../types.js" ;
1515import {
1616 getSignatureFromBytes ,
17+ parseTransactionError ,
1718 pollConfirmTransaction ,
1819} from "@macalinao/gill-extra" ;
1920import {
2021 compressTransactionMessageUsingAddressLookupTables ,
22+ getSolanaErrorFromTransactionError ,
2123 signAndSendTransactionMessageWithSigners ,
2224} from "@solana/kit" ;
23- import { createTransaction } from "gill" ;
25+ import { createTransaction , simulateTransactionFactory } from "gill" ;
2426
2527export interface CreateSendTXParams {
2628 signer : TransactionSendingSigner | null ;
@@ -41,6 +43,7 @@ export const createSendTX = ({
4143 onTransactionStatusEvent,
4244 getExplorerLink,
4345} : CreateSendTXParams ) : SendTXFunction => {
46+ const simulateTransaction = simulateTransactionFactory ( { rpc } ) ;
4447 return async (
4548 name : string ,
4649 ixs : readonly Instruction [ ] ,
@@ -70,15 +73,8 @@ export const createSendTX = ({
7073 feePayer : signer ,
7174 instructions : [ ...ixs ] ,
7275 latestBlockhash,
73- // the compute budget values are HIGHLY recommend to be set in order to maximize your transaction landing rate
74- computeUnitLimit :
75- options . computeUnitLimit === null
76- ? undefined
77- : ( options . computeUnitLimit ?? 1_400_000 ) ,
78- computeUnitPrice :
79- options . computeUnitPrice === null
80- ? undefined
81- : ( options . computeUnitPrice ?? 100_000n ) ,
76+ computeUnitLimit : options . computeUnitLimit ,
77+ computeUnitPrice : options . computeUnitPrice ,
8278 } ) ;
8379
8480 // Apply address lookup tables if provided to compress the transaction
@@ -91,6 +87,24 @@ export const createSendTX = ({
9187 )
9288 : transactionMessage ;
9389
90+ // preflight
91+ if ( ! options . skipPreflight ) {
92+ const simulationResult = await simulateTransaction (
93+ finalTransactionMessage ,
94+ ) ;
95+ if ( simulationResult . value . err ) {
96+ onTransactionStatusEvent ( {
97+ ...baseEvent ,
98+ type : "error-simulation-failed" ,
99+ errorMessage : parseTransactionError (
100+ simulationResult . value . err ,
101+ simulationResult . value . logs ,
102+ ) ,
103+ } ) ;
104+ throw getSolanaErrorFromTransactionError ( simulationResult . value . err ) ;
105+ }
106+ }
107+
94108 onTransactionStatusEvent ( {
95109 ...baseEvent ,
96110 type : "awaiting-wallet-signature" ,
0 commit comments