diff --git a/src/contracts/NFT.ts b/src/contracts/NFT.ts index 4abe74ea8..dda4daa98 100644 --- a/src/contracts/NFT.ts +++ b/src/contracts/NFT.ts @@ -15,7 +15,7 @@ import { getEventFromTx, sendTx } from '../utils/ContractUtils' import { calculateActiveTemplateIndex, getOceanArtifactsAdressesByChainId -} from '../utils/Assets' +} from '../utils/Adresses' export class Nft extends SmartContract { getDefaultAbi() { diff --git a/src/utils/Adresses.ts b/src/utils/Adresses.ts new file mode 100644 index 000000000..e97410bb8 --- /dev/null +++ b/src/utils/Adresses.ts @@ -0,0 +1,90 @@ +import { ethers, Signer } from 'ethers' +import { NftFactory } from '../contracts/NFTFactory' +import fs from 'fs' +// eslint-disable-next-line import/no-named-default +import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/interfaces/IERC20Template.sol/IERC20Template.json' +// eslint-disable-next-line import/no-named-default +import { default as Addresses } from '@oceanprotocol/contracts/addresses/address.json' +/** + * Get the artifacts address from the address.json file + * either from the env or from the ocean-contracts dir + * @returns data or null + */ +export function getOceanArtifactsAdresses(): any { + try { + if (process.env.ADDRESS_FILE) { + // eslint-disable-next-line security/detect-non-literal-fs-filename + const data = fs.readFileSync(process.env.ADDRESS_FILE, 'utf8') + return JSON.parse(data) + } + return Addresses + } catch (error) { + return Addresses + } +} + +/** + * Get the artifacts address from the address.json file, for the given chain + * either from the env or from the ocean-contracts dir, safer than above, because sometimes the network name + * is mispeled, best example "optimism_sepolia" vs "optimism-sepolia" + * @returns data or null + */ +export function getOceanArtifactsAdressesByChainId(chain: number): any { + try { + // eslint-disable-next-line security/detect-non-literal-fs-filename + const data = getOceanArtifactsAdresses() + if (data) { + const networks = Object.keys(data) + for (const network of networks) { + if (data[network].chainId === chain) { + return data[network] + } + } + } + } catch (error) { + console.error(error) + } + return null +} +/** + * Use this function to accurately calculate the template index, and also checking if the template is active + * @param owner the signer account + * @param nftContractAddress the nft contract address, usually artifactsAddresses.ERC721Factory + * @param template the template ID or template address (from smart contract getId() function) + * @returns index of the template on the list + */ +export async function calculateActiveTemplateIndex( + owner: Signer, + nftContractAddress: string, // addresses.ERC721Factory, + template: string | number +): Promise { + // is an ID number? + const isTemplateID = typeof template === 'number' + + const factoryERC721 = new NftFactory(nftContractAddress, owner) + const currentTokenCount = await factoryERC721.getCurrentTokenTemplateCount() + for (let i = 1; i <= currentTokenCount; i++) { + const tokenTemplate = await factoryERC721.getTokenTemplate(i) + + const erc20Template = new ethers.Contract( + tokenTemplate.templateAddress, + ERC20Template.abi, + owner + ) + + // check for ID + if (isTemplateID) { + const id = await erc20Template.connect(owner).getId() + if (tokenTemplate.isActive && id.toString() === template.toString()) { + return i + } + } else if ( + tokenTemplate.isActive && + tokenTemplate.templateAddress === template.toString() + ) { + return i + } + } + // if nothing is found it returns -1 + return -1 +} diff --git a/src/utils/Assets.ts b/src/utils/Assets.ts index 7fa95b47f..0322314d9 100644 --- a/src/utils/Assets.ts +++ b/src/utils/Assets.ts @@ -3,10 +3,6 @@ import { ethers, Signer } from 'ethers' import { ConfigHelper } 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' @@ -17,10 +13,10 @@ import { DispenserCreationParams } from '../@types/Dispenser' import { FreCreationParams } from '../@types/FixedPrice' import { getEventFromTx } from './ContractUtils' import { ProviderInstance } from '../services/Provider' -// eslint-disable-next-line import/no-named-default -import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/interfaces/IERC20Template.sol/IERC20Template.json' + import AccessListFactory from '@oceanprotocol/contracts/artifacts/contracts/accesslists/AccessListFactory.sol/AccessListFactory.json' import ERC20Template4 from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template4.sol/ERC20Template4.json' +import { calculateActiveTemplateIndex } from './Adresses' // import * as hre from 'hardhat' @@ -31,90 +27,6 @@ export function useOasisSDK(network: string | number): boolean { return config && config.sdk === 'oasis' } -/** - * Get the artifacts address from the address.json file - * either from the env or from the ocean-contracts dir - * @returns data or null - */ -export function getOceanArtifactsAdresses(): any { - try { - if (process.env.ADDRESS_FILE) { - // eslint-disable-next-line security/detect-non-literal-fs-filename - const data = fs.readFileSync(process.env.ADDRESS_FILE, 'utf8') - return JSON.parse(data) - } - return Addresses - } catch (error) { - return Addresses - } -} - -/** - * Get the artifacts address from the address.json file, for the given chain - * either from the env or from the ocean-contracts dir, safer than above, because sometimes the network name - * is mispeled, best example "optimism_sepolia" vs "optimism-sepolia" - * @returns data or null - */ -export function getOceanArtifactsAdressesByChainId(chain: number): any { - try { - // eslint-disable-next-line security/detect-non-literal-fs-filename - const data = getOceanArtifactsAdresses() - if (data) { - const networks = Object.keys(data) - for (const network of networks) { - if (data[network].chainId === chain) { - return data[network] - } - } - } - } catch (error) { - console.error(error) - } - return null -} - -/** - * Use this function to accurately calculate the template index, and also checking if the template is active - * @param owner the signer account - * @param nftContractAddress the nft contract address, usually artifactsAddresses.ERC721Factory - * @param template the template ID or template address (from smart contract getId() function) - * @returns index of the template on the list - */ -export async function calculateActiveTemplateIndex( - owner: Signer, - nftContractAddress: string, // addresses.ERC721Factory, - template: string | number -): Promise { - // is an ID number? - const isTemplateID = typeof template === 'number' - - const factoryERC721 = new NftFactory(nftContractAddress, owner) - const currentTokenCount = await factoryERC721.getCurrentTokenTemplateCount() - for (let i = 1; i <= currentTokenCount; i++) { - const tokenTemplate = await factoryERC721.getTokenTemplate(i) - - const erc20Template = new ethers.Contract( - tokenTemplate.templateAddress, - ERC20Template.abi, - owner - ) - - // check for ID - if (isTemplateID) { - const id = await erc20Template.connect(owner).getId() - if (tokenTemplate.isActive && id.toString() === template.toString()) { - return i - } - } else if ( - tokenTemplate.isActive && - tokenTemplate.templateAddress === template.toString() - ) { - return i - } - } - // if nothing is found it returns -1 - return -1 -} /** * * @param name asset name