44 MessageCompiledInstruction ,
55 Keypair ,
66 Message ,
7+ ConfirmOptions ,
78} from "@solana/web3.js" ;
89import {
910 Program ,
@@ -42,7 +43,7 @@ export async function getIdlByPath<Idl>(idlPath: string): Promise<Idl> {
4243 * Fetches an Anchor IDL from a program on-chain
4344 *
4445 * @param programId - Public key of the program
45- * @param connection - Solana connection object
46+ * @param provider - Anchor Provider instance (you can use createProviderForConnection to create)
4647 * @returns The fetched IDL
4748 * @throws If IDL cannot be found for the program
4849 *
@@ -56,27 +57,45 @@ export async function getIdlByPath<Idl>(idlPath: string): Promise<Idl> {
5657 */
5758export async function getIdlByProgramId < Idl > (
5859 programId : PublicKey ,
59- connection : Connection ,
60+ provider : AnchorProvider ,
6061) : Promise < Idl > {
61- let wallet = new NodeWallet ( new Keypair ( ) ) ;
62- const provider = new AnchorProvider ( connection , wallet , {
63- commitment : "confirmed" ,
64- } ) ;
65-
6662 var idl = await Program . fetchIdl ( programId , provider ) ;
6763 if ( ! idl )
6864 throw new Error ( `IDL not found for program ${ programId . toString ( ) } ` ) ;
6965
7066 return idl as Idl ;
7167}
7268
69+ /**
70+ * Creates an Anchor provider for a given connection
71+ *
72+ * @param connection - The Solana connection object
73+ * @param keypair - Optional keypair to use for the provider (defaults to a new random keypair)
74+ * @param options - Optional configuration options for the provider
75+ * @returns An Anchor provider instance
76+ *
77+ * @example
78+ * ```typescript
79+ * const provider = createProviderForConnection(connection);
80+ * ```
81+ */
82+ export function createProviderForConnection (
83+ connection : Connection ,
84+ keypair : Keypair = new Keypair ( ) ,
85+ options : ConfirmOptions = {
86+ commitment : "confirmed" ,
87+ } ,
88+ ) : AnchorProvider {
89+ return new AnchorProvider ( connection , new NodeWallet ( keypair ) , options ) ;
90+ }
91+
7392/**
7493 * Fetches and parses an account's data using an Anchor IDL
7594 *
7695 * @param idl - The Anchor IDL (use getIdlByProgramId or getIdlByPath to obtain)
7796 * @param accountName - The name of the account as defined in the IDL
7897 * @param accountAddress - The public key of the account to fetch
79- * @param connection - Optional connection object (uses default provider if not specified )
98+ * @param provider - Anchor Provider instance (you can use createProviderForConnection to create )
8099 * @param programId - Optional program ID needed for legacy IDLs
81100 * @returns The decoded account data
82101 *
@@ -90,17 +109,9 @@ export async function getIdlParsedAccountData<T = any>(
90109 idl : Idl ,
91110 accountName : string ,
92111 accountAddress : PublicKey ,
93- connection : Connection ,
112+ provider : AnchorProvider ,
94113 programId ?: PublicKey ,
95114) : Promise < T > {
96- // Get or create provider
97- let wallet = new NodeWallet ( new Keypair ( ) ) ;
98-
99- const provider = new AnchorProvider ( connection , wallet , {
100- commitment : "confirmed" ,
101- } ) ;
102-
103- // Create program
104115 const program = new Program ( formatIdl ( idl , programId ?. toString ( ) ) , provider ) ;
105116
106117 const accountInfo = await provider . connection . getAccountInfo ( accountAddress ) ;
@@ -117,7 +128,7 @@ export async function getIdlParsedAccountData<T = any>(
117128 *
118129 * @param idl - The Anchor IDL (use getIdlByProgramId or getIdlByPath to obtain)
119130 * @param signature - Transaction signature to parse events from
120- * @param connection - Connection object (uses default provider if not specified )
131+ * @param provider - Anchor Provider instance (you can use createProviderForConnection to create )
121132 * @param programId - Optional program ID needed for legacy IDLs
122133 * @returns Array of parsed events with their name and data
123134 *
@@ -130,20 +141,14 @@ export async function getIdlParsedAccountData<T = any>(
130141export async function parseAnchorTransactionEvents (
131142 idl : Idl ,
132143 signature : string ,
133- connection : Connection ,
144+ provider : AnchorProvider ,
134145 programId ?: PublicKey ,
135146) : Promise <
136147 Array < {
137148 name : string ;
138149 data : any ;
139150 } >
140151> {
141- let wallet = new NodeWallet ( new Keypair ( ) ) ;
142-
143- const provider = new AnchorProvider ( connection , wallet , {
144- commitment : "confirmed" ,
145- } ) ;
146-
147152 const program = new Program ( formatIdl ( idl , programId ?. toString ( ) ) , provider ) ;
148153 const parser = new EventParser ( program . programId , program . coder ) ;
149154
@@ -215,13 +220,9 @@ export type DecodedTransaction = {
215220export async function decodeAnchorTransaction (
216221 idl : Idl ,
217222 signature : string ,
218- connection : Connection ,
223+ provider : AnchorProvider ,
219224 programId ?: PublicKey ,
220225) : Promise < DecodedTransaction > {
221- let wallet = new NodeWallet ( new Keypair ( ) ) ;
222- const provider = new AnchorProvider ( connection , wallet , {
223- commitment : "confirmed" ,
224- } ) ;
225226 const program = new Program ( formatIdl ( idl , programId ?. toString ( ) ) , provider ) ;
226227 const accountsCoder = new BorshAccountsCoder ( program . idl ) ;
227228 const instructionCoder = new BorshInstructionCoder ( program . idl ) ;
0 commit comments