@orderly.network/wallet-connector 2.0.0 → 2.0.1-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +10 -5
- package/dist/index.d.ts +10 -5
- package/dist/index.js +466 -51
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +469 -56
- package/dist/index.mjs.map +1 -1
- package/package.json +13 -10
package/dist/index.d.mts
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
import React, { PropsWithChildren } from 'react';
|
|
2
|
-
import { WalletAdapterNetwork, Adapter } from '@solana/wallet-adapter-base';
|
|
2
|
+
import { WalletAdapterNetwork, Adapter, WalletError } from '@solana/wallet-adapter-base';
|
|
3
|
+
import { Optional } from '@orderly.network/types';
|
|
4
|
+
import { InitOptions } from '@web3-onboard/core';
|
|
3
5
|
|
|
4
|
-
|
|
6
|
+
type ConnectorInitOptions = Optional<InitOptions, "apiKey" | "connect" | "wallets" | "chains" | "appMetadata" | "accountCenter" | "theme">;
|
|
7
|
+
type SolanaInitialProps = PropsWithChildren<{
|
|
5
8
|
network?: WalletAdapterNetwork;
|
|
6
9
|
endPoint?: string;
|
|
7
10
|
wallets?: Adapter[];
|
|
8
|
-
|
|
9
|
-
|
|
11
|
+
onError?: (error: WalletError, adapter?: Adapter) => void;
|
|
12
|
+
}>;
|
|
13
|
+
type EvmInitialProps = PropsWithChildren<{
|
|
10
14
|
apiKey?: string;
|
|
15
|
+
options?: ConnectorInitOptions;
|
|
11
16
|
skipInit?: boolean;
|
|
12
|
-
}
|
|
17
|
+
}>;
|
|
13
18
|
|
|
14
19
|
interface WalletConnectorProviderProps {
|
|
15
20
|
solanaInitial?: SolanaInitialProps;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
import React, { PropsWithChildren } from 'react';
|
|
2
|
-
import { WalletAdapterNetwork, Adapter } from '@solana/wallet-adapter-base';
|
|
2
|
+
import { WalletAdapterNetwork, Adapter, WalletError } from '@solana/wallet-adapter-base';
|
|
3
|
+
import { Optional } from '@orderly.network/types';
|
|
4
|
+
import { InitOptions } from '@web3-onboard/core';
|
|
3
5
|
|
|
4
|
-
|
|
6
|
+
type ConnectorInitOptions = Optional<InitOptions, "apiKey" | "connect" | "wallets" | "chains" | "appMetadata" | "accountCenter" | "theme">;
|
|
7
|
+
type SolanaInitialProps = PropsWithChildren<{
|
|
5
8
|
network?: WalletAdapterNetwork;
|
|
6
9
|
endPoint?: string;
|
|
7
10
|
wallets?: Adapter[];
|
|
8
|
-
|
|
9
|
-
|
|
11
|
+
onError?: (error: WalletError, adapter?: Adapter) => void;
|
|
12
|
+
}>;
|
|
13
|
+
type EvmInitialProps = PropsWithChildren<{
|
|
10
14
|
apiKey?: string;
|
|
15
|
+
options?: ConnectorInitOptions;
|
|
11
16
|
skipInit?: boolean;
|
|
12
|
-
}
|
|
17
|
+
}>;
|
|
13
18
|
|
|
14
19
|
interface WalletConnectorProviderProps {
|
|
15
20
|
solanaInitial?: SolanaInitialProps;
|
package/dist/index.js
CHANGED
|
@@ -6,12 +6,22 @@ var walletAdapterBase = require('@solana/wallet-adapter-base');
|
|
|
6
6
|
var walletAdapterReactUi = require('@solana/wallet-adapter-react-ui');
|
|
7
7
|
var web3_js = require('@solana/web3.js');
|
|
8
8
|
require('@solana/wallet-adapter-react-ui/styles.css');
|
|
9
|
+
var walletAdapterMobile = require('@solana-mobile/wallet-adapter-mobile');
|
|
10
|
+
var walletAdapterWallets = require('@solana/wallet-adapter-wallets');
|
|
11
|
+
var utils = require('@orderly.network/utils');
|
|
9
12
|
var hooks = require('@orderly.network/hooks');
|
|
10
13
|
var types = require('@orderly.network/types');
|
|
14
|
+
var ui = require('@orderly.network/ui');
|
|
15
|
+
var injectedModule = require('@web3-onboard/injected-wallets');
|
|
16
|
+
var react = require('@web3-onboard/react');
|
|
17
|
+
var binanceModule = require('@binance/w3w-blocknative-connector');
|
|
18
|
+
var lodash = require('lodash');
|
|
11
19
|
|
|
12
20
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
13
21
|
|
|
14
22
|
var React__default = /*#__PURE__*/_interopDefault(React);
|
|
23
|
+
var injectedModule__default = /*#__PURE__*/_interopDefault(injectedModule);
|
|
24
|
+
var binanceModule__default = /*#__PURE__*/_interopDefault(binanceModule);
|
|
15
25
|
|
|
16
26
|
// src/initSolana.tsx
|
|
17
27
|
function InitSolana({ children, ...props }) {
|
|
@@ -19,81 +29,464 @@ function InitSolana({ children, ...props }) {
|
|
|
19
29
|
const endPoint = React.useMemo(() => {
|
|
20
30
|
return props.endPoint ?? web3_js.clusterApiUrl(network);
|
|
21
31
|
}, [network]);
|
|
32
|
+
const mobileWalletNotFoundHanlder = (adapter) => {
|
|
33
|
+
console.log("-- mobile wallet adapter", adapter);
|
|
34
|
+
return Promise.reject(new walletAdapterBase.WalletNotReadyError("wallet not ready"));
|
|
35
|
+
};
|
|
36
|
+
const handleSolanaError = (error, adapter) => {
|
|
37
|
+
console.log("-- solanan error", error);
|
|
38
|
+
console.log("-- solana adapter", adapter);
|
|
39
|
+
};
|
|
22
40
|
const wallets = React.useMemo(() => {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
41
|
+
let uri = "";
|
|
42
|
+
if (typeof window !== "undefined") {
|
|
43
|
+
const location = utils.getGlobalObject().location;
|
|
44
|
+
uri = `${location.protocol}//${location.host}`;
|
|
45
|
+
}
|
|
46
|
+
return props.wallets ?? [
|
|
47
|
+
new walletAdapterWallets.PhantomWalletAdapter(),
|
|
48
|
+
new walletAdapterMobile.SolanaMobileWalletAdapter({
|
|
49
|
+
addressSelector: walletAdapterMobile.createDefaultAddressSelector(),
|
|
50
|
+
appIdentity: {
|
|
51
|
+
uri
|
|
52
|
+
},
|
|
53
|
+
authorizationResultCache: walletAdapterMobile.createDefaultAuthorizationResultCache(),
|
|
54
|
+
chain: network,
|
|
55
|
+
onWalletNotFound: mobileWalletNotFoundHanlder
|
|
56
|
+
})
|
|
57
|
+
];
|
|
58
|
+
}, [props.wallets, network]);
|
|
59
|
+
return /* @__PURE__ */ React__default.default.createElement(walletAdapterReact.ConnectionProvider, { endpoint: endPoint }, /* @__PURE__ */ React__default.default.createElement(walletAdapterReact.WalletProvider, { wallets, onError: props.onError ?? handleSolanaError }, /* @__PURE__ */ React__default.default.createElement(walletAdapterReactUi.WalletModalProvider, { className: "oui-pointer-events-auto" }, children)));
|
|
26
60
|
}
|
|
27
|
-
|
|
28
|
-
|
|
61
|
+
var getChainsArray = () => {
|
|
62
|
+
return Array.from(types.chainsInfoMap.values()).map((chain) => {
|
|
63
|
+
return {
|
|
64
|
+
id: chain.chainInfo.chainId,
|
|
65
|
+
token: chain.chainInfo.nativeCurrency.symbol,
|
|
66
|
+
label: chain.chainInfo.chainName,
|
|
67
|
+
rpcUrl: chain.chainInfo.rpcUrls[0],
|
|
68
|
+
blockExplorerUrl: chain.chainInfo.blockExplorerUrls[0]
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
var binance = binanceModule__default.default({ options: { lng: "en" } });
|
|
73
|
+
var initConfig = (apiKey, options) => {
|
|
74
|
+
const defaultOptions = {
|
|
75
|
+
apiKey,
|
|
76
|
+
connect: {
|
|
77
|
+
// autoConnectAllPreviousWallet: true,
|
|
78
|
+
autoConnectLastWallet: true
|
|
79
|
+
},
|
|
80
|
+
wallets: [injectedModule__default.default(), binance],
|
|
81
|
+
chains: getChainsArray(),
|
|
82
|
+
appMetadata: {
|
|
83
|
+
name: "Orderly",
|
|
84
|
+
// icon: blocknativeIcon,
|
|
85
|
+
description: "Orderly",
|
|
86
|
+
recommendedInjectedWallets: [
|
|
87
|
+
{ name: "Coinbase", url: "https://wallet.coinbase.com/" },
|
|
88
|
+
{ name: "MetaMask", url: "https://metamask.io" },
|
|
89
|
+
{ name: "Trezor", url: "https://trezor.io/" },
|
|
90
|
+
{ name: "Walletconnect", url: "https://walletconnect.com/" },
|
|
91
|
+
{ name: "Ledger", url: "https://www.ledger.com/" }
|
|
92
|
+
],
|
|
93
|
+
agreement: {
|
|
94
|
+
version: "1.0.0",
|
|
95
|
+
termsUrl: "https://www.blocknative.com/terms-conditions",
|
|
96
|
+
privacyUrl: "https://www.blocknative.com/privacy-policy"
|
|
97
|
+
},
|
|
98
|
+
gettingStartedGuide: "https://blocknative.com",
|
|
99
|
+
explore: "https://blocknative.com"
|
|
100
|
+
},
|
|
101
|
+
accountCenter: {
|
|
102
|
+
desktop: {
|
|
103
|
+
enabled: false
|
|
104
|
+
},
|
|
105
|
+
mobile: {
|
|
106
|
+
enabled: false
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
theme: "dark"
|
|
110
|
+
};
|
|
111
|
+
const mergedOptions = lodash.merge(defaultOptions, options);
|
|
112
|
+
return react.init(mergedOptions);
|
|
113
|
+
};
|
|
114
|
+
var SolanaChains = /* @__PURE__ */ new Map([[walletAdapterBase.WalletAdapterNetwork.Devnet, 901901901], [walletAdapterBase.WalletAdapterNetwork.Mainnet, 900900900]]);
|
|
115
|
+
|
|
116
|
+
// src/useSOL.tsx
|
|
117
|
+
function useSOL({ network }) {
|
|
118
|
+
const [wallet, setWallet] = React.useState(null);
|
|
119
|
+
const { isMobile } = ui.useScreen();
|
|
120
|
+
const { connection } = walletAdapterReact.useConnection();
|
|
121
|
+
const { setVisible: setModalVisible, visible } = walletAdapterReactUi.useWalletModal();
|
|
29
122
|
const {
|
|
30
|
-
connect: connectSolanaWallet,
|
|
31
|
-
wallet: selectedSolanaWallet,
|
|
32
|
-
connecting: solanaConnecting,
|
|
33
|
-
disconnect: solanaDisconnect,
|
|
34
123
|
signMessage,
|
|
35
124
|
sendTransaction,
|
|
36
|
-
publicKey
|
|
125
|
+
publicKey,
|
|
126
|
+
wallet: solanaWallet,
|
|
127
|
+
connect: solanaConnect,
|
|
128
|
+
disconnect: solanaDisconnect,
|
|
129
|
+
connecting
|
|
37
130
|
} = walletAdapterReact.useWallet();
|
|
38
|
-
const
|
|
131
|
+
const selectModalVisibleRef = React.useRef(false);
|
|
132
|
+
const [connected, setConnected] = React.useState(false);
|
|
133
|
+
const isManual = React.useRef(false);
|
|
134
|
+
const solanaPromiseRef = React.useRef({
|
|
135
|
+
walletSelect: null,
|
|
136
|
+
connect: null,
|
|
137
|
+
walletSelectResolve: () => {
|
|
138
|
+
},
|
|
139
|
+
walletSelectReject: () => {
|
|
140
|
+
},
|
|
141
|
+
connectReject: () => {
|
|
142
|
+
},
|
|
143
|
+
connectResolve: () => {
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
const ee = hooks.useEventEmitter();
|
|
147
|
+
const initPromiseRef = () => {
|
|
148
|
+
console.log("-- init solana promise");
|
|
149
|
+
solanaPromiseRef.current.walletSelectResolve = () => {
|
|
150
|
+
};
|
|
151
|
+
solanaPromiseRef.current.walletSelectReject = () => {
|
|
152
|
+
};
|
|
153
|
+
solanaPromiseRef.current.connectReject = () => {
|
|
154
|
+
};
|
|
155
|
+
solanaPromiseRef.current.connectReject = () => {
|
|
156
|
+
};
|
|
157
|
+
solanaPromiseRef.current.connect = null;
|
|
158
|
+
solanaPromiseRef.current.walletSelect = null;
|
|
159
|
+
solanaPromiseRef.current.walletSelect = new Promise((resolve, reject) => {
|
|
160
|
+
solanaPromiseRef.current.walletSelectResolve = resolve;
|
|
161
|
+
solanaPromiseRef.current.walletSelectReject = reject;
|
|
162
|
+
});
|
|
163
|
+
solanaPromiseRef.current.connect = new Promise((resolve, reject) => {
|
|
164
|
+
solanaPromiseRef.current.connectResolve = resolve;
|
|
165
|
+
solanaPromiseRef.current.connectReject = reject;
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
const handleSolanaError = (e) => {
|
|
169
|
+
console.log("solan connect error", e);
|
|
170
|
+
if (e instanceof walletAdapterBase.WalletNotReadyError) {
|
|
171
|
+
console.log("-- need toast wallet not ready");
|
|
172
|
+
ee.emit("wallet:connect-error", { message: "Please open the wallet app and use the in-app browser." });
|
|
173
|
+
}
|
|
174
|
+
solanaDisconnect().then();
|
|
175
|
+
};
|
|
39
176
|
const connect = async () => {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
177
|
+
initPromiseRef();
|
|
178
|
+
isManual.current = true;
|
|
179
|
+
if (!solanaPromiseRef.current) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
if (!solanaWallet) {
|
|
183
|
+
setModalVisible(true);
|
|
184
|
+
selectModalVisibleRef.current = true;
|
|
185
|
+
} else {
|
|
186
|
+
solanaPromiseRef.current.walletSelectResolve(solanaWallet);
|
|
187
|
+
if (!publicKey) {
|
|
188
|
+
try {
|
|
189
|
+
await solanaConnect();
|
|
190
|
+
} catch (e) {
|
|
191
|
+
solanaPromiseRef.current.connectReject(e);
|
|
192
|
+
}
|
|
193
|
+
} else {
|
|
194
|
+
solanaPromiseRef.current.connectResolve({
|
|
195
|
+
userAddress: publicKey.toBase58(),
|
|
196
|
+
signMessage,
|
|
197
|
+
sendTransaction
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
console.log("-- connect fn", solanaWallet, publicKey);
|
|
202
|
+
return Promise.all([
|
|
203
|
+
solanaPromiseRef.current.walletSelect,
|
|
204
|
+
solanaPromiseRef.current.connect
|
|
205
|
+
]).then(([wallet2, { userAddress, signMessage: signMessage2, sendTransaction: sendTransaction2 }]) => {
|
|
206
|
+
const tempWallet = {
|
|
207
|
+
label: wallet2.adapter.name,
|
|
208
|
+
icon: "",
|
|
209
|
+
provider: {
|
|
210
|
+
signMessage: signMessage2,
|
|
211
|
+
connection,
|
|
212
|
+
sendTransaction: sendTransaction2
|
|
213
|
+
},
|
|
214
|
+
accounts: [
|
|
215
|
+
{
|
|
216
|
+
address: userAddress
|
|
217
|
+
}
|
|
218
|
+
],
|
|
219
|
+
chains: [
|
|
220
|
+
{
|
|
221
|
+
id: SolanaChains.get(network),
|
|
222
|
+
namespace: types.ChainNamespace.solana
|
|
223
|
+
}
|
|
224
|
+
]
|
|
225
|
+
};
|
|
226
|
+
setWallet(tempWallet);
|
|
227
|
+
setConnected(true);
|
|
228
|
+
return [tempWallet];
|
|
229
|
+
}).catch((e) => {
|
|
230
|
+
console.log("connect solana error", e);
|
|
231
|
+
handleSolanaError(e);
|
|
232
|
+
return Promise.reject(e);
|
|
233
|
+
}).finally(() => {
|
|
234
|
+
isManual.current = false;
|
|
50
235
|
});
|
|
51
|
-
return [];
|
|
52
236
|
};
|
|
53
237
|
const disconnect = async () => {
|
|
238
|
+
console.log("--- discconnect sol");
|
|
54
239
|
await solanaDisconnect();
|
|
240
|
+
setWallet(null);
|
|
241
|
+
setConnected(false);
|
|
55
242
|
return [];
|
|
56
243
|
};
|
|
57
|
-
React.
|
|
58
|
-
if (!selectedSolanaWallet)
|
|
59
|
-
return;
|
|
60
|
-
if (visible)
|
|
61
|
-
return;
|
|
62
|
-
console.log("-- selectedSolanaWallet", selectedSolanaWallet, visible);
|
|
63
|
-
connectSolanaWallet().then((res) => {
|
|
64
|
-
console.log("-- publick", publicKey?.toBase58());
|
|
65
|
-
console.log("-- res", res);
|
|
66
|
-
}).catch((err) => {
|
|
67
|
-
console.log("-- error", err);
|
|
68
|
-
});
|
|
69
|
-
}, [selectedSolanaWallet, visible]);
|
|
70
|
-
const connecting = false;
|
|
71
|
-
const wallet = React.useMemo(() => {
|
|
72
|
-
console.log("-- publiKey", publicKey);
|
|
244
|
+
const connectedChain = React.useMemo(() => {
|
|
73
245
|
if (!publicKey) {
|
|
74
246
|
return null;
|
|
75
247
|
}
|
|
76
248
|
return {
|
|
77
|
-
|
|
249
|
+
id: 901901901,
|
|
250
|
+
namespace: types.ChainNamespace.solana
|
|
251
|
+
};
|
|
252
|
+
}, [publicKey]);
|
|
253
|
+
React.useEffect(() => {
|
|
254
|
+
if (selectModalVisibleRef.current) {
|
|
255
|
+
if (!visible && !solanaWallet && solanaPromiseRef.current) {
|
|
256
|
+
console.log("-- select modal visible ref", selectModalVisibleRef.current);
|
|
257
|
+
console.log("-- use reject solana select modal");
|
|
258
|
+
solanaPromiseRef.current.walletSelectReject("user reject");
|
|
259
|
+
selectModalVisibleRef.current = false;
|
|
260
|
+
} else if (solanaWallet) {
|
|
261
|
+
selectModalVisibleRef.current = false;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}, [visible, solanaWallet, solanaPromiseRef.current, selectModalVisibleRef.current]);
|
|
265
|
+
React.useEffect(() => {
|
|
266
|
+
if (!solanaWallet || !publicKey) {
|
|
267
|
+
console.log("--- not connect sol", solanaWallet, publicKey);
|
|
268
|
+
setConnected(false);
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
console.log("-- publick", {
|
|
272
|
+
publicKey: publicKey.toBase58(),
|
|
273
|
+
isManual: isManual.current
|
|
274
|
+
});
|
|
275
|
+
if (isManual.current) {
|
|
276
|
+
if (solanaPromiseRef.current) {
|
|
277
|
+
solanaPromiseRef.current.connectResolve({
|
|
278
|
+
userAddress: publicKey?.toBase58(),
|
|
279
|
+
signMessage,
|
|
280
|
+
sendTransaction
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
console.log("-- tt");
|
|
286
|
+
setWallet({
|
|
287
|
+
label: solanaWallet.adapter.name,
|
|
78
288
|
icon: "",
|
|
79
289
|
provider: {
|
|
80
290
|
signMessage,
|
|
81
291
|
connection,
|
|
82
292
|
sendTransaction
|
|
83
293
|
},
|
|
84
|
-
accounts: [
|
|
85
|
-
|
|
86
|
-
|
|
294
|
+
accounts: [
|
|
295
|
+
{
|
|
296
|
+
address: publicKey.toBase58()
|
|
297
|
+
}
|
|
298
|
+
],
|
|
87
299
|
chains: [
|
|
88
300
|
{
|
|
89
|
-
id:
|
|
301
|
+
id: SolanaChains.get(network),
|
|
90
302
|
namespace: types.ChainNamespace.solana
|
|
91
303
|
}
|
|
92
304
|
]
|
|
305
|
+
});
|
|
306
|
+
setConnected(true);
|
|
307
|
+
}, [publicKey, solanaWallet, signMessage, isManual, connection, sendTransaction, network]);
|
|
308
|
+
React.useEffect(() => {
|
|
309
|
+
if (!publicKey) {
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
const id = connection.onAccountChange(publicKey, (updatedAccountInfo, context) => {
|
|
313
|
+
console.log("--- account change", updatedAccountInfo, context);
|
|
314
|
+
}, { commitment: "confirmed" });
|
|
315
|
+
return () => {
|
|
316
|
+
if (id) {
|
|
317
|
+
connection.removeAccountChangeListener(id).then();
|
|
318
|
+
}
|
|
93
319
|
};
|
|
94
|
-
}, [
|
|
95
|
-
|
|
320
|
+
}, [connection, publicKey]);
|
|
321
|
+
React.useEffect(() => {
|
|
322
|
+
if (!solanaWallet) {
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
console.log("-- public key", publicKey, { isMobile });
|
|
326
|
+
if (isMobile && solanaWallet.readyState === walletAdapterBase.WalletReadyState.Loadable && !isManual.current) {
|
|
327
|
+
solanaDisconnect().then();
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
console.log("-- solana refresh auto connect", solanaWallet);
|
|
331
|
+
if (solanaPromiseRef.current) {
|
|
332
|
+
solanaPromiseRef.current.walletSelectResolve(solanaWallet);
|
|
333
|
+
}
|
|
334
|
+
solanaConnect().then((res) => {
|
|
335
|
+
console.log("-- connect res", res);
|
|
336
|
+
}).catch((e) => {
|
|
337
|
+
solanaPromiseRef.current.connectReject(e);
|
|
338
|
+
handleSolanaError(e);
|
|
339
|
+
});
|
|
340
|
+
}, [solanaWallet, solanaConnect, publicKey, solanaDisconnect, handleSolanaError, isMobile]);
|
|
341
|
+
return {
|
|
342
|
+
connected,
|
|
343
|
+
connect,
|
|
344
|
+
disconnect,
|
|
345
|
+
connecting,
|
|
346
|
+
wallet,
|
|
347
|
+
connectedChain
|
|
96
348
|
};
|
|
349
|
+
}
|
|
350
|
+
function useEvm() {
|
|
351
|
+
const [
|
|
352
|
+
{
|
|
353
|
+
wallet,
|
|
354
|
+
// the wallet that has been connected or null if not yet connected
|
|
355
|
+
connecting
|
|
356
|
+
// boolean indicating if connection is in progress
|
|
357
|
+
},
|
|
358
|
+
connect,
|
|
359
|
+
// function to call to initiate user to connect wallet
|
|
360
|
+
evmDisconnect,
|
|
361
|
+
// function to call with wallet<DisconnectOptions> to disconnect wallet
|
|
362
|
+
updateBalances,
|
|
363
|
+
// function to be called with an optional array of wallet addresses connected through Onboard to update balance or empty/no params to update all connected wallets
|
|
364
|
+
setWalletModules,
|
|
365
|
+
// function to be called with an array of wallet modules to conditionally allow connection of wallet types i.e. setWalletModules([ledger, trezor, injected])
|
|
366
|
+
setPrimaryWallet
|
|
367
|
+
// function that can set the primary wallet and/or primary account within that wallet. The wallet that is set needs to be passed in for the first parameter and if you would like to set the primary account, the address of that account also needs to be passed in
|
|
368
|
+
] = react.useConnectWallet();
|
|
369
|
+
const [
|
|
370
|
+
{
|
|
371
|
+
chains,
|
|
372
|
+
// the list of chains that web3-onboard was initialized with
|
|
373
|
+
connectedChain: evmConnectChain,
|
|
374
|
+
// the current chain the user's wallet is connected to
|
|
375
|
+
settingChain
|
|
376
|
+
// boolean indicating if the chain is in the process of being set
|
|
377
|
+
},
|
|
378
|
+
setChain
|
|
379
|
+
// function to call to initiate user to switch chains in their wallet
|
|
380
|
+
] = react.useSetChain();
|
|
381
|
+
const connected = React.useMemo(() => {
|
|
382
|
+
return !!(wallet && wallet.accounts && wallet.accounts[0] && wallet.accounts[0].address);
|
|
383
|
+
}, [wallet]);
|
|
384
|
+
const disconnect = async () => {
|
|
385
|
+
console.log("-- disconnect evm xxxxxxxxxxxx");
|
|
386
|
+
if (!wallet) {
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
return evmDisconnect({
|
|
390
|
+
label: wallet.label
|
|
391
|
+
});
|
|
392
|
+
};
|
|
393
|
+
const connectedChain = React.useMemo(() => {
|
|
394
|
+
return evmConnectChain ? { ...evmConnectChain, id: parseInt(evmConnectChain.id) } : null;
|
|
395
|
+
}, [evmConnectChain]);
|
|
396
|
+
const changeChain = (chain) => {
|
|
397
|
+
return setChain(chain);
|
|
398
|
+
};
|
|
399
|
+
return {
|
|
400
|
+
connect,
|
|
401
|
+
connected,
|
|
402
|
+
disconnect,
|
|
403
|
+
connecting,
|
|
404
|
+
wallet,
|
|
405
|
+
connectedChain,
|
|
406
|
+
changeChain
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// src/main.tsx
|
|
411
|
+
function Main(props) {
|
|
412
|
+
const sol = useSOL({ network: props.solanaNetwork });
|
|
413
|
+
const evm = useEvm();
|
|
414
|
+
const [namespace, setNamespace] = React.useState(null);
|
|
415
|
+
const newNamespace = React.useRef();
|
|
416
|
+
const connect = async (options) => {
|
|
417
|
+
if (Array.from(SolanaChains.values()).includes(options.chainId)) {
|
|
418
|
+
newNamespace.current = types.ChainNamespace.solana;
|
|
419
|
+
return sol.connect().then((res) => {
|
|
420
|
+
console.log("-- connect sol", res);
|
|
421
|
+
if (res) {
|
|
422
|
+
return res;
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
newNamespace.current = types.ChainNamespace.evm;
|
|
427
|
+
const evmOption = options.autoSelect ? {
|
|
428
|
+
autoSelect: options.autoSelect
|
|
429
|
+
} : void 0;
|
|
430
|
+
return evm.connect(evmOption).then((res) => {
|
|
431
|
+
console.log("-- connect evm", res);
|
|
432
|
+
if (!res.length) {
|
|
433
|
+
return Promise.reject({ message: "user reject" });
|
|
434
|
+
}
|
|
435
|
+
return res;
|
|
436
|
+
}).catch((e) => {
|
|
437
|
+
console.log("-- connect evm error", e);
|
|
438
|
+
return Promise.reject(e);
|
|
439
|
+
});
|
|
440
|
+
};
|
|
441
|
+
const disconnect = async () => {
|
|
442
|
+
if (namespace === types.ChainNamespace.evm) {
|
|
443
|
+
return evm.disconnect();
|
|
444
|
+
}
|
|
445
|
+
if (namespace === types.ChainNamespace.solana) {
|
|
446
|
+
return sol.disconnect();
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
const connecting = newNamespace.current == types.ChainNamespace.solana ? sol.connecting : evm.connecting;
|
|
450
|
+
const wallet = namespace === types.ChainNamespace.solana && sol.connected ? sol.wallet : namespace === types.ChainNamespace.evm && evm.connected ? evm.wallet : null;
|
|
451
|
+
const connectedChain = namespace === types.ChainNamespace.solana ? sol.connectedChain : evm.connectedChain;
|
|
452
|
+
const setChain = (chain) => {
|
|
453
|
+
const chainId = typeof chain.chainId === "number" ? chain.chainId : utils.hex2int(chain.chainId);
|
|
454
|
+
let tempNamespace = types.ChainNamespace.evm;
|
|
455
|
+
if (Array.from(SolanaChains.values()).includes(chainId)) {
|
|
456
|
+
tempNamespace = types.ChainNamespace.solana;
|
|
457
|
+
}
|
|
458
|
+
console.log("--- namespace", {
|
|
459
|
+
namespace,
|
|
460
|
+
tempNamespace
|
|
461
|
+
});
|
|
462
|
+
if (namespace === tempNamespace && namespace === types.ChainNamespace.evm) {
|
|
463
|
+
return evm.changeChain(chain);
|
|
464
|
+
}
|
|
465
|
+
if (namespace !== tempNamespace) {
|
|
466
|
+
return connect({ chainId }).then();
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
React.useEffect(() => {
|
|
470
|
+
if (sol.connected && evm.connected) {
|
|
471
|
+
if (newNamespace.current === types.ChainNamespace.solana) {
|
|
472
|
+
evm.disconnect().then();
|
|
473
|
+
setNamespace(types.ChainNamespace.solana);
|
|
474
|
+
return;
|
|
475
|
+
} else {
|
|
476
|
+
setNamespace(types.ChainNamespace.evm);
|
|
477
|
+
sol.disconnect().then();
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
if (sol.connected) {
|
|
482
|
+
setNamespace(types.ChainNamespace.solana);
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
if (evm.connected) {
|
|
486
|
+
setNamespace(types.ChainNamespace.evm);
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
}, [newNamespace.current, sol.connected, evm.connected]);
|
|
97
490
|
return /* @__PURE__ */ React__default.default.createElement(
|
|
98
491
|
hooks.WalletConnectorContext.Provider,
|
|
99
492
|
{
|
|
@@ -102,18 +495,40 @@ function Main(props) {
|
|
|
102
495
|
disconnect,
|
|
103
496
|
connecting,
|
|
104
497
|
wallet,
|
|
105
|
-
|
|
106
|
-
connectedChain
|
|
107
|
-
|
|
108
|
-
namespace: types.ChainNamespace.solana
|
|
109
|
-
}
|
|
498
|
+
setChain,
|
|
499
|
+
connectedChain,
|
|
500
|
+
namespace
|
|
110
501
|
}
|
|
111
502
|
},
|
|
112
503
|
props.children
|
|
113
504
|
);
|
|
114
505
|
}
|
|
506
|
+
function InitEvm(props) {
|
|
507
|
+
const [initialized, setInitialized] = React.useState(!!props.skipInit);
|
|
508
|
+
const { get, register } = hooks.useSimpleDI();
|
|
509
|
+
React.useEffect(() => {
|
|
510
|
+
document.body.style.setProperty("--onboard-modal-z-index", "88");
|
|
511
|
+
}, []);
|
|
512
|
+
React.useEffect(() => {
|
|
513
|
+
if (props.skipInit) {
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
let onboardAPI = get("onboardAPI");
|
|
517
|
+
if (onboardAPI) {
|
|
518
|
+
console.log("[Orderly SDK]:onboardAPI already initialized");
|
|
519
|
+
setInitialized(true);
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
onboardAPI = initConfig(props.apiKey, props.options);
|
|
523
|
+
register("onboardAPI", onboardAPI);
|
|
524
|
+
setInitialized(true);
|
|
525
|
+
}, []);
|
|
526
|
+
if (!initialized)
|
|
527
|
+
return null;
|
|
528
|
+
return props.children;
|
|
529
|
+
}
|
|
115
530
|
function WalletConnectorProvider(props) {
|
|
116
|
-
return /* @__PURE__ */ React__default.default.createElement(InitSolana, { ...props.solanaInitial ?? {} }, /* @__PURE__ */ React__default.default.createElement(Main,
|
|
531
|
+
return /* @__PURE__ */ React__default.default.createElement(InitSolana, { ...props.solanaInitial ?? {} }, /* @__PURE__ */ React__default.default.createElement(InitEvm, { ...props.evmInitial ?? {} }, /* @__PURE__ */ React__default.default.createElement(Main, { solanaNetwork: props.solanaInitial?.network ?? walletAdapterBase.WalletAdapterNetwork.Devnet }, props.children)));
|
|
117
532
|
}
|
|
118
533
|
|
|
119
534
|
exports.WalletConnectorProvider = WalletConnectorProvider;
|