-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96598fc
commit 783f0d9
Showing
3 changed files
with
138 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { useMemo } from "react"; | ||
import { | ||
type AccountInterface, | ||
type Call, | ||
type EstimateFeeDetails, | ||
type EstimateFeeResponse, | ||
type SimulateTransactionResponse, | ||
type SimulateTransactionDetails, | ||
TransactionType, | ||
} from "starknet"; | ||
|
||
import { useAccount, useInvalidateOnBlock } from "@starknet-react/core"; | ||
import { | ||
QueryKey, | ||
useQuery, | ||
UseQueryResult, | ||
type UseQueryOptions as UseQueryOptions_, | ||
} from "@tanstack/react-query"; | ||
|
||
export type SimulateTransactionsArgs = { | ||
/** List of smart contract calls to simulate. */ | ||
calls?: Call[]; | ||
/** Simualte Transaction options. */ | ||
options?: SimulateTransactionDetails; | ||
}; | ||
type UseQueryProps< | ||
TQueryFnData = unknown, | ||
TError = unknown, | ||
TData = TQueryFnData, | ||
TQueryKey extends QueryKey = QueryKey, | ||
> = Pick< | ||
UseQueryOptions_<TQueryFnData, TError, TData, TQueryKey>, | ||
"enabled" | "refetchInterval" | "retry" | "retryDelay" | ||
>; | ||
/** Options for `useSimulateTransactions`. */ | ||
export type UseSimulateTransactionsProps = SimulateTransactionsArgs & | ||
UseQueryProps< | ||
SimulateTransactionResponse, | ||
Error, | ||
SimulateTransactionResponse, | ||
ReturnType<typeof queryKey> | ||
> & { | ||
/** Refresh data at every block. */ | ||
watch?: boolean; | ||
}; | ||
|
||
/** Value returned from `useSimulateTransactions`. */ | ||
export type UseSimulateTransactionsResult = UseQueryResult< | ||
SimulateTransactionResponse, | ||
Error | ||
>; | ||
|
||
/** | ||
* Hook to estimate fees for smart contract calls. | ||
* | ||
* @remarks | ||
* | ||
* The hook only performs estimation if the `calls` is not undefined. | ||
*/ | ||
export function useSimulateTransactions({ | ||
calls, | ||
options, | ||
watch = false, | ||
enabled: enabled_ = true, | ||
...props | ||
}: UseSimulateTransactionsProps): UseSimulateTransactionsResult { | ||
const { account } = useAccount(); | ||
|
||
const queryKey_ = useMemo( | ||
() => queryKey({ calls, options }), | ||
[calls, options], | ||
); | ||
|
||
const enabled = useMemo(() => Boolean(enabled_ && calls), [enabled_, calls]); | ||
|
||
useInvalidateOnBlock({ | ||
enabled: Boolean(enabled && watch), | ||
queryKey: queryKey_, | ||
}); | ||
|
||
return useQuery({ | ||
queryKey: queryKey_, | ||
queryFn: queryFn({ | ||
account, | ||
calls, | ||
options, | ||
}), | ||
enabled, | ||
...props, | ||
}); | ||
} | ||
|
||
function queryKey({ calls, options }: SimulateTransactionsArgs) { | ||
return [ | ||
{ | ||
entity: "simulateTransactions", | ||
calls, | ||
options, | ||
}, | ||
] as const; | ||
} | ||
|
||
function queryFn({ | ||
account, | ||
calls, | ||
options, | ||
}: { account?: AccountInterface } & SimulateTransactionsArgs) { | ||
return async () => { | ||
if (!account) throw new Error("account is required"); | ||
if (!calls || calls.length === 0) throw new Error("calls are required"); | ||
const callMap = calls.map((call) => ({ | ||
type: TransactionType.INVOKE, | ||
...call, | ||
})); | ||
return account?.simulateTransaction(callMap, options); | ||
}; | ||
} |