From dce77b1e59a0c5d75d1de56699e1f8f8f104cc8c Mon Sep 17 00:00:00 2001 From: paulo-ocean Date: Fri, 29 Nov 2024 13:15:09 +0000 Subject: [PATCH 1/6] first pass --- src/contracts/NFTFactory.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/contracts/NFTFactory.ts b/src/contracts/NFTFactory.ts index 68dfc4232..19c2f3247 100644 --- a/src/contracts/NFTFactory.ts +++ b/src/contracts/NFTFactory.ts @@ -1,13 +1,5 @@ import { BigNumber, ethers } from 'ethers' import ERC721Factory from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json' -import { - generateDtName, - ZERO_ADDRESS, - sendTx, - getEventFromTx, - getTokenDecimals, - LoggerInstance -} from '../utils' import { AbiItem, FreCreationParams, @@ -19,6 +11,10 @@ import { ReceiptOrEstimate } from '../@types' import { SmartContractWithAddress } from './SmartContractWithAddress' +import { generateDtName } from '../utils/DatatokenName' +import { ZERO_ADDRESS } from '../utils/Constants' +import { getEventFromTx, getTokenDecimals, sendTx } from '../utils/ContractUtils' +import { LoggerInstance } from '../utils/Logger' /** * Provides an interface for NFT Factory contract From 795dcc398edf3caa548276101a724658a7f2b7df Mon Sep 17 00:00:00 2001 From: paulo-ocean Date: Fri, 29 Nov 2024 14:31:21 +0000 Subject: [PATCH 2/6] 2nd pass --- src/config/ConfigHelper.ts | 2 +- src/utils/ContractUtils.ts | 3 ++- src/utils/TokenUtils.ts | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/config/ConfigHelper.ts b/src/config/ConfigHelper.ts index 4379a9aa0..647633a9b 100644 --- a/src/config/ConfigHelper.ts +++ b/src/config/ConfigHelper.ts @@ -2,7 +2,7 @@ import { default as DefaultContractsAddresses } from '@oceanprotocol/contracts/addresses/address.json' import fs from 'fs' import { Config } from '.' -import { LoggerInstance } from '../utils' +import { LoggerInstance } from '../utils/Logger' const configHelperNetworksBase: Config = { chainId: null, diff --git a/src/utils/ContractUtils.ts b/src/utils/ContractUtils.ts index 98d0baa26..459249b8a 100644 --- a/src/utils/ContractUtils.ts +++ b/src/utils/ContractUtils.ts @@ -1,7 +1,8 @@ import { ethers, Signer, providers, Contract, ContractFunction, BigNumber } from 'ethers' import { Config, KNOWN_CONFIDENTIAL_EVMS } from '../config' -import { LoggerInstance, minAbi } from '.' +import { LoggerInstance } from './Logger' +import { minAbi } from './minAbi' const MIN_GAS_FEE_POLYGON = 30000000000 // minimum recommended 30 gwei polygon main and mumbai fees const MIN_GAS_FEE_SEPOLIA = 4000000000 // minimum 4 gwei for eth sepolia testnet diff --git a/src/utils/TokenUtils.ts b/src/utils/TokenUtils.ts index d1c37bea3..e66af8dfa 100644 --- a/src/utils/TokenUtils.ts +++ b/src/utils/TokenUtils.ts @@ -1,8 +1,10 @@ import Decimal from 'decimal.js' import { ethers, Signer } from 'ethers' -import { amountToUnits, unitsToAmount, minAbi, sendTx, LoggerInstance } from '.' import { Config } from '../config' import { ReceiptOrEstimate, ReceiptOrDecimal } from '../@types' +import { minAbi } from './minAbi' +import { amountToUnits, sendTx, unitsToAmount } from './ContractUtils' +import { LoggerInstance } from './Logger' /** * Approve spender to spent amount tokens From bd519f399e690402ab114485291ab6b8cd38d6e4 Mon Sep 17 00:00:00 2001 From: paulo-ocean Date: Fri, 29 Nov 2024 14:53:15 +0000 Subject: [PATCH 3/6] 3rd pass --- src/contracts/Dispenser.ts | 2 +- src/contracts/FixedRateExchange.ts | 3 ++- src/contracts/NFT.ts | 4 +++- src/contracts/Router.ts | 2 +- src/contracts/SmartContract.ts | 4 ++-- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/contracts/Dispenser.ts b/src/contracts/Dispenser.ts index 93b48ba83..54486438f 100644 --- a/src/contracts/Dispenser.ts +++ b/src/contracts/Dispenser.ts @@ -1,9 +1,9 @@ import Decimal from 'decimal.js' import DispenserAbi from '@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json' -import { sendTx } from '../utils' import { Datatoken } from './Datatoken' import { SmartContractWithAddress } from './SmartContractWithAddress' import { DispenserToken, ReceiptOrEstimate, AbiItem } from '../@types' +import { sendTx } from '../utils/ContractUtils' export class Dispenser extends SmartContractWithAddress { getDefaultAbi() { diff --git a/src/contracts/FixedRateExchange.ts b/src/contracts/FixedRateExchange.ts index a79effb2a..f2750b091 100644 --- a/src/contracts/FixedRateExchange.ts +++ b/src/contracts/FixedRateExchange.ts @@ -1,5 +1,5 @@ import FixedRateExchangeAbi from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json' -import { sendTx, ZERO_ADDRESS } from '../utils' +import { sendTx } from '../utils/ContractUtils' import { PriceAndFees, FeesInfo, @@ -8,6 +8,7 @@ import { AbiItem } from '../@types' import { SmartContractWithAddress } from './SmartContractWithAddress' +import { ZERO_ADDRESS } from '../utils/Constants' export class FixedRateExchange extends SmartContractWithAddress { getDefaultAbi() { diff --git a/src/contracts/NFT.ts b/src/contracts/NFT.ts index ada38fe56..a40d172c3 100644 --- a/src/contracts/NFT.ts +++ b/src/contracts/NFT.ts @@ -1,6 +1,5 @@ import { BigNumber, ethers } from 'ethers' import ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json' -import { generateDtName, sendTx, getEventFromTx, ZERO_ADDRESS } from '../utils' import { MetadataProof, MetadataAndTokenURI, @@ -13,6 +12,9 @@ import { calculateActiveTemplateIndex, getOceanArtifactsAdressesByChainId } from '../utils/Assets' +import { generateDtName } from '../utils/DatatokenName' +import { ZERO_ADDRESS } from '../utils/Constants' +import { getEventFromTx, sendTx } from '../utils/ContractUtils' export class Nft extends SmartContract { getDefaultAbi() { diff --git a/src/contracts/Router.ts b/src/contracts/Router.ts index 0fe161bbb..7fb68bc0c 100644 --- a/src/contracts/Router.ts +++ b/src/contracts/Router.ts @@ -1,5 +1,5 @@ import FactoryRouter from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json' -import { sendTx } from '../utils' +import { sendTx } from '../utils/ContractUtils' import { Operation, ReceiptOrEstimate, AbiItem } from '../@types' import { SmartContractWithAddress } from './SmartContractWithAddress' diff --git a/src/contracts/SmartContract.ts b/src/contracts/SmartContract.ts index 703a6ba80..3b359eda1 100644 --- a/src/contracts/SmartContract.ts +++ b/src/contracts/SmartContract.ts @@ -1,13 +1,13 @@ import { ethers, Signer, Contract } from 'ethers' import { AbiItem } from '../@types' import { Config, ConfigHelper } from '../config' +import * as sapphire from '@oasisprotocol/sapphire-paratime' import { amountToUnits, getFairGasPrice, setContractDefaults, unitsToAmount -} from '../utils' -import * as sapphire from '@oasisprotocol/sapphire-paratime' +} from '../utils/ContractUtils' export abstract class SmartContract { public signer: Signer From bf94e102e53dd07eac6c671ef222b96e34ffca7f Mon Sep 17 00:00:00 2001 From: paulo-ocean Date: Fri, 29 Nov 2024 15:30:55 +0000 Subject: [PATCH 4/6] 4th pass --- src/contracts/AccessList.ts | 2 +- src/contracts/AccessListFactory.ts | 4 +++- src/contracts/Datatoken.ts | 3 ++- src/contracts/Datatoken4.ts | 2 +- src/contracts/NFT.ts | 7 ++++--- src/contracts/df/DfRewards.ts | 2 +- src/contracts/df/DfStrategyV1.ts | 2 +- src/contracts/ve/VeAllocate.ts | 2 +- src/contracts/ve/VeFeeDistributor.ts | 2 +- src/contracts/ve/VeOcean.ts | 2 +- src/services/Aquarius.ts | 3 ++- src/services/Provider.ts | 2 +- src/utils/OrderUtils.ts | 26 +++++++++++--------------- 13 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/contracts/AccessList.ts b/src/contracts/AccessList.ts index 3b058505b..c0ee79ba8 100644 --- a/src/contracts/AccessList.ts +++ b/src/contracts/AccessList.ts @@ -1,6 +1,6 @@ import { Signer } from 'ethers' import AccessList from '@oceanprotocol/contracts/artifacts/contracts/accesslists/AccessList.sol/AccessList.json' -import { sendTx } from '../utils' +import { sendTx } from '../utils/ContractUtils' import { AbiItem, ReceiptOrEstimate } from '../@types' import { Config } from '../config' import { SmartContractWithAddress } from './SmartContractWithAddress' diff --git a/src/contracts/AccessListFactory.ts b/src/contracts/AccessListFactory.ts index 672e450f5..e9de1321c 100644 --- a/src/contracts/AccessListFactory.ts +++ b/src/contracts/AccessListFactory.ts @@ -1,9 +1,11 @@ import { BigNumber, Signer } from 'ethers' import { Config } from '../config' import AccessListFactory from '@oceanprotocol/contracts/artifacts/contracts/accesslists/AccessListFactory.sol/AccessListFactory.json' -import { generateDtName, sendTx, getEventFromTx, ZERO_ADDRESS } from '../utils' +import { sendTx, getEventFromTx } from '../utils/ContractUtils' +import { ZERO_ADDRESS } from '../utils/Constants' import { AbiItem, ReceiptOrEstimate } from '../@types' import { SmartContractWithAddress } from './SmartContractWithAddress' +import { generateDtName } from '../utils/DatatokenName' /** * Provides an interface for Access List Factory contract diff --git a/src/contracts/Datatoken.ts b/src/contracts/Datatoken.ts index 8b24ba7bc..919cc49df 100644 --- a/src/contracts/Datatoken.ts +++ b/src/contracts/Datatoken.ts @@ -2,7 +2,8 @@ import { ethers, Signer } from 'ethers' import Decimal from 'decimal.js' import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json' import ERC20TemplateEnterprise from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20TemplateEnterprise.sol/ERC20TemplateEnterprise.json' -import { amountToUnits, sendTx, ZERO_ADDRESS } from '../utils' +import { amountToUnits, sendTx } from '../utils/ContractUtils' +import { ZERO_ADDRESS } from '../utils/Constants' import { AbiItem, ConsumeMarketFee, diff --git a/src/contracts/Datatoken4.ts b/src/contracts/Datatoken4.ts index e7bc937a3..110c28355 100644 --- a/src/contracts/Datatoken4.ts +++ b/src/contracts/Datatoken4.ts @@ -5,7 +5,7 @@ import ERC20Template4 from '@oceanprotocol/contracts/artifacts/contracts/templat import { AbiItem, ReceiptOrEstimate } from '../@types' import { AccessListContract } from './AccessList' import { Config } from '../config' -import { sendTx } from '../utils' +import { sendTx } from '../utils/ContractUtils' export class Datatoken4 extends Datatoken { public accessList: AccessListContract diff --git a/src/contracts/NFT.ts b/src/contracts/NFT.ts index a40d172c3..4abe74ea8 100644 --- a/src/contracts/NFT.ts +++ b/src/contracts/NFT.ts @@ -8,13 +8,14 @@ import { AbiItem } from '../@types' import { SmartContract } from './SmartContract' + +import { generateDtName } from '../utils/DatatokenName' +import { ZERO_ADDRESS } from '../utils/Constants' +import { getEventFromTx, sendTx } from '../utils/ContractUtils' import { calculateActiveTemplateIndex, getOceanArtifactsAdressesByChainId } from '../utils/Assets' -import { generateDtName } from '../utils/DatatokenName' -import { ZERO_ADDRESS } from '../utils/Constants' -import { getEventFromTx, sendTx } from '../utils/ContractUtils' export class Nft extends SmartContract { getDefaultAbi() { diff --git a/src/contracts/df/DfRewards.ts b/src/contracts/df/DfRewards.ts index f3f96ee01..0657be87a 100644 --- a/src/contracts/df/DfRewards.ts +++ b/src/contracts/df/DfRewards.ts @@ -1,5 +1,5 @@ import dfRewardsABI from '@oceanprotocol/contracts/artifacts/contracts/df/DFRewards.sol/DFRewards.json' -import { sendTx } from '../../utils' +import { sendTx } from '../../utils/ContractUtils' import { SmartContractWithAddress } from '../SmartContractWithAddress' import { AbiItem, ReceiptOrEstimate } from '../../@types' diff --git a/src/contracts/df/DfStrategyV1.ts b/src/contracts/df/DfStrategyV1.ts index 7868e591b..f2338dad5 100644 --- a/src/contracts/df/DfStrategyV1.ts +++ b/src/contracts/df/DfStrategyV1.ts @@ -1,5 +1,5 @@ import dfStrategyV1ABI from '@oceanprotocol/contracts/artifacts/contracts/df/DFStrategyV1.sol/DFStrategyV1.json' -import { sendTx } from '../../utils' +import { sendTx } from '../../utils/ContractUtils' import { SmartContractWithAddress } from '../SmartContractWithAddress' import { ReceiptOrEstimate, AbiItem } from '../../@types' /** diff --git a/src/contracts/ve/VeAllocate.ts b/src/contracts/ve/VeAllocate.ts index 3f9da1e0e..d0da53c56 100644 --- a/src/contracts/ve/VeAllocate.ts +++ b/src/contracts/ve/VeAllocate.ts @@ -1,5 +1,5 @@ import veAllocateABI from '@oceanprotocol/contracts/artifacts/contracts/ve/veAllocate.sol/veAllocate.json' -import { sendTx } from '../../utils' +import { sendTx } from '../../utils/ContractUtils' import { SmartContractWithAddress } from '../SmartContractWithAddress' import { ReceiptOrEstimate, AbiItem } from '../../@types' /** diff --git a/src/contracts/ve/VeFeeDistributor.ts b/src/contracts/ve/VeFeeDistributor.ts index 5a272c6f3..768ff50db 100644 --- a/src/contracts/ve/VeFeeDistributor.ts +++ b/src/contracts/ve/VeFeeDistributor.ts @@ -1,5 +1,5 @@ import veFeeABI from '@oceanprotocol/contracts/artifacts/contracts/ve/veFeeDistributor.vy/veFeeDistributor.json' -import { sendTx } from '../../utils' +import { sendTx } from '../../utils/ContractUtils' import { SmartContractWithAddress } from '../SmartContractWithAddress' import { ReceiptOrEstimate, AbiItem } from '../../@types' /** diff --git a/src/contracts/ve/VeOcean.ts b/src/contracts/ve/VeOcean.ts index ed5efd7f3..dbf0e6775 100644 --- a/src/contracts/ve/VeOcean.ts +++ b/src/contracts/ve/VeOcean.ts @@ -1,5 +1,5 @@ import veOceanABI from '@oceanprotocol/contracts/artifacts/contracts/ve/veOCEAN.vy/veOCEAN.json' -import { sendTx } from '../../utils' +import { sendTx } from '../../utils/ContractUtils' import { SmartContractWithAddress } from '../SmartContractWithAddress' import { ReceiptOrEstimate, AbiItem } from '../../@types' /** diff --git a/src/services/Aquarius.ts b/src/services/Aquarius.ts index e3d02e1bc..426715ae4 100644 --- a/src/services/Aquarius.ts +++ b/src/services/Aquarius.ts @@ -1,6 +1,7 @@ import fetch from 'cross-fetch' -import { LoggerInstance, sleep } from '../utils' +import { LoggerInstance } from '../utils/Logger' import { Asset, DDO, ValidateMetadata } from '../@types' +import { sleep } from '../utils/General' export interface SearchQuery { from?: number diff --git a/src/services/Provider.ts b/src/services/Provider.ts index fd899f73b..e5e497347 100644 --- a/src/services/Provider.ts +++ b/src/services/Provider.ts @@ -1,6 +1,6 @@ import fetch from 'cross-fetch' import { ethers, Signer, providers } from 'ethers' -import { LoggerInstance } from '../utils' +import { LoggerInstance } from '../utils/Logger' import { Arweave, FileInfo, diff --git a/src/utils/OrderUtils.ts b/src/utils/OrderUtils.ts index 6bd08fdd2..c25f0973e 100644 --- a/src/utils/OrderUtils.ts +++ b/src/utils/OrderUtils.ts @@ -1,20 +1,16 @@ import { Signer } from 'ethers' -import { - ProviderInstance, - Datatoken, - Dispenser, - Config, - OrderParams, - Asset, - FreOrderParams, - approve, - FixedRateExchange, - ConsumeMarketFee, - ProviderFees, - ZERO_ADDRESS, - approveWei -} from '../index' import Decimal from 'decimal.js' +import { Asset } from '../@types/Asset' +import { Config } from '../config/Config' +import { Datatoken } from '../contracts/Datatoken' +import { ConsumeMarketFee, OrderParams } from '../@types/Datatoken' +import { ProviderFees } from '../@types/Provider' +import { ProviderInstance } from '../services/Provider' +import { ZERO_ADDRESS } from './Constants' +import { approve, approveWei } from './TokenUtils' +import { Dispenser } from '../contracts/Dispenser' +import { FixedRateExchange } from '../contracts/FixedRateExchange' +import { FreOrderParams } from '../@types/FixedPrice' /** * Orders an asset based on the specified pricing schema and configuration. From 60d027294a2893410860f1180ff47177e12e45de Mon Sep 17 00:00:00 2001 From: paulo-ocean Date: Fri, 29 Nov 2024 15:47:22 +0000 Subject: [PATCH 5/6] done --- src/contracts/NFT.ts | 2 +- src/utils/Adresses.ts | 90 ++++++++++++++++++++++++++++++++++++++++++ src/utils/Assets.ts | 92 +------------------------------------------ 3 files changed, 93 insertions(+), 91 deletions(-) create mode 100644 src/utils/Adresses.ts 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 From 8533904f4c3c523f2f1eb6eca4dec201dab8bd28 Mon Sep 17 00:00:00 2001 From: paulo-ocean Date: Fri, 29 Nov 2024 16:05:07 +0000 Subject: [PATCH 6/6] fix import on test --- test/unit/AssetUtils.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unit/AssetUtils.test.ts b/test/unit/AssetUtils.test.ts index 0c46387aa..3fcd6c924 100644 --- a/test/unit/AssetUtils.test.ts +++ b/test/unit/AssetUtils.test.ts @@ -1,7 +1,8 @@ import { assert } from 'chai' import { KNOWN_CONFIDENTIAL_EVMS } from '../../src/config' import { provider, getAddresses } from '../config' -import { calculateActiveTemplateIndex, useOasisSDK } from '../../src/utils' +import { calculateActiveTemplateIndex } from '../../src/utils/Adresses' +import { useOasisSDK } from '../../src/utils/Assets' import { Signer } from 'ethers/lib/ethers' let nftOwner: Signer