-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanchorProgram.ts
36 lines (31 loc) · 1010 Bytes
/
anchorProgram.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import * as anchor from '@project-serum/anchor'
import { PublicKey } from '@solana/web3.js';
import { DEVNET_PROGRAM_ID, DEVNET_RPC } from './constants';
import { IDLData, IDLType } from "@/util/idl";
export const getProvider = (wallet: anchor.Wallet, rpc_url?: string) => {
const opts = {
preflightCommitment: 'processed' as anchor.web3.ConfirmOptions,
};
const connectionURI =
rpc_url || process.env.NEXT_PUBLIC_RPC_URL || DEVNET_RPC
const connection = new anchor.web3.Connection(
connectionURI,
opts.preflightCommitment
);
const provider = new anchor.AnchorProvider(
connection,
wallet,
opts.preflightCommitment
);
return provider;
};
export const anchorProgram = (wallet: anchor.Wallet, network?: string) => {
const provider = getProvider(wallet, network);
const idl = IDLData as anchor.Idl;
const program = new anchor.Program(
idl,
new PublicKey(DEVNET_PROGRAM_ID),
provider
) as unknown as anchor.Program<IDLType>;
return program;
};