1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

fix review

This commit is contained in:
paulo-ocean 2024-09-11 10:14:25 +01:00
parent dc25a90871
commit 61fff3441d
4 changed files with 22 additions and 172 deletions

View File

@ -44,11 +44,6 @@ export class Config {
*/
public nftFactoryAddress?: string
/**
* ERC20 templates addresses
*/
public eRC20Template?: any
/**
* datatokens ABI
* @type {string}

View File

@ -180,16 +180,10 @@ export const configHelperNetworks: Config[] = [
}
]
export const KNOWN_CONFIDENTIAL_EVMS = {
// 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 const KNOWN_CONFIDENTIAL_EVMS = [
23294, // oasis_sapphire
23295 // oasis_sapphire_testnet
]
export class ConfigHelper {
/* Load contract addresses from env ADDRESS_FILE (generated by ocean-contracts) */
@ -202,7 +196,6 @@ export class ConfigHelper {
FixedPrice,
Dispenser,
ERC721Factory,
ERC20Template, // added to Config
OPFCommunityFeeCollector,
Ocean,
chainId,
@ -218,7 +211,6 @@ export class ConfigHelper {
} = customAddresses[network]
configAddresses = {
nftFactoryAddress: ERC721Factory,
eRC20Template: ERC20Template, // added to Config
opfCommunityFeeCollector: OPFCommunityFeeCollector,
fixedRateExchangeAddress: FixedPrice,
dispenserAddress: Dispenser,
@ -280,22 +272,6 @@ 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
@ -307,24 +283,6 @@ export class ConfigHelper {
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
@ -344,7 +302,10 @@ export class ConfigHelper {
}
const contractAddressesConfig = this.getAddressesFromEnv(config.network, addresses)
config.confidentialEVM = this.checkConfigConfidential(network)
config.confidentialEVM =
filterBy === 'chainId'
? KNOWN_CONFIDENTIAL_EVMS.includes(Number(network))
: network.toString().includes('oasis_sap')
config = { ...config, ...contractAddressesConfig }

View File

@ -71,43 +71,6 @@ export function getOceanArtifactsAdressesByChainId(chain: number): any {
return null
}
/**
* Use this function if don't need to check if the template if active
* (not 100% reliable if we need to check at smart contract level)
* @param chainID the chain identifier
* @param template the id or the template address
* @returns the index of the template from the 'ERC20Template' object
*/
export async function getTemplateIndexOnList(
chainID: number,
template: string | number
): Promise<number> {
let index = -1
const artifacts = await getOceanArtifactsAdressesByChainId(chainID)
if (artifacts) {
const templatesAvailable = artifacts.ERC20Template
if (templatesAvailable) {
// template address?
if (typeof template === 'string') {
const templateAddresses: any[] = Object.values(templatesAvailable)
index = templateAddresses.findIndex(function (item) {
return item === template
})
} else {
const templateIndexes = Object.keys(templatesAvailable)
index = templateIndexes.findIndex(function (item) {
return item.indexOf(template.toString()) !== -1
})
}
if (index !== -1) {
index += 1
}
}
}
// index or -1 if not found
return index
}
/**
* Use this function to accurately calculate the template index, and also checking if the template is active
* @param owner the signer account
@ -156,16 +119,14 @@ export async function calculateActiveTemplateIndex(
* @param symbol asse symbol
* @param owner owner address
* @param assetUrl asset url
* @param templateIndex 1,2 or 4
* @param templateIDorAddress either template address or id
* @param ddo ddo
* @param encryptDDO encrypt or not?
* @param providerUrl the provider URL
* @param providerFeeToken the provider fee token
* @param nftContractAddress the nft contract address
* @param aquariusInstance aquarius, could be node instance url
* @param dispenserAddress dispenser address
* @param fixedRateAddress fixed rate exchange address
* @param baseTokenAddress base token address (ocean)
* @param filesObject if present and confidential evm, add it to token create params
* @returns ddo id as string
*/
export async function createAsset(
@ -173,7 +134,7 @@ export async function createAsset(
symbol: string,
owner: Signer,
assetUrl: any,
template: string | number, // If string, it's template address , otherwise, it's templateId
templateIDorAddress: string | number, // If string, it's template address , otherwise, it's templateId
ddo: any,
encryptDDO: boolean = true, // default is true
providerUrl: string,
@ -181,13 +142,9 @@ export async function createAsset(
nftContractAddress: string, // addresses.ERC721Factory,
aquariusInstance: Aquarius,
filesObject?: any
// fixed rate
// dispenserAddress?: string,
// fixedRateAddress?: string,
// baseTokenAddress?: string // ocean token?
): Promise<string> {
const isAddress = typeof template === 'string'
const isTemplateIndex = typeof template === 'number'
const isAddress = typeof templateIDorAddress === 'string'
const isTemplateIndex = typeof templateIDorAddress === 'number'
if (!isAddress && !isTemplateIndex) {
throw new Error('Invalid template! Must be a "number" or a "string"')
}
@ -201,7 +158,7 @@ export async function createAsset(
let templateIndex = await calculateActiveTemplateIndex(
owner,
nftContractAddress,
template
templateIDorAddress
)
if (templateIndex < 1) {
@ -239,7 +196,7 @@ export async function createAsset(
}
// include fileObject in the DT constructor
if (config.confidentialEVM) {
if (config.confidentialEVM && templateIndex === 4) {
datatokenParams.filesObject = filesObject
}
@ -291,8 +248,10 @@ export async function createAsset(
// create the files encrypted string
assetUrl.datatokenAddress = datatokenAddressAsset
assetUrl.nftAddress = nftAddress
// if template 4 no need to encrypt it??
if (!config.confidentialEVM) {
// if confidential EVM no need to make encrypt call here
if (config.confidentialEVM) {
ddo.services[0].files = null // null on confidental EVM
} else {
ddo.services[0].files = await ProviderInstance.encrypt(assetUrl, chainID, providerUrl)
}

View File

@ -1,11 +1,7 @@
import { assert } from 'chai'
import { KNOWN_CONFIDENTIAL_EVMS } from '../../src/config'
import { provider, getAddresses } from '../config'
import {
calculateActiveTemplateIndex,
getTemplateIndexOnList,
isConfidentialEVM
} from '../../src/utils'
import { calculateActiveTemplateIndex, isConfidentialEVM } from '../../src/utils'
import { Signer } from 'ethers/lib/ethers'
let nftOwner: Signer
@ -17,18 +13,9 @@ describe('Asset utils (createAsset)', () => {
})
it('should check if confidential EVM', async () => {
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.networks) {
for (const chain of KNOWN_CONFIDENTIAL_EVMS) {
assert(
isConfidentialEVM(chain.chainId) === true,
isConfidentialEVM(chain) === true,
`Chain Id: "${chain}" is not a confidental EVM`
)
}
@ -41,58 +28,6 @@ describe('Asset utils (createAsset)', () => {
)
})
it('should get correct template index from contract artifacts (using SC address as template)', async () => {
const wrongOne = await getTemplateIndexOnList(
KNOWN_CONFIDENTIAL_EVMS.networks[1].chainId, // testnet chain
'12'
)
assert(wrongOne === -1, 'wrong template index, should be inexistent!')
const okIndex = await getTemplateIndexOnList(
KNOWN_CONFIDENTIAL_EVMS.networks[0].chainId, // mainnet chain
'0x4dD281EB67DED07E76E413Df16176D66ae69e240'
)
assert(okIndex >= 1, 'wrong template index, should exist!')
const okIndexNonConfidential = await getTemplateIndexOnList(
11155111, // sepolia
'0xDEfD0018969cd2d4E648209F876ADe184815f038'
)
assert(okIndexNonConfidential === 2, 'Should be template 2 for sepolia!')
const notOkIndexNonConfidential = await getTemplateIndexOnList(
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 getTemplateIndexOnList(
KNOWN_CONFIDENTIAL_EVMS.networks[1].chainId, // testnet chain
4
)
assert(okTemplate === 4, 'wrong template index, should be index 4!')
const wrongOne = await getTemplateIndexOnList(
KNOWN_CONFIDENTIAL_EVMS.networks[0].chainId, // mainnet chain
6
)
assert(wrongOne === -1, 'wrong template index, should only exist 5!')
const okIndexNonConfidential = await getTemplateIndexOnList(
11155111, // sepolia
2 // ok template
)
assert(okIndexNonConfidential === 2, 'Should be template 2 for sepolia!')
const notOkIndexNonConfidential = await getTemplateIndexOnList(
11155111, // sepolia
3 // wrong template
)
assert(notOkIndexNonConfidential === -1, 'Template 3 should not exist on sepolia!')
})
// checking if active by connecting to the smart contract as well
it('Calculate index - Should get correct template index from contract getId() (using template ID as template)', async () => {
const okTemplate = await calculateActiveTemplateIndex(