Skip to content

Commit 098bd47

Browse files
refactor: move defaults hub address in config
1 parent f4345b3 commit 098bd47

File tree

7 files changed

+41
-51
lines changed

7 files changed

+41
-51
lines changed

docs/classes/internal_.IExecContractsClient.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ Create a client for IExec contracts
4444
| Name | Type | Description |
4545
| :------ | :------ | :------ |
4646
| `args` | `Object` | - |
47-
| `args.chainId` | `string` \| `number` | id of the chain to use (used to resolve IExec contract address) |
47+
| `args.chainId` | `string` \| `number` | id of the chain |
4848
| `args.confirms?` | `number` | number of block to wait for transactions confirmation (default 1) |
49-
| `args.hubAddress?` | `string` | override the IExec contract address to target a custom instance |
49+
| `args.hubAddress` | `string` | IExec contract address |
5050
| `args.isNative?` | `boolean` | true if IExec contract use the chain native token |
5151
| `args.provider` | `Provider` | ethers Provider |
5252
| `args.signer?` | `Signer` | ethers Signer, required to sign transactions and messages |

src/common/utils/IExecContractsClient.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ export default class IExecContractsClient {
1414
*/
1515
signer?: Signer;
1616
/**
17-
* id of the chain to use (used to resolve IExec contract address)
17+
* id of the chain
1818
*/
1919
chainId: number | string;
2020
/**
21-
* override the IExec contract address to target a custom instance
21+
* IExec contract address
2222
*/
23-
hubAddress?: string;
23+
hubAddress: string;
2424
/**
2525
* if false set the gasPrice to 0 (default true)
2626
*/

src/common/utils/IExecContractsClient.js

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Debug from 'debug';
22
import { Contract } from 'ethers';
33
import { version as pocoVersion } from '../generated/@iexec/poco/package.js';
4-
import { networks as iexecProxyNetworks } from '../generated/@iexec/poco/ERC1538Proxy.js';
54
import iexecTokenDesc from '../generated/@iexec/poco/IexecInterfaceToken.js';
65
import iexecNativeDesc from '../generated/@iexec/poco/IexecInterfaceNative.js';
76
import appRegistryDesc from '../generated/@iexec/poco/AppRegistry.js';
@@ -20,17 +19,6 @@ const gasPriceByNetwork = {
2019
134: 0n,
2120
};
2221

23-
const getHubAddress = (chainId) => {
24-
if (
25-
iexecProxyNetworks &&
26-
iexecProxyNetworks[chainId] &&
27-
iexecProxyNetworks[chainId].address
28-
) {
29-
return iexecProxyNetworks[chainId].address;
30-
}
31-
throw Error(`Missing iExec contract default address for chain ${chainId}`);
32-
};
33-
3422
const getIsNative = (chainId) => nativeNetworks.includes(chainId);
3523

3624
const getGasPriceOverride = (chainId) => gasPriceByNetwork[chainId];
@@ -76,19 +64,12 @@ const getContractsDescMap = (isNative) => ({
7664
},
7765
});
7866

79-
const createClient = ({
80-
ethSigner,
81-
ethProvider,
82-
chainId,
83-
globalHubAddress,
84-
isNative,
85-
}) => {
67+
const createClient = ({ ethSigner, ethProvider, hubAddress, isNative }) => {
8668
const cachedAddresses = {};
69+
if (!hubAddress) throw Error('Missing iExec contract address');
8770

8871
const contractsDescMap = getContractsDescMap(isNative);
8972

90-
const hubAddress = globalHubAddress || getHubAddress(chainId);
91-
9273
const getContract = (objName, address) => {
9374
try {
9475
const { contractDesc } = contractsDescMap[objName];
@@ -184,6 +165,7 @@ class IExecContractsClient {
184165
} = {}) {
185166
const stringChainId = `${chainId}`;
186167
if (!provider) throw Error('missing provider key');
168+
if (!hubAddress) throw Error('missing hubAddress key');
187169
if (!stringChainId) throw Error('missing chainId key');
188170
if (!Number.isInteger(confirms) || confirms <= 0)
189171
throw Error('invalid confirms');
@@ -206,8 +188,7 @@ class IExecContractsClient {
206188
const client = createClient({
207189
ethSigner: signer,
208190
ethProvider: provider,
209-
chainId: stringChainId,
210-
globalHubAddress: hubAddress,
191+
hubAddress,
211192
isNative: native,
212193
});
213194

src/common/utils/config.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { Network, EnsPlugin } from 'ethers';
22
import { TEE_FRAMEWORKS } from './constant.js';
33
import { address as voucherHubBellecourAddress } from '../generated/@iexec/voucher-contracts/deployments/bellecour/VoucherHubERC1967Proxy.js';
4+
import { networks as iexecProxyNetworks } from '../generated/@iexec/poco/ERC1538Proxy.js';
45

56
const networkConfigs = [
67
{
78
id: 134,
89
name: 'bellecour',
9-
hub: undefined, // use default
10+
hub: iexecProxyNetworks[134].address,
1011
host: 'https://bellecour.iex.ec',
11-
ensRegistry: '0x5f5B93fca68c9C79318d1F3868A354EE67D8c006', // use ethers default '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e'
12+
ensRegistry: '0x5f5B93fca68c9C79318d1F3868A354EE67D8c006',
1213
ensPublicResolver: '0x1347d8a1840A810B990d0B774A6b7Bb8A1bd62BB',
1314
sms: {
1415
[TEE_FRAMEWORKS.SCONE]: 'https://sms.iex.ec',
@@ -31,7 +32,7 @@ const networkConfigs = [
3132
{
3233
id: 1,
3334
name: 'mainnet',
34-
hub: undefined, // use default
35+
hub: iexecProxyNetworks[1].address,
3536
host: 'mainnet',
3637
ensRegistry: undefined, // use ethers default
3738
ensPublicResolver: '0x4976fb03C32e5B8cfe2b6cCB31c09Ba78EBaBa41',

src/lib/IExecConfig.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,18 @@ export default class IExecConfig {
144144
const contractsPromise = (async () => {
145145
const { chainId } = await networkPromise;
146146
const chainConfDefaults = await chainConfDefaultsPromise;
147+
const resolvedHubAddress = hubAddress || chainConfDefaults.hub;
148+
if (!resolvedHubAddress) {
149+
throw new ConfigurationError(
150+
`hubAddress option not set and no default value for your chain ${chainId}`,
151+
);
152+
}
147153
try {
148154
return new IExecContractsClient({
149155
chainId,
150156
provider,
151157
signer,
152-
hubAddress: hubAddress || chainConfDefaults.hub,
158+
hubAddress: resolvedHubAddress,
153159
useGas,
154160
confirms,
155161
isNative,
@@ -177,9 +183,7 @@ export default class IExecConfig {
177183
);
178184
}
179185
const bridgedChainId =
180-
bridgedNetworkConf.chainId !== undefined
181-
? bridgedNetworkConf.chainId
182-
: chainConfDefaults.bridge && chainConfDefaults.bridge.bridgedChainId;
186+
bridgedNetworkConf.chainId ?? chainConfDefaults.bridge?.bridgedChainId;
183187
if (!bridgedChainId) {
184188
throw new ConfigurationError(
185189
`Missing chainId in bridgedNetworkConf and no default value for your chain ${chainId}`,
@@ -189,30 +193,28 @@ export default class IExecConfig {
189193
allowExperimentalNetworks,
190194
});
191195
const bridgedRpcUrl =
192-
bridgedNetworkConf.rpcURL !== undefined
193-
? bridgedNetworkConf.rpcURL
194-
: bridgedChainConfDefaults.host;
196+
bridgedNetworkConf.rpcURL ?? bridgedChainConfDefaults.host;
195197
if (!bridgedRpcUrl) {
196198
throw new ConfigurationError(
197199
`Missing rpcURL in bridgedNetworkConf and no default value for bridged chain ${bridgedChainId}`,
198200
);
199201
}
200202
const bridgedBridgeAddress =
201-
bridgedNetworkConf.bridgeAddress !== undefined
202-
? bridgedNetworkConf.bridgeAddress
203-
: bridgedChainConfDefaults.bridge &&
204-
bridgedChainConfDefaults.bridge.contract;
203+
bridgedNetworkConf.bridgeAddress ??
204+
bridgedChainConfDefaults.bridge?.contract;
205205
if (!bridgedBridgeAddress) {
206206
throw new ConfigurationError(
207207
`Missing bridgeAddress in bridgedNetworkConf and no default value for bridged chain ${bridgedChainId}`,
208208
);
209209
}
210+
const bridgedHubAddress =
211+
bridgedNetworkConf.hubAddress ?? bridgedChainConfDefaults.hub;
210212
const contracts = await contractsPromise;
211213
return {
212214
chainId: bridgedChainId,
213215
rpcURL: bridgedRpcUrl,
214216
isNative: !contracts.isNative,
215-
hubAddress: bridgedNetworkConf.hubAddress,
217+
hubAddress: bridgedHubAddress,
216218
bridgeAddress: bridgedBridgeAddress,
217219
};
218220
})();
@@ -223,16 +225,22 @@ export default class IExecConfig {
223225

224226
const bridgedContractsPromise = (async () => {
225227
const bridgedConf = await bridgedConfPromise;
228+
const { hubAddress, chainId, isNative, rpcURL } = bridgedConf;
229+
if (!hubAddress) {
230+
throw new ConfigurationError(
231+
`Missing hubAddress in bridgedNetworkConf and no default value for bridged chain ${chainId}`,
232+
);
233+
}
226234
try {
227235
return new IExecContractsClient({
228-
chainId: bridgedConf.chainId,
229-
provider: getReadOnlyProvider(bridgedConf.rpcURL, {
236+
chainId,
237+
provider: getReadOnlyProvider(rpcURL, {
230238
providers: providerOptions,
231239
allowExperimentalNetworks,
232240
}),
233-
hubAddress: bridgedConf.hubAddress,
241+
hubAddress,
234242
confirms,
235-
isNative: bridgedConf.isNative,
243+
isNative,
236244
});
237245
} catch (err) {
238246
throw new ConfigurationError(

test/lib/e2e/IExecConfig.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ describe('[IExecConfig]', () => {
433433

434434
await expect(config.resolveContractsClient()).rejects.toThrow(
435435
Error(
436-
'Failed to create contracts client: Missing iExec contract default address for chain 421614',
436+
'hubAddress option not set and no default value for your chain 421614',
437437
),
438438
);
439439
});
@@ -542,7 +542,7 @@ describe('[IExecConfig]', () => {
542542

543543
await expect(config.resolveContractsClient()).rejects.toThrow(
544544
Error(
545-
'Failed to create contracts client: Missing iExec contract default address for chain 421614',
545+
'hubAddress option not set and no default value for your chain 421614',
546546
),
547547
);
548548
});
@@ -800,7 +800,7 @@ describe('[IExecConfig]', () => {
800800
const promise = config.resolveContractsClient();
801801
await expect(promise).rejects.toThrow(
802802
Error(
803-
`Failed to create contracts client: Missing iExec contract default address for chain ${unknownTestChain.chainId}`,
803+
`hubAddress option not set and no default value for your chain ${unknownTestChain.chainId}`,
804804
),
805805
);
806806
await expect(promise).rejects.toThrow(errors.ConfigurationError);

test/lib/unit/config.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('getChainDefaults', () => {
2424
ensPublicResolver: '0x1347d8a1840A810B990d0B774A6b7Bb8A1bd62BB',
2525
ensRegistry: '0x5f5B93fca68c9C79318d1F3868A354EE67D8c006',
2626
host: 'https://bellecour.iex.ec',
27-
hub: undefined,
27+
hub: '0x3eca1B216A7DF1C7689aEb259fFB83ADFB894E7f',
2828
iexecGateway: 'https://api.market.v8-bellecour.iex.ec',
2929
ipfsGateway: 'https://ipfs-gateway.v8-bellecour.iex.ec',
3030
name: 'bellecour',

0 commit comments

Comments
 (0)