1- import { AbstractProvider , AbstractSigner , Eip1193Provider } from 'ethers' ;
1+ import {
2+ AbstractProvider ,
3+ AbstractSigner ,
4+ Eip1193Provider ,
5+ } from 'ethers' ;
26import { GraphQLClient } from 'graphql-request' ;
37import { IExec } from 'iexec' ;
48import {
5- DEFAULT_CONTRACT_ADDRESS ,
6- DEFAULT_DEBUG_SMS_URL ,
7- DEFAULT_IEXEC_IPFS_NODE ,
8- DEFAULT_IPFS_GATEWAY ,
9- DEFAULT_SHARING_CONTRACT_ADDRESS ,
10- DEFAULT_SUBGRAPH_URL ,
9+ CHAIN_CONFIG ,
10+ DEFAULT_CHAIN_ID ,
1111} from '../config/config.js' ;
1212import {
1313 AddressOrENS ,
1414 DataProtectorConfigOptions ,
1515 Web3SignerProvider ,
1616} from './types/index.js' ;
17+ import { getChainIdFromProvider } from '../utils/getChainId.js' ;
18+
19+ type EthersCompatibleProvider =
20+ | AbstractProvider
21+ | AbstractSigner
22+ | Eip1193Provider
23+ | Web3SignerProvider
24+ | string ;
1725
1826abstract class IExecDataProtectorModule {
1927 protected dataprotectorContractAddress : AddressOrENS ;
@@ -30,51 +38,60 @@ abstract class IExecDataProtectorModule {
3038
3139 protected iexecDebug : IExec ;
3240
33- constructor (
34- ethProvider ?:
35- | AbstractProvider
36- | AbstractSigner
37- | Eip1193Provider
38- | Web3SignerProvider
39- | string ,
41+ protected constructor ( ) {
42+ // Prevent direct instantiation; use create() instead
43+ }
44+
45+ static async create (
46+ ethProvider ?: EthersCompatibleProvider ,
4047 options ?: DataProtectorConfigOptions
41- ) {
42- const ipfsGateway = options ?. ipfsGateway || DEFAULT_IPFS_GATEWAY ;
48+ ) : Promise < IExecDataProtectorModule > {
49+ const instance = Object . create ( this . prototype ) as IExecDataProtectorModule ;
50+ const resolvedEthProvider = ethProvider || 'bellecour' ;
51+ const chainId = await getChainIdFromProvider ( resolvedEthProvider ) ;
52+ const config = CHAIN_CONFIG [ chainId ] || CHAIN_CONFIG [ DEFAULT_CHAIN_ID ] ;
4353
4454 try {
45- this . iexec = new IExec (
46- { ethProvider : ethProvider || 'bellecour' } ,
55+ instance . iexec = new IExec (
56+ { ethProvider : resolvedEthProvider } ,
4757 {
48- ipfsGatewayURL : ipfsGateway ,
58+ ipfsGatewayURL : config . ipfsGateway ,
4959 ...options ?. iexecOptions ,
5060 }
5161 ) ;
52- this . iexecDebug = new IExec (
53- { ethProvider : ethProvider || 'bellecour' } ,
62+
63+ instance . iexecDebug = new IExec (
64+ { ethProvider : resolvedEthProvider } ,
5465 {
55- ipfsGatewayURL : ipfsGateway ,
66+ ipfsGatewayURL : config . ipfsGateway ,
5667 ...options ?. iexecOptions ,
57- smsURL : options ?. iexecOptions ?. smsDebugURL || DEFAULT_DEBUG_SMS_URL ,
68+ smsURL : options ?. iexecOptions ?. smsDebugURL || config . smsDebugURL ,
5869 }
5970 ) ;
60- } catch ( e ) {
61- throw new Error ( `Unsupported ethProvider, ${ e . message } ` ) ;
71+ } catch ( e : any ) {
72+ throw new Error ( `Unsupported ethProvider: ${ e . message } ` ) ;
6273 }
74+
6375 try {
64- this . graphQLClient = new GraphQLClient (
65- options ?. subgraphUrl || DEFAULT_SUBGRAPH_URL
76+ instance . graphQLClient = new GraphQLClient (
77+ options ?. subgraphUrl || config . subgraphUrl
6678 ) ;
67- } catch ( error ) {
79+ } catch ( error : any ) {
6880 throw new Error ( `Failed to create GraphQLClient: ${ error . message } ` ) ;
6981 }
70- this . dataprotectorContractAddress =
82+
83+ instance . dataprotectorContractAddress =
7184 options ?. dataprotectorContractAddress ?. toLowerCase ( ) ||
72- DEFAULT_CONTRACT_ADDRESS ;
73- this . sharingContractAddress =
85+ config . dataprotectorContractAddress ;
86+
87+ instance . sharingContractAddress =
7488 options ?. sharingContractAddress ?. toLowerCase ( ) ||
75- DEFAULT_SHARING_CONTRACT_ADDRESS ;
76- this . ipfsNode = options ?. ipfsNode || DEFAULT_IEXEC_IPFS_NODE ;
77- this . ipfsGateway = ipfsGateway ;
89+ config . sharingContractAddress ;
90+
91+ instance . ipfsNode = options ?. ipfsNode || config . ipfsNode ;
92+ instance . ipfsGateway = config . ipfsGateway ;
93+
94+ return instance ;
7895 }
7996}
8097
0 commit comments