-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathGlobalContext.tsx
260 lines (233 loc) · 7.23 KB
/
GlobalContext.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
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
import { ethers, JsonRpcProvider } from "ethers";
import React, {
createContext,
ReactNode,
useContext,
useEffect,
useMemo,
useState,
} from "react";
import { Chain, useNetwork } from "wagmi";
import { arbitrum, avalanche, mainnet } from "wagmi/chains";
import {
injective,
injectivetestnet,
juno,
neutron,
neutrontestnet,
osmosis,
osmosistestnet,
seitestnet2,
} from "graz/chains";
import PythAbi from "../abis/IPyth.json";
import PythErrorsAbi from "../abis/PythErrors.json";
import { ChainInfo } from "@keplr-wallet/types";
/** Global information available to all components on any page. */
export interface GlobalContextData {
// This key-value store is the primary method of sharing state between components in an mdx file.
// This store simplifies writing markdown (as you don't have to wrap the entire markdown file in a stateful component).
// The typical usage of the store is to set keys via form fields, then use those keys to query APIs or render code snippets.
// Note that key/value pairs are shared between pages as well.
keyValueStore: Record<string, string>;
setKeyValueStore: React.Dispatch<
React.SetStateAction<Record<string, string>>
>;
// Ethereum-specific connection information /////////////////////////////////////
// ID of the current chain
networkName: string;
setNetworkName: React.Dispatch<React.SetStateAction<string>>;
pythAddressConfig: PythAddressConfig;
currentChainConfig?: Chain;
// Provider for reading from the chain.
provider: ethers.Provider;
setProvider: React.Dispatch<React.SetStateAction<ethers.Provider>>;
// The pyth contract on the current chain
pythContract: ethers.Contract;
// The ABI for the pyth contract
pythAbi: any[];
// Cosmos chain configuration
cosmosChainId: string;
setCosmosChainId: React.Dispatch<React.SetStateAction<string>>;
cosmosChainConfig: PythCosmosConfig;
}
export type NetworkType = "mainnet" | "testnet";
export const PriceServiceUrls: Record<string, string> = {
mainnet: "https://xc-mainnet.pyth.network",
testnet: "https://xc-testnet.pyth.network",
};
export interface PythAddressConfig {
chainId: number;
pythAddress: string;
networkType: NetworkType;
}
// Network data for Ethereum and Avalanche
export const PythAddresses: Record<string, PythAddressConfig> = {
ethereum: {
chainId: 1,
pythAddress: "0x4305FB66699C3B2702D4d05CF36551390A4c69C6",
networkType: "mainnet",
},
avalanche: {
chainId: 43114,
pythAddress: "0x4305FB66699C3B2702D4d05CF36551390A4c69C6",
networkType: "mainnet",
},
arbitrum: {
chainId: 42161,
pythAddress: "0xff1a0f4744e8582DF1aE09D5611b887B6a12925C",
networkType: "mainnet",
},
};
export interface PythCosmosConfig {
chainId: string;
pythAddress: string;
// TODO: stable/beta :(
networkType: NetworkType;
}
export const PythCosmosAddresses: Record<string, PythCosmosConfig> = {
injective_mainnet: {
// TODO: can't seem to connect to this RPC right now
chainId: "injective-1",
pythAddress: `inj12j43nf2f0qumnt2zrrmpvnsqgzndxefujlvr08`,
networkType: "mainnet",
},
osmosis_mainnet: {
chainId: "osmosis-1",
pythAddress: `osmo13ge29x4e2s63a8ytz2px8gurtyznmue4a69n5275692v3qn3ks8q7cwck7`,
networkType: "mainnet",
},
injective_testnet: {
chainId: "injective-888",
pythAddress: `inj18hckkzqf47mdhd734g6papk6wj20y24rm43sk9`,
networkType: "mainnet",
},
sei_atlantic_2: {
chainId: "atlantic-2",
pythAddress: `sei1w2rxq6eckak47s25crxlhmq96fzjwdtjgdwavn56ggc0qvxvw7rqczxyfy`,
networkType: "mainnet",
},
neutron_pion_1: {
chainId: "pion-1",
pythAddress: `neutron1f86ct5az9qpz2hqfd5uxru02px2a3tz5zkw7hugd7acqq496dcms22ehpy`,
networkType: "mainnet",
},
juno: {
chainId: "juno-1",
pythAddress: `juno1eacsrua27njc35pxz37y97gmcjs899t59f8pf0rkejjyvtmhws5q6lxsdd`,
networkType: "mainnet",
},
neutron: {
chainId: "neutron-1",
pythAddress:
"neutron1m2emc93m9gpwgsrsf2vylv9xvgqh654630v7dfrhrkmr5slly53spg85wv",
networkType: "mainnet",
},
osmosis_testnet_5: {
chainId: "osmo-test-5",
pythAddress:
"osmo1hpdzqku55lmfmptpyj6wdlugqs5etr6teqf7r4yqjjrxjznjhtuqqu5kdh",
networkType: "mainnet",
},
};
const GlobalContext = createContext<GlobalContextData>({} as GlobalContextData);
export const useGlobalContext = () => useContext(GlobalContext);
const contractAbi = [...PythAbi, ...PythErrorsAbi];
const CHAINS = [mainnet, avalanche, arbitrum];
export const CosmosChains = [
osmosis,
osmosistestnet,
injective,
injectivetestnet,
seitestnet2,
neutron,
neutrontestnet,
juno,
];
export function getCosmosChainFromConfig(chainId: string): ChainInfo {
return CosmosChains.find((chain) => chain.chainId == chainId)!;
}
export const GlobalContextProvider = ({
children,
}: {
children: ReactNode;
}) => {
const [queryParameters, setQueryParameters] = useState<
Record<string, string>
>({});
const defaultChainName = "arbitrum";
// TODO: we may need to support "no network" as the default, because this may require a wallet.
const [networkName, setNetworkName] = useState<string>(defaultChainName);
const [pythAddressConfig, setPythAddressConfig] = useState<PythAddressConfig>(
PythAddresses[defaultChainName]
);
const [provider, setProvider] = useState<ethers.Provider>(
ethers.getDefaultProvider("https://arb1.arbitrum.io/rpc")
);
const { chain } = useNetwork();
const defaultChain = CHAINS.find(
(chain) => chain.network === defaultChainName
);
const [currentChainConfig, setCurrentChainConfig] = useState<
Chain | undefined
>(defaultChain);
const pythContract = useMemo(() => {
return new ethers.Contract(
pythAddressConfig.pythAddress,
contractAbi,
provider
);
}, [pythAddressConfig, provider]);
const [cosmosChainId, setCosmosChainId] = useState<string>("neutron");
const cosmosChainConfig = useMemo<PythCosmosConfig>(
() => PythCosmosAddresses[cosmosChainId],
[cosmosChainId]
);
useEffect(() => {
async function helper() {
setPythAddressConfig(PythAddresses[networkName]);
const chainId = PythAddresses[networkName].chainId;
const chain = CHAINS.find((chain) => chain.id === chainId);
if (chain) {
const rpcUrl = chain.rpcUrls.default.http[0];
const provider = new JsonRpcProvider(rpcUrl);
setProvider(provider);
setCurrentChainConfig(chain);
}
}
helper();
}, [networkName]);
useEffect(() => {
if (chain) {
const chainId = chain.id;
const pythAddressConfig = Object.entries(PythAddresses).find(
([, value]) => value.chainId === chainId
);
if (pythAddressConfig) {
const [key] = pythAddressConfig;
setNetworkName(key);
}
setCurrentChainConfig(chain);
}
}, [chain]);
return (
<GlobalContext.Provider
value={{
keyValueStore: queryParameters,
setKeyValueStore: setQueryParameters,
networkName,
setNetworkName,
pythAddressConfig,
currentChainConfig,
provider,
setProvider,
pythContract: pythContract,
pythAbi: contractAbi,
cosmosChainId,
setCosmosChainId,
cosmosChainConfig,
}}
>
{children}
</GlobalContext.Provider>
);
};