diff --git a/src/context/story.tsx b/src/context/story.tsx index f2648aec..1e2d2ab5 100644 --- a/src/context/story.tsx +++ b/src/context/story.tsx @@ -2,9 +2,9 @@ import { StoryClient } from '@story-protocol/core-sdk' import { PropsWithChildren, createContext, useContext, useEffect, useState } from 'react' import { storyChain } from 'src/services/wagmi/config' import { defaultNftContractAbi } from 'src/utils/defaultNftContractAbi' -import { Address, createPublicClient, createWalletClient, custom } from 'viem' +import { Address, createPublicClient, createWalletClient, custom, http } from 'viem' -const storyChainId = '1513' +const storyChainId = '0x5e9' interface StoryContextType { txLoading: boolean @@ -42,8 +42,39 @@ export default function StoryProvider({ children }: PropsWithChildren) { const initializeStoryClient = async () => { try { - if (!window.ethereum) return + const chainId = await window.ethereum.request({ method: 'eth_chainId' }) + console.log(chainId) + if (chainId != storyChainId) { + if (window.ethereum) { + try { + await window.ethereum.request({ + method: 'wallet_addEthereumChain', + params: [ + { + chainId: storyChainId, + chainName: 'Story Iliad Network', + nativeCurrency: { + name: 'IP', + symbol: 'IP', + decimals: 18, + }, + rpcUrls: ['https://testnet.storyrpc.io/'], + blockExplorerUrls: ['https://testnet.storyscan.xyz/'], + }, + ], + }) + await window.ethereum.request({ + method: 'wallet_switchEthereumChain', + params: [{ chainId: storyChainId }], + }) + } catch (error) { + console.error(error) + } + } else { + console.log('Ethereum provider not found') + } + } if (!client || !walletAddress) { const [account]: [Address] = await window.ethereum.request({ method: 'eth_requestAccounts', @@ -51,13 +82,12 @@ export default function StoryProvider({ children }: PropsWithChildren) { const config = { account: account, transport: custom(window.ethereum), - chainId: '1513', + chainId: 'iliad', } as any const client = StoryClient.newClient(config) setWalletAddress(account) setClient(client) } - const chainId = await window.ethereum.request({ method: 'eth_chainId' }) if (chainId !== storyChainId) { await window.ethereum.request({ method: 'wallet_switchEthereumChain', @@ -65,7 +95,7 @@ export default function StoryProvider({ children }: PropsWithChildren) { }) } } catch (error) { - console.error(error) + console.error(error) } } diff --git a/src/pages/launchpad/[slug]/collectionDetail.tsx b/src/pages/launchpad/[slug]/collectionDetail.tsx index d89828ae..10db30c2 100644 --- a/src/pages/launchpad/[slug]/collectionDetail.tsx +++ b/src/pages/launchpad/[slug]/collectionDetail.tsx @@ -31,7 +31,6 @@ import { useWaitForTransactionReceipt, useWriteContract, } from 'wagmi' - export default function Page(props) { if (props.justHead) { return <> @@ -105,16 +104,6 @@ function CollectionDetail() { } }) - useEffect(() => { - if (chainId != 11155111 && isConnected) { - switchChain( - { - chainId: 11155111, - }, - { onSuccess: (data) => console.log(data || 'switch chain'), onError: (data) => console.error(data) } - ) - } - }, [chainId, isConnected]) useEffect(() => { console.log('hash result', result) if (result.data && hash) { @@ -160,17 +149,11 @@ function CollectionDetail() { address: launchpadContractAddress, abi, functionName: 'mintNFT', - args: [ - address, - data.ip_asset_id, - BigInt(amount), - usdtAddress, - BigInt(amount * data.mint_price), - ], + args: [address, data.ip_asset_id, BigInt(amount), usdtAddress, BigInt(amount * data.mint_price)], }) setTimeout(() => { refetch() - }, 5000) + }, 20000) setHash(hash) } catch (error) { setProcessingText('') @@ -184,6 +167,7 @@ function CollectionDetail() { try { setConfirmOpen(false) setProcessingText(t('Please confirm the transaction in your wallet')) + if (allowanceBalance < amount * data.mint_price) { await writeContractAsync({ address: usdtAddress, @@ -191,7 +175,10 @@ function CollectionDetail() { functionName: 'approve', args: [launchpadContractAddress, '9999999999999999999999999999999999999999999999999999999999'], }) - setTimeout(mintNFT, 5000) + setTimeout(async () => { + await allowance.refetch() + mintNFT() + }, 20000) } else { mintNFT() } diff --git a/src/pages/profile/launchpad/[id]/launchpadDetail.tsx b/src/pages/profile/launchpad/[id]/launchpadDetail.tsx index 9f45dff4..fb06c712 100644 --- a/src/pages/profile/launchpad/[id]/launchpadDetail.tsx +++ b/src/pages/profile/launchpad/[id]/launchpadDetail.tsx @@ -183,7 +183,7 @@ function LaunchpadDetail({ licensorIpId: launchpad.data.ip_asset_id, licenseTermsId: registerTermResponse.licenseTermsId, amount: launchpad.data.max_supply, - receiver: address, + receiver: launchpadContractAddress, }) console.log(`License minted`) const hash = await writeContractAsync({ diff --git a/src/pages/register_ip_asset/index.tsx b/src/pages/register_ip_asset/index.tsx index 1c23614d..6509014f 100644 --- a/src/pages/register_ip_asset/index.tsx +++ b/src/pages/register_ip_asset/index.tsx @@ -54,15 +54,19 @@ function RegisterIPAssets() { idRef.current = setTimeout(() => registerExistingNFT(tokenId, nftContract), 60000) try { setTxLoading(true) + console.log('start reg') let ipId = await client.ipAsset.ipAssetRegistryClient.ipId({ - chainId: BigInt(11155111), + chainId: BigInt(storyChain.id), tokenContract: nftContract, tokenId: BigInt(tokenId), }) + console.log('ipId', ipId) let isReg = await client.ipAsset.ipAssetRegistryClient.isRegistered({ id: ipId, }) + console.log('isReg', isReg) if (!isReg) { + console.log('start reg tx') await client.ipAsset.register({ nftContract, tokenId,