UNPKG

@suiware/kit

Version:

Opinionated React components and hooks for building Sui dApps.

64 lines (61 loc) 2.02 kB
import { __async } from './chunk-IQXHJV5O.mjs'; import { useSuiClientContext, useCurrentAccount } from '@mysten/dapp-kit'; import { formatAddress } from '@mysten/sui/utils'; import { requestSuiFromFaucetV2, getFaucetHost } from '@mysten/sui/faucet'; var fundAddress = (address, network) => __async(null, null, function* () { return yield requestSuiFromFaucetV2({ host: getFaucetHost(network), recipient: address }); }); var getTestnetFaucetLink = (address) => { return `https://faucet.sui.io/?address=${address}`; }; // src/hooks/useFaucet.tsx var useFaucet = ({ onError, onSuccess }) => { const ctx = useSuiClientContext(); const currentAccount = useCurrentAccount(); const fund = (address) => __async(null, null, function* () { if (!["localnet", "devnet", "testnet"].includes(ctx.network)) { onError != null && onError(null, `The ${ctx.network} network does not have a faucet`); return; } const fundedAddress = address == null ? currentAccount == null ? void 0 : currentAccount.address : void 0; if (fundedAddress == null) { onError != null && onError(null, "Please connect your wallet first"); return; } if (ctx.network === "testnet") { window.open(getTestnetFaucetLink(fundedAddress), "_blank"); return; } try { const { status } = yield fundAddress( fundedAddress, ctx.network ); if (status !== "Success") { onError != null && onError( new Error(status.Failure.internal), "Cannot fund the address on this network at the moment" ); } } catch (e) { onError != null && onError(e, "Cannot fund the address"); return; } onSuccess != null && onSuccess( `The ${formatAddress(fundedAddress)} address has been funded successfully` ); }); return { fund }; }; var useFaucet_default = useFaucet; export { useFaucet_default }; //# sourceMappingURL=chunk-543KNPTE.mjs.map //# sourceMappingURL=chunk-543KNPTE.mjs.map