@@ -23,75 +23,108 @@ type EthersCompatibleProvider =
2323 | Web3SignerProvider
2424 | string ;
2525
26+ interface IExecDataProtectorResolvedConfig {
27+ dataprotectorContractAddress : AddressOrENS ;
28+ sharingContractAddress : AddressOrENS ;
29+ graphQLClient : GraphQLClient ;
30+ ipfsNode : string ;
31+ ipfsGateway : string ;
32+ iexec : IExec ;
33+ iexecDebug : IExec ;
34+ }
35+
2636abstract class IExecDataProtectorModule {
27- protected dataprotectorContractAddress : AddressOrENS ;
37+ protected dataprotectorContractAddress ! : AddressOrENS ;
2838
29- protected sharingContractAddress : AddressOrENS ;
39+ protected sharingContractAddress ! : AddressOrENS ;
3040
31- protected graphQLClient : GraphQLClient ;
41+ protected graphQLClient ! : GraphQLClient ;
3242
33- protected ipfsNode : string ;
43+ protected ipfsNode ! : string ;
3444
35- protected ipfsGateway : string ;
45+ protected ipfsGateway ! : string ;
3646
37- protected iexec : IExec ;
47+ protected iexec ! : IExec ;
3848
39- protected iexecDebug : IExec ;
49+ protected iexecDebug ! : IExec ;
4050
41- protected constructor ( ) {
42- // Prevent direct instantiation; use create() instead
43- }
51+ private initPromise : Promise < void > | null = null ;
52+
53+ private ethProvider : EthersCompatibleProvider ;
4454
45- static async create (
55+ private options : DataProtectorConfigOptions ;
56+
57+ constructor (
4658 ethProvider ?: EthersCompatibleProvider ,
4759 options ?: DataProtectorConfigOptions
48- ) : Promise < IExecDataProtectorModule > {
49- const instance = Object . create ( this . prototype ) as IExecDataProtectorModule ;
50- const resolvedEthProvider = ethProvider || 'bellecour' ;
51- const chainId = await getChainIdFromProvider ( resolvedEthProvider ) ;
60+ ) {
61+ this . ethProvider = ethProvider || 'bellecour' ;
62+ this . options = options || { } ;
63+ }
64+
65+ protected async init ( ) : Promise < void > {
66+ if ( ! this . initPromise ) {
67+ this . initPromise = this . resolveConfig ( ) . then ( ( config ) => {
68+ this . dataprotectorContractAddress = config . dataprotectorContractAddress ;
69+ this . sharingContractAddress = config . sharingContractAddress ;
70+ this . graphQLClient = config . graphQLClient ;
71+ this . ipfsNode = config . ipfsNode ;
72+ this . ipfsGateway = config . ipfsGateway ;
73+ this . iexec = config . iexec ;
74+ this . iexecDebug = config . iexecDebug ;
75+ } ) ;
76+ }
77+ return this . initPromise ;
78+ }
79+
80+ private async resolveConfig ( ) : Promise < IExecDataProtectorResolvedConfig > {
81+ const chainId = await getChainIdFromProvider ( this . ethProvider ) ;
5282 const config = CHAIN_CONFIG [ chainId ] || CHAIN_CONFIG [ DEFAULT_CHAIN_ID ] ;
5383
84+ let iexec : IExec , iexecDebug : IExec , graphQLClient : GraphQLClient ;
85+
5486 try {
55- instance . iexec = new IExec (
56- { ethProvider : resolvedEthProvider } ,
87+ iexec = new IExec (
88+ { ethProvider : this . ethProvider } ,
5789 {
5890 ipfsGatewayURL : config . ipfsGateway ,
59- ...options ?. iexecOptions ,
91+ ...this . options ?. iexecOptions ,
6092 }
6193 ) ;
6294
63- instance . iexecDebug = new IExec (
64- { ethProvider : resolvedEthProvider } ,
95+ iexecDebug = new IExec (
96+ { ethProvider : this . ethProvider } ,
6597 {
6698 ipfsGatewayURL : config . ipfsGateway ,
67- ...options ?. iexecOptions ,
68- smsURL : options ?. iexecOptions ?. smsDebugURL || config . smsDebugURL ,
99+ ...this . options ?. iexecOptions ,
100+ smsURL : this . options ?. iexecOptions ?. smsDebugURL || config . smsDebugURL ,
69101 }
70102 ) ;
71103 } catch ( e : any ) {
72104 throw new Error ( `Unsupported ethProvider: ${ e . message } ` ) ;
73105 }
74106
75107 try {
76- instance . graphQLClient = new GraphQLClient (
77- options ?. subgraphUrl || config . subgraphUrl
108+ graphQLClient = new GraphQLClient (
109+ this . options ?. subgraphUrl || config . subgraphUrl
78110 ) ;
79111 } catch ( error : any ) {
80112 throw new Error ( `Failed to create GraphQLClient: ${ error . message } ` ) ;
81113 }
82114
83- instance . dataprotectorContractAddress =
84- options ?. dataprotectorContractAddress ?. toLowerCase ( ) ||
85- config . dataprotectorContractAddress ;
86-
87- instance . sharingContractAddress =
88- options ?. sharingContractAddress ?. toLowerCase ( ) ||
89- config . sharingContractAddress ;
90-
91- instance . ipfsNode = options ?. ipfsNode || config . ipfsNode ;
92- instance . ipfsGateway = config . ipfsGateway ;
93-
94- return instance ;
115+ return {
116+ dataprotectorContractAddress :
117+ this . options ?. dataprotectorContractAddress ?. toLowerCase ( ) ||
118+ config . dataprotectorContractAddress ,
119+ sharingContractAddress :
120+ this . options ?. sharingContractAddress ?. toLowerCase ( ) ||
121+ config . sharingContractAddress ,
122+ graphQLClient,
123+ ipfsNode : this . options ?. ipfsNode || config . ipfsNode ,
124+ ipfsGateway : config . ipfsGateway ,
125+ iexec,
126+ iexecDebug,
127+ } ;
95128 }
96129}
97130
0 commit comments