|
1 | 1 | import { AbstractProvider, AbstractSigner, Eip1193Provider } from 'ethers'; |
2 | 2 | import { GraphQLClient } from 'graphql-request'; |
3 | 3 | import { IExec } from 'iexec'; |
4 | | -import { |
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, |
11 | | -} from '../config/config.js'; |
| 4 | +import { CHAIN_CONFIG } from '../config/config.js'; |
| 5 | +import { getChainIdFromProvider } from '../utils/getChainId.js'; |
12 | 6 | import { |
13 | 7 | AddressOrENS, |
14 | 8 | DataProtectorConfigOptions, |
15 | 9 | Web3SignerProvider, |
16 | 10 | } from './types/index.js'; |
17 | 11 |
|
| 12 | +type EthersCompatibleProvider = |
| 13 | + | AbstractProvider |
| 14 | + | AbstractSigner |
| 15 | + | Eip1193Provider |
| 16 | + | Web3SignerProvider |
| 17 | + | string; |
| 18 | + |
| 19 | +interface IExecDataProtectorResolvedConfig { |
| 20 | + dataprotectorContractAddress: AddressOrENS; |
| 21 | + sharingContractAddress: AddressOrENS; |
| 22 | + graphQLClient: GraphQLClient; |
| 23 | + ipfsNode: string; |
| 24 | + ipfsGateway: string; |
| 25 | + defaultWorkerpool: string; |
| 26 | + iexec: IExec; |
| 27 | + iexecDebug: IExec; |
| 28 | +} |
| 29 | + |
18 | 30 | abstract class IExecDataProtectorModule { |
19 | | - protected dataprotectorContractAddress: AddressOrENS; |
| 31 | + protected dataprotectorContractAddress!: AddressOrENS; |
| 32 | + |
| 33 | + protected sharingContractAddress!: AddressOrENS; |
| 34 | + |
| 35 | + protected graphQLClient!: GraphQLClient; |
20 | 36 |
|
21 | | - protected sharingContractAddress: AddressOrENS; |
| 37 | + protected ipfsNode!: string; |
22 | 38 |
|
23 | | - protected graphQLClient: GraphQLClient; |
| 39 | + protected ipfsGateway!: string; |
24 | 40 |
|
25 | | - protected ipfsNode: string; |
| 41 | + protected defaultWorkerpool!: string; |
26 | 42 |
|
27 | | - protected ipfsGateway: string; |
| 43 | + protected iexec!: IExec; |
28 | 44 |
|
29 | | - protected iexec: IExec; |
| 45 | + protected iexecDebug!: IExec; |
30 | 46 |
|
31 | | - protected iexecDebug: IExec; |
| 47 | + private initPromise: Promise<void> | null = null; |
| 48 | + |
| 49 | + private ethProvider: EthersCompatibleProvider; |
| 50 | + |
| 51 | + private options: DataProtectorConfigOptions; |
32 | 52 |
|
33 | 53 | constructor( |
34 | | - ethProvider?: |
35 | | - | AbstractProvider |
36 | | - | AbstractSigner |
37 | | - | Eip1193Provider |
38 | | - | Web3SignerProvider |
39 | | - | string, |
| 54 | + ethProvider?: EthersCompatibleProvider, |
40 | 55 | options?: DataProtectorConfigOptions |
41 | 56 | ) { |
42 | | - const ipfsGateway = options?.ipfsGateway || DEFAULT_IPFS_GATEWAY; |
| 57 | + this.ethProvider = ethProvider || 'bellecour'; |
| 58 | + this.options = options || {}; |
| 59 | + } |
| 60 | + |
| 61 | + protected async init(): Promise<void> { |
| 62 | + if (!this.initPromise) { |
| 63 | + this.initPromise = this.resolveConfig().then((config) => { |
| 64 | + this.dataprotectorContractAddress = config.dataprotectorContractAddress; |
| 65 | + this.sharingContractAddress = config.sharingContractAddress; |
| 66 | + this.graphQLClient = config.graphQLClient; |
| 67 | + this.ipfsNode = config.ipfsNode; |
| 68 | + this.ipfsGateway = config.ipfsGateway; |
| 69 | + this.defaultWorkerpool = config.defaultWorkerpool; |
| 70 | + this.iexec = config.iexec; |
| 71 | + this.iexecDebug = config.iexecDebug; |
| 72 | + }); |
| 73 | + } |
| 74 | + return this.initPromise; |
| 75 | + } |
| 76 | + |
| 77 | + private async resolveConfig(): Promise<IExecDataProtectorResolvedConfig> { |
| 78 | + const chainId = await getChainIdFromProvider(this.ethProvider); |
| 79 | + const chainDefaultConfig = CHAIN_CONFIG[chainId]; |
| 80 | + |
| 81 | + const subgraphUrl = |
| 82 | + this.options?.subgraphUrl || chainDefaultConfig?.subgraphUrl; |
| 83 | + const dataprotectorContractAddress = |
| 84 | + this.options?.dataprotectorContractAddress || |
| 85 | + chainDefaultConfig?.dataprotectorContractAddress; |
| 86 | + const sharingContractAddress = |
| 87 | + this.options?.sharingContractAddress || |
| 88 | + chainDefaultConfig?.sharingContractAddress; |
| 89 | + const ipfsGateway = |
| 90 | + this.options?.ipfsGateway || chainDefaultConfig?.ipfsGateway; |
| 91 | + const defaultWorkerpool = chainDefaultConfig?.workerpoolAddress; |
| 92 | + const ipfsNode = this.options?.ipfsNode || chainDefaultConfig?.ipfsNode; |
| 93 | + const smsURL = |
| 94 | + this.options?.iexecOptions?.smsDebugURL || |
| 95 | + chainDefaultConfig?.smsDebugURL; |
| 96 | + |
| 97 | + const missing = []; |
| 98 | + if (!subgraphUrl) missing.push('subgraphUrl'); |
| 99 | + if (!dataprotectorContractAddress) |
| 100 | + missing.push('dataprotectorContractAddress'); |
| 101 | + if (!sharingContractAddress) missing.push('sharingContractAddress'); |
| 102 | + if (!ipfsGateway) missing.push('ipfsGateway'); |
| 103 | + if (!defaultWorkerpool) missing.push('defaultWorkerpool'); |
| 104 | + if (!ipfsNode) missing.push('ipfsNode'); |
| 105 | + if (!smsURL) missing.push('smsDebugURL'); |
| 106 | + |
| 107 | + if (missing.length > 0) { |
| 108 | + throw new Error( |
| 109 | + `Missing required configuration for chainId ${chainId}: ${missing.join( |
| 110 | + ', ' |
| 111 | + )}` |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + let iexec: IExec, iexecDebug: IExec, graphQLClient: GraphQLClient; |
43 | 116 |
|
44 | 117 | try { |
45 | | - this.iexec = new IExec( |
46 | | - { ethProvider: ethProvider || 'bellecour' }, |
| 118 | + iexec = new IExec( |
| 119 | + { ethProvider: this.ethProvider }, |
47 | 120 | { |
48 | 121 | ipfsGatewayURL: ipfsGateway, |
49 | | - ...options?.iexecOptions, |
| 122 | + ...this.options?.iexecOptions, |
50 | 123 | } |
51 | 124 | ); |
52 | | - this.iexecDebug = new IExec( |
53 | | - { ethProvider: ethProvider || 'bellecour' }, |
| 125 | + |
| 126 | + iexecDebug = new IExec( |
| 127 | + { ethProvider: this.ethProvider }, |
54 | 128 | { |
55 | 129 | ipfsGatewayURL: ipfsGateway, |
56 | | - ...options?.iexecOptions, |
57 | | - smsURL: options?.iexecOptions?.smsDebugURL || DEFAULT_DEBUG_SMS_URL, |
| 130 | + ...this.options?.iexecOptions, |
| 131 | + smsURL, |
58 | 132 | } |
59 | 133 | ); |
60 | | - } catch (e) { |
61 | | - throw new Error(`Unsupported ethProvider, ${e.message}`); |
| 134 | + } catch (e: any) { |
| 135 | + throw new Error(`Unsupported ethProvider: ${e.message}`); |
62 | 136 | } |
| 137 | + |
63 | 138 | try { |
64 | | - this.graphQLClient = new GraphQLClient( |
65 | | - options?.subgraphUrl || DEFAULT_SUBGRAPH_URL |
66 | | - ); |
67 | | - } catch (error) { |
| 139 | + graphQLClient = new GraphQLClient(subgraphUrl); |
| 140 | + } catch (error: any) { |
68 | 141 | throw new Error(`Failed to create GraphQLClient: ${error.message}`); |
69 | 142 | } |
70 | | - this.dataprotectorContractAddress = |
71 | | - options?.dataprotectorContractAddress?.toLowerCase() || |
72 | | - DEFAULT_CONTRACT_ADDRESS; |
73 | | - this.sharingContractAddress = |
74 | | - options?.sharingContractAddress?.toLowerCase() || |
75 | | - DEFAULT_SHARING_CONTRACT_ADDRESS; |
76 | | - this.ipfsNode = options?.ipfsNode || DEFAULT_IEXEC_IPFS_NODE; |
77 | | - this.ipfsGateway = ipfsGateway; |
| 143 | + |
| 144 | + return { |
| 145 | + dataprotectorContractAddress: dataprotectorContractAddress.toLowerCase(), |
| 146 | + sharingContractAddress: sharingContractAddress.toLowerCase(), |
| 147 | + defaultWorkerpool, |
| 148 | + graphQLClient, |
| 149 | + ipfsNode, |
| 150 | + ipfsGateway, |
| 151 | + iexec, |
| 152 | + iexecDebug, |
| 153 | + }; |
78 | 154 | } |
79 | 155 | } |
80 | 156 |
|
|
0 commit comments