-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.ts
312 lines (266 loc) · 8.67 KB
/
index.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
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
import {
ChainName,
WormholeContext,
WormholeConfig,
ChainResourceMap,
} from 'sdklegacy';
import MAINNET from './mainnet';
import TESTNET from './testnet';
import DEVNET from './devnet';
import type { WormholeConnectConfig } from './types';
import {
Network,
InternalConfig,
Route,
WrappedTokenAddressCache,
} from './types';
import {
mergeCustomTokensConfig,
mergeNttGroups,
validateDefaults,
} from './utils';
import { wrapEventHandler } from './events';
import { SDKConverter } from './converter';
import {
wormhole as getWormholeV2,
Wormhole as WormholeV2,
Network as NetworkV2,
Token as TokenV2,
Chain as ChainV2,
ChainTokens as ChainTokensV2,
WormholeConfigOverrides as WormholeConfigOverridesV2,
} from '@wormhole-foundation/sdk';
import '@wormhole-foundation/sdk/addresses';
import evm from '@wormhole-foundation/sdk/evm';
import solana from '@wormhole-foundation/sdk/solana';
import aptos from '@wormhole-foundation/sdk/aptos';
import sui from '@wormhole-foundation/sdk/sui';
import cosmwasm from '@wormhole-foundation/sdk/cosmwasm';
import algorand from '@wormhole-foundation/sdk/algorand';
export function buildConfig(
customConfig?: WormholeConnectConfig
): InternalConfig<NetworkV2> {
const network = (
customConfig?.network ||
customConfig?.env || // TODO remove; deprecated
import.meta.env.REACT_APP_CONNECT_ENV?.toLowerCase() ||
'mainnet'
).toLowerCase() as Network;
if (!['mainnet', 'testnet', 'devnet'].includes(network))
throw new Error(`Invalid env "${network}"`);
const networkData = { MAINNET, DEVNET, TESTNET }[network.toUpperCase()]!;
const tokens = mergeCustomTokensConfig(
networkData.tokens,
customConfig?.tokensConfig
);
const sdkConfig = WormholeContext.getConfig(network);
const rpcs = Object.assign(
{},
sdkConfig.rpcs,
networkData.rpcs,
customConfig?.rpcs
);
const wh = getWormholeContext(network, sdkConfig, rpcs);
if (customConfig?.bridgeDefaults) {
validateDefaults(customConfig.bridgeDefaults, networkData.chains, tokens);
}
const sdkConverter = new SDKConverter(wh);
return {
wh,
sdkConfig,
sdkConverter,
v2Network: sdkConverter.toNetworkV2(network),
network,
isMainnet: network === 'mainnet',
// External resources
rpcs,
rest: Object.assign(
{},
sdkConfig.rest,
networkData.rest,
customConfig?.rest
),
graphql: Object.assign({}, networkData.graphql, customConfig?.graphql),
wormholeApi: {
mainnet: 'https://api.wormholescan.io/',
testnet: 'https://api.testnet.wormholescan.io/',
devnet: '',
}[network],
wormholeRpcHosts: {
mainnet: [
'https://wormhole-v2-mainnet-api.mcf.rocks',
'https://wormhole-v2-mainnet-api.chainlayer.network',
'https://wormhole-v2-mainnet-api.staking.fund',
],
testnet: [
'https://guardian.testnet.xlabs.xyz',
'https://guardian-01.testnet.xlabs.xyz',
'https://guardian-02.testnet.xlabs.xyz',
],
devnet: ['http://localhost:7071'],
}[network],
coinGeckoApiKey: customConfig?.coinGeckoApiKey,
// Callbacks
triggerEvent: wrapEventHandler(customConfig?.eventHandler),
validateTransfer: customConfig?.validateTransferHandler,
// White lists
chains: networkData.chains,
chainsArr: Object.values(networkData.chains).filter((chain) => {
return customConfig?.networks
? customConfig.networks!.includes(chain.key)
: true;
}),
tokens,
tokensArr: Object.values(tokens).filter((token) => {
return customConfig?.tokens
? customConfig.tokens!.includes(token.key)
: true;
}),
// For token bridge ^_^
wrappedTokenAddressCache: new WrappedTokenAddressCache(
tokens,
sdkConverter
),
gasEstimates: networkData.gasEstimates,
// TODO: routes that aren't supported yet are disabled
routes: (customConfig?.routes ?? Object.values(Route)).filter((r) =>
[
Route.Bridge,
Route.Relay,
Route.NttManual,
Route.NttRelay,
Route.CCTPManual,
Route.CCTPRelay,
].includes(r as Route)
),
// UI details
cta: customConfig?.cta,
explorer: customConfig?.explorer,
attestUrl: {
mainnet: 'https://portalbridge.com/advanced-tools/#/register',
devnet: '',
testnet:
'https://wormhole-foundation.github.io/example-token-bridge-ui/#/register',
}[network],
bridgeDefaults: customConfig?.bridgeDefaults,
cctpWarning: customConfig?.cctpWarning?.href || '',
pageHeader: customConfig?.pageHeader,
pageSubHeader: customConfig?.pageSubHeader,
menu: customConfig?.menu ?? [],
searchTx: customConfig?.searchTx,
moreTokens: customConfig?.moreTokens,
moreNetworks: customConfig?.moreNetworks,
partnerLogo: customConfig?.partnerLogo,
walletConnectProjectId:
customConfig?.walletConnectProjectId ??
import.meta.env.REACT_APP_WALLET_CONNECT_PROJECT_ID,
showHamburgerMenu: customConfig?.showHamburgerMenu ?? false,
previewMode: !!customConfig?.previewMode,
// Route options
ethBridgeMaxAmount: customConfig?.ethBridgeMaxAmount ?? 5,
wstETHBridgeMaxAmount: customConfig?.wstETHBridgeMaxAmount ?? 5,
// NTT config
nttGroups: mergeNttGroups(
tokens,
networkData.nttGroups,
customConfig?.nttGroups
),
// Guardian set
guardianSet: networkData.guardianSet,
// Render redesign views
useRedesign: customConfig?.useRedesign,
};
}
// Running buildConfig with no argument generates the default configuration
const config = buildConfig();
export default config;
// TODO SDKV2: REMOVE
export function getWormholeContext(
network: Network,
sdkConfig: WormholeConfig,
rpcs: ChainResourceMap
): WormholeContext {
const wh: WormholeContext = new WormholeContext(network, {
...sdkConfig,
...{ rpcs },
});
return wh;
}
export function getDefaultWormholeContext(network: Network): WormholeContext {
const sdkConfig = WormholeContext.getConfig(network);
const networkData = { mainnet: MAINNET, devnet: DEVNET, testnet: TESTNET }[
network
]!;
const rpcs = Object.assign({}, sdkConfig.rpcs, networkData.rpcs);
return getWormholeContext(network, sdkConfig, rpcs);
}
export async function getWormholeContextV2(): Promise<WormholeV2<NetworkV2>> {
if (config.v2Wormhole) return config.v2Wormhole;
config.v2Wormhole = await newWormholeContextV2();
return config.v2Wormhole;
}
export async function newWormholeContextV2(): Promise<WormholeV2<NetworkV2>> {
const v2Config: WormholeConfigOverridesV2<NetworkV2> = { chains: {} };
for (const key in config.chains) {
const chainV1 = key as ChainName;
const chainConfigV1 = config.chains[chainV1]!;
const chainContextV1 = chainConfigV1.context;
const chainV2 = config.sdkConverter.toChainV2(
chainV1 as ChainName
) as ChainV2;
const rpc = config.rpcs[chainV1];
const tokenMap: ChainTokensV2 = {};
for (const token of config.tokensArr) {
const nativeChainV2 = config.sdkConverter.toChainV2(token.nativeChain);
const tokenV2: Partial<TokenV2> = {
key: token.key,
chain: chainV2,
symbol: token.symbol,
};
if (nativeChainV2 == chainV2) {
const decimals =
token.decimals[chainContextV1] ?? token.decimals.default;
if (!decimals) {
continue;
} else {
tokenV2.decimals = decimals;
}
const address = config.sdkConverter.getNativeTokenAddressV2(token);
if (!address) throw new Error('Token must have address');
tokenV2.address = address;
} else {
tokenV2.original = nativeChainV2;
if (token.foreignAssets) {
const fa = token.foreignAssets[chainV1]!;
if (!fa) {
continue;
} else {
tokenV2.address = fa.address;
tokenV2.decimals = fa.decimals;
}
} else {
continue;
}
}
tokenMap[token.key] = tokenV2 as TokenV2;
}
v2Config.chains![chainV2] = { rpc, tokenMap };
}
return await getWormholeV2(
config.v2Network,
[evm, solana, aptos, cosmwasm, sui, algorand],
v2Config
);
}
// setConfig can be called afterwards to override the default config with integrator-provided config
export function setConfig(customConfig?: WormholeConnectConfig) {
const newConfig: InternalConfig<NetworkV2> = buildConfig(customConfig);
// We overwrite keys in the existing object so the references to the config
// imported elsewhere point to the new values
for (const key in newConfig) {
/* @ts-ignore */
config[key] = newConfig[key];
}
}
// TODO: add config validation step to buildConfig
//validateConfigs();