UNPKG

gogo-movement-starter

Version:

GOGO Movement Network React Starter Template - Complete React + Vite + Move contracts setup

131 lines (115 loc) 3.42 kB
import { useState, useCallback } from 'react' import { movementAptosClient } from '../movement/MovementClient' import useGoGoWallet from './useGoGoWallet' export interface TransactionResult { success: boolean hash?: string error?: string } export function useMovementClient() { const { isConnected } = useGoGoWallet() const [client] = useState(() => movementAptosClient) const [loading, setLoading] = useState(false) // Get account info const getAccountInfo = useCallback(async (address: string) => { if (!address) throw new Error('Address is required') setLoading(true) try { return await client.getAccountInfo(address) } finally { setLoading(false) } }, [client]) // Get account balance const getBalance = useCallback(async (address: string, coinType?: string) => { if (!address) throw new Error('Address is required') setLoading(true) try { return await client.getAccountBalance(address, coinType) } finally { setLoading(false) } }, [client]) // Get account resources const getResources = useCallback(async (address: string) => { if (!address) throw new Error('Address is required') setLoading(true) try { return await client.getAccountResources(address) } finally { setLoading(false) } }, [client]) // View function (read-only) - simplified for Movement Network const viewFunction = useCallback(async (payload: { function: string typeArguments?: string[] functionArguments?: any[] }) => { setLoading(true) try { // For Movement Network, we'll simulate timestamp function if (payload.function === '0x1::timestamp::now_microseconds') { return [Date.now() * 1000] // Current time in microseconds } console.log('View function called:', payload) return ['Simulated result'] } finally { setLoading(false) } }, [client]) // Submit transaction through wallet (simplified for demo) const submitTransaction = useCallback(async (payload: { function: string typeArguments?: string[] functionArguments?: any[] }): Promise<TransactionResult> => { if (!isConnected) { return { success: false, error: 'Wallet not connected' } } setLoading(true) try { // For demo purposes, simulate a successful transaction console.log('Transaction payload:', payload) return { success: true, hash: '0x' + Math.random().toString(16).substr(2, 64) } } catch (error: any) { console.error('Transaction failed:', error) return { success: false, error: error.message || 'Transaction failed' } } finally { setLoading(false) } }, [isConnected]) // Get transaction by hash const getTransaction = useCallback(async (hash: string) => { if (!hash) throw new Error('Transaction hash is required') setLoading(true) try { return await client.getTransaction(hash) } finally { setLoading(false) } }, [client]) return { // Client management client, networkInfo: client.getNetworkConfig(), // State loading, isConnected, // Account operations getAccountInfo, getBalance, getResources, // Transaction operations viewFunction, submitTransaction, getTransaction, } } export default useMovementClient