@suiware/kit
Version:
Opinionated React components and hooks for building Sui dApps.
64 lines (61 loc) • 1.99 kB
JavaScript
import { __async } from './chunk-IQXHJV5O.mjs';
import { useSuiClientContext, useCurrentAccount } from '@mysten/dapp-kit';
import { formatAddress } from '@mysten/sui/utils';
import { requestSuiFromFaucetV1, getFaucetHost } from '@mysten/sui/faucet';
var fundAddress = (address, network) => __async(void 0, null, function* () {
return yield requestSuiFromFaucetV1({
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(void 0, 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 { error } = yield fundAddress(
fundedAddress,
ctx.network
);
if (error) {
onError != null && onError(
new Error(error),
"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-BP5VBWVD.mjs.map
//# sourceMappingURL=chunk-BP5VBWVD.mjs.map