diff --git a/.changeset/stupid-toes-pull.md b/.changeset/stupid-toes-pull.md new file mode 100644 index 00000000000..9f35b327d3f --- /dev/null +++ b/.changeset/stupid-toes-pull.md @@ -0,0 +1,5 @@ +--- +"@thirdweb-dev/react-core": patch +--- + +Add hook for transferring native token diff --git a/packages/react-core/src/evm/hooks/async/token.ts b/packages/react-core/src/evm/hooks/async/token.ts index b7e44b4aa2e..a1141f3b116 100644 --- a/packages/react-core/src/evm/hooks/async/token.ts +++ b/packages/react-core/src/evm/hooks/async/token.ts @@ -2,7 +2,7 @@ import { requiredParamInvariant, RequiredParam, } from "../../../core/query-utils/required-param"; -import { useSDKChainId } from "../useSDK"; +import { useSDK, useSDKChainId } from "../useSDK"; import { ClaimTokenParams, getErc20, @@ -13,6 +13,7 @@ import { } from "../../types"; import { cacheKeys, + invalidateBalances, invalidateContractAndBalances, } from "../../utils/cache-keys"; import { useQueryWithNetwork } from "../query-utils/useQueryWithNetwork"; @@ -329,6 +330,51 @@ export function useTransferToken(contract: RequiredParam) { ); } +/** + * A hook to transfer native token (of the active chain) to another wallet + * + * @example + * ```jsx + * const Component = () => { + * const { + * mutate: transferNativeToken, + * isLoading, + * error, + * } = useTransferNativeToken(); + * + * if (error) { + * console.error("failed to transfer tokens", error); + * } + * + * return ( + * + * ); + * }; + * ``` + * + * @returns a mutation object that can be used to transfer native tokens + */ +export function useTransferNativeToken() { + const sdk = useSDK(); + const activeChainId = useSDKChainId(); + const queryClient = useQueryClient(); + return useMutation( + (data: TokenParams) => { + const { to, amount } = data; + invariant(sdk, "SDK is not initialized"); + return sdk.wallet.transfer(to, amount); + }, + { + onSettled: () => invalidateBalances(queryClient, activeChainId), + }, + ); +} + /** * Airdrop tokens to a list of wallets * diff --git a/packages/react-core/src/evm/index.ts b/packages/react-core/src/evm/index.ts index ada5fdc893c..c589d707f05 100644 --- a/packages/react-core/src/evm/index.ts +++ b/packages/react-core/src/evm/index.ts @@ -193,6 +193,7 @@ export { useTransferToken, useTransferBatchToken, useBurnToken, + useTransferNativeToken, } from "./hooks/async/token"; // account factory diff --git a/packages/react-core/src/evm/utils/cache-keys.ts b/packages/react-core/src/evm/utils/cache-keys.ts index 2acb9fff57c..eaf3b158be4 100644 --- a/packages/react-core/src/evm/utils/cache-keys.ts +++ b/packages/react-core/src/evm/utils/cache-keys.ts @@ -58,12 +58,22 @@ export function invalidateContractAndBalances( ), ), ), - queryClient.invalidateQueries( - enforceCachePrefix(createCacheKeyWithNetwork(["balance"], chainId)), - ), + invalidateBalances(queryClient, chainId), ]); } +/** + * @internal + */ +export function invalidateBalances( + queryClient: QueryClient, + chainId: RequiredParam, +) { + return queryClient.invalidateQueries( + enforceCachePrefix(createCacheKeyWithNetwork(["balance"], chainId)), + ); +} + /** @internal */