Skip to content

Commit 4cc2b8e

Browse files
refactor(sdk): IExecWeb3telegram uses async config and chain detection
1 parent ab33c42 commit 4cc2b8e

File tree

1 file changed

+123
-44
lines changed

1 file changed

+123
-44
lines changed
Lines changed: 123 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,88 @@
11
import { AbstractProvider, AbstractSigner, Eip1193Provider } from 'ethers';
22
import { IExec } from 'iexec';
3+
import { GraphQLClient } from 'graphql-request';
34
import { fetchUserContacts } from './fetchUserContacts.js';
45
import { fetchMyContacts } from './fetchMyContacts.js';
56
import { sendTelegram } from './sendTelegram.js';
67
import {
78
Contact,
89
FetchUserContactsParams,
910
SendTelegramParams,
10-
Web3SignerProvider,
1111
AddressOrENS,
1212
Web3TelegramConfigOptions,
1313
SendTelegramResponse,
14+
Web3SignerProvider,
1415
FetchMyContactsParams,
1516
} from './types.js';
16-
import { GraphQLClient } from 'graphql-request';
17-
import {
18-
WEB3TELEGRAM_DAPP_ADDRESS,
19-
IPFS_UPLOAD_URL,
20-
DEFAULT_IPFS_GATEWAY,
21-
DATAPROTECTOR_SUBGRAPH_ENDPOINT,
22-
WHITELIST_SMART_CONTRACT_ADDRESS,
23-
} from '../config/config.js';
17+
import { CHAIN_CONFIG } from '../config/config.js';
2418
import { isValidProvider } from '../utils/validators.js';
19+
import { getChainIdFromProvider } from '../utils/getChainId.js';
20+
21+
type EthersCompatibleProvider =
22+
| AbstractProvider
23+
| AbstractSigner
24+
| Eip1193Provider
25+
| Web3SignerProvider
26+
| string;
27+
28+
interface Web3telegramResolvedConfig {
29+
dappAddressOrENS: AddressOrENS;
30+
dappWhitelistAddress: AddressOrENS;
31+
graphQLClient: GraphQLClient;
32+
ipfsNode: string;
33+
ipfsGateway: string;
34+
defaultWorkerpool: string;
35+
iexec: IExec;
36+
}
2537

2638
export class IExecWeb3telegram {
27-
private iexec: IExec;
39+
private dappAddressOrENS!: AddressOrENS;
40+
41+
private dappWhitelistAddress!: AddressOrENS;
2842

29-
private ipfsNode: string;
43+
private graphQLClient!: GraphQLClient;
3044

31-
private ipfsGateway: string;
45+
private ipfsNode!: string;
3246

33-
private dataProtectorSubgraph: string;
47+
private ipfsGateway!: string;
3448

35-
private dappAddressOrENS: AddressOrENS;
49+
private defaultWorkerpool!: string;
3650

37-
private dappWhitelistAddress: AddressOrENS;
51+
private iexec!: IExec;
3852

39-
private graphQLClient: GraphQLClient;
53+
private initPromise: Promise<void> | null = null;
54+
55+
private ethProvider: EthersCompatibleProvider;
56+
57+
private options: Web3TelegramConfigOptions;
4058

4159
constructor(
42-
ethProvider?:
43-
| Eip1193Provider
44-
| AbstractProvider
45-
| AbstractSigner
46-
| Web3SignerProvider
47-
| string,
60+
ethProvider?: EthersCompatibleProvider,
4861
options?: Web3TelegramConfigOptions
4962
) {
50-
try {
51-
this.iexec = new IExec(
52-
{ ethProvider: ethProvider || 'bellecour' },
53-
options?.iexecOptions
54-
);
55-
} catch (e) {
56-
throw Error('Unsupported ethProvider');
57-
}
63+
this.ethProvider = ethProvider || 'bellecour';
64+
this.options = options || {};
65+
}
5866

59-
try {
60-
this.dataProtectorSubgraph =
61-
options?.dataProtectorSubgraph || DATAPROTECTOR_SUBGRAPH_ENDPOINT;
62-
this.graphQLClient = new GraphQLClient(this.dataProtectorSubgraph);
63-
} catch (e) {
64-
throw Error('Impossible to create GraphQLClient');
67+
async init(): Promise<void> {
68+
if (!this.initPromise) {
69+
this.initPromise = this.resolveConfig().then((config) => {
70+
this.dappAddressOrENS = config.dappAddressOrENS;
71+
this.dappWhitelistAddress = config.dappWhitelistAddress;
72+
this.graphQLClient = config.graphQLClient;
73+
this.ipfsNode = config.ipfsNode;
74+
this.ipfsGateway = config.ipfsGateway;
75+
this.defaultWorkerpool = config.defaultWorkerpool;
76+
this.iexec = config.iexec;
77+
});
6578
}
66-
67-
this.dappAddressOrENS =
68-
options?.dappAddressOrENS || WEB3TELEGRAM_DAPP_ADDRESS;
69-
this.ipfsNode = options?.ipfsNode || IPFS_UPLOAD_URL;
70-
this.ipfsGateway = options?.ipfsGateway || DEFAULT_IPFS_GATEWAY;
71-
this.dappWhitelistAddress =
72-
options?.dappWhitelistAddress || WHITELIST_SMART_CONTRACT_ADDRESS;
79+
return this.initPromise;
7380
}
7481

7582
async fetchMyContacts(args?: FetchMyContactsParams): Promise<Contact[]> {
83+
await this.init();
7684
await isValidProvider(this.iexec);
85+
7786
return fetchMyContacts({
7887
...args,
7988
iexec: this.iexec,
@@ -83,7 +92,9 @@ export class IExecWeb3telegram {
8392
});
8493
}
8594

86-
fetchUserContacts(args: FetchUserContactsParams): Promise<Contact[]> {
95+
async fetchUserContacts(args: FetchUserContactsParams): Promise<Contact[]> {
96+
await this.init();
97+
8798
return fetchUserContacts({
8899
...args,
89100
iexec: this.iexec,
@@ -94,9 +105,12 @@ export class IExecWeb3telegram {
94105
}
95106

96107
async sendTelegram(args: SendTelegramParams): Promise<SendTelegramResponse> {
108+
await this.init();
97109
await isValidProvider(this.iexec);
98110
return sendTelegram({
99111
...args,
112+
workerpoolAddressOrEns:
113+
args.workerpoolAddressOrEns || this.defaultWorkerpool,
100114
iexec: this.iexec,
101115
ipfsNode: this.ipfsNode,
102116
ipfsGateway: this.ipfsGateway,
@@ -105,4 +119,69 @@ export class IExecWeb3telegram {
105119
graphQLClient: this.graphQLClient,
106120
});
107121
}
122+
123+
private async resolveConfig(): Promise<Web3telegramResolvedConfig> {
124+
const chainId = await getChainIdFromProvider(this.ethProvider);
125+
const chainDefaultConfig = CHAIN_CONFIG[chainId];
126+
127+
const subgraphUrl =
128+
this.options?.dataProtectorSubgraph ||
129+
chainDefaultConfig?.dataProtectorSubgraph;
130+
const dappAddressOrENS =
131+
this.options?.dappAddressOrENS || chainDefaultConfig?.dappAddress;
132+
const dappWhitelistAddress =
133+
this.options?.dappWhitelistAddress ||
134+
chainDefaultConfig?.whitelistSmartContract;
135+
const ipfsGateway =
136+
this.options?.ipfsGateway || chainDefaultConfig?.ipfsGateway;
137+
const defaultWorkerpool = chainDefaultConfig?.prodWorkerpoolAddress;
138+
const ipfsNode =
139+
this.options?.ipfsNode || chainDefaultConfig?.ipfsUploadUrl;
140+
141+
const missing = [];
142+
if (!subgraphUrl) missing.push('dataProtectorSubgraph');
143+
if (!dappAddressOrENS) missing.push('dappAddress');
144+
if (!dappWhitelistAddress) missing.push('whitelistSmartContract');
145+
if (!ipfsGateway) missing.push('ipfsGateway');
146+
if (!defaultWorkerpool) missing.push('prodWorkerpoolAddress');
147+
if (!ipfsNode) missing.push('ipfsUploadUrl');
148+
149+
if (missing.length > 0) {
150+
throw new Error(
151+
`Missing required configuration for chainId ${chainId}: ${missing.join(
152+
', '
153+
)}`
154+
);
155+
}
156+
157+
let iexec: IExec, graphQLClient: GraphQLClient;
158+
159+
try {
160+
iexec = new IExec(
161+
{ ethProvider: this.ethProvider },
162+
{
163+
ipfsGatewayURL: ipfsGateway,
164+
...this.options?.iexecOptions,
165+
}
166+
);
167+
} catch (e: any) {
168+
throw new Error(`Unsupported ethProvider: ${e.message}`);
169+
}
170+
171+
try {
172+
graphQLClient = new GraphQLClient(subgraphUrl);
173+
} catch (error: any) {
174+
throw new Error(`Failed to create GraphQLClient: ${error.message}`);
175+
}
176+
177+
return {
178+
dappAddressOrENS,
179+
dappWhitelistAddress: dappWhitelistAddress.toLowerCase(),
180+
defaultWorkerpool,
181+
graphQLClient,
182+
ipfsNode,
183+
ipfsGateway,
184+
iexec,
185+
};
186+
}
108187
}

0 commit comments

Comments
 (0)