-
Notifications
You must be signed in to change notification settings - Fork 0
/
note.jsx
324 lines (292 loc) · 9.52 KB
/
note.jsx
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//const priceClass = new Price();
const [pythOffChainPrice, setPythOffChainPrice] = useState();
useEffect(() => {
// The Pyth price service client is used to retrieve the current Pyth prices and the price update data that
// needs to be posted on-chain with each transaction.
const pythPriceService = new EvmPriceServiceConnection(
PYTH_CONFIG.priceServiceUrl,
{
logger: {
error: console.error,
warn: console.warn,
info: () => undefined,
debug: () => undefined,
trace: () => undefined,
},
}
);
pythPriceService.subscribePriceFeedUpdates(
[PYTH_CONFIG.eth.pythPriceFeedId, PYTH_CONFIG.btc.pythPriceFeedId],
(priceFeed) => {
const price = priceFeed.getPriceUnchecked(); // Fine to use unchecked (not checking for staleness) because this must be a recent price given that it comes from a websocket subscription.
setPythOffChainPrice((prev) => ({ ...prev, [priceFeed.id]: price }));
}
);
}, []);
const ethPriceFeedId = PYTH_CONFIG.eth.pythPriceFeedId;
if (pythOffChainPrice === undefined) {
console.log("pyth is undefined");
} else if (
pythOffChainPrice[
"ca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6"
] === undefined
) {
console.log("ethereum price feed is undefined");
} else {
console.log(
"1245 data is: ",
pythOffChainPrice[
"ca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6"
].getPriceAsNumberUnchecked()
);
}
//const ethPrice = new Price({ pythOffChainPrice });
// Subscribe to the price feeds given by `priceId`. The callback will be invoked every time the requested feed
// gets a price update.
/* let ethereumPythPrice;
connection.subscribePriceFeedUpdates(priceIds, (priceFeed) => {
console.log(
`Received update for ${priceFeed.id}: ${formatUnits(
priceFeed.getPriceNoOlderThan(60).price,
8
)}`
);
ethereumPythPrice = formatUnits(priceFeed.getPriceNoOlderThan(60).price, 8);
setEthereumPrice(ethereumPythPrice);
});
//setEthereumPrice(formatUnits(priceFeed.getPriceNoOlderThan(60).price, 8));
// When using the subscription, make sure to close the websocket upon termination to finish the process gracefully.
setTimeout(() => {
connection.closeWebSocket();
}, 60000); */
/* const connection = new EvmPriceServiceConnection(
"https://xc-testnet.pyth.network"
); // See Price Service endpoints section below for other endpoints
const priceIds = [
// You can find the ids of prices at https://pyth.network/developers/price-feed-ids#pyth-evm-testnet
//"0xf9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b", // BTC/USD price id in testnet
"0xca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6", // ETH/USD price id in testnet
];
*/
/* //orderClose
const { config: orderCloseConfig, orderCloseError } = usePrepareContractWrite(
{
address: orderBookContractAddress,
abi: orderBookAbi.abi,
functionName: "orderClose",
args: [1, 2, priceFeedUpdateData],
value: getUpdateFeeData,
onSuccess(data) {
console.log("Success", data.result);
},
}
);
const {
data: orderCloseData,
isSuccess: orderCloseSuccess,
isLoading: orderCloseIsLoading,
write: orderClose,
} = useContractWrite(orderCloseConfig); */
//orderClose
const {
config: orderCloseConfig,
data: orderClosePrepareData,
error: orderCloseError,
isError: orderCloseIsError,
status: orderClosePrepareStatus,
} = usePrepareContractWrite({
address: orderBookContractAddress,
abi: orderBookAbi.abi,
functionName: "orderClose",
args: [0, 1, mockPythUpdateDataArray],
value: 1,
cacheTime: 5_000,
onSuccess(data) {
console.log("Success", data.result);
},
});
console.log("orderCloseIsError is in error", orderCloseIsError);
console.log("orderCloseConfig is:", orderCloseConfig);
console.log("orderClosePrepareData is: ", orderClosePrepareData);
console.log("Order close prepare status", orderClosePrepareStatus);
const {
data: orderCloseData,
error: orderCloseWriteError,
isSuccess: orderCloseSuccess,
isLoading: orderCloseIsLoading,
status: orderCloseWriteStatus,
write: orderClose,
} = useContractWrite(orderCloseConfig);
console.log("Order Close write data 1245", orderCloseData);
console.log("order close write is loading: ", orderCloseIsLoading);
console.log("write order close error is: ", orderCloseWriteError);
console.log("Order close write status is: ", orderCloseWriteStatus);
//console.log("1245 data is: ", pythOffChainPrice);
const tradeCloseHandler = () => {
console.log("Order close function 1245", orderClose);
orderClose?.();
};
//Maybe for later
const alchemyApiKey = process.env.NEXT_PUBLIC_ALCHEMY_API_KEY;
/* Defining the localchain becasue the provided localchain from WAGMI has a different Id of 1337 */
const localhost = {
id: 31337,
name: "developmentNetwork",
network: "localhost",
nativeCurrency: {
decimals: 18,
name: "Ether",
symbol: "ETH",
},
rpcUrls: {
default: {
http: ["http://127.0.0.1:8545"],
},
public: {
http: ["http://127.0.0.1:8545"],
},
},
};
/* const chains = [
arbitrum,
mainnet,
polygon,
sepolia,
zkSync,
zkSyncTestnet,
localhost,
]; */
const chains = [sepolia, localhost];
const projectId = "b95db88f2294ab412d2b370774f19d3e";
const { publicClient } = configureChains(chains, [
alchemyProvider({ apiKey: alchemyApiKey }),
w3mProvider({ projectId }),
]);
const wagmiConfig = createConfig({
autoConnect: true,
connectors: w3mConnectors({ projectId, chains }),
publicClient,
});
//might need
<div className="grid h-full w-full py-5 md:grid-flow-row">
<div className="border-solid border-4 border-gray-700 rounded-xl shadow-2xl shadow-slate-700 col-span-2 mx-4 flex">
<div className="grid grid-rows-6">
<div className="row-span-1 md:hidden"></div>
<div className="row-span-5 h-fit p-4 max-w-screen">
<TradingViewWidget assetSelect={tradingViewAsset} />
</div>
</div>
</div>
<div className="lg:col-span-1 md:col-span-3">
<div>
<div className="border-solid border-4 border-gray-700 rounded-xl shadow-2xl shadow-slate-700 mx-4 my-10 flex ">
<div className="h-full w-full flex px-6 justify-center">
<InputTradeValues assetChange={tradingViewAssetChangeHandler} />
</div>
</div>
</div>
</div>
</div>;
//removed from the leaderboards page
/* const filterStandings = async (dbRef)={
} */
const standingsData = async () => {
const firebaseDb = await getDatabaseInstance();
const dbRef = ref(firebaseDb, "/standings");
const queryRef = query(dbRef, orderByChild("payouts"), limitToLast(5));
/* return await onValue(
queryRef,
(snapshot) => {
const dataArray = [];
snapshot.forEach((childSnapshot) => {
console.log(childSnapshot.val());
dataArray.push(childSnapshot.val());
});
},
{ onlyOnce: true }
); */
const dataArray = [];
let counter = 0;
const numberOfResults = 5;
const standingsArray = (await get(queryRef)).forEach((snapshot) => {
//dataArray.push(snapshot.val());
if (snapshot.val() == undefined || counter >= numberOfResults) {
return true;
}
counter++;
const standingsObject = { [snapshot.key]: snapshot.val() };
dataArray.push(standingsObject);
//console.log("from the page: ", dataArray);
//console.log("parent snapshot is:", snapshot.val());
/* snapshot.forEach((childSnapshot) => {
//console.log(childSnapshot.val());
//dataArray.push(childSnapshot.val());
if (counter >= numberOfResults) {
return true;
}
}); */
});
/* console.log(
"From the page:!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ",
standingsArray,
counter
); */
if (standingsArray === true) {
return dataArray;
} else if (standingsArray === false) {
setTimeout(() => {
console.log("The timer has kicked in");
return dataArray;
}, 3000);
console.log("something went wrong");
}
// This will return all the data without filtering or ordering
/* const snapshot = await get(child(dbRef, "/"));
if (snapshot.exists()) {
//console.log("from the leaderboards", snapshot.val());
return snapshot.val();
} else {
console.log("No data available");
} */
};
const onValueArray = [];
const getLeaderBoardData = async () => {
const onValueDb = await getDatabaseInstance();
const onValueDbRef = ref(onValueDb, "/standings");
const onValueRef = query(
onValueDbRef,
orderByChild("payouts"),
limitToLast(5)
);
onValue(
onValueRef,
(snapshot) => {
snapshot.forEach((childSnapshot) => {
//console.log("the childrens snapshot is: ", childSnapshot.val());
onValueArray.push(childSnapshot.val());
});
console.log("the data from snapshot is: ", onValueArray);
return onValueArray;
},
{ onlyOnce: true }
);
/* if (onValueArray?.length === 0) {
setTimeout(() => {
console.log("from the timeout function", onValueArray);
return onValueArray;
}, 5000);
} */
};
/* const checkGetLeaderBoardData = async () => {
await getLeaderBoardData();
console.log("inside the new function");
if (onValueArray?.length === 0) {
const timer = setTimeout(() => {
console.log("from the timeout function", onValueArray);
return onValueArray;
}, 5000);
}
};
const standings = await standingsData(); */
//console.log("the standings are: ", standings);
//const standings = await checkGetLeaderBoardData();