useFlowScript

useFlowScript

Hook for executing Flow scripts and retrieving data from the Flow blockchain.

Import

import { useFlowScript } from '@doodlesteam/flooks';

Usage

const MyComponent = () => {
  const { data, isLoading, error } = useFlowScript<string[]>({
    code: `
    import NonFungibleToken from 0x...
    import Doodles from 0x...
 
    pub fun main(address: Address): [UInt64] {
        let account = getAccount(address)
 
        let collectionRef = account
            .getCapability(Doodles.CollectionPublicPath)
            .borrow<&{NonFungibleToken.CollectionPublic}>()
            ?? panic("Could not borrow capability from public collection")
 
        return collectionRef.getIDs()
    }`,
    args: ['0xf8d6e0586b0a20c7'],
  });
 
  if (isLoading) return <div>Loading...</div>;
  if (error) return <div>Error: {error.message}</div>;
 
  return <div>Script Result: {JSON.stringify(data)}</div>;
};

API Reference

Props

useFlowScript accepts a UseFlowScriptProps object:

  • code: string - The Cadence script code to be executed.
  • args: FlowType[] (optional) - Arguments to be passed to the script.
  • scopeKey: string (optional) - A unique key to cache the query. Acts as the queryKey in @tanstack/react-query, but it already includes your Flow script and args.
  • select: (data: TQueryFnData) => TData (optional) - A function to transform or select a part of the returned data.
  • Additional properties from UseQueryOptions of @tanstack/react-query for more advanced query handling.

Return Value

The hook returns a UseQueryResult object, which includes:

  • data: The result of the script execution.
  • isLoading: A boolean indicating if the query is in progress.
  • error: An error object if the query failed.
  • refetch: A function to refetch the query.