-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkspaceProvider.tsx
55 lines (45 loc) · 1.35 KB
/
WorkspaceProvider.tsx
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
46
47
48
49
50
51
52
53
54
55
import { createContext, useContext } from "react"
import {
Program,
AnchorProvider,
Idl,
setProvider,
} from "@project-serum/anchor"
import { AnchorTreasureHunters, IDL } from "../utils/anchor_treasure_hunters"
import { Connection } from "@solana/web3.js"
import { useAnchorWallet, useConnection } from "@solana/wallet-adapter-react"
import { PROGRAM_ID } from "../utils/constants"
const WorkspaceContext = createContext({})
const programId = PROGRAM_ID
interface Workspace {
connection?: Connection
provider?: AnchorProvider
program?: Program<AnchorTreasureHunters>
}
const WorkspaceProvider = ({ children }: any) => {
const wallet = useAnchorWallet() || MockWallet
const { connection } = useConnection()
const provider = new AnchorProvider(connection, wallet, {})
setProvider(provider)
const program = new Program(IDL as Idl, programId)
const workspace = {
connection,
provider,
program,
}
return (
<WorkspaceContext.Provider value={workspace}>
{children}
</WorkspaceContext.Provider>
)
}
const useWorkspace = (): Workspace => {
return useContext(WorkspaceContext)
}
import { Keypair } from "@solana/web3.js"
const MockWallet = {
publicKey: Keypair.generate().publicKey,
signTransaction: () => Promise.reject(),
signAllTransactions: () => Promise.reject(),
}
export { WorkspaceProvider, useWorkspace }