Skip to content

Commit 23db97c

Browse files
author
Kien Ngo
authored
[react-core] Add hook: useTransferNativeToken (#2008)
1 parent 0f54e99 commit 23db97c

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

.changeset/stupid-toes-pull.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@thirdweb-dev/react-core": patch
3+
---
4+
5+
Add hook for transferring native token

packages/react-core/src/evm/hooks/async/token.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
requiredParamInvariant,
33
RequiredParam,
44
} from "../../../core/query-utils/required-param";
5-
import { useSDKChainId } from "../useSDK";
5+
import { useSDK, useSDKChainId } from "../useSDK";
66
import {
77
ClaimTokenParams,
88
getErc20,
@@ -13,6 +13,7 @@ import {
1313
} from "../../types";
1414
import {
1515
cacheKeys,
16+
invalidateBalances,
1617
invalidateContractAndBalances,
1718
} from "../../utils/cache-keys";
1819
import { useQueryWithNetwork } from "../query-utils/useQueryWithNetwork";
@@ -323,6 +324,51 @@ export function useTransferToken(contract: RequiredParam<TokenContract>) {
323324
);
324325
}
325326

327+
/**
328+
* A hook to transfer native token (of the active chain) to another wallet
329+
*
330+
* @example
331+
* ```jsx
332+
* const Component = () => {
333+
* const {
334+
* mutate: transferNativeToken,
335+
* isLoading,
336+
* error,
337+
* } = useTransferNativeToken();
338+
*
339+
* if (error) {
340+
* console.error("failed to transfer tokens", error);
341+
* }
342+
*
343+
* return (
344+
* <button
345+
* disabled={isLoading}
346+
* onClick={() => transferNativeToken({ to: "{{wallet_address}}", amount: "0.1" })}
347+
* >
348+
* Transfer
349+
* </button>
350+
* );
351+
* };
352+
* ```
353+
*
354+
* @returns a mutation object that can be used to transfer native tokens
355+
*/
356+
export function useTransferNativeToken() {
357+
const sdk = useSDK();
358+
const activeChainId = useSDKChainId();
359+
const queryClient = useQueryClient();
360+
return useMutation(
361+
(data: TokenParams) => {
362+
const { to, amount } = data;
363+
invariant(sdk, "SDK is not initialized");
364+
return sdk.wallet.transfer(to, amount);
365+
},
366+
{
367+
onSettled: () => invalidateBalances(queryClient, activeChainId),
368+
},
369+
);
370+
}
371+
326372
/**
327373
* Airdrop tokens to a list of wallets
328374
*

packages/react-core/src/evm/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ export {
195195
useTransferToken,
196196
useTransferBatchToken,
197197
useBurnToken,
198+
useTransferNativeToken,
198199
} from "./hooks/async/token";
199200

200201
// account factory

packages/react-core/src/evm/utils/cache-keys.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,22 @@ export function invalidateContractAndBalances(
5858
),
5959
),
6060
),
61-
queryClient.invalidateQueries(
62-
enforceCachePrefix(createCacheKeyWithNetwork(["balance"], chainId)),
63-
),
61+
invalidateBalances(queryClient, chainId),
6462
]);
6563
}
6664

65+
/**
66+
* @internal
67+
*/
68+
export function invalidateBalances(
69+
queryClient: QueryClient,
70+
chainId: RequiredParam<SUPPORTED_CHAIN_ID>,
71+
) {
72+
return queryClient.invalidateQueries(
73+
enforceCachePrefix(createCacheKeyWithNetwork(["balance"], chainId)),
74+
);
75+
}
76+
6777
/**
6878
@internal
6979
*/

0 commit comments

Comments
 (0)