forked from GitBolt/solana-bank-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.ts
45 lines (39 loc) · 1.32 KB
/
helper.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
37
38
39
40
41
42
43
44
45
export const truncatedPublicKey = (publicKey: string, length?: number) => {
if (!publicKey) return;
if (!length) {
length = 5;
}
return publicKey.replace(publicKey.slice(length, 44 - length), '...');
};
import * as anchor from '@coral-xyz/anchor'
import { PublicKey } from '@solana/web3.js';
import { DEVNET_PROGRAM_ID, LOCALNET_PROGRAM_ID, DEVNET_RPC, LOCALNET_RPC} from '@/util/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 || LOCALNET_RPC
console.log("URI: ", connectionURI)
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(LOCALNET_PROGRAM_ID),
provider
) as unknown as anchor.Program<IDLType>;
return program;
};