-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathglobal.js
More file actions
178 lines (160 loc) · 5.33 KB
/
Copy pathglobal.js
File metadata and controls
178 lines (160 loc) · 5.33 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { createContext, useCallback, useEffect, useState } from "react";
import { LAMPORTS_PER_SOL } from "@solana/web3.js";
import { useAnchorWallet, useConnection } from "@solana/wallet-adapter-react";
import { getMasterAccountPk, getPrgram, getBetAccountPk } from "../utils/program";
import toast from "react-hot-toast";
export const GlobalContext = createContext({
isConnected: null,
waLLet: null,
hasUserAccount: null,
allBets: null,
fetchBets: null,
})
export const GlobalState = ({children}) => {
const [program, setProgram] = useState();
const [isConnected, setIsConnected] = useState();
const [masterAccount, setMasterAccount] = useState();
const [allBets, setAllBets] = useState();
const [userBets, setUserBets] = useState();
const {connection} = useConnection();
const waLLet = useAnchorWallet();
// Set Program
useEffect(() => {
if(connection) {
setProgram(getPrgram(connection, waLLet ?? {}))
} else{
setProgram(null);
}
}, [connection,waLLet])
//Check wallet connection
useEffect(() => {
setIsConnected(!!waLLet?.publicKey)
}, [waLLet]);
const fetchMasterAccount = useCallback(async () => {
if(!program) return;
try{
const masterAccountPk = await getMasterAccountPk();
const masterAccount = await program.account.master.fetch(masterAccountPk);
setMasterAccount(masterAccount);
} catch(e){
console.log("could not fetch master account: ", e.message);
setMasterAccount(null);
}
})
// check for master account
useEffect(() => {
if(!masterAccount && program) {
fetchMasterAccount();
}
}, [masterAccount, program])
const fetchBets = useCallback(async () => {
if(!program) return;
const allBetsResult = await program.account.bet.all();
const allBets = allBetsResult.map((bet) => bet.account);
setAllBets(allBets);
}, [program])
useEffect(() => {
// fetch all bets if allbets doesn't exist.
if(!allBets){
fetchBets();
}
}, [allBets, fetchBets])
const createBet = useCallback(
async(amount, price, duration, pythPrice) => {
if(!masterAccount) return;
try{
const betId = masterAccount.lastBetId.addn(1);
const res = await getBetAccountPk(betId);
console.log({betPk: res});
const txHash = await program.methods
.createBet(amount, price, duration, pythPrice)
.accounts({
bet: await getBetAccountPk(betId),
master: await getMasterAccountPk(),
player: waLLet.publicKey,
})
.rpc()
await connection.confirmTransaction(txHash);
console.log("Created bet!", txHash);
toast.success("Created bet!")
} catch(e) {
toast.error("Failed to create bet!");
console.log(e.message);
}
},
[masterAccount]
)
const closeBet = useCallback(
async(bet) => {
if(!masterAccount) return;
try{
const txHash = await program.methods
.closeBet()
.accounts({
bet: await getBetAccountPk(bet.id),
player: waLLet.publicKey
})
.rpc()
toast.success("Closed Bet")
} catch(e) {
toast.error("Failed to close bet")
console.log("Could not close bet", e.message);
}
},[masterAccount]
)
const enterBet = useCallback(
async(price, bet) => {
if(!masterAccount) return;
try{
const txHash = await program.methods
.enterBet(price)
.accounts({
bet: await getBetAccountPk(bet.id),
player: waLLet.publicKey
})
.rpc();
toast.success("Entered Bet!");
}
catch(e) {
console.log("Couldn't enter bet", e.message);
toast.error("Failed to enter bet!");
}
},[masterAccount]
)
const claimBet = useCallback(
async(bet) => {
if(!masterAccount) return;
try{
const txHash = await program.methods
.claimBet()
.account({
bet: await getBetAccountPk(bet.id),
pyth: bet.pythPriceKey,
playerA: bet.predictionA.player,
playerB: bet.predictionB.player,
signer: waLLet.publicKey,
})
.rpc()
console.log("Claimed Bet");
}
catch(e){
console.log("Couldn't claim", e.message);
toast.error("Failed to claim");
}
},[masterAccount]
)
return (
<GlobalContext.Provider
value={{
masterAccount,
allBets,
createBet,
closeBet,
enterBet,
claimBet,
}}
>
{children}
</GlobalContext.Provider>
)
}