Skip to content

Commit a0ec68b

Browse files
refactor: rename getChainConfig to getChainDefaultConfig and improve resolveConfig behavior
1 parent 53ec9ee commit a0ec68b

File tree

8 files changed

+68
-44
lines changed

8 files changed

+68
-44
lines changed

src/config/config.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,18 @@ const CHAIN_CONFIG: Record<number, ChainConfig> = {
4141
},
4242
};
4343

44-
export const getChainConfig = (
44+
export const getChainDefaultConfig = (
4545
chainId: number,
4646
options?: { allowExperimentalNetworks?: boolean }
47-
): ChainConfig => {
47+
): ChainConfig | null => {
4848
const config = CHAIN_CONFIG[chainId];
4949

5050
if (!config) {
51-
throw new Error(`Chain configuration not found for chainId: ${chainId}`);
51+
return null;
5252
}
5353

5454
if (config.isExperimental && !options?.allowExperimentalNetworks) {
55-
throw new Error(
56-
`Experimental network ${chainId} is not allowed. Use allowExperimentalNetworks option to enable it.`
57-
);
55+
return null;
5856
}
5957

6058
return config;

src/web3mail/IExecWeb3mail.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from './types.js';
1717
import { isValidProvider } from '../utils/validators.js';
1818
import { getChainIdFromProvider } from '../utils/getChainId.js';
19-
import { getChainConfig } from '../config/config.js';
19+
import { getChainDefaultConfig } from '../config/config.js';
2020

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

123123
private async resolveConfig(): Promise<Web3mailResolvedConfig> {
124124
const chainId = await getChainIdFromProvider(this.ethProvider);
125-
const chainDefaultConfig = getChainConfig(chainId, {
125+
const chainDefaultConfig = getChainDefaultConfig(chainId, {
126126
allowExperimentalNetworks: this.options.allowExperimentalNetworks,
127127
});
128+
129+
// assigning config from options and fallback to default config
128130
const subgraphUrl =
129131
this.options?.dataProtectorSubgraph ||
130132
chainDefaultConfig?.dataProtectorSubgraph;
@@ -139,6 +141,7 @@ export class IExecWeb3mail {
139141
const ipfsNode =
140142
this.options?.ipfsNode || chainDefaultConfig?.ipfsUploadUrl;
141143

144+
// throw if any config field is missing
142145
const missing = [];
143146
if (!subgraphUrl) missing.push('dataProtectorSubgraph');
144147
if (!dappAddressOrENS) missing.push('dappAddress');

tests/e2e/constructor.test.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
getTestWeb3SignerProvider,
88
MAX_EXPECTED_WEB2_SERVICES_TIME,
99
} from '../test-utils.js';
10-
import { DEFAULT_CHAIN_ID, getChainConfig } from '../../src/config/config.js';
10+
import { DEFAULT_CHAIN_ID, getChainDefaultConfig } from '../../src/config/config.js';
1111

1212
describe('IExecWeb3mail()', () => {
1313
it('instantiates with a valid ethProvider', async () => {
@@ -26,7 +26,9 @@ describe('IExecWeb3mail()', () => {
2626
);
2727
await web3mail.init();
2828
const ipfsGateway = web3mail['ipfsGateway'];
29-
expect(ipfsGateway).toStrictEqual(getChainConfig(DEFAULT_CHAIN_ID).ipfsGateway);
29+
const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID);
30+
expect(defaultConfig).not.toBeNull();
31+
expect(ipfsGateway).toStrictEqual(defaultConfig!.ipfsGateway);
3032
});
3133

3234
it('should use provided ipfs gateway url when ipfsGateway is provided', async () => {
@@ -49,9 +51,11 @@ describe('IExecWeb3mail()', () => {
4951
getTestWeb3SignerProvider(wallet.privateKey)
5052
);
5153
await web3mail.init();
52-
const graphQLClientUrl = web3mail['graphQLClient'];
53-
expect(graphQLClientUrl['url']).toBe(
54-
getChainConfig(DEFAULT_CHAIN_ID).dataProtectorSubgraph);
54+
const graphQLClient = web3mail['graphQLClient'];
55+
const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID);
56+
expect(defaultConfig).not.toBeNull();
57+
expect(graphQLClient['url']).toBe(
58+
defaultConfig!.dataProtectorSubgraph);
5559
});
5660

5761
it('should use provided data Protector Subgraph URL when subgraphUrl is provided', async () => {
@@ -138,7 +142,7 @@ describe('IExecWeb3mail()', () => {
138142
it('should throw a configuration error', async () => {
139143
const web3mail = new IExecWeb3mail(experimentalNetworkSigner);
140144
await expect(web3mail.init()).rejects.toThrow(
141-
'Experimental network 421614 is not allowed. Use allowExperimentalNetworks option to enable it.'
145+
'Missing required configuration for chainId 421614: dataProtectorSubgraph, dappAddress, whitelistSmartContract, ipfsGateway, prodWorkerpoolAddress, ipfsUploadUrl'
142146
);
143147
});
144148
});

tests/e2e/fetchMyContacts.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
waitSubgraphIndexing,
1717
} from '../test-utils.js';
1818
import IExec from 'iexec/IExec';
19-
import { DEFAULT_CHAIN_ID, getChainConfig } from '../../src/config/config.js';
19+
import { DEFAULT_CHAIN_ID, getChainDefaultConfig } from '../../src/config/config.js';
2020

2121
describe('web3mail.fetchMyContacts()', () => {
2222
let wallet: HDNodeWallet;
@@ -41,7 +41,7 @@ describe('web3mail.fetchMyContacts()', () => {
4141
'pass with a granted access for a specific requester',
4242
async () => {
4343
await dataProtector.grantAccess({
44-
authorizedApp: getChainConfig(DEFAULT_CHAIN_ID).dappAddress,
44+
authorizedApp: getChainDefaultConfig(DEFAULT_CHAIN_ID).dappAddress,
4545
protectedData: protectedData.address,
4646
authorizedUser: wallet.address,
4747
});
@@ -65,7 +65,7 @@ describe('web3mail.fetchMyContacts()', () => {
6565
'pass with a granted access for any requester',
6666
async () => {
6767
const grantedAccessForAnyRequester = await dataProtector.grantAccess({
68-
authorizedApp: getChainConfig(DEFAULT_CHAIN_ID).dappAddress,
68+
authorizedApp: getChainDefaultConfig(DEFAULT_CHAIN_ID).dappAddress,
6969
protectedData: protectedData.address,
7070
authorizedUser: NULL_ADDRESS,
7171
});
@@ -104,7 +104,7 @@ describe('web3mail.fetchMyContacts()', () => {
104104
const encryptionKey = await iexec.dataset.generateEncryptionKey();
105105
await iexec.dataset.pushDatasetSecret(dataset.address, encryptionKey);
106106
await dataProtector.grantAccess({
107-
authorizedApp: getChainConfig(DEFAULT_CHAIN_ID).dappAddress,
107+
authorizedApp: getChainDefaultConfig(DEFAULT_CHAIN_ID).dappAddress,
108108
protectedData: dataset.address,
109109
authorizedUser: wallet.address,
110110
});
@@ -126,7 +126,7 @@ describe('web3mail.fetchMyContacts()', () => {
126126
await waitSubgraphIndexing();
127127

128128
await dataProtector.grantAccess({
129-
authorizedApp: getChainConfig(DEFAULT_CHAIN_ID).dappAddress,
129+
authorizedApp: getChainDefaultConfig(DEFAULT_CHAIN_ID).dappAddress,
130130
protectedData: notValidProtectedData.address,
131131
authorizedUser: wallet.address,
132132
});

tests/e2e/fetchUserContacts.test.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
} from '@iexec/dataprotector';
55
import { beforeAll, describe, expect, it } from '@jest/globals';
66
import { HDNodeWallet, Wallet } from 'ethers';
7-
import { DEFAULT_CHAIN_ID, getChainConfig } from '../../src/config/config.js';
7+
import { DEFAULT_CHAIN_ID, getChainDefaultConfig } from '../../src/config/config.js';
88
import { IExecWeb3mail, WorkflowError } from '../../src/index.js';
99
import {
1010
MAX_EXPECTED_BLOCKTIME,
@@ -44,14 +44,18 @@ describe('web3mail.fetchMyContacts()', () => {
4444
async () => {
4545
const user1 = Wallet.createRandom().address;
4646
const user2 = Wallet.createRandom().address;
47+
const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID);
48+
expect(defaultConfig).not.toBeNull();
49+
const authorizedApp = defaultConfig!.dappAddress;
50+
4751
await dataProtector.grantAccess({
48-
authorizedApp: getChainConfig(DEFAULT_CHAIN_ID).dappAddress,
52+
authorizedApp: authorizedApp,
4953
protectedData: protectedData1.address,
5054
authorizedUser: user1,
5155
});
5256

5357
await dataProtector.grantAccess({
54-
authorizedApp: getChainConfig(DEFAULT_CHAIN_ID).dappAddress,
58+
authorizedApp: authorizedApp,
5559
protectedData: protectedData2.address,
5660
authorizedUser: user2,
5761
});
@@ -71,8 +75,12 @@ describe('web3mail.fetchMyContacts()', () => {
7175
'Test that the protected data can be accessed by authorized user',
7276
async () => {
7377
const userWithAccess = Wallet.createRandom().address;
78+
const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID);
79+
expect(defaultConfig).not.toBeNull();
80+
const authorizedApp = defaultConfig!.dappAddress;
81+
7482
await dataProtector.grantAccess({
75-
authorizedApp: getChainConfig(DEFAULT_CHAIN_ID).dappAddress,
83+
authorizedApp: authorizedApp,
7684
protectedData: protectedData1.address,
7785
authorizedUser: userWithAccess,
7886
});

tests/e2e/sendEmail.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
} from '../test-utils.js';
2525
import { IExec } from 'iexec';
2626
import { NULL_ADDRESS } from 'iexec/utils';
27-
import { DEFAULT_CHAIN_ID, getChainConfig } from '../../src/config/config.js';
27+
import { DEFAULT_CHAIN_ID, getChainDefaultConfig } from '../../src/config/config.js';
2828

2929
describe('web3mail.sendEmail()', () => {
3030
let consumerWallet: HDNodeWallet;
@@ -62,7 +62,9 @@ describe('web3mail.sendEmail()', () => {
6262
TEST_CHAIN.appOwnerWallet.privateKey
6363
);
6464
const resourceProvider = new IExec({ ethProvider }, iexecOptions);
65-
await createAndPublishAppOrders(resourceProvider, getChainConfig(DEFAULT_CHAIN_ID).dappAddress);
65+
const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID);
66+
expect(defaultConfig).not.toBeNull();
67+
await createAndPublishAppOrders(resourceProvider, defaultConfig!.dappAddress);
6668

6769
learnProdWorkerpoolAddress = await resourceProvider.ens.resolveName(
6870
TEST_CHAIN.learnProdWorkerpool
@@ -98,7 +100,7 @@ describe('web3mail.sendEmail()', () => {
98100
iexecOptions
99101
);
100102
await dataProtector.grantAccess({
101-
authorizedApp: getChainConfig(DEFAULT_CHAIN_ID).dappAddress,
103+
authorizedApp: getChainDefaultConfig(DEFAULT_CHAIN_ID).dappAddress,
102104
protectedData: validProtectedData.address,
103105
authorizedUser: consumerWallet.address, // consumer wallet
104106
numberOfAccess: 1000,
@@ -283,7 +285,7 @@ describe('web3mail.sendEmail()', () => {
283285

284286
//grant access to whitelist
285287
await dataProtector.grantAccess({
286-
authorizedApp: getChainConfig(DEFAULT_CHAIN_ID).whitelistSmartContract, //whitelist address
288+
authorizedApp: getChainDefaultConfig(DEFAULT_CHAIN_ID).whitelistSmartContract, //whitelist address
287289
protectedData: protectedDataForWhitelist.address,
288290
authorizedUser: consumerWallet.address, // consumer wallet
289291
numberOfAccess: 1000,

tests/unit/fetchMyContacts.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, expect, it, jest } from '@jest/globals';
22
import { Address } from 'iexec';
33
import { type FetchMyContacts } from '../../src/web3mail/fetchMyContacts.js';
44
import { getRandomAddress } from '../test-utils.js';
5-
import { DEFAULT_CHAIN_ID, getChainConfig } from '../../src/config/config.js';
5+
import { DEFAULT_CHAIN_ID, getChainDefaultConfig } from '../../src/config/config.js';
66

77
jest.unstable_mockModule('../../src/utils/subgraphQuery.js', () => ({
88
getValidContact: jest.fn(),
@@ -75,16 +75,16 @@ describe('fetchMyContacts', () => {
7575
iexec: iexec,
7676
// @ts-expect-error No need for graphQLClient here
7777
graphQLClient: {},
78-
dappAddressOrENS: getChainConfig(DEFAULT_CHAIN_ID).dappAddress,
79-
dappWhitelistAddress: getChainConfig(DEFAULT_CHAIN_ID).whitelistSmartContract,
78+
dappAddressOrENS: getChainDefaultConfig(DEFAULT_CHAIN_ID).dappAddress,
79+
dappWhitelistAddress: getChainDefaultConfig(DEFAULT_CHAIN_ID).whitelistSmartContract,
8080
isUserStrict: false,
8181
});
8282
const userAddress = (await iexec.wallet.getAddress()).toLowerCase();
8383
expect(iexec.orderbook.fetchDatasetOrderbook).toHaveBeenNthCalledWith(
8484
1,
8585
'any',
8686
{
87-
app: getChainConfig(DEFAULT_CHAIN_ID).dappAddress.toLowerCase(),
87+
app: getChainDefaultConfig(DEFAULT_CHAIN_ID).dappAddress.toLowerCase(),
8888
requester: userAddress,
8989
isAppStrict: true,
9090
isRequesterStrict: false,
@@ -95,7 +95,7 @@ describe('fetchMyContacts', () => {
9595
2,
9696
'any',
9797
{
98-
app: getChainConfig(DEFAULT_CHAIN_ID).whitelistSmartContract.toLowerCase(),
98+
app: getChainDefaultConfig(DEFAULT_CHAIN_ID).whitelistSmartContract.toLowerCase(),
9999
requester: userAddress,
100100
isAppStrict: true,
101101
isRequesterStrict: false,
@@ -141,16 +141,16 @@ describe('fetchMyContacts', () => {
141141
iexec: iexec,
142142
// @ts-expect-error No need for graphQLClient here
143143
graphQLClient: {},
144-
dappAddressOrENS: getChainConfig(DEFAULT_CHAIN_ID).dappAddress,
145-
dappWhitelistAddress: getChainConfig(DEFAULT_CHAIN_ID).whitelistSmartContract.toLowerCase(),
144+
dappAddressOrENS: getChainDefaultConfig(DEFAULT_CHAIN_ID).dappAddress,
145+
dappWhitelistAddress: getChainDefaultConfig(DEFAULT_CHAIN_ID).whitelistSmartContract.toLowerCase(),
146146
isUserStrict: true,
147147
});
148148
const userAddress = (await iexec.wallet.getAddress()).toLowerCase();
149149
expect(iexec.orderbook.fetchDatasetOrderbook).toHaveBeenNthCalledWith(
150150
1,
151151
'any',
152152
{
153-
app: getChainConfig(DEFAULT_CHAIN_ID).dappAddress.toLowerCase(),
153+
app: getChainDefaultConfig(DEFAULT_CHAIN_ID).dappAddress.toLowerCase(),
154154
requester: userAddress,
155155
isAppStrict: true,
156156
isRequesterStrict: true,
@@ -161,7 +161,7 @@ describe('fetchMyContacts', () => {
161161
2,
162162
'any',
163163
{
164-
app: getChainConfig(DEFAULT_CHAIN_ID).whitelistSmartContract.toLowerCase(),
164+
app: getChainDefaultConfig(DEFAULT_CHAIN_ID).whitelistSmartContract.toLowerCase(),
165165
requester: userAddress,
166166
isAppStrict: true,
167167
isRequesterStrict: true,

tests/unit/sendEmail.test.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ValidationError } from 'yup';
33
import { type SendEmail } from '../../src/web3mail/sendEmail.js';
44
import { getRandomAddress, TEST_CHAIN } from '../test-utils.js';
55
import { mockAllForSendEmail } from '../utils/mockAllForSendEmail.js';
6-
import { DEFAULT_CHAIN_ID, getChainConfig } from '../../src/config/config.js';
6+
import { DEFAULT_CHAIN_ID, getChainDefaultConfig } from '../../src/config/config.js';
77

88
jest.unstable_mockModule('../../src/utils/subgraphQuery.js', () => ({
99
checkProtectedDataValidity: jest.fn(),
@@ -175,22 +175,31 @@ describe('sendEmail', () => {
175175
graphQLClient: {},
176176
// @ts-expect-error No need for iexec here
177177
iexec,
178-
ipfsGateway: getChainConfig(DEFAULT_CHAIN_ID).ipfsGateway,
179-
ipfsNode: getChainConfig(DEFAULT_CHAIN_ID).ipfsUploadUrl,
180-
workerpoolAddressOrEns: getChainConfig(DEFAULT_CHAIN_ID).prodWorkerpoolAddress,
181-
dappAddressOrENS: getChainConfig(DEFAULT_CHAIN_ID).dappAddress,
182-
dappWhitelistAddress: getChainConfig(DEFAULT_CHAIN_ID).whitelistSmartContract.toLowerCase(),
178+
ipfsGateway: getChainDefaultConfig(DEFAULT_CHAIN_ID)?.ipfsGateway,
179+
ipfsNode: getChainDefaultConfig(DEFAULT_CHAIN_ID)?.ipfsUploadUrl,
180+
workerpoolAddressOrEns: getChainDefaultConfig(DEFAULT_CHAIN_ID)?.prodWorkerpoolAddress,
181+
dappAddressOrENS: getChainDefaultConfig(DEFAULT_CHAIN_ID)?.dappAddress,
182+
dappWhitelistAddress: getChainDefaultConfig(DEFAULT_CHAIN_ID)?.whitelistSmartContract.toLowerCase(),
183183
emailSubject: 'e2e mail object for test',
184184
emailContent: OVERSIZED_CONTENT,
185185
protectedData,
186186
});
187187

188188
// --- THEN
189+
const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID);
190+
expect(defaultConfig).not.toBeNull();
191+
const mockConfig = {
192+
ipfsGateway: defaultConfig!.ipfsGateway,
193+
ipfsNode: defaultConfig!.ipfsUploadUrl,
194+
workerpoolAddressOrEns: defaultConfig!.prodWorkerpoolAddress,
195+
dappAddressOrENS: defaultConfig!.dappAddress,
196+
dappWhitelistAddress: defaultConfig!.whitelistSmartContract.toLowerCase(),
197+
};
189198
expect(iexec.orderbook.fetchWorkerpoolOrderbook).toHaveBeenNthCalledWith(
190199
1,
191200
{
192201
workerpool: TEST_CHAIN.prodWorkerpool,
193-
app: getChainConfig(DEFAULT_CHAIN_ID).dappAddress.toLowerCase(),
202+
app: defaultConfig!.dappAddress.toLowerCase(),
194203
dataset: protectedData,
195204
requester: userAddress,
196205
isRequesterStrict: false,
@@ -203,7 +212,7 @@ describe('sendEmail', () => {
203212
2,
204213
{
205214
workerpool: TEST_CHAIN.prodWorkerpool,
206-
app: getChainConfig(DEFAULT_CHAIN_ID).whitelistSmartContract.toLowerCase(),
215+
app: defaultConfig!.whitelistSmartContract.toLowerCase(),
207216
dataset: protectedData,
208217
requester: userAddress,
209218
isRequesterStrict: false,

0 commit comments

Comments
 (0)