Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/happy-areas-spend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@macalinao/gill-extra": patch
"@macalinao/grill": patch
---

Add parseTransactionError, new error type
6 changes: 6 additions & 0 deletions .changeset/silent-suits-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@macalinao/gill-extra": minor
"@macalinao/grill": minor
---

Change compute unit stuff to be not included by default
1 change: 1 addition & 0 deletions packages/gill-extra/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export * from "./get-solscan-explorer-link.js";
export * from "./ixs/index.js";
export * from "./poll-confirm-transaction.js";
export * from "./transaction.js";
export * from "./transaction-error.js";
export * from "./types.js";
22 changes: 22 additions & 0 deletions packages/gill-extra/src/transaction-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { TransactionError } from "@solana/kit";
import { getSolanaErrorFromTransactionError } from "@solana/kit";

export const parseTransactionError = (
err: TransactionError,
logs: string[] | null,
): string => {
// First, try to extract Anchor error from logs
const anchorError = [...(logs ?? [])]
.reverse()
.find((log) => log.includes("AnchorError"));

if (anchorError) {
const errorMessageStart = anchorError.indexOf("Error Message: ");
if (errorMessageStart !== -1) {
return anchorError.slice(errorMessageStart + 15).trim();
}
}

const solanaError = getSolanaErrorFromTransactionError(err);
return solanaError.message;
};
24 changes: 12 additions & 12 deletions packages/gill-extra/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@ import type {
Instruction,
Signature,
} from "@solana/kit";
import type { CreateTransactionInput } from "gill";

export interface SendTXOptions {
lookupTables?: AddressesByLookupTableAddress;
/**
* Compute unit limit for the transaction.
* Set to null to omit compute unit limit instruction.
* Defaults to 1,400,000 if not specified.
*/
computeUnitLimit?: number | null;
export interface SendTXOptions
extends Pick<
CreateTransactionInput<0>,
"computeUnitLimit" | "computeUnitPrice"
> {
/**
* Compute unit price for the transaction in microlamports.
* Set to null to omit compute unit price instruction.
* Defaults to 100,000 if not specified.
* Address lookup tables (optional)
*/
computeUnitPrice?: bigint | null;
lookupTables?: AddressesByLookupTableAddress;
/**
* Whether to wait for account refetch after transaction confirmation.
* When true (default), the function will wait for all writable accounts
Expand All @@ -28,6 +24,10 @@ export interface SendTXOptions {
* @default true
*/
waitForAccountRefetch?: boolean;
/**
* If true, skips the pre-flight simulation.
*/
skipPreflight?: boolean;
}

export type SendTXFunction = (
Expand Down
21 changes: 21 additions & 0 deletions packages/grill/src/providers/grill-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,27 @@ export const GrillProvider: FC<GrillProviderProps> = ({
toastIds.current.delete(txId);
break;
}
case "error-simulation-failed": {
console.error("Simulation failed", event);
const description = `Simulation failed: ${event.errorMessage}`;
if (existingToastId) {
// Update existing toast to error
toast.error(event.title, {
id: existingToastId,
description,
duration: errorToastDuration,
});
} else {
// Create new error toast if somehow we don't have one
toast.error(event.title, {
description,
duration: errorToastDuration,
});
}
// Clean up toast ID after error
toastIds.current.delete(txId);
break;
}
}
},
[
Expand Down
4 changes: 4 additions & 0 deletions packages/grill/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export type TransactionStatusEvent = {
sig: Signature;
explorerLink: string;
}
| {
type: "error-simulation-failed";
errorMessage: string;
}
);

export type TransactionStatusEventCallback = (
Expand Down
34 changes: 24 additions & 10 deletions packages/grill/src/utils/internal/create-send-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import type { SolanaClient } from "gill";
import type { TransactionStatusEvent } from "../../types.js";
import {
getSignatureFromBytes,
parseTransactionError,
pollConfirmTransaction,
} from "@macalinao/gill-extra";
import {
compressTransactionMessageUsingAddressLookupTables,
getSolanaErrorFromTransactionError,
signAndSendTransactionMessageWithSigners,
} from "@solana/kit";
import { createTransaction } from "gill";
import { createTransaction, simulateTransactionFactory } from "gill";

export interface CreateSendTXParams {
signer: TransactionSendingSigner | null;
Expand All @@ -41,6 +43,7 @@ export const createSendTX = ({
onTransactionStatusEvent,
getExplorerLink,
}: CreateSendTXParams): SendTXFunction => {
const simulateTransaction = simulateTransactionFactory({ rpc });
return async (
name: string,
ixs: readonly Instruction[],
Expand Down Expand Up @@ -70,15 +73,8 @@ export const createSendTX = ({
feePayer: signer,
instructions: [...ixs],
latestBlockhash,
// the compute budget values are HIGHLY recommend to be set in order to maximize your transaction landing rate
computeUnitLimit:
options.computeUnitLimit === null
? undefined
: (options.computeUnitLimit ?? 1_400_000),
computeUnitPrice:
options.computeUnitPrice === null
? undefined
: (options.computeUnitPrice ?? 100_000n),
computeUnitLimit: options.computeUnitLimit,
computeUnitPrice: options.computeUnitPrice,
});

// Apply address lookup tables if provided to compress the transaction
Expand All @@ -91,6 +87,24 @@ export const createSendTX = ({
)
: transactionMessage;

// preflight
if (!options.skipPreflight) {
const simulationResult = await simulateTransaction(
finalTransactionMessage,
);
if (simulationResult.value.err) {
onTransactionStatusEvent({
...baseEvent,
type: "error-simulation-failed",
errorMessage: parseTransactionError(
simulationResult.value.err,
simulationResult.value.logs,
),
});
throw getSolanaErrorFromTransactionError(simulationResult.value.err);
}
}

onTransactionStatusEvent({
...baseEvent,
type: "awaiting-wallet-signature",
Expand Down