-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiexecSdkClient.ts
More file actions
59 lines (50 loc) · 1.47 KB
/
iexecSdkClient.ts
File metadata and controls
59 lines (50 loc) · 1.47 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
import { IExec, IExecConfig, Eip1193Provider } from 'iexec';
import { type Connector } from 'wagmi';
import { getChainFromId } from '@/utils/chain.utils';
let iExec: IExec | null = null;
let readonlyIExec: IExec | null = null;
// Basic promise queue for pending getIExec() requests
const IEXEC_CLIENT_RESOLVES: Array<Promise<IExec>> = [];
// Clean both SDKs
export function cleanIExecSDKs() {
iExec = null;
readonlyIExec = null;
}
export async function initIExecSDKs({ connector }: { connector?: Connector }) {
if (!connector || !connector.getProvider) {
cleanIExecSDKs();
return;
}
const provider = (await connector.getProvider()) as Eip1193Provider;
if (!provider) {
cleanIExecSDKs();
return;
}
// Initialize
const config = new IExecConfig(
{ ethProvider: provider },
{ allowExperimentalNetworks: true }
);
iExec = new IExec(config);
IEXEC_CLIENT_RESOLVES.forEach((resolve) => resolve(iExec!));
IEXEC_CLIENT_RESOLVES.length = 0;
}
export function getIExec(): Promise<IExec> {
if (!iExec) {
return new Promise((resolve) => {
IEXEC_CLIENT_RESOLVES.push(resolve);
});
}
return Promise.resolve(iExec);
}
export function getReadonlyIExec(chainId: number): IExec {
const chain = getChainFromId(chainId);
if (!chain) throw new Error(`Unknown chainId ${chainId}`);
if (!readonlyIExec) {
readonlyIExec = new IExec(
{ ethProvider: chain.id },
{ allowExperimentalNetworks: true }
);
}
return readonlyIExec;
}