From 24099bfb1d03e071b1f9dac3f4f7083c8efbfdc3 Mon Sep 17 00:00:00 2001 From: enrique Date: Wed, 25 Oct 2023 11:30:51 +0200 Subject: [PATCH 1/5] feat: make load dispenser optional --- src/keeper/Keeper.ts | 44 +++++++++++++++++++++++-------- test/keeper/ContractEvent.test.ts | 2 ++ test/nevermined/Accounts.test.ts | 6 ++++- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/keeper/Keeper.ts b/src/keeper/Keeper.ts index 3d666fa5d..bd8080ebe 100644 --- a/src/keeper/Keeper.ts +++ b/src/keeper/Keeper.ts @@ -153,17 +153,17 @@ export class Keeper extends Instantiable { } // Optionals - try { - this.instances.dispenser = await Dispenser.getInstance(this.instantiableConfig) - } catch { - this.logger.debug('Dispenser not available on this network.') - } - - try { - this.instances.token = await Token.getInstance(this.instantiableConfig) - } catch { - this.logger.debug('Token not available on this network.') - } + // try { + // this.instances.dispenser = await Dispenser.getInstance(this.instantiableConfig) + // } catch { + // this.logger.debug('Dispenser not available on this network.') + // } + + // try { + // this.instances.token = await Token.getInstance(this.instantiableConfig) + // } catch { + // this.logger.debug('Token not available on this network.') + // } // Main contracts this.dispenser = this.instances.dispenser @@ -464,6 +464,28 @@ export class Keeper extends Instantiable { } return this.royalties.curve } + + public async loadDispenserInstance() { + if (this.instances.dispenser) return this.instances.dispenser + try { + this.instances.dispenser = await Dispenser.getInstance(this.instantiableConfig) + this.dispenser = await Dispenser.getInstance(this.instantiableConfig) + } catch { + this.logger.debug('Dispenser not available on this network.') + } + return this.instances.dispenser + } + + public async loadTokenInstance() { + if (this.instances.token) return this.instances.token + try { + this.instances.token = await Token.getInstance(this.instantiableConfig) + this.token = await Token.getInstance(this.instantiableConfig) + } catch { + this.logger.debug('Token not available on this network.') + } + return this.instances.token + } } export default Keeper diff --git a/test/keeper/ContractEvent.test.ts b/test/keeper/ContractEvent.test.ts index b87da4333..b7738d475 100644 --- a/test/keeper/ContractEvent.test.ts +++ b/test/keeper/ContractEvent.test.ts @@ -18,6 +18,8 @@ describe('ContractEvent', () => { before(async () => { await TestContractHandler.prepareContracts() nevermined = await Nevermined.getInstance(config) + nevermined.keeper.dispenser = await nevermined.keeper.loadDispenserInstance() + nevermined.keeper.token = await nevermined.keeper.loadTokenInstance() web3 = await Web3Provider.getWeb3(config) ;[account1, account2, account3, account4] = await nevermined.accounts.list() }) diff --git a/test/nevermined/Accounts.test.ts b/test/nevermined/Accounts.test.ts index 7ffde001f..aa623ff92 100644 --- a/test/nevermined/Accounts.test.ts +++ b/test/nevermined/Accounts.test.ts @@ -10,10 +10,14 @@ use(spies) describe('Accounts', () => { let accounts: AccountsApi + let nevermined: Nevermined before(async () => { // eslint-disable-next-line @typescript-eslint/no-extra-semi - ;({ accounts } = await Nevermined.getInstance(config)) + nevermined = await Nevermined.getInstance(config) + nevermined.keeper.dispenser = await nevermined.keeper.loadDispenserInstance() + nevermined.keeper.token = await nevermined.keeper.loadTokenInstance() + accounts = nevermined.accounts }) afterEach(() => { From 6f610b4d6372c0d0843b70083b7018708f5a948d Mon Sep 17 00:00:00 2001 From: enrique Date: Wed, 25 Oct 2023 14:36:47 +0200 Subject: [PATCH 2/5] feat: add as an option in the config --- aave_integration/config.ts | 1 + integration/config.ts | 1 + src/keeper/Keeper.ts | 38 ++++--------------------------- src/models/NeverminedOptions.ts | 2 ++ test/config.ts | 1 + test/keeper/ContractEvent.test.ts | 2 -- test/nevermined/Accounts.test.ts | 2 -- 7 files changed, 9 insertions(+), 38 deletions(-) diff --git a/aave_integration/config.ts b/aave_integration/config.ts index 641e700c6..518d48f06 100644 --- a/aave_integration/config.ts +++ b/aave_integration/config.ts @@ -18,4 +18,5 @@ export default { treasuryAddress: '0xd0A1E359811322d97991E03f863a0C30C2cF029C', agreementFee: 15, }, + testNet: true, } as NeverminedOptions diff --git a/integration/config.ts b/integration/config.ts index 55d369145..47b00b0a7 100644 --- a/integration/config.ts +++ b/integration/config.ts @@ -24,6 +24,7 @@ const configBase: NeverminedOptions = { ipfsProjectId, ipfsProjectSecret, verbose: logLevel, + testNet: true, } if (process.env.NETWORK_NAME === 'testing') { diff --git a/src/keeper/Keeper.ts b/src/keeper/Keeper.ts index bd8080ebe..f8dedc7ca 100644 --- a/src/keeper/Keeper.ts +++ b/src/keeper/Keeper.ts @@ -152,18 +152,10 @@ export class Keeper extends Instantiable { ) } - // Optionals - // try { - // this.instances.dispenser = await Dispenser.getInstance(this.instantiableConfig) - // } catch { - // this.logger.debug('Dispenser not available on this network.') - // } - - // try { - // this.instances.token = await Token.getInstance(this.instantiableConfig) - // } catch { - // this.logger.debug('Token not available on this network.') - // } + if (this.config.testNet) { + this.instances.dispenser = await Dispenser.getInstance(this.instantiableConfig) + this.instances.token = await Token.getInstance(this.instantiableConfig) + } // Main contracts this.dispenser = this.instances.dispenser @@ -464,28 +456,6 @@ export class Keeper extends Instantiable { } return this.royalties.curve } - - public async loadDispenserInstance() { - if (this.instances.dispenser) return this.instances.dispenser - try { - this.instances.dispenser = await Dispenser.getInstance(this.instantiableConfig) - this.dispenser = await Dispenser.getInstance(this.instantiableConfig) - } catch { - this.logger.debug('Dispenser not available on this network.') - } - return this.instances.dispenser - } - - public async loadTokenInstance() { - if (this.instances.token) return this.instances.token - try { - this.instances.token = await Token.getInstance(this.instantiableConfig) - this.token = await Token.getInstance(this.instantiableConfig) - } catch { - this.logger.debug('Token not available on this network.') - } - return this.instances.token - } } export default Keeper diff --git a/src/models/NeverminedOptions.ts b/src/models/NeverminedOptions.ts index 82eb17872..f912250da 100644 --- a/src/models/NeverminedOptions.ts +++ b/src/models/NeverminedOptions.ts @@ -83,4 +83,6 @@ export class NeverminedOptions { * Use a gas station to calculate transaction fees */ public gasStationUri?: string + + public testNet?: boolean } diff --git a/test/config.ts b/test/config.ts index aee61e244..137c9a1e2 100644 --- a/test/config.ts +++ b/test/config.ts @@ -18,6 +18,7 @@ const config = { treasuryAddress: '0xd0A1E359811322d97991E03f863a0C30C2cF029C', agreementFee: 15, }, + testNet: true, } as NeverminedOptions if (process.env.SEED_WORDS) { diff --git a/test/keeper/ContractEvent.test.ts b/test/keeper/ContractEvent.test.ts index b7738d475..b87da4333 100644 --- a/test/keeper/ContractEvent.test.ts +++ b/test/keeper/ContractEvent.test.ts @@ -18,8 +18,6 @@ describe('ContractEvent', () => { before(async () => { await TestContractHandler.prepareContracts() nevermined = await Nevermined.getInstance(config) - nevermined.keeper.dispenser = await nevermined.keeper.loadDispenserInstance() - nevermined.keeper.token = await nevermined.keeper.loadTokenInstance() web3 = await Web3Provider.getWeb3(config) ;[account1, account2, account3, account4] = await nevermined.accounts.list() }) diff --git a/test/nevermined/Accounts.test.ts b/test/nevermined/Accounts.test.ts index aa623ff92..3eb5e98f8 100644 --- a/test/nevermined/Accounts.test.ts +++ b/test/nevermined/Accounts.test.ts @@ -15,8 +15,6 @@ describe('Accounts', () => { before(async () => { // eslint-disable-next-line @typescript-eslint/no-extra-semi nevermined = await Nevermined.getInstance(config) - nevermined.keeper.dispenser = await nevermined.keeper.loadDispenserInstance() - nevermined.keeper.token = await nevermined.keeper.loadTokenInstance() accounts = nevermined.accounts }) From 3000267d5154216f88a68ad3efa64875242aded7 Mon Sep 17 00:00:00 2001 From: enrique Date: Wed, 25 Oct 2023 16:21:34 +0200 Subject: [PATCH 3/5] feat: implement isTestnet method --- aave_integration/config.ts | 1 - src/keeper/Keeper.ts | 4 +-- src/keeper/utils.ts | 59 ++++++++++++++++++++++++++++++++- src/models/NeverminedOptions.ts | 2 -- test/config.ts | 1 - 5 files changed, 60 insertions(+), 7 deletions(-) diff --git a/aave_integration/config.ts b/aave_integration/config.ts index 518d48f06..641e700c6 100644 --- a/aave_integration/config.ts +++ b/aave_integration/config.ts @@ -18,5 +18,4 @@ export default { treasuryAddress: '0xd0A1E359811322d97991E03f863a0C30C2cF029C', agreementFee: 15, }, - testNet: true, } as NeverminedOptions diff --git a/src/keeper/Keeper.ts b/src/keeper/Keeper.ts index f8dedc7ca..6e4129b0d 100644 --- a/src/keeper/Keeper.ts +++ b/src/keeper/Keeper.ts @@ -151,8 +151,9 @@ export class Keeper extends Instantiable { `Keeper could not connect to ${await this.getNetworkName()} - ${err.message} ${err.stack}`, ) } + const chainId = Number((await this.web3.getNetwork()).chainId) - if (this.config.testNet) { + if (KeeperUtils.isTestnet(chainId)) { this.instances.dispenser = await Dispenser.getInstance(this.instantiableConfig) this.instances.token = await Token.getInstance(this.instantiableConfig) } @@ -210,7 +211,6 @@ export class Keeper extends Instantiable { eventHandler: new EventHandler(), } // version - const chainId = Number((await this.web3.getNetwork()).chainId) this.network = { chainId, version: this.didRegistry.version.replace('v', ''), diff --git a/src/keeper/utils.ts b/src/keeper/utils.ts index dfdbb65fc..02f9bf81e 100644 --- a/src/keeper/utils.ts +++ b/src/keeper/utils.ts @@ -60,7 +60,64 @@ export async function getNetworkName(networkId: number): Promise { throw new KeeperError(`Network with id ${networkId} not supported.`) } } - +export async function isTestnet(networkId: number): Promise { + switch (networkId) { + case 1: + return false + case 2: + return true + case 3: + return true + case 4: + return true + case 5: + return true + case 77: + return false + case 99: + return false + case 42: + return true + case 100: + return false + case 137: + return false + case 1337: + return true + case 3141: + return true + case 10200: + return true + case 31337: + return true + case 8996: + return true + case 8997: + return true + case 8998: + return true + case 42220: + return false + case 44787: + return true + case 62320: + return true + case 80001: + return true + case 42161: + return false + case 421613: + return true + case 1313161554: + return false + case 1313161555: + return true + case 1313161556: + return true + default: + throw new KeeperError(`Network with id ${networkId} not supported.`) + } +} // Wrapper for implementing web3 provider. Needed for OpenGSN export interface JsonRpcPayload { jsonrpc: string diff --git a/src/models/NeverminedOptions.ts b/src/models/NeverminedOptions.ts index f912250da..82eb17872 100644 --- a/src/models/NeverminedOptions.ts +++ b/src/models/NeverminedOptions.ts @@ -83,6 +83,4 @@ export class NeverminedOptions { * Use a gas station to calculate transaction fees */ public gasStationUri?: string - - public testNet?: boolean } diff --git a/test/config.ts b/test/config.ts index 137c9a1e2..aee61e244 100644 --- a/test/config.ts +++ b/test/config.ts @@ -18,7 +18,6 @@ const config = { treasuryAddress: '0xd0A1E359811322d97991E03f863a0C30C2cF029C', agreementFee: 15, }, - testNet: true, } as NeverminedOptions if (process.env.SEED_WORDS) { From ee8c43a8ef00715df1ade93583eb4f99b8eeb541 Mon Sep 17 00:00:00 2001 From: enrique Date: Wed, 25 Oct 2023 16:46:03 +0200 Subject: [PATCH 4/5] fix: remove testNet option --- integration/config.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/integration/config.ts b/integration/config.ts index 47b00b0a7..55d369145 100644 --- a/integration/config.ts +++ b/integration/config.ts @@ -24,7 +24,6 @@ const configBase: NeverminedOptions = { ipfsProjectId, ipfsProjectSecret, verbose: logLevel, - testNet: true, } if (process.env.NETWORK_NAME === 'testing') { From 5cfe57d14d6541b8b64aeb34b38e759d314c9308 Mon Sep 17 00:00:00 2001 From: enrique Date: Wed, 25 Oct 2023 16:48:48 +0200 Subject: [PATCH 5/5] feat: prepare to bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cd7679dde..a4ebc81be 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nevermined-io/sdk", - "version": "2.0.0", + "version": "2.0.1", "description": "Javascript SDK for connecting with Nevermined Data Platform ", "main": "./dist/node/sdk.js", "typings": "./dist/node/sdk.d.ts",