Skip to content

Commit 7bffcd3

Browse files
committed
fix(svm): restore explicit SetComputeUnitLimit instruction
@solana/kit's setTransactionMessageComputeUnitLimit sets a transaction-level property instead of prepending an explicit ComputeBudget instruction. The x402 facilitator expects the actual instruction in the transaction, causing invalid_compute_limit_instruction errors. Build the instruction manually (discriminator 0x02 + u32 LE) and prepend it, matching the original @solana-program/compute-budget behavior without the dependency.
1 parent bf5c3a3 commit 7bffcd3

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

packages/x402-proxy/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "x402-proxy",
33
"private": true,
4-
"version": "0.10.11",
4+
"version": "0.10.12",
55
"description": "curl for x402 paid APIs. Auto-pays any endpoint on Base, Solana, and Tempo.",
66
"type": "module",
77
"sideEffects": false,

packages/x402-proxy/src/lib/optimized-svm-scheme.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
mainnet,
1010
partiallySignTransactionMessageWithSigners,
1111
pipe,
12+
prependTransactionMessageInstruction,
1213
SOLANA_ERROR__RPC__TRANSPORT_HTTP_ERROR,
13-
setTransactionMessageComputeUnitLimit,
1414
setTransactionMessageComputeUnitPrice,
1515
setTransactionMessageFeePayer,
1616
setTransactionMessageLifetimeUsingBlockhash,
@@ -24,9 +24,18 @@ import {
2424
import type { PaymentRequirements, SchemeNetworkClient } from "@x402/core/types";
2525

2626
const MEMO_PROGRAM_ADDRESS: Address = "MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr" as Address;
27+
const COMPUTE_BUDGET_PROGRAM: Address = "ComputeBudget111111111111111111111111111111" as Address;
2728
const COMPUTE_UNIT_LIMIT = 20_000;
2829
const COMPUTE_UNIT_PRICE_MICROLAMPORTS = 1n;
2930

31+
/** Build a SetComputeUnitLimit instruction (discriminator 0x02 + u32 LE). */
32+
function getSetComputeUnitLimitInstruction(units: number) {
33+
const data = new Uint8Array(5);
34+
data[0] = 0x02;
35+
new DataView(data.buffer).setUint32(1, units, true);
36+
return { programAddress: COMPUTE_BUDGET_PROGRAM, accounts: [] as const, data };
37+
}
38+
3039
const USDC_MINT: Address = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" as Address;
3140
const USDC_DECIMALS = 6;
3241

@@ -151,8 +160,12 @@ export class OptimizedSvmScheme implements SchemeNetworkClient {
151160
const tx = pipe(
152161
createTransactionMessage({ version: 0 }),
153162
(tx) => setTransactionMessageComputeUnitPrice(COMPUTE_UNIT_PRICE_MICROLAMPORTS, tx),
154-
(tx) => setTransactionMessageComputeUnitLimit(COMPUTE_UNIT_LIMIT, tx),
155163
(tx) => setTransactionMessageFeePayer(feePayer, tx),
164+
(tx) =>
165+
prependTransactionMessageInstruction(
166+
getSetComputeUnitLimitInstruction(COMPUTE_UNIT_LIMIT),
167+
tx,
168+
),
156169
(tx) => appendTransactionMessageInstructions([transferIx, memoIx], tx),
157170
(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
158171
);

0 commit comments

Comments
 (0)