From 57ffc127930a4ed2eecd8e6159c005777ee9c20f Mon Sep 17 00:00:00 2001 From: paulo-ocean Date: Thu, 5 Sep 2024 12:42:21 +0100 Subject: [PATCH] wip: calculate template index --- src/utils/Assets.ts | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/utils/Assets.ts b/src/utils/Assets.ts index f8972b7e..b9e28b0f 100644 --- a/src/utils/Assets.ts +++ b/src/utils/Assets.ts @@ -73,9 +73,30 @@ export function getOceanArtifactsAdressesByChainId(chain: number): any { return null } -export async function calculateTemplateIndex(chainID: number): Promise { +async function calculateTemplateIndex( + chainID: number, + template: string | number +): Promise { + let index = -1 const artifacts = await getOceanArtifactsAdressesByChainId(chainID) - return -1 + 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.indexOf(template) !== -1 + }) + } else { + const templateIndexes = Object.keys(templatesAvailable) + index = templateIndexes.findIndex(function (item) { + return item.indexOf(template.toString()) !== -1 + }) + } + } + } + return index } /** * @@ -122,6 +143,11 @@ export async function createAsset( const config = new ConfigHelper().getConfig(parseInt(String(chainID))) + const templateIndex = await calculateTemplateIndex(chainID, template) + if (templateIndex < 1) { + throw new Error(`Invalid template index: ${templateIndex}`) + } + const nft = new Nft(owner, chainID) const nftFactory = new NftFactory(nftContractAddress, owner)