1- // hooks/transactions/useContractRead.ts
2-
31import { useState , useEffect } from "react" ;
42import { PublicKey } from "@solana/web3.js" ;
53import useLocalSolana from "./useLocalSolana" ;
4+ import { BN } from "@coral-xyz/anchor" ;
5+
6+ interface ContractReadResult {
7+ data : any ;
8+ fee : bigint | null ;
9+ loadingContract : boolean ;
10+ }
611
712export const useContractRead = (
8- contractAddress : string ,
13+ contractAddress : string | null ,
914 method : string
10- ) => {
15+ ) : ContractReadResult => {
16+ const [ fee , setFee ] = useState < bigint | null > ( null ) ;
1117 const [ data , setData ] = useState < any > ( null ) ;
12- const [ loading , setLoading ] = useState ( true ) ;
13- const [ error , setError ] = useState < string | null > ( null ) ;
14- const { program, connection, getConnection } = useLocalSolana ( ) ;
18+ const [ loadingContract , setLoading ] = useState ( true ) ;
19+ const { program, getConnection, getEscrowStatePDA, isConnectionReady } = useLocalSolana ( ) ;
1520
1621 useEffect ( ( ) => {
1722 const fetchData = async ( ) => {
23+
24+ // If contractAddress is null, skip fetching
25+ if ( ! contractAddress ) {
26+ setLoading ( false ) ;
27+ return ;
28+ }
29+
30+ // Check if connection is ready
31+ if ( ! isConnectionReady ) {
32+ console . warn ( "Connection is not ready" ) ;
33+ setLoading ( false ) ;
34+ return ;
35+ }
36+
37+ // Check if connection and program are available
38+ if ( ! program ) {
39+ console . warn ( "Program not established" ) ;
40+ setLoading ( false ) ;
41+ return ;
42+ }
43+
44+ // Check if contract address is provided
45+ if ( ! contractAddress ) {
46+ console . warn ( "Contract address is required" ) ;
47+ setLoading ( false ) ;
48+ return ;
49+ }
50+
1851 try {
1952 setLoading ( true ) ;
2053
21- if ( ! connection ) {
22- throw new Error ( "Connection not established" ) ;
54+ let publicKey : PublicKey ;
55+ if ( method === "escrowState" ) {
56+ // Get the PDA for the escrow state
57+ const escrowStateAddress = getEscrowStatePDA ( contractAddress ) ;
58+ if ( ! escrowStateAddress ) {
59+ console . warn ( "Unable to find LocalSolana account" ) ;
60+ setLoading ( false ) ;
61+ return ;
62+ }
63+ publicKey = escrowStateAddress ;
64+ } else {
65+ publicKey = new PublicKey ( contractAddress ) ;
2366 }
2467
25- if ( ! contractAddress ) {
26- throw new Error ( "Contract address is required" ) ;
27- }
68+ console . log ( "Fetching account info for publicKey:" , publicKey . toBase58 ( ) ) ;
2869
29- const publicKey = new PublicKey ( contractAddress ) ;
70+ // Fetch account information
3071 const accountInfo = await getConnection ( ) . getAccountInfo ( publicKey ) ;
31-
3272 if ( ! accountInfo ?. data ) {
33- throw new Error ( "Unable to retrieve account information" ) ;
73+ console . warn ( "Unable to retrieve account information" ) ;
74+ setLoading ( false ) ;
75+ return ;
3476 }
3577
36- // Decode data using the Anchor program
37- const decodedData = program ?. account [ method ] ?. coder ?. accounts . decode (
38- method ,
39- accountInfo . data
40- ) ;
78+ // Decode data based on the method
79+ let decodedData ;
80+ switch ( method ) {
81+ case "escrow" :
82+ decodedData = program . account . escrow . coder . accounts . decode (
83+ "escrow" ,
84+ accountInfo . data
85+ ) ;
86+ if ( decodedData ?. fee ) {
87+ setFee ( BigInt ( decodedData . fee . toString ( ) ) ) ;
88+ }
89+ break ;
4190
42- setData ( decodedData || null ) ;
91+ case "escrowState" :
92+ decodedData = program . account . escrowState . coder . accounts . decode (
93+ "escrowState" ,
94+ accountInfo . data
95+ ) ;
96+
97+ if ( decodedData ?. fee ) {
98+ setFee ( BigInt ( decodedData . fee . toString ( ) ) ) ;
99+ }
100+
101+ setData ( decodedData ) ;
102+
103+ default :
104+ console . warn ( `Unsupported method: ${ method } ` ) ;
105+ setLoading ( false ) ;
106+ return ;
107+ }
43108 } catch ( err : any ) {
44109 console . error ( "Error fetching contract data:" , err . message ) ;
45- setError ( err . message ) ;
110+ setFee ( null ) ;
46111 setData ( null ) ;
47112 } finally {
48113 setLoading ( false ) ;
49114 }
50115 } ;
51116
52117 fetchData ( ) ;
53- } , [ contractAddress , method , connection ] ) ;
118+ } , [ contractAddress , method , program , isConnectionReady ] ) ;
54119
55- return { data , loading , error } ;
56- } ;
120+ return { fee , data , loadingContract } ;
121+ } ;
0 commit comments