Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reduce number of calls to chainId #560

Merged
merged 5 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file. Dates are d

Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

#### [v2.0.0-rc2](https://github.com/nevermined-io/sdk-js/compare/v2.0.0-rc1...v2.0.0-rc2)

> 26 July 2023

- Support of assets with multiple services of the same type [`#555`](https://github.com/nevermined-io/sdk-js/pull/555)
- feat: make keeper network info static [`89511f1`](https://github.com/nevermined-io/sdk-js/commit/89511f11140398c146246a19438e958938b83376)
- fix: bring back network info getters [`1ef0547`](https://github.com/nevermined-io/sdk-js/commit/1ef05470e53cbe91fcba5aa6e951bbd10b60decc)
- chore: coming back to original attribute name [`941f96f`](https://github.com/nevermined-io/sdk-js/commit/941f96f485fee18398fe7e1517e8a43e85361754)

#### [v2.0.0-rc1](https://github.com/nevermined-io/sdk-js/compare/v2.0.0-rc0...v2.0.0-rc1)

> 25 July 2023
Expand Down Expand Up @@ -348,7 +357,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
- ci: testing with https services [`bb96077`](https://github.com/nevermined-io/sdk-js/commit/bb96077c9403ea7ae26195a51d8a92e144cea384)
- linting [`d2547ed`](https://github.com/nevermined-io/sdk-js/commit/d2547edfbfebb49b488c97c4ce9e6d6f2ee849e4)

#### [v1.0.0](https://github.com/nevermined-io/sdk-js/compare/v1.0.0-rc9...v1.0.0)
### [v1.0.0](https://github.com/nevermined-io/sdk-js/compare/v1.0.0-rc9...v1.0.0)

> 14 February 2023

Expand Down Expand Up @@ -1087,7 +1096,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

> 2 May 2022

- add aave service agreement to ddo when creating nft721 asset [`#248`](https://github.com/nevermined-io/sdk-js/pull/248)
- add aave service agreement to ddo when creating nft721 asset [`#248`](https://github.com/nevermined-io/sdk-js/pull/248)
- Adding v0.19.21 Changelog updates [`c93cdc5`](https://github.com/nevermined-io/sdk-js/commit/c93cdc55f139a43db4130ccb0f80924d2645a931)

#### [v0.19.21](https://github.com/nevermined-io/sdk-js/compare/v0.19.20...v0.19.21)
Expand Down Expand Up @@ -1350,7 +1359,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

> 18 January 2022

- Removing not used parameter [`#186`](https://github.com/nevermined-io/sdk-js/pull/186)
- Removing not used parameter [`#186`](https://github.com/nevermined-io/sdk-js/pull/186)
- [wip] Create agreement+pay in one transaction [`#183`](https://github.com/nevermined-io/sdk-js/pull/183)
- Adapting to contracts `v1.3.3` [`#177`](https://github.com/nevermined-io/sdk-js/pull/177)
- Adding v0.17.2 Changelog updates [`5eddda4`](https://github.com/nevermined-io/sdk-js/commit/5eddda43954e013e6e6f7344c9a877d801aacb5c)
Expand Down Expand Up @@ -1856,7 +1865,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
- Feature/sync develop [`#268`](https://github.com/nevermined-io/sdk-js/pull/268)
- Quick fix for non-eip1559 networks [`#266`](https://github.com/nevermined-io/sdk-js/pull/266)
- integrate Permissions and refactor search query [`#264`](https://github.com/nevermined-io/sdk-js/pull/264)
- add aave service agreement to ddo when creating nft721 asset [`#248`](https://github.com/nevermined-io/sdk-js/pull/248)
- add aave service agreement to ddo when creating nft721 asset [`#248`](https://github.com/nevermined-io/sdk-js/pull/248)
- Get the keeper version from the artifacts instead of package version [`#244`](https://github.com/nevermined-io/sdk-js/pull/244)
- replace `metadata-api` url by `marketplace-api` and sort type [`#243`](https://github.com/nevermined-io/sdk-js/pull/243)
- fixing issues with BigNumbers [`#246`](https://github.com/nevermined-io/sdk-js/pull/246)
Expand Down Expand Up @@ -1895,7 +1904,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
- Feature/190 add did to sec markets [`#191`](https://github.com/nevermined-io/sdk-js/pull/191)
- Adding utility methods for getting nft token uri [`#189`](https://github.com/nevermined-io/sdk-js/pull/189)
- Lint ... [`#187`](https://github.com/nevermined-io/sdk-js/pull/187)
- Removing not used parameter [`#186`](https://github.com/nevermined-io/sdk-js/pull/186)
- Removing not used parameter [`#186`](https://github.com/nevermined-io/sdk-js/pull/186)
- [wip] Create agreement+pay in one transaction [`#183`](https://github.com/nevermined-io/sdk-js/pull/183)
- Adapting to contracts `v1.3.3` [`#177`](https://github.com/nevermined-io/sdk-js/pull/177)
- Correct typo in route [`#184`](https://github.com/nevermined-io/sdk-js/pull/184)
Expand Down
2 changes: 1 addition & 1 deletion integration/nevermined/Artifacts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('Artifacts', () => {
assert.isDefined(nvm)
assert.isDefined(nvm.keeper)
assert.isDefined(nvm.keeper.didRegistry)
assert.oneOf((await nvm.keeper.getNetworkName()).toLowerCase(), networkName)
assert.oneOf(await nvm.keeper.getNetworkName(), networkName)
assert.oneOf(await nvm.keeper.getNetworkId(), networkId)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('Claim aborted agreements End-to-End', () => {
nevermined = await Nevermined.getInstance(config)
;[, publisher, collector1, , other] = await nevermined.accounts.list()

const networkName = (await nevermined.keeper.getNetworkName()).toLowerCase()
const networkName = await nevermined.keeper.getNetworkName()
const erc721ABI = await ContractHandler.getABI(
'NFT721Upgradeable',
config.artifactsFolder,
Expand Down
2 changes: 1 addition & 1 deletion integration/nevermined/NFT1155.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('NFT1155 End-to-End', () => {

describe('As user I can deploy Nevermined ERC-1155 NFT contract instances', () => {
it('Using the ABI', async () => {
const networkName = (await nevermined.keeper.getNetworkName()).toLowerCase()
const networkName = await nevermined.keeper.getNetworkName()
const erc1155ABI = await ContractHandler.getABI(
'NFT1155Upgradeable',
config.artifactsFolder,
Expand Down
4 changes: 2 additions & 2 deletions integration/nevermined/NFT1155Api.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function makeTest(isCustom) {
)

if (isCustom) {
const networkName = (await nevermined.keeper.getNetworkName()).toLowerCase()
const networkName = await nevermined.keeper.getNetworkName()
const erc1155ABI = await ContractHandler.getABI(
'NFT1155Upgradeable',
config.artifactsFolder,
Expand Down Expand Up @@ -468,7 +468,7 @@ function makeTest(isCustom) {

describe('Node should not be able to transfer the nft without the operator role', () => {
it('should create the subscription NFT without granting Nevermined the operator role', async () => {
const networkName = (await nevermined.keeper.getNetworkName()).toLowerCase()
const networkName = await nevermined.keeper.getNetworkName()
const erc1155ABI = await ContractHandler.getABI(
'NFT1155Upgradeable',
config.artifactsFolder,
Expand Down
2 changes: 1 addition & 1 deletion integration/nevermined/NFT721.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Nfts721 operations', async () => {
// Accounts
;[deployer, artist, collector] = await nevermined.accounts.list()

const networkName = (await nevermined.keeper.getNetworkName()).toLowerCase()
const networkName = await nevermined.keeper.getNetworkName()
const erc721ABI = await ContractHandler.getABI(
'NFT721Upgradeable',
config.artifactsFolder,
Expand Down
2 changes: 1 addition & 1 deletion integration/nevermined/NFT721Api.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('NFTs721 Api End-to-End', () => {
nevermined = await Nevermined.getInstance(config)
;[, artist, collector1, , gallery] = await nevermined.accounts.list()

const networkName = (await nevermined.keeper.getNetworkName()).toLowerCase()
const networkName = await nevermined.keeper.getNetworkName()
const erc721ABI = await ContractHandler.getABI(
'NFT721Upgradeable',
config.artifactsFolder,
Expand Down
4 changes: 2 additions & 2 deletions integration/nevermined/NFT721Templates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('NFT721Templates E2E', () => {
nevermined = await Nevermined.getInstance(config)
;[owner, artist, collector1, collector2, gallery] = await nevermined.accounts.list()

const networkName = (await nevermined.keeper.getNetworkName()).toLowerCase()
const networkName = await nevermined.keeper.getNetworkName()
const erc721ABI = await ContractHandler.getABI(
'NFT721Upgradeable',
config.artifactsFolder,
Expand Down Expand Up @@ -609,7 +609,7 @@ describe('NFT721Templates E2E', () => {
collector2.getId(),
)

const networkName = (await nevermined.keeper.getNetworkName()).toLowerCase()
const networkName = await nevermined.keeper.getNetworkName()
const erc721ABI = await ContractHandler.getABI(
'NFT721Upgradeable',
config.artifactsFolder,
Expand Down
2 changes: 1 addition & 1 deletion integration/nevermined/NFTsWithMultipleServices.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('E2E Flow for NFTs with multiple services', () => {

neverminedNodeAddress = await nevermined.services.node.getProviderAddress()

const networkName = (await nevermined.keeper.getNetworkName()).toLowerCase()
const networkName = await nevermined.keeper.getNetworkName()
const erc721ABI = await ContractHandler.getABI(
'NFT721Upgradeable',
config.artifactsFolder,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"test": "mocha --opts test/mocha.opts ./test/**/*.test.ts",
"test:watch": "mocha -w --watch-extensions js,ts,json",
"test:cover": "nyc --report-dir coverage/unit mocha --opts test/mocha.opts ./test/**/*.test.ts",
"integration": "mocha --opts integration/mocha.opts ",
"integration": "mocha --opts integration/mocha.opts ./integration/nevermined/*.test.ts",
"integration:subgraph": "mocha --opts integration/mocha.opts ./integration/**/*.test.subgraph.ts",
"integration:production": "export NETWORK_NAME=production; yarn integration",
"integration:staging": "export NETWORK_NAME=staging; yarn integration",
Expand Down Expand Up @@ -66,7 +66,7 @@
"cross-fetch": "^4.0.0",
"crypto-browserify": "^3.12.0",
"deprecated-decorator": "^0.1.6",
"ethers": "^6.6.4",
"ethers": "^6.6.5",
"graphql": "^16.7.1",
"https-browserify": "^1.0.0",
"jose": "^4.5.1",
Expand Down
10 changes: 5 additions & 5 deletions src/Instantiable.abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export interface InstantiableConfig {
circuitsFolder?: string
}

export function generateIntantiableConfigFromConfig(
export async function generateIntantiableConfigFromConfig(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo in the function name, generateInstantiableConfigFromConfig

config: NeverminedOptions,
): Partial<InstantiableConfig> {
): Promise<Partial<InstantiableConfig>> {
const logLevel =
typeof config.verbose !== 'number'
? config.verbose
Expand All @@ -24,7 +24,7 @@ export function generateIntantiableConfigFromConfig(
: (config.verbose as LogLevel)
return {
config,
web3: Web3Provider.getWeb3(config),
web3: await Web3Provider.getWeb3(config),
logger: new Logger(logLevel),
artifactsFolder: config.artifactsFolder,
circuitsFolder: config.circuitsFolder,
Expand All @@ -41,8 +41,8 @@ export abstract class Instantiable {

public get web3() {
if (!this._instantiableConfig?.web3) {
this.logger.warn('ethers.Provider instance is not defined. Using default instance.')
return Web3Provider.getWeb3()
this.logger.error('Web3 Provider not initialized')
throw new Error('Web3 Provider not initialized')
}
return this._instantiableConfig.web3
}
Expand Down
16 changes: 8 additions & 8 deletions src/keeper/ContractHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ export class ContractHandler extends Instantiable {
artifactsFolder: string,
address?: string,
): Promise<ethers.BaseContract> {
const networkId = await this.nevermined.keeper.getNetworkId()
const where = (await this.nevermined.keeper.getNetworkName()).toLowerCase()
const chainId = await this.nevermined.keeper.getNetworkId()
const where = await this.nevermined.keeper.getNetworkName()
try {
this.logger.debug(`ContractHandler :: get :: ${artifactsFolder} and address ${address}`)
return (
ContractHandler.getContract(what, networkId, address) ||
(await this.load(what, where, networkId, artifactsFolder, address))
ContractHandler.getContract(what, chainId, address) ||
(await this.load(what, where, chainId, artifactsFolder, address))
)
} catch (err) {
if (!optional) {
Expand Down Expand Up @@ -98,7 +98,7 @@ export class ContractHandler extends Instantiable {
}

public async getVersion(contractName: string, artifactsFolder: string): Promise<string> {
const where = (await this.nevermined.keeper.getNetworkName()).toLowerCase()
const where = await this.nevermined.keeper.getNetworkName()
let artifact
this.logger.debug(
`ContractHandler :: getVersion :: Trying to read ${artifactsFolder}/${contractName}.${where}.json`,
Expand Down Expand Up @@ -257,9 +257,9 @@ export class ContractHandler extends Instantiable {

public async getFeeData(gasPrice?: bigint, maxFeePerGas?: bigint, maxPriorityFeePerGas?: bigint) {
// Custom gas fee for polygon networks
const networkId = await this.nevermined.keeper.getNetworkId()
if (networkId === 137 || networkId === 80001) {
return this.getFeeDataPolygon(networkId)
const chainId = await this.nevermined.keeper.getNetworkId()
if (chainId === 137 || chainId === 80001) {
return this.getFeeDataPolygon(chainId)
}

const feeData = await this.web3.getFeeData()
Expand Down
89 changes: 59 additions & 30 deletions src/keeper/Keeper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,13 @@ export class Keeper extends Instantiable {
eventHandler: new EventHandler(),
}
// version
this.version = this.didRegistry.version.replace('v', '')
const chainId = Number((await this.web3.getNetwork()).chainId)
this.network = {
chainId,
version: this.didRegistry.version.replace('v', ''),
name: await KeeperUtils.getNetworkName(chainId),
loading: false,
}
}

/**
Expand Down Expand Up @@ -328,16 +334,25 @@ export class Keeper extends Instantiable {
eventHandler: EventHandler
}

/**
* Version of the artifacts in use
*/
public version: string

/**
* Network id loaded from web3
*/
protected network: {
id?: number
/**
* chainId of the network
*/
chainId?: number
/**
* Name of the network
*/
name?: string
/**
* Version of the artifacts in use
*/
version?: string
/**
* True if keeper is still connecting
*/
loading: boolean
} = {
loading: true,
Expand All @@ -363,47 +378,61 @@ export class Keeper extends Instantiable {
return Object.values(this.templates).find((template) => template.contractName === name)
}

/**
* Returns a Access template by name.
* @param name - Template name.
* @returns Agreement template instance.
*/
public getAccessTemplateByName(name: string): GenericAccess {
return this.templateList.find((template) => template.contractName === name)
}

/**
* Returns a template by address.
* @param address - Template address.
* @returns Agreement template instance.
*/
public getTemplateByAddress(address: string) {
return Object.values(this.templates).find((template) => template.address === address)
}

/**
* Returns the network by name.
* @returns Network name.
*/
public async getNetworkName(): Promise<string> {
return KeeperUtils.getNetworkName(await this.getNetworkId())
if (!this.network.name) {
this.network.name = await KeeperUtils.getNetworkName(await this.getNetworkId())
}
return this.network.name
}

/**
* Returns the id of the network.
* @returns Network ID.
*/
public async getNetworkId(): Promise<number> {
if (this.network.loading) {
if (!this.network.chainId) {
this.network.loading = false
this.network.id = Number((await this.web3.getNetwork()).chainId)
this.network.chainId = Number((await this.web3.getNetwork()).chainId)
}

while (!this.network.id) {
while (!this.network.chainId) {
await new Promise((resolve) => setTimeout(resolve, 1))
}

return this.network.id
return this.network.chainId
}

/**
* Returns the network version.
* @returns Network version.
*/
public getNetworkVersion(): string {
if (!this.network.version) {
this.network.version = this.didRegistry.version.replace('v', '')
}
return this.network.version
}

/**
* Returns a Access template by name.
* @param name - Template name.
* @returns Agreement template instance.
*/
public getAccessTemplateByName(name: string): GenericAccess {
return this.templateList.find((template) => template.contractName === name)
}

/**
* Returns a template by address.
* @param address - Template address.
* @returns Agreement template instance.
*/
public getTemplateByAddress(address: string) {
return Object.values(this.templates).find((template) => template.address === address)
}

public getAllInstances() {
Expand Down
Loading