diff --git a/src/config/ConfigHelper.ts b/src/config/ConfigHelper.ts index 55cecea3..decc9049 100644 --- a/src/config/ConfigHelper.ts +++ b/src/config/ConfigHelper.ts @@ -160,7 +160,7 @@ export const configHelperNetworks: Config[] = [ { ...configHelperNetworksBase, chainId: 23295, - network: 'oasis_saphire_testnet', + network: 'oasis_sapphire_testnet', nodeUri: 'https://testnet.sapphire.oasis.dev', subgraphUri: 'https://v4.subgraph.sapphire-testnet.oceanprotocol.com/subgraphs/name/oceanprotocol/ocean-subgraph', @@ -181,9 +181,16 @@ export const configHelperNetworks: Config[] = [ ] export const KNOWN_CONFIDENTIAL_EVMS = { - names: ['asis_saphire_testnet', 'oasis_saphire'], - chainIds: [23295, 23294] + // there are some typos around these names (on addresses.json is just one 'p' on 'sapphire') + networks: [ + { + name: ['oasis_sapphire', 'oasis_saphire'], // include alias or typos + chainId: 23294 + }, + { name: ['oasis_sapphire_testnet', 'oasis_saphire_testnet'], chainId: 23295 } + ] } + export class ConfigHelper { /* Load contract addresses from env ADDRESS_FILE (generated by ocean-contracts) */ public getAddressesFromEnv(network: string, customAddresses?: any): Partial { @@ -271,6 +278,22 @@ export class ConfigHelper { return configAddresses } + private checkConfigConfidential(network: string | number): boolean { + let search + if (typeof network === 'string') { + search = KNOWN_CONFIDENTIAL_EVMS.networks.filter((netInfo) => { + return netInfo.name.includes(network.toString()) + }) + + // chain id + } else { + search = KNOWN_CONFIDENTIAL_EVMS.networks.filter((netInfo) => { + return netInfo.chainId === Number(network) + }) + } + return search.length > 0 + } + /** * Returns the config object for a specific network supported by the oceanprotocol stack * @param {string | number} network the network's chainId or name @@ -279,8 +302,27 @@ export class ConfigHelper { */ public getConfig(network: string | number, infuraProjectId?: string): Config { const filterBy = typeof network === 'string' ? 'network' : 'chainId' + let config = configHelperNetworks.find((c) => c[filterBy] === network) + if (!config) { + // check typos of network name + const checkAlternateNames = + filterBy === 'network' && network.toString().includes('oasis_sap') + if (checkAlternateNames) { + let networkName = network.toString() + // 1st search was with 2 'pp' characters + if (networkName.indexOf('sapp') > -1) { + networkName = networkName.replace('sapp', 'sap') + // try just one 'p + } else { + networkName = networkName.replace('sap', 'sapp') + // try with 2 pp + } + config = configHelperNetworks.find((c) => c[filterBy] === networkName) + } + } + if (!config) { LoggerInstance.error(`No config found for given network '${network}'`) return null @@ -299,15 +341,8 @@ export class ConfigHelper { addresses = null } const contractAddressesConfig = this.getAddressesFromEnv(config.network, addresses) - // network name - if (filterBy === 'network') { - config.confidentialEVM = KNOWN_CONFIDENTIAL_EVMS.names.includes( - network.toString().toLowerCase() - ) - // chain id - } else { - config.confidentialEVM = KNOWN_CONFIDENTIAL_EVMS.chainIds.includes(Number(network)) - } + + config.confidentialEVM = this.checkConfigConfidential(network) config = { ...config, ...contractAddressesConfig } diff --git a/src/utils/Assets.ts b/src/utils/Assets.ts index a0bfab45..15744db4 100644 --- a/src/utils/Assets.ts +++ b/src/utils/Assets.ts @@ -1,31 +1,22 @@ import { SHA256 } from 'crypto-js' import { ethers, Signer } from 'ethers' -import { - Aquarius, - DatatokenCreateParams, - Nft, - NftCreateData, - NftFactory, - ProviderInstance, - ZERO_ADDRESS, - // approveWei, - // ProviderComputeInitialize, - // ConsumeMarketFee, - // Datatoken, - // Config, - // DDO, - // ProviderFees, - getEventFromTx, - DispenserCreationParams, - FreCreationParams, - ConfigHelper -} from '../../src' +import { ConfigHelper, KNOWN_CONFIDENTIAL_EVMS } from '../../src/config' import { hexlify } from 'ethers/lib/utils' import { createHash } from 'crypto' import fs from 'fs' // eslint-disable-next-line import/no-named-default import { default as Addresses } from '@oceanprotocol/contracts/addresses/address.json' +import { Aquarius } from '../services/Aquarius' +import { NftFactory } from '../contracts/NFTFactory' +import { Nft } from '../contracts/NFT' +import { DatatokenCreateParams } from '../@types/Datatoken' +import { NftCreateData } from '../@types/NFTFactory' +import { ZERO_ADDRESS } from './Constants' +import { DispenserCreationParams } from '../@types/Dispenser' +import { FreCreationParams } from '../@types/FixedPrice' +import { getEventFromTx } from './ContractUtils' +import { ProviderInstance } from '../services/Provider' // template address OR templateId export function isConfidentialEVM(network: string | number): boolean { @@ -88,7 +79,7 @@ export async function calculateTemplateIndex( if (typeof template === 'string') { const templateAddresses: any[] = Object.values(templatesAvailable) index = templateAddresses.findIndex(function (item) { - return item.indexOf(template) !== -1 + return item === template }) } else { const templateIndexes = Object.keys(templatesAvailable) @@ -96,6 +87,9 @@ export async function calculateTemplateIndex( return item.indexOf(template.toString()) !== -1 }) } + if (index !== -1) { + index += 1 + } } } return index diff --git a/test/unit/AssetUtils.test.ts b/test/unit/AssetUtils.test.ts index 3adce9b7..3282bc7b 100644 --- a/test/unit/AssetUtils.test.ts +++ b/test/unit/AssetUtils.test.ts @@ -4,15 +4,18 @@ import { calculateTemplateIndex, isConfidentialEVM } from '../../src/utils' describe('Asset utils (createAsset)', () => { it('should check if confidential EVM', async () => { - for (const name of KNOWN_CONFIDENTIAL_EVMS.names) { - assert( - isConfidentialEVM(name) === true, - `Network: "${name}" is not a confidental EVM` - ) + for (const network of KNOWN_CONFIDENTIAL_EVMS.networks) { + network.name.map((networkName) => { + assert( + isConfidentialEVM(networkName) === true, + `Network: "${networkName}" is not a confidental EVM` + ) + return true + }) } - for (const chain of KNOWN_CONFIDENTIAL_EVMS.chainIds) { + for (const chain of KNOWN_CONFIDENTIAL_EVMS.networks) { assert( - isConfidentialEVM(chain) === true, + isConfidentialEVM(chain.chainId) === true, `Chain Id: "${chain}" is not a confidental EVM` ) } @@ -25,17 +28,55 @@ describe('Asset utils (createAsset)', () => { ) }) - it('should get correct template index from contract artifacts', async () => { + it('should get correct template index from contract artifacts (using SC address as template)', async () => { const wrongOne = await calculateTemplateIndex( - KNOWN_CONFIDENTIAL_EVMS.chainIds[0], // testnet chain + KNOWN_CONFIDENTIAL_EVMS.networks[1].chainId, // testnet chain '12' ) assert(wrongOne === -1, 'wrong template index, should be inexistent!') const okIndex = await calculateTemplateIndex( - KNOWN_CONFIDENTIAL_EVMS.chainIds[1], // mainnet chain + KNOWN_CONFIDENTIAL_EVMS.networks[0].chainId, // mainnet chain '0x4dD281EB67DED07E76E413Df16176D66ae69e240' ) assert(okIndex >= 1, 'wrong template index, should exist!') + + const okIndexNonConfidential = await calculateTemplateIndex( + 11155111, // sepolia + '0xDEfD0018969cd2d4E648209F876ADe184815f038' + ) + assert(okIndexNonConfidential === 2, 'Should be template 2 for sepolia!') + + const notOkIndexNonConfidential = await calculateTemplateIndex( + 11155111, // sepolia + '0xDEfD0018969cd2d4E648209F876ADe184815f022' // wrong template + ) + assert(notOkIndexNonConfidential === -1, 'Template should not exist on sepolia!') + }) + + it('should get correct template index from contract artifacts (using template ID as template)', async () => { + const okTemplate = await calculateTemplateIndex( + KNOWN_CONFIDENTIAL_EVMS.networks[1].chainId, // testnet chain + 4 + ) + assert(okTemplate === 4, 'wrong template index, should be index 4!') + + const wrongOne = await calculateTemplateIndex( + KNOWN_CONFIDENTIAL_EVMS.networks[0].chainId, // mainnet chain + 6 + ) + assert(wrongOne === -1, 'wrong template index, should only exist 5!') + + const okIndexNonConfidential = await calculateTemplateIndex( + 11155111, // sepolia + 2 // ok template + ) + assert(okIndexNonConfidential === 2, 'Should be template 2 for sepolia!') + + const notOkIndexNonConfidential = await calculateTemplateIndex( + 11155111, // sepolia + 3 // wrong template + ) + assert(notOkIndexNonConfidential === -1, 'Template 3 should not exist on sepolia!') }) })