Skip to content

Commit ef0c2a3

Browse files
feat: add support for experimental networks
1 parent 05822f8 commit ef0c2a3

File tree

5 files changed

+78
-10
lines changed

5 files changed

+78
-10
lines changed

src/config/config.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ interface ChainConfig {
2020
ipfsUploadUrl: string;
2121
ipfsGateway: string;
2222
whitelistSmartContract: string;
23+
isExperimental?: boolean;
2324
}
2425

25-
export const CHAIN_CONFIG: Record<number, ChainConfig> = {
26+
const CHAIN_CONFIG: Record<number, ChainConfig> = {
2627
[CHAIN_IDS.BELLECOUR]: {
2728
name: 'bellecour',
2829
dappAddress: 'web3mail.apps.iexec.eth',
@@ -33,12 +34,32 @@ export const CHAIN_CONFIG: Record<number, ChainConfig> = {
3334
ipfsGateway: 'https://ipfs-gateway.v8-bellecour.iex.ec',
3435
whitelistSmartContract: '0x781482C39CcE25546583EaC4957Fb7Bf04C277D2',
3536
},
37+
[CHAIN_IDS.ARBITRUM_SEPOLIA]: {
38+
name: 'arbitrum-sepolia-testnet',
39+
dappAddress: 'web3mail.apps.iexec.eth',
40+
prodWorkerpoolAddress: '0x39c3cdd91a7f1c4ed59108a9da4e79de9a1c1b59',
41+
dataProtectorSubgraph:
42+
'https://thegraph.iex.ec/subgraphs/name/bellecour/dataprotector-v2',
43+
ipfsGateway: 'https://ipfs-gateway.arbitrum-sepolia-testnet.iex.ec',
44+
ipfsUploadUrl: 'https://ipfs-upload.arbitrum-sepolia-testnet.iex.ec',
45+
whitelistSmartContract: '0x781482C39CcE25546583EaC4957Fb7Bf04C277D2', // TODO: add the correct address
46+
isExperimental: true,
47+
},
3648
};
3749

38-
export function getChainConfig(chainId: number): ChainConfig {
50+
export const getChainConfig = (
51+
chainId: number,
52+
options?: { allowExperimentalNetworks?: boolean }
53+
): ChainConfig => {
3954
const config = CHAIN_CONFIG[chainId];
55+
4056
if (!config) {
41-
throw new Error(`Unsupported chain ID: ${chainId}`);
57+
throw new Error(`Chain configuration not found for chainId: ${chainId}`);
4258
}
59+
60+
if (config.isExperimental && !options?.allowExperimentalNetworks) {
61+
throw new Error(`Experimental network ${chainId} is not allowed. Use allowExperimentalNetworks option to enable it.`);
62+
}
63+
4364
return config;
44-
}
65+
};

src/utils/getWeb3Provider.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { getSignerFromPrivateKey } from 'iexec/utils';
22
import { Web3SignerProvider } from '../web3mail/types.js';
33

4-
export const getWeb3Provider = (privateKey: string): Web3SignerProvider =>
5-
getSignerFromPrivateKey('bellecour', privateKey);
4+
export const getWeb3Provider = (
5+
privateKey: string,
6+
options: { allowExperimentalNetworks?: boolean; host?: number | string } = {}
7+
): Web3SignerProvider => {
8+
const chainHost = options?.host ? `${options.host}` : 'bellecour';
9+
return getSignerFromPrivateKey(chainHost, privateKey, {
10+
allowExperimentalNetworks: options?.allowExperimentalNetworks,
11+
providers: {},
12+
});
13+
};

src/web3mail/IExecWeb3mail.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import {
1414
Web3SignerProvider,
1515
FetchMyContactsParams,
1616
} from './types.js';
17-
import { CHAIN_CONFIG } from '../config/config.js';
1817
import { isValidProvider } from '../utils/validators.js';
1918
import { getChainIdFromProvider } from '../utils/getChainId.js';
19+
import { getChainConfig } from '../config/config.js';
2020

2121
type EthersCompatibleProvider =
2222
| AbstractProvider
@@ -122,8 +122,9 @@ export class IExecWeb3mail {
122122

123123
private async resolveConfig(): Promise<Web3mailResolvedConfig> {
124124
const chainId = await getChainIdFromProvider(this.ethProvider);
125-
const chainDefaultConfig = CHAIN_CONFIG[chainId];
126-
125+
const chainDefaultConfig = getChainConfig(chainId, {
126+
allowExperimentalNetworks: this.options.allowExperimentalNetworks,
127+
});
127128
const subgraphUrl =
128129
this.options?.dataProtectorSubgraph ||
129130
chainDefaultConfig?.dataProtectorSubgraph;
@@ -162,6 +163,7 @@ export class IExecWeb3mail {
162163
{
163164
ipfsGatewayURL: ipfsGateway,
164165
...this.options?.iexecOptions,
166+
allowExperimentalNetworks: this.options.allowExperimentalNetworks,
165167
}
166168
);
167169
} catch (e: any) {

src/web3mail/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ export type Web3MailConfigOptions = {
108108
* If not provided, the default IPFS gateway URL will be used.
109109
*/
110110
ipfsGateway?: string;
111+
112+
/**
113+
* if true allows using a provider connected to an experimental networks (default false)
114+
*
115+
* ⚠️ experimental networks are networks on which the iExec's stack is partially deployed, experimental networks can be subject to instabilities or discontinuity. Access is provided without warranties.
116+
*/
117+
allowExperimentalNetworks?: boolean;
111118
};
112119

113120
export type DappAddressConsumer = {

tests/e2e/constructor.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// needed to access and assert IExecDataProtector's private properties
33
import { describe, expect, it } from '@jest/globals';
44
import { Wallet } from 'ethers';
5-
import { IExecWeb3mail } from '../../src/index.js';
5+
import { getWeb3Provider, IExecWeb3mail } from '../../src/index.js';
66
import {
77
getTestWeb3SignerProvider,
88
MAX_EXPECTED_WEB2_SERVICES_TIME,
@@ -124,4 +124,34 @@ describe('IExecWeb3mail()', () => {
124124
},
125125
MAX_EXPECTED_WEB2_SERVICES_TIME
126126
);
127+
128+
describe('When instantiating SDK with an experimental network', () => {
129+
const experimentalNetworkSigner = getWeb3Provider(
130+
Wallet.createRandom().privateKey,
131+
{
132+
host: 421614,
133+
allowExperimentalNetworks: true,
134+
}
135+
);
136+
137+
describe('Without allowExperimentalNetworks', () => {
138+
it('should throw a configuration error', async () => {
139+
const web3mail = new IExecWeb3mail(experimentalNetworkSigner);
140+
await expect(web3mail.init()).rejects.toThrow(
141+
'Experimental network 421614 is not allowed. Use allowExperimentalNetworks option to enable it.'
142+
);
143+
});
144+
});
145+
146+
describe('With allowExperimentalNetworks: true', () => {
147+
it('should resolve the configuration', async () => {
148+
const web3mail = new IExecWeb3mail(
149+
experimentalNetworkSigner,
150+
{ allowExperimentalNetworks: true }
151+
);
152+
await expect(web3mail.init()).resolves.toBeUndefined();
153+
expect(web3mail).toBeInstanceOf(IExecWeb3mail);
154+
});
155+
});
156+
});
127157
});

0 commit comments

Comments
 (0)