-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf94e10
commit 60d0272
Showing
3 changed files
with
93 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters