@@ -9,28 +9,23 @@ import {
99 mainnet ,
1010 partiallySignTransactionMessageWithSigners ,
1111 pipe ,
12- prependTransactionMessageInstruction ,
1312 SOLANA_ERROR__RPC__TRANSPORT_HTTP_ERROR ,
13+ setTransactionMessageComputeUnitLimit ,
14+ setTransactionMessageComputeUnitPrice ,
1415 setTransactionMessageFeePayer ,
1516 setTransactionMessageLifetimeUsingBlockhash ,
1617 type TransactionSigner ,
1718} from "@solana/kit" ;
1819import {
19- getSetComputeUnitLimitInstruction ,
20- setTransactionMessageComputeUnitPrice ,
21- } from "@solana-program/compute-budget" ;
22- import { TOKEN_PROGRAM_ADDRESS } from "@solana-program/token" ;
23- import {
24- fetchMint ,
2520 findAssociatedTokenPda ,
2621 getTransferCheckedInstruction ,
27- TOKEN_2022_PROGRAM_ADDRESS ,
28- } from "@solana-program/token-2022 " ;
22+ TOKEN_PROGRAM_ADDRESS ,
23+ } from "@solana-program/token" ;
2924import type { PaymentRequirements , SchemeNetworkClient } from "@x402/core/types" ;
3025
3126const MEMO_PROGRAM_ADDRESS : Address = "MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr" as Address ;
3227const COMPUTE_UNIT_LIMIT = 20_000 ;
33- const COMPUTE_UNIT_PRICE_MICROLAMPORTS = 1 ;
28+ const COMPUTE_UNIT_PRICE_MICROLAMPORTS = 1n ;
3429
3530const USDC_MINT : Address = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" as Address ;
3631const USDC_DECIMALS = 6 ;
@@ -107,50 +102,34 @@ export class OptimizedSvmScheme implements SchemeNetworkClient {
107102
108103 async createPaymentPayload ( x402Version : number , paymentRequirements : PaymentRequirements ) {
109104 const rpc = this . rpc ;
110-
111105 const asset = paymentRequirements . asset as Address ;
112106
113- let tokenProgramAddress : Address ;
114- let decimals : number ;
115- if ( asset === USDC_MINT ) {
116- tokenProgramAddress = TOKEN_PROGRAM_ADDRESS ;
117- decimals = USDC_DECIMALS ;
118- } else {
119- const tokenMint = await fetchMint ( rpc , asset ) ;
120- tokenProgramAddress = tokenMint . programAddress ;
121- if (
122- tokenProgramAddress !== TOKEN_PROGRAM_ADDRESS &&
123- tokenProgramAddress !== TOKEN_2022_PROGRAM_ADDRESS
124- ) {
125- throw new Error ( "Asset was not created by a known token program" ) ;
126- }
127- decimals = tokenMint . data . decimals ;
107+ if ( asset !== USDC_MINT ) {
108+ throw new Error ( `Unsupported asset: ${ asset } . Only USDC is supported.` ) ;
128109 }
129110
130- const [ sourceATA ] = await findAssociatedTokenPda ( {
131- mint : asset ,
132- owner : this . signer . address ,
133- tokenProgram : tokenProgramAddress ,
134- } ) ;
111+ const [ [ sourceATA ] , [ destinationATA ] ] = await Promise . all ( [
112+ findAssociatedTokenPda ( {
113+ mint : asset ,
114+ owner : this . signer . address ,
115+ tokenProgram : TOKEN_PROGRAM_ADDRESS ,
116+ } ) ,
117+ findAssociatedTokenPda ( {
118+ mint : asset ,
119+ owner : paymentRequirements . payTo as Address ,
120+ tokenProgram : TOKEN_PROGRAM_ADDRESS ,
121+ } ) ,
122+ ] ) ;
135123
136- const [ destinationATA ] = await findAssociatedTokenPda ( {
124+ const transferIx = getTransferCheckedInstruction ( {
125+ source : sourceATA ,
137126 mint : asset ,
138- owner : paymentRequirements . payTo as Address ,
139- tokenProgram : tokenProgramAddress ,
127+ destination : destinationATA ,
128+ authority : this . signer ,
129+ amount : BigInt ( paymentRequirements . amount ) ,
130+ decimals : USDC_DECIMALS ,
140131 } ) ;
141132
142- const transferIx = getTransferCheckedInstruction (
143- {
144- source : sourceATA ,
145- mint : asset ,
146- destination : destinationATA ,
147- authority : this . signer ,
148- amount : BigInt ( paymentRequirements . amount ) ,
149- decimals,
150- } ,
151- { programAddress : tokenProgramAddress } ,
152- ) ;
153-
154133 const feePayer = paymentRequirements . extra ?. feePayer as Address ;
155134 if ( ! feePayer ) {
156135 throw new Error ( "feePayer is required in paymentRequirements.extra for SVM transactions" ) ;
@@ -172,12 +151,8 @@ export class OptimizedSvmScheme implements SchemeNetworkClient {
172151 const tx = pipe (
173152 createTransactionMessage ( { version : 0 } ) ,
174153 ( tx ) => setTransactionMessageComputeUnitPrice ( COMPUTE_UNIT_PRICE_MICROLAMPORTS , tx ) ,
154+ ( tx ) => setTransactionMessageComputeUnitLimit ( COMPUTE_UNIT_LIMIT , tx ) ,
175155 ( tx ) => setTransactionMessageFeePayer ( feePayer , tx ) ,
176- ( tx ) =>
177- prependTransactionMessageInstruction (
178- getSetComputeUnitLimitInstruction ( { units : COMPUTE_UNIT_LIMIT } ) ,
179- tx ,
180- ) ,
181156 ( tx ) => appendTransactionMessageInstructions ( [ transferIx , memoIx ] , tx ) ,
182157 ( tx ) => setTransactionMessageLifetimeUsingBlockhash ( latestBlockhash , tx ) ,
183158 ) ;
0 commit comments