Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ocean committed Nov 29, 2024
1 parent bf94e10 commit 60d0272
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 91 deletions.
2 changes: 1 addition & 1 deletion src/contracts/NFT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
90 changes: 90 additions & 0 deletions src/utils/Adresses.ts
Original file line number Diff line number Diff line change
@@ -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<number> {
// 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
}
92 changes: 2 additions & 90 deletions src/utils/Assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'

Expand All @@ -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<number> {
// 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
Expand Down

0 comments on commit 60d0272

Please sign in to comment.