4
4
MessageCompiledInstruction ,
5
5
Keypair ,
6
6
Message ,
7
+ ConfirmOptions ,
7
8
} from "@solana/web3.js" ;
8
9
import {
9
10
Program ,
@@ -42,7 +43,7 @@ export async function getIdlByPath<Idl>(idlPath: string): Promise<Idl> {
42
43
* Fetches an Anchor IDL from a program on-chain
43
44
*
44
45
* @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)
46
47
* @returns The fetched IDL
47
48
* @throws If IDL cannot be found for the program
48
49
*
@@ -56,27 +57,45 @@ export async function getIdlByPath<Idl>(idlPath: string): Promise<Idl> {
56
57
*/
57
58
export async function getIdlByProgramId < Idl > (
58
59
programId : PublicKey ,
59
- connection : Connection ,
60
+ provider : AnchorProvider ,
60
61
) : Promise < Idl > {
61
- let wallet = new NodeWallet ( new Keypair ( ) ) ;
62
- const provider = new AnchorProvider ( connection , wallet , {
63
- commitment : "confirmed" ,
64
- } ) ;
65
-
66
62
var idl = await Program . fetchIdl ( programId , provider ) ;
67
63
if ( ! idl )
68
64
throw new Error ( `IDL not found for program ${ programId . toString ( ) } ` ) ;
69
65
70
66
return idl as Idl ;
71
67
}
72
68
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
+
73
92
/**
74
93
* Fetches and parses an account's data using an Anchor IDL
75
94
*
76
95
* @param idl - The Anchor IDL (use getIdlByProgramId or getIdlByPath to obtain)
77
96
* @param accountName - The name of the account as defined in the IDL
78
97
* @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 )
80
99
* @param programId - Optional program ID needed for legacy IDLs
81
100
* @returns The decoded account data
82
101
*
@@ -90,17 +109,9 @@ export async function getIdlParsedAccountData<T = any>(
90
109
idl : Idl ,
91
110
accountName : string ,
92
111
accountAddress : PublicKey ,
93
- connection : Connection ,
112
+ provider : AnchorProvider ,
94
113
programId ?: PublicKey ,
95
114
) : 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
104
115
const program = new Program ( formatIdl ( idl , programId ?. toString ( ) ) , provider ) ;
105
116
106
117
const accountInfo = await provider . connection . getAccountInfo ( accountAddress ) ;
@@ -117,7 +128,7 @@ export async function getIdlParsedAccountData<T = any>(
117
128
*
118
129
* @param idl - The Anchor IDL (use getIdlByProgramId or getIdlByPath to obtain)
119
130
* @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 )
121
132
* @param programId - Optional program ID needed for legacy IDLs
122
133
* @returns Array of parsed events with their name and data
123
134
*
@@ -130,20 +141,14 @@ export async function getIdlParsedAccountData<T = any>(
130
141
export async function parseAnchorTransactionEvents (
131
142
idl : Idl ,
132
143
signature : string ,
133
- connection : Connection ,
144
+ provider : AnchorProvider ,
134
145
programId ?: PublicKey ,
135
146
) : Promise <
136
147
Array < {
137
148
name : string ;
138
149
data : any ;
139
150
} >
140
151
> {
141
- let wallet = new NodeWallet ( new Keypair ( ) ) ;
142
-
143
- const provider = new AnchorProvider ( connection , wallet , {
144
- commitment : "confirmed" ,
145
- } ) ;
146
-
147
152
const program = new Program ( formatIdl ( idl , programId ?. toString ( ) ) , provider ) ;
148
153
const parser = new EventParser ( program . programId , program . coder ) ;
149
154
@@ -215,13 +220,9 @@ export type DecodedTransaction = {
215
220
export async function decodeAnchorTransaction (
216
221
idl : Idl ,
217
222
signature : string ,
218
- connection : Connection ,
223
+ provider : AnchorProvider ,
219
224
programId ?: PublicKey ,
220
225
) : Promise < DecodedTransaction > {
221
- let wallet = new NodeWallet ( new Keypair ( ) ) ;
222
- const provider = new AnchorProvider ( connection , wallet , {
223
- commitment : "confirmed" ,
224
- } ) ;
225
226
const program = new Program ( formatIdl ( idl , programId ?. toString ( ) ) , provider ) ;
226
227
const accountsCoder = new BorshAccountsCoder ( program . idl ) ;
227
228
const instructionCoder = new BorshInstructionCoder ( program . idl ) ;
0 commit comments