From cf928a38dced281b255d8794c36b9a1544faac6a Mon Sep 17 00:00:00 2001 From: Cryptoryda <113293883+cryptoryda@users.noreply.github.com> Date: Thu, 2 Jan 2025 16:21:59 +0000 Subject: [PATCH] fix(disputelp.ts) - input validation, improved loging --- scripts/disputeIp.ts | 83 ++++++++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 29 deletions(-) diff --git a/scripts/disputeIp.ts b/scripts/disputeIp.ts index a1a4f9e..3398e38 100644 --- a/scripts/disputeIp.ts +++ b/scripts/disputeIp.ts @@ -3,27 +3,21 @@ import { Address, http, toHex } from 'viem' import { mintNFT } from './utils/mintNFT' import { NFTContractAddress, RPCProviderUrl, account } from './utils/utils' -// BEFORE YOU RUN THIS FUNCTION: Make sure to read the README which contains -// instructions for running this "Simple Mint and Register" example. +// BEFORE RUNNING: Ensure proper setup as per the README instructions. -const main = async function () { - // 1. Set up your Story Config - // - // Docs: https://docs.story.foundation/docs/typescript-sdk-setup - const config: StoryConfig = { - account: account, - transport: http(RPCProviderUrl), - chainId: 'odyssey', - } - const client = StoryClient.newClient(config) +const validateSetup = () => { + if (!RPCProviderUrl) throw new Error('RPCProviderUrl is not set.') + if (!NFTContractAddress) throw new Error('NFTContractAddress is not set.') + if (!account || !account.address) throw new Error('Account configuration is missing.') +} - // 2. Register an IP Asset - // - // Docs: https://docs.story.foundation/docs/register-an-nft-as-an-ip-asset - const tokenId = await mintNFT(account.address, 'test-uri') - const ipResponse: RegisterIpAndAttachPilTermsResponse = await client.ipAsset.registerIpAndAttachPilTerms({ +const registerIpAsset = async ( + client: StoryClient, + tokenId: string // Ensure this expects a string +): Promise => { + return await client.ipAsset.registerIpAndAttachPilTerms({ nftContract: NFTContractAddress, - tokenId: tokenId!, + tokenId: tokenId, // Now safely passed as a string terms: [], ipMetadata: { ipMetadataURI: 'test-uri', @@ -33,20 +27,51 @@ const main = async function () { }, txOptions: { waitForTransaction: true }, }) - console.log(`Root IPA created at transaction hash ${ipResponse.txHash}, IPA ID: ${ipResponse.ipId}`) - console.log(`View on the explorer: https://explorer.story.foundation/ipa/${ipResponse.ipId}`) - - // 3. Raise a Dispute - // - // Docs: https://docs.story.foundation/docs/dispute-module - const disputeResponse = await client.dispute.raiseDispute({ - targetIpId: ipResponse.ipId as Address, - // this is "PLAGIARISM" in base32, and is currently the only whitelisted - // tag for protocol v1.2 +} + +const raiseDispute = async (client: StoryClient, ipId: Address) => { + return await client.dispute.raiseDispute({ + targetIpId: ipId, + // "PLAGIARISM" in base32, currently the only whitelisted tag for protocol v1.2 targetTag: '0x504c414749415249534d00000000000000000000000000000000000000000000', cid: 'QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR', }) - console.log(`Dispute raised at transaction hash ${disputeResponse.txHash}, Dispute ID: ${disputeResponse.disputeId}`) +} + +const main = async function () { + try { + // 1. Validate setup + validateSetup() + + // 2. Set up Story Config + console.log('Setting up Story configuration...') + const config: StoryConfig = { + account: account, + transport: http(RPCProviderUrl), + chainId: 'odyssey', // Change if you're using a different chain. + } + const client = StoryClient.newClient(config) + + // 3. Mint an NFT + console.log('Minting NFT...') + const tokenId = await mintNFT(account.address, 'test-uri') + if (!tokenId) throw new Error('Failed to mint NFT.') + + // 4. Register IP Asset + console.log('Registering IP Asset...') + const ipResponse = await registerIpAsset(client, tokenId.toString()) + console.log(`Root IPA created at transaction hash: ${ipResponse.txHash}`) + console.log(`IPA ID: ${ipResponse.ipId}`) + console.log(`View on the explorer: https://explorer.story.foundation/ipa/${ipResponse.ipId}`) + + // 5. Raise a Dispute + console.log('Raising a dispute...') + const disputeResponse = await raiseDispute(client, ipResponse.ipId as Address) + console.log(`Dispute raised at transaction hash: ${disputeResponse.txHash}`) + console.log(`Dispute ID: ${disputeResponse.disputeId}`) + } catch (error) { + console.error('An error occurred:', error) + } } main()