Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
imhson committed Sep 12, 2024
1 parent 72d369e commit d563025
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
42 changes: 36 additions & 6 deletions src/context/story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,30 +42,60 @@ 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',
})
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',
params: [{ chainId: storyChainId }],
})
}
} catch (error) {
console.error(error)
console.error(error)
}
}

Expand Down
27 changes: 7 additions & 20 deletions src/pages/launchpad/[slug]/collectionDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
useWaitForTransactionReceipt,
useWriteContract,
} from 'wagmi'

export default function Page(props) {
if (props.justHead) {
return <></>
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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('')
Expand All @@ -184,14 +167,18 @@ function CollectionDetail() {
try {
setConfirmOpen(false)
setProcessingText(t('Please confirm the transaction in your wallet'))

if (allowanceBalance < amount * data.mint_price) {
await writeContractAsync({
address: usdtAddress,
abi: usdtAbi,
functionName: 'approve',
args: [launchpadContractAddress, '9999999999999999999999999999999999999999999999999999999999'],
})
setTimeout(mintNFT, 5000)
setTimeout(async () => {
await allowance.refetch()
mintNFT()
}, 20000)
} else {
mintNFT()
}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/profile/launchpad/[id]/launchpadDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
6 changes: 5 additions & 1 deletion src/pages/register_ip_asset/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d563025

Please sign in to comment.