@@ -14,6 +14,7 @@ import { TNTFactoryAbi } from "@/utils/contractsABI/TNTFactory";
1414import { Info } from "lucide-react" ;
1515import { useTheme } from "next-themes" ;
1616import { getPublicClient } from "@wagmi/core" ;
17+ import { decodeEventLog } from "viem" ;
1718
1819interface DeployContractProps {
1920 tokenName : string ;
@@ -22,6 +23,9 @@ interface DeployContractProps {
2223 imageURL : string ;
2324}
2425
26+ const TNTCREATED_EVENT_NOT_FOUND =
27+ "TNTCreated event not found in transaction logs" ;
28+
2529const fields = [
2630 {
2731 id : "tokenName" ,
@@ -157,38 +161,33 @@ export default function CreateTNT() {
157161 console . log ( "Transaction confirmed! Receipt:" , receipt ) ;
158162
159163 if ( receipt . status === "success" ) {
160- // Extract the new TNT contract address from logs
161- let newTNTAddress = null ;
162-
163- // Look for TNTCreated event in logs
164- try {
165- const publicClient = getPublicClient ( config as any , { chainId } ) ;
166- if ( publicClient ) {
167- // Decode logs to find the TNTCreated event
168- for ( const log of receipt . logs ) {
169- if (
170- log . address . toLowerCase ( ) === factoryAddress . toLowerCase ( )
171- ) {
172- // This is likely our TNTCreated event
173- // The first topic after the event signature should be the owner
174- // The data should contain the TNT address
175- if ( log . topics . length > 1 && log . data ) {
176- // Try to decode the TNT address from the log data
177- // For now, we'll use a placeholder - in a real implementation,
178- // you'd properly decode the event logs
179- console . log ( "TNT created successfully, log:" , log ) ;
180- newTNTAddress = log . data ; // This is a simplified approach
181- }
182- }
164+ // Extract the new TNT contract address from TNTCreated event
165+ let newTNTAddress : `0x${string } ` | null = null ;
166+
167+ for ( const log of receipt . logs ) {
168+ if ( log . address . toLowerCase ( ) !== factoryAddress . toLowerCase ( ) )
169+ continue ;
170+ try {
171+ const decoded = decodeEventLog ( {
172+ abi : TNTFactoryAbi ,
173+ data : log . data ,
174+ topics : log . topics ,
175+ } ) ;
176+ if (
177+ decoded . eventName === "TNTCreated" &&
178+ decoded . args &&
179+ "tntAddress" in decoded . args
180+ ) {
181+ newTNTAddress = decoded . args . tntAddress as `0x${string } `;
182+ break ;
183183 }
184+ } catch {
185+ continue ;
184186 }
185- } catch ( logError ) {
186- console . warn ( "Could not decode TNT address from logs:" , logError ) ;
187187 }
188188
189- // If we couldn't get the address from logs, create a placeholder
190189 if ( ! newTNTAddress ) {
191- newTNTAddress = `0x ${ txHash . slice ( 2 , 42 ) } ` ; // Use part of tx hash as placeholder
190+ throw new Error ( TNTCREATED_EVENT_NOT_FOUND ) ;
192191 }
193192
194193 const txDetails = {
@@ -215,10 +214,15 @@ export default function CreateTNT() {
215214 throw new Error ( "Transaction failed" ) ;
216215 }
217216 } catch ( waitError ) {
217+ const isDecodingFailure =
218+ waitError instanceof Error &&
219+ waitError . message === TNTCREATED_EVENT_NOT_FOUND ;
220+ if ( isDecodingFailure ) throw waitError ;
221+
218222 console . error ( "Error waiting for transaction:" , waitError ) ;
219223 toast . error ( "Transaction confirmation failed. Please check manually." ) ;
220224
221- // Still save the transaction attempt
225+ // Still save the transaction attempt (no contractAddress)
222226 const txDetails = {
223227 tokenName : tokenName . trim ( ) ,
224228 tokenSymbol : tokenSymbol . trim ( ) ,
@@ -258,6 +262,10 @@ export default function CreateTNT() {
258262 toast . error (
259263 "Network error. Please check your connection and try again."
260264 ) ;
265+ } else if ( error ?. message === TNTCREATED_EVENT_NOT_FOUND ) {
266+ toast . error (
267+ "Transaction succeeded but the TNT address could not be read from the receipt. Check the block explorer for the contract address."
268+ ) ;
261269 } else {
262270 toast . error (
263271 `Failed to deploy TNT: ${
0 commit comments