Skip to content

[react-core] Add hook: useTransferNativeToken #2008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stupid-toes-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/react-core": patch
---

Add hook for transferring native token
48 changes: 47 additions & 1 deletion packages/react-core/src/evm/hooks/async/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -13,6 +13,7 @@ import {
} from "../../types";
import {
cacheKeys,
invalidateBalances,
invalidateContractAndBalances,
} from "../../utils/cache-keys";
import { useQueryWithNetwork } from "../query-utils/useQueryWithNetwork";
Expand Down Expand Up @@ -329,6 +330,51 @@ export function useTransferToken(contract: RequiredParam<TokenContract>) {
);
}

/**
* 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 (
* <button
* disabled={isLoading}
* onClick={() => transferNativeToken({ to: "{{wallet_address}}", amount: "0.1" })}
* >
* Transfer
* </button>
* );
* };
* ```
*
* @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
*
Expand Down
1 change: 1 addition & 0 deletions packages/react-core/src/evm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export {
useTransferToken,
useTransferBatchToken,
useBurnToken,
useTransferNativeToken,
} from "./hooks/async/token";

// account factory
Expand Down
16 changes: 13 additions & 3 deletions packages/react-core/src/evm/utils/cache-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,22 @@ export function invalidateContractAndBalances(
),
),
),
queryClient.invalidateQueries(
enforceCachePrefix(createCacheKeyWithNetwork(["balance"], chainId)),
),
invalidateBalances(queryClient, chainId),
]);
}

/**
* @internal
*/
export function invalidateBalances(
queryClient: QueryClient,
chainId: RequiredParam<SUPPORTED_CHAIN_ID>,
) {
return queryClient.invalidateQueries(
enforceCachePrefix(createCacheKeyWithNetwork(["balance"], chainId)),
);
}

/**
@internal
*/
Expand Down