diff --git a/.gitignore b/.gitignore index 5c281c9..2450061 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ /.env /*.log /*.env +**/.env # TypeScript specific *.tsbuildinfo diff --git a/package-lock.json b/package-lock.json index 20c7ee6..db003d6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,7 @@ "buffer": "^6.0.3", "ethers": "^6.8.1", "graphql-request": "^6.1.0", - "iexec": "^8.13.1", + "iexec": "^8.16.1", "kubo-rpc-client": "^4.1.3", "yup": "^1.1.1" }, @@ -5322,9 +5322,9 @@ ] }, "node_modules/iexec": { - "version": "8.15.0", - "resolved": "https://registry.npmjs.org/iexec/-/iexec-8.15.0.tgz", - "integrity": "sha512-aU11SU6vn4QgPb2oZ3o+DkhOW9f7pRgxOYxmbpWdTdjcqHTCKmVLi0LRpMeQfXkdDlAVCnYssIOynNARpPFYuQ==", + "version": "8.16.1", + "resolved": "https://registry.npmjs.org/iexec/-/iexec-8.16.1.tgz", + "integrity": "sha512-0Kxt5z2gpjcwRkGmfr3/ckOk1G3p6G4s4CQzjtgKo8v64giLneO/PcbQAkRLW7imCNegvQL6Bpw1oQCwELYHqw==", "license": "Apache-2.0", "dependencies": { "@ensdomains/ens-contracts": "^1.2.5", diff --git a/package.json b/package.json index 6cf708d..b919f23 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "buffer": "^6.0.3", "ethers": "^6.8.1", "graphql-request": "^6.1.0", - "iexec": "^8.13.1", + "iexec": "^8.16.1", "kubo-rpc-client": "^4.1.3", "yup": "^1.1.1" }, diff --git a/src/config/config.ts b/src/config/config.ts index b709e7f..17e39db 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -3,13 +3,7 @@ export const MAX_DESIRED_APP_ORDER_PRICE = 0; export const MAX_DESIRED_WORKERPOOL_ORDER_PRICE = 0; export const ANY_DATASET_ADDRESS = 'any'; -export const CHAIN_IDS = { - BELLECOUR: 134, - AVALANCHE_FUJI: 43113, - ARBITRUM_SEPOLIA: 421614, -} as const; - -export const DEFAULT_CHAIN_ID = CHAIN_IDS.BELLECOUR; +export const DEFAULT_CHAIN_ID = 134; interface ChainConfig { name: string; @@ -19,10 +13,11 @@ interface ChainConfig { ipfsUploadUrl: string; ipfsGateway: string; whitelistSmartContract: string; + isExperimental?: boolean; } export const CHAIN_CONFIG: Record = { - [CHAIN_IDS.BELLECOUR]: { + 134: { name: 'bellecour', dappAddress: 'web3telegram.apps.iexec.eth', prodWorkerpoolAddress: 'prod-v8-bellecour.main.pools.iexec.eth', @@ -32,12 +27,29 @@ export const CHAIN_CONFIG: Record = { ipfsGateway: 'https://ipfs-gateway.v8-bellecour.iex.ec', whitelistSmartContract: '0x192C6f5AccE52c81Fcc2670f10611a3665AAA98F', }, + 421614: { + name: 'arbitrum-sepolia-testnet', + dappAddress: 'web3telegram.apps.iexec.eth', + prodWorkerpoolAddress: '0x39c3cdd91a7f1c4ed59108a9da4e79de9a1c1b59', + dataProtectorSubgraph: + 'https://thegraph.arbitrum-sepolia-testnet.iex.ec/api/subgraphs/id/5YjRPLtjS6GH6bB4yY55Qg4HzwtRGQ8TaHtGf9UBWWd', + ipfsGateway: 'https://ipfs-gateway.arbitrum-sepolia-testnet.iex.ec', + ipfsUploadUrl: 'https://ipfs-upload.arbitrum-sepolia-testnet.iex.ec', + whitelistSmartContract: '0x7291ff96100DA6CF97933C225B86124ef95aEc9b', // TODO: add the correct address + isExperimental: true, + }, }; -export function getChainConfig(chainId: number): ChainConfig { +export const getChainDefaultConfig = ( + chainId: number, + options?: { allowExperimentalNetworks?: boolean } +): ChainConfig | null => { const config = CHAIN_CONFIG[chainId]; if (!config) { - throw new Error(`Unsupported chain ID: ${chainId}`); + return null; + } + if (config.isExperimental && !options?.allowExperimentalNetworks) { + return null; } return config; -} +}; diff --git a/src/utils/getWeb3Provider.ts b/src/utils/getWeb3Provider.ts index 72a103f..d9c6eb7 100644 --- a/src/utils/getWeb3Provider.ts +++ b/src/utils/getWeb3Provider.ts @@ -1,5 +1,13 @@ import { getSignerFromPrivateKey } from 'iexec/utils'; import { Web3SignerProvider } from '../web3telegram/types.js'; -export const getWeb3Provider = (privateKey: string): Web3SignerProvider => - getSignerFromPrivateKey('bellecour', privateKey); +export const getWeb3Provider = ( + privateKey: string, + options: { allowExperimentalNetworks?: boolean; host?: number | string } = {} +): Web3SignerProvider => { + const chainHost = options?.host ? `${options.host}` : 'bellecour'; + return getSignerFromPrivateKey(chainHost, privateKey, { + allowExperimentalNetworks: options?.allowExperimentalNetworks, + providers: {}, + }); +}; diff --git a/src/web3telegram/IExecWeb3telegram.ts b/src/web3telegram/IExecWeb3telegram.ts index 3186a5c..2f65415 100644 --- a/src/web3telegram/IExecWeb3telegram.ts +++ b/src/web3telegram/IExecWeb3telegram.ts @@ -14,7 +14,7 @@ import { Web3SignerProvider, FetchMyContactsParams, } from './types.js'; -import { CHAIN_CONFIG } from '../config/config.js'; +import { getChainDefaultConfig } from '../config/config.js'; import { isValidProvider } from '../utils/validators.js'; import { getChainIdFromProvider } from '../utils/getChainId.js'; @@ -122,7 +122,9 @@ export class IExecWeb3telegram { private async resolveConfig(): Promise { const chainId = await getChainIdFromProvider(this.ethProvider); - const chainDefaultConfig = CHAIN_CONFIG[chainId]; + const chainDefaultConfig = getChainDefaultConfig(chainId, { + allowExperimentalNetworks: this.options.allowExperimentalNetworks, + }); const subgraphUrl = this.options?.dataProtectorSubgraph || @@ -162,6 +164,7 @@ export class IExecWeb3telegram { { ipfsGatewayURL: ipfsGateway, ...this.options?.iexecOptions, + allowExperimentalNetworks: this.options.allowExperimentalNetworks, } ); } catch (e: any) { diff --git a/src/web3telegram/types.ts b/src/web3telegram/types.ts index 6f98671..ee81fe4 100644 --- a/src/web3telegram/types.ts +++ b/src/web3telegram/types.ts @@ -107,6 +107,13 @@ export type Web3TelegramConfigOptions = { * If not provided, the default IPFS gateway URL for the detected chain will be used. */ ipfsGateway?: string; + + /** + * if true allows using a provider connected to an experimental networks (default false) + * + * ⚠️ 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. + */ + allowExperimentalNetworks?: boolean; }; export type DappAddressConsumer = { diff --git a/tests/.env b/tests/.env deleted file mode 100644 index 20cf5ec..0000000 --- a/tests/.env +++ /dev/null @@ -1,8 +0,0 @@ -############ THIS FILE IS GENERATED ############ -# run "node prepare-test-env.js" to regenerate # -################################################ - -# blockchain node to use as the reference for the local fork -BELLECOUR_FORK_URL=https://bellecour.iex.ec -# block number to fork from -BELLECOUR_FORK_BLOCK=34403728 \ No newline at end of file diff --git a/tests/e2e/constructor.test.ts b/tests/e2e/constructor.test.ts index e02a27d..1c15b61 100644 --- a/tests/e2e/constructor.test.ts +++ b/tests/e2e/constructor.test.ts @@ -2,12 +2,15 @@ // needed to access and assert IExecDataProtector's private properties import { describe, expect, it } from '@jest/globals'; import { Wallet } from 'ethers'; -import { IExecWeb3telegram } from '../../src/index.js'; +import { getWeb3Provider, IExecWeb3telegram } from '../../src/index.js'; import { getTestWeb3SignerProvider, MAX_EXPECTED_WEB2_SERVICES_TIME, } from '../test-utils.js'; -import { CHAIN_CONFIG, CHAIN_IDS } from '../../src/config/config.js'; +import { + DEFAULT_CHAIN_ID, + getChainDefaultConfig, +} from '../../src/config/config.js'; describe('IExecWeb3telegram()', () => { it('instantiates with a valid ethProvider', async () => { @@ -25,7 +28,9 @@ describe('IExecWeb3telegram()', () => { ); await web3telegram.init(); const ipfsGateway = web3telegram['ipfsGateway']; - expect(ipfsGateway).toStrictEqual(CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].ipfsGateway); + const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID); + expect(defaultConfig).not.toBeNull(); + expect(ipfsGateway).toStrictEqual(defaultConfig!.ipfsGateway); }); it('should use provided ipfs gateway url when ipfsGateway is provided', async () => { @@ -48,8 +53,10 @@ describe('IExecWeb3telegram()', () => { getTestWeb3SignerProvider(wallet.privateKey) ); await web3telegram.init(); - const graphQLClientUrl = web3telegram['graphQLClient']; - expect(graphQLClientUrl['url']).toBe(CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dataProtectorSubgraph); + const graphQLClient = web3telegram['graphQLClient']; + const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID); + expect(defaultConfig).not.toBeNull(); + expect(graphQLClient['url']).toBe(defaultConfig!.dataProtectorSubgraph); }); it('should use provided data Protector Subgraph URL when subgraphUrl is provided', async () => { @@ -102,11 +109,101 @@ describe('IExecWeb3telegram()', () => { expect(ipfsNode).toStrictEqual(customIpfsNode); expect(ipfsGateway).toStrictEqual(customIpfsGateway); expect(dappAddressOrENS).toStrictEqual(customDapp); - expect(whitelistAddress).toStrictEqual(customDappWhitelistAddress.toLowerCase()); + expect(whitelistAddress).toStrictEqual( + customDappWhitelistAddress.toLowerCase() + ); expect(await iexec.config.resolveSmsURL()).toBe(smsURL); expect(await iexec.config.resolveIexecGatewayURL()).toBe(iexecGatewayURL); }); + describe('When instantiating SDK with an experimental network', () => { + const experimentalNetworkSigner = getWeb3Provider( + Wallet.createRandom().privateKey, + { + host: 421614, + allowExperimentalNetworks: true, + } + ); + + describe('Without allowExperimentalNetworks', () => { + it('should throw a configuration error', async () => { + const web3telegram = new IExecWeb3telegram(experimentalNetworkSigner); + await expect(web3telegram.init()).rejects.toThrow( + 'Missing required configuration for chainId 421614: dataProtectorSubgraph, dappAddress, whitelistSmartContract, ipfsGateway, prodWorkerpoolAddress, ipfsUploadUrl' + ); + }); + }); + + describe('With allowExperimentalNetworks: true', () => { + it('should resolve the configuration', async () => { + const web3telegram = new IExecWeb3telegram(experimentalNetworkSigner, { + allowExperimentalNetworks: true, + }); + await expect(web3telegram.init()).resolves.toBeUndefined(); + expect(web3telegram).toBeInstanceOf(IExecWeb3telegram); + }); + + it('should use Arbitrum Sepolia default configuration', async () => { + const web3telegram = new IExecWeb3telegram(experimentalNetworkSigner, { + allowExperimentalNetworks: true, + }); + await web3telegram.init(); + + const arbitrumSepoliaConfig = getChainDefaultConfig(421614, { + allowExperimentalNetworks: true, + }); + expect(arbitrumSepoliaConfig).not.toBeNull(); + + expect(web3telegram['ipfsGateway']).toBe( + arbitrumSepoliaConfig!.ipfsGateway + ); + expect(web3telegram['ipfsNode']).toBe(arbitrumSepoliaConfig!.ipfsUploadUrl); + expect(web3telegram['dappAddressOrENS']).toBe( + arbitrumSepoliaConfig!.dappAddress + ); + expect(web3telegram['dappWhitelistAddress']).toBe( + arbitrumSepoliaConfig!.whitelistSmartContract.toLowerCase() + ); + expect(web3telegram['defaultWorkerpool']).toBe( + arbitrumSepoliaConfig!.prodWorkerpoolAddress + ); + expect(web3telegram['graphQLClient']['url']).toBe( + arbitrumSepoliaConfig!.dataProtectorSubgraph + ); + }); + + it('should allow custom configuration override for Arbitrum Sepolia', async () => { + const customIpfsGateway = 'https://custom-arbitrum-ipfs.com'; + const customDappAddress = 'custom.arbitrum.app.eth'; + + const web3telegram = new IExecWeb3telegram(experimentalNetworkSigner, { + allowExperimentalNetworks: true, + ipfsGateway: customIpfsGateway, + dappAddressOrENS: customDappAddress, + }); + await web3telegram.init(); + + expect(web3telegram['ipfsGateway']).toBe(customIpfsGateway); + expect(web3telegram['dappAddressOrENS']).toBe(customDappAddress); + + const arbitrumSepoliaConfig = getChainDefaultConfig(421614, { + allowExperimentalNetworks: true, + }); + expect(arbitrumSepoliaConfig).not.toBeNull(); + expect(web3telegram['ipfsNode']).toBe(arbitrumSepoliaConfig!.ipfsUploadUrl); + expect(web3telegram['dappWhitelistAddress']).toBe( + arbitrumSepoliaConfig!.whitelistSmartContract.toLowerCase() + ); + expect(web3telegram['defaultWorkerpool']).toBe( + arbitrumSepoliaConfig!.prodWorkerpoolAddress + ); + expect(web3telegram['graphQLClient']['url']).toBe( + arbitrumSepoliaConfig!.dataProtectorSubgraph + ); + }); + }); + }); + it( 'When calling a read method should work as expected', async () => { diff --git a/tests/e2e/fetchMyContacts.test.ts b/tests/e2e/fetchMyContacts.test.ts index f738ed5..9c4c2be 100644 --- a/tests/e2e/fetchMyContacts.test.ts +++ b/tests/e2e/fetchMyContacts.test.ts @@ -1,11 +1,11 @@ import { describe, expect, it } from '@jest/globals'; import { IExecDataProtector } from '@iexec/dataprotector'; import { IExecWeb3telegram } from '../../src/index.js'; -import { CHAIN_CONFIG, CHAIN_IDS } from '../../src/config/config.js'; import { - getTestConfig, - waitSubgraphIndexing, -} from '../test-utils.js'; + DEFAULT_CHAIN_ID, + getChainDefaultConfig, +} from '../../src/config/config.js'; +import { getTestConfig, waitSubgraphIndexing } from '../test-utils.js'; import { HDNodeWallet, Wallet } from 'ethers'; import { NULL_ADDRESS } from 'iexec/utils'; import { @@ -22,6 +22,7 @@ describe('web3telegram.fetchMyContacts()', () => { let web3telegram: IExecWeb3telegram; let dataProtector: IExecDataProtector; let protectedData: any; + const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID); beforeAll(async () => { wallet = Wallet.createRandom(); @@ -38,7 +39,7 @@ describe('web3telegram.fetchMyContacts()', () => { 'pass with a granted access for a specific requester', async () => { await dataProtector.core.grantAccess({ - authorizedApp: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress, + authorizedApp: defaultConfig.dappAddress, protectedData: protectedData.address, authorizedUser: wallet.address, }); @@ -48,11 +49,11 @@ describe('web3telegram.fetchMyContacts()', () => { }); expect( foundContactForASpecificRequester && - foundContactForASpecificRequester.address + foundContactForASpecificRequester.address ).toBeDefined(); expect( foundContactForASpecificRequester && - foundContactForASpecificRequester.address + foundContactForASpecificRequester.address ).toBe(protectedData.address.toLocaleLowerCase()); }, MAX_EXPECTED_WEB2_SERVICES_TIME @@ -63,7 +64,7 @@ describe('web3telegram.fetchMyContacts()', () => { async () => { const grantedAccessForAnyRequester = await dataProtector.core.grantAccess( { - authorizedApp: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress, + authorizedApp: defaultConfig.dappAddress, protectedData: protectedData.address, authorizedUser: NULL_ADDRESS, } @@ -103,7 +104,7 @@ describe('web3telegram.fetchMyContacts()', () => { const encryptionKey = await iexec.dataset.generateEncryptionKey(); await iexec.dataset.pushDatasetSecret(dataset.address, encryptionKey); await dataProtector.core.grantAccess({ - authorizedApp: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress, + authorizedApp: defaultConfig.dappAddress, protectedData: dataset.address, authorizedUser: wallet.address, }); @@ -125,7 +126,7 @@ describe('web3telegram.fetchMyContacts()', () => { await waitSubgraphIndexing(); await dataProtector.core.grantAccess({ - authorizedApp: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress, + authorizedApp: defaultConfig.dappAddress, protectedData: notValidProtectedData.address, authorizedUser: wallet.address, }); diff --git a/tests/e2e/fetchUserContacts.test.ts b/tests/e2e/fetchUserContacts.test.ts index 447e466..dd95d92 100644 --- a/tests/e2e/fetchUserContacts.test.ts +++ b/tests/e2e/fetchUserContacts.test.ts @@ -4,7 +4,10 @@ import { } from '@iexec/dataprotector'; import { beforeAll, describe, expect, it } from '@jest/globals'; import { HDNodeWallet, Wallet } from 'ethers'; -import { CHAIN_CONFIG, CHAIN_IDS } from '../../src/config/config.js'; +import { + DEFAULT_CHAIN_ID, + getChainDefaultConfig, +} from '../../src/config/config.js'; import { IExecWeb3telegram, WorkflowError } from '../../src/index.js'; import { MAX_EXPECTED_BLOCKTIME, @@ -44,14 +47,15 @@ describe('web3telegram.fetchMyContacts()', () => { async () => { const user1 = Wallet.createRandom().address; const user2 = Wallet.createRandom().address; + const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID); await dataProtector.grantAccess({ - authorizedApp: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress, + authorizedApp: defaultConfig.dappAddress, protectedData: protectedData1.address, authorizedUser: user1, }); await dataProtector.grantAccess({ - authorizedApp: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress, + authorizedApp: defaultConfig.dappAddress, protectedData: protectedData2.address, authorizedUser: user2, }); @@ -71,8 +75,9 @@ describe('web3telegram.fetchMyContacts()', () => { 'Test that the protected data can be accessed by authorized user', async () => { const userWithAccess = Wallet.createRandom().address; + const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID); await dataProtector.grantAccess({ - authorizedApp: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress, + authorizedApp: defaultConfig.dappAddress, protectedData: protectedData1.address, authorizedUser: userWithAccess, }); diff --git a/tests/e2e/sendTelegram.test.ts b/tests/e2e/sendTelegram.test.ts index 7008b93..61e2d61 100644 --- a/tests/e2e/sendTelegram.test.ts +++ b/tests/e2e/sendTelegram.test.ts @@ -4,7 +4,10 @@ import { } from '@iexec/dataprotector'; import { beforeAll, describe, expect, it } from '@jest/globals'; import { HDNodeWallet } from 'ethers'; -import { CHAIN_CONFIG, CHAIN_IDS } from '../../src/config/config.js'; +import { + DEFAULT_CHAIN_ID, + getChainDefaultConfig, +} from '../../src/config/config.js'; import { IExecWeb3telegram, WorkflowError } from '../../src/index.js'; import { MAX_EXPECTED_BLOCKTIME, @@ -38,6 +41,7 @@ describe('web3telegram.sendTelegram()', () => { let learnProdWorkerpoolAddress: string; const iexecOptions = getTestIExecOption(); const prodWorkerpoolPublicPrice = 1000; + const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID); beforeAll(async () => { // (default) prod workerpool (not free) always available @@ -64,7 +68,7 @@ describe('web3telegram.sendTelegram()', () => { const resourceProvider = new IExec({ ethProvider }, iexecOptions); await createAndPublishAppOrders( resourceProvider, - CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress + defaultConfig!.dappAddress ); learnProdWorkerpoolAddress = await resourceProvider.ens.resolveName( @@ -101,7 +105,7 @@ describe('web3telegram.sendTelegram()', () => { iexecOptions ); await dataProtector.grantAccess({ - authorizedApp: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress, + authorizedApp: defaultConfig.dappAddress, protectedData: validProtectedData.address, authorizedUser: consumerWallet.address, // consumer wallet numberOfAccess: 1000, @@ -291,10 +295,9 @@ describe('web3telegram.sendTelegram()', () => { name: 'test do not use', }); await waitSubgraphIndexing(); - //grant access to whitelist await dataProtector.grantAccess({ - authorizedApp: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].whitelistSmartContract, //whitelist address + authorizedApp: defaultConfig.whitelistSmartContract, //whitelist address protectedData: protectedDataForWhitelist.address, authorizedUser: consumerWallet.address, // consumer wallet numberOfAccess: 1000, @@ -459,8 +462,8 @@ describe('web3telegram.sendTelegram()', () => { expect(sendTelegramResponse.taskId).toBeDefined(); }, 2 * MAX_EXPECTED_BLOCKTIME + - MAX_EXPECTED_WEB2_SERVICES_TIME + - MAX_EXPECTED_SUBGRAPH_INDEXING_TIME + MAX_EXPECTED_WEB2_SERVICES_TIME + + MAX_EXPECTED_SUBGRAPH_INDEXING_TIME ); }); @@ -516,8 +519,8 @@ describe('web3telegram.sendTelegram()', () => { ); }, 2 * MAX_EXPECTED_BLOCKTIME + - MAX_EXPECTED_WEB2_SERVICES_TIME + - MAX_EXPECTED_SUBGRAPH_INDEXING_TIME + MAX_EXPECTED_WEB2_SERVICES_TIME + + MAX_EXPECTED_SUBGRAPH_INDEXING_TIME ); it( 'should create task if user approves the non sponsored amount', @@ -567,8 +570,8 @@ describe('web3telegram.sendTelegram()', () => { expect(sendTelegramResponse.taskId).toBeDefined(); }, 2 * MAX_EXPECTED_BLOCKTIME + - MAX_EXPECTED_WEB2_SERVICES_TIME + - MAX_EXPECTED_SUBGRAPH_INDEXING_TIME + MAX_EXPECTED_WEB2_SERVICES_TIME + + MAX_EXPECTED_SUBGRAPH_INDEXING_TIME ); }); describe('and workerpoolMaxPrice does NOT covers the non sponsored amount', () => { @@ -620,8 +623,8 @@ describe('web3telegram.sendTelegram()', () => { ); }, 2 * MAX_EXPECTED_BLOCKTIME + - MAX_EXPECTED_WEB2_SERVICES_TIME + - MAX_EXPECTED_SUBGRAPH_INDEXING_TIME + MAX_EXPECTED_WEB2_SERVICES_TIME + + MAX_EXPECTED_SUBGRAPH_INDEXING_TIME ); }); }); diff --git a/tests/unit/fetchMyContacts.test.ts b/tests/unit/fetchMyContacts.test.ts index 476a1b7..34e9b44 100644 --- a/tests/unit/fetchMyContacts.test.ts +++ b/tests/unit/fetchMyContacts.test.ts @@ -1,6 +1,9 @@ import { describe, expect, it, jest } from '@jest/globals'; import { Address } from 'iexec'; -import { CHAIN_CONFIG, CHAIN_IDS } from '../../src/config/config.js'; +import { + DEFAULT_CHAIN_ID, + getChainDefaultConfig, +} from '../../src/config/config.js'; import { type FetchMyContacts } from '../../src/web3telegram/fetchMyContacts.js'; import { getRandomAddress } from '../test-utils.js'; @@ -11,6 +14,7 @@ jest.unstable_mockModule('../../src/utils/subgraphQuery.js', () => ({ describe('fetchMyContacts', () => { let testedModule: any; let fetchMyContacts: FetchMyContacts; + const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID); beforeAll(async () => { // import tested module after all mocked modules @@ -75,15 +79,15 @@ describe('fetchMyContacts', () => { iexec: iexec, // @ts-expect-error No need for graphQLClient here graphQLClient: {}, - dappAddressOrENS: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress, - dappWhitelistAddress: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].whitelistSmartContract, + dappAddressOrENS: defaultConfig.dappAddress, + dappWhitelistAddress: defaultConfig.whitelistSmartContract, }); const userAddress = (await iexec.wallet.getAddress()).toLowerCase(); expect(iexec.orderbook.fetchDatasetOrderbook).toHaveBeenNthCalledWith( 1, 'any', { - app: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress.toLowerCase(), + app: defaultConfig.dappAddress.toLowerCase(), requester: userAddress, isAppStrict: true, isRequesterStrict: false, @@ -94,7 +98,7 @@ describe('fetchMyContacts', () => { 2, 'any', { - app: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].whitelistSmartContract.toLowerCase(), + app: defaultConfig.whitelistSmartContract.toLowerCase(), requester: userAddress, isAppStrict: true, isRequesterStrict: false, @@ -140,8 +144,8 @@ describe('fetchMyContacts', () => { iexec: iexec, // @ts-expect-error No need for graphQLClient here graphQLClient: {}, - dappAddressOrENS: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress, - dappWhitelistAddress: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].whitelistSmartContract, + dappAddressOrENS: defaultConfig.dappAddress, + dappWhitelistAddress: defaultConfig.whitelistSmartContract, isUserStrict: true, }); const userAddress = (await iexec.wallet.getAddress()).toLowerCase(); @@ -149,7 +153,7 @@ describe('fetchMyContacts', () => { 1, 'any', { - app: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress.toLowerCase(), + app: defaultConfig.dappAddress.toLowerCase(), requester: userAddress, isAppStrict: true, isRequesterStrict: true, @@ -160,7 +164,7 @@ describe('fetchMyContacts', () => { 2, 'any', { - app: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].whitelistSmartContract.toLowerCase(), + app: defaultConfig.whitelistSmartContract.toLowerCase(), requester: userAddress, isAppStrict: true, isRequesterStrict: true, diff --git a/tests/unit/sendTelegram.test.ts b/tests/unit/sendTelegram.test.ts index 28029a4..9d58b33 100644 --- a/tests/unit/sendTelegram.test.ts +++ b/tests/unit/sendTelegram.test.ts @@ -1,7 +1,10 @@ import { expect, it, jest } from '@jest/globals'; import { type SendTelegram } from '../../src/web3telegram/sendTelegram.js'; import { getRandomAddress } from '../test-utils.js'; -import { CHAIN_CONFIG, CHAIN_IDS } from '../../src/config/config.js'; +import { + DEFAULT_CHAIN_ID, + getChainDefaultConfig, +} from '../../src/config/config.js'; import { mockAllForSendTelegram } from '../utils/mockAllForSendTelegram.js'; jest.unstable_mockModule('../../src/utils/subgraphQuery.js', () => ({ @@ -9,13 +12,16 @@ jest.unstable_mockModule('../../src/utils/subgraphQuery.js', () => ({ })); jest.unstable_mockModule('../../src/utils/ipfs-service.js', () => ({ - add: jest.fn(() => Promise.resolve('QmSBoN71925mWJ6acehqDLQrrxihxX55EXrqHxpYja4HCG')), + add: jest.fn(() => + Promise.resolve('QmSBoN71925mWJ6acehqDLQrrxihxX55EXrqHxpYja4HCG') + ), get: jest.fn(() => Promise.resolve(new ArrayBuffer(8))), })); describe('sendTelegram', () => { let testedModule: any; let sendTelegram: SendTelegram; + const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID); beforeAll(async () => { // import tested module after all mocked modules @@ -37,11 +43,11 @@ describe('sendTelegram', () => { sendTelegram({ graphQLClient: { request: jest.fn() } as any, iexec: mockAllForSendTelegram() as any, - workerpoolAddressOrEns: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].prodWorkerpoolAddress, - dappAddressOrENS: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress, - dappWhitelistAddress: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].whitelistSmartContract, - ipfsNode: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].ipfsUploadUrl, - ipfsGateway: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].ipfsGateway, + workerpoolAddressOrEns: defaultConfig.prodWorkerpoolAddress, + dappAddressOrENS: defaultConfig.dappAddress, + dappWhitelistAddress: defaultConfig.whitelistSmartContract, + ipfsNode: defaultConfig.ipfsUploadUrl, + ipfsGateway: defaultConfig.ipfsGateway, senderName: 'AB', // Trop court, déclenche l'erreur Yup ...sendTelegramParams, }) @@ -70,11 +76,11 @@ describe('sendTelegram', () => { sendTelegram({ graphQLClient: { request: jest.fn() } as any, iexec: mockAllForSendTelegram() as any, - workerpoolAddressOrEns: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].prodWorkerpoolAddress, - dappAddressOrENS: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress, - dappWhitelistAddress: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].whitelistSmartContract, - ipfsNode: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].ipfsUploadUrl, - ipfsGateway: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].ipfsGateway, + workerpoolAddressOrEns: defaultConfig.prodWorkerpoolAddress, + dappAddressOrENS: defaultConfig.dappAddress, + dappWhitelistAddress: defaultConfig.whitelistSmartContract, + ipfsNode: defaultConfig.ipfsUploadUrl, + ipfsGateway: defaultConfig.ipfsGateway, senderName: 'AB', // Trop court, déclenche l'erreur Yup ...sendTelegramParams, }) @@ -103,11 +109,11 @@ describe('sendTelegram', () => { sendTelegram({ graphQLClient: { request: jest.fn() } as any, iexec: mockAllForSendTelegram() as any, - workerpoolAddressOrEns: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].prodWorkerpoolAddress, - dappAddressOrENS: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress, - dappWhitelistAddress: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].whitelistSmartContract, - ipfsNode: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].ipfsUploadUrl, - ipfsGateway: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].ipfsGateway, + workerpoolAddressOrEns: defaultConfig.prodWorkerpoolAddress, + dappAddressOrENS: defaultConfig.dappAddress, + dappWhitelistAddress: defaultConfig.whitelistSmartContract, + ipfsNode: defaultConfig.ipfsUploadUrl, + ipfsGateway: defaultConfig.ipfsGateway, ...sendTelegramParams, }) ).rejects.toMatchObject({ @@ -135,11 +141,11 @@ describe('sendTelegram', () => { sendTelegram({ graphQLClient: { request: jest.fn() } as any, iexec: mockAllForSendTelegram() as any, - workerpoolAddressOrEns: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].prodWorkerpoolAddress, - dappAddressOrENS: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress, - dappWhitelistAddress: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].whitelistSmartContract, - ipfsNode: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].ipfsUploadUrl, - ipfsGateway: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].ipfsGateway, + workerpoolAddressOrEns: defaultConfig.prodWorkerpoolAddress, + dappAddressOrENS: defaultConfig.dappAddress, + dappWhitelistAddress: defaultConfig.whitelistSmartContract, + ipfsNode: defaultConfig.ipfsUploadUrl, + ipfsGateway: defaultConfig.ipfsGateway, ...sendTelegramParams, }) ).rejects.toMatchObject({ @@ -169,11 +175,11 @@ describe('sendTelegram', () => { sendTelegram({ graphQLClient: { request: jest.fn() } as any, iexec: mockAllForSendTelegram() as any, - workerpoolAddressOrEns: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].prodWorkerpoolAddress, - dappAddressOrENS: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress, - dappWhitelistAddress: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].whitelistSmartContract, - ipfsNode: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].ipfsUploadUrl, - ipfsGateway: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].ipfsGateway, + workerpoolAddressOrEns: defaultConfig.prodWorkerpoolAddress, + dappAddressOrENS: defaultConfig.dappAddress, + dappWhitelistAddress: defaultConfig.whitelistSmartContract, + ipfsNode: defaultConfig.ipfsUploadUrl, + ipfsGateway: defaultConfig.ipfsGateway, ...sendTelegramParams, }) ).rejects.toMatchObject({ @@ -208,11 +214,11 @@ describe('sendTelegram', () => { await sendTelegram({ graphQLClient: { request: jest.fn() } as any, iexec, - workerpoolAddressOrEns: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].prodWorkerpoolAddress, - dappAddressOrENS: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress, - dappWhitelistAddress: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].whitelistSmartContract, - ipfsNode: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].ipfsUploadUrl, - ipfsGateway: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].ipfsGateway, + workerpoolAddressOrEns: defaultConfig.prodWorkerpoolAddress, + dappAddressOrENS: defaultConfig.dappAddress, + dappWhitelistAddress: defaultConfig.whitelistSmartContract, + ipfsNode: defaultConfig.ipfsUploadUrl, + ipfsGateway: defaultConfig.ipfsGateway, telegramContent: 'e2e telegram content for test', protectedData, }); @@ -222,8 +228,8 @@ describe('sendTelegram', () => { expect(iexec.orderbook.fetchWorkerpoolOrderbook).toHaveBeenNthCalledWith( 1, { - workerpool: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].prodWorkerpoolAddress, - app: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].dappAddress.toLowerCase(), + workerpool: defaultConfig.prodWorkerpoolAddress, + app: defaultConfig.dappAddress.toLowerCase(), dataset: protectedData, requester: userAddress, isRequesterStrict: false, @@ -235,8 +241,8 @@ describe('sendTelegram', () => { expect(iexec.orderbook.fetchWorkerpoolOrderbook).toHaveBeenNthCalledWith( 2, { - workerpool: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].prodWorkerpoolAddress, - app: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].whitelistSmartContract.toLowerCase(), + workerpool: defaultConfig.prodWorkerpoolAddress, + app: defaultConfig.whitelistSmartContract.toLowerCase(), dataset: protectedData, requester: userAddress, isRequesterStrict: false, @@ -248,8 +254,8 @@ describe('sendTelegram', () => { expect(iexec.orderbook.fetchWorkerpoolOrderbook).toHaveBeenNthCalledWith( 2, { - workerpool: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].prodWorkerpoolAddress, - app: CHAIN_CONFIG[CHAIN_IDS.BELLECOUR].whitelistSmartContract.toLowerCase(), + workerpool: defaultConfig.prodWorkerpoolAddress, + app: defaultConfig.whitelistSmartContract.toLowerCase(), dataset: protectedData, requester: userAddress, isRequesterStrict: false,