UNPKG

@daimo/pay

Version:

Seamless crypto payments. Onboard users from any chain, any coin into your app with one click.

77 lines (74 loc) 2.68 kB
import { worldchainUSDC } from '@daimo/pay-common'; import { MiniKit } from '@worldcoin/minikit-js'; import { useState, useEffect, useCallback } from 'react'; import { getAddress } from 'viem'; import { ROUTES } from '../constants/routes.js'; import { useDaimoPay } from '../hooks/useDaimoPay.js'; import { usePayContext } from '../hooks/usePayContext.js'; import { useWorldSignIn } from './useWorldSignIn.js'; function useWithdrawToWorld() { const { signInWithWorld } = useWorldSignIn(); const [isMiniKitReady, setIsMiniKitReady] = useState(false); const pay = useDaimoPay(); const context = usePayContext(); const { paymentState, log } = context; const [modalOptions, setModalOptions] = useState({ closeOnSuccess: false, resetOnSuccess: false }); useEffect(() => { log("[WORLD] Installing MiniKit"); const result = MiniKit.install(); log("[WORLD] MiniKit install result", result); log("[WORLD] MiniKit is installed", MiniKit.isInstalled()); setIsMiniKitReady(MiniKit.isInstalled()); }, []); const showSpinner = useCallback(() => { log(`[WORLD] showing spinner ${pay.order?.id}`); context.showPayment(modalOptions); context.setRoute(ROUTES.CONFIRMATION); }, [pay.order?.id, modalOptions]); useEffect(() => { if (pay.paymentState === "payment_started") { showSpinner(); } }, [pay.paymentState, showSpinner]); const withdrawToWorld = useCallback( async ({ appId, toUnits, closeOnSuccess = false, resetOnSuccess = false }) => { if (!isMiniKitReady) { console.error("[WORLD_WITHDRAW] MiniKit is not installed"); return null; } const worldUser = await signInWithWorld(); if (!worldUser?.walletAddress) { log("[WORLD_WITHDRAW] user is not signed in"); return null; } await paymentState.setPayParams({ appId, toChain: worldchainUSDC.chainId, toToken: getAddress(worldchainUSDC.token), toUnits, toAddress: getAddress(worldUser.walletAddress), intent: "Withdraw to World App" }); const hydratedState = await pay.hydrateOrder(); const intentAddress = hydratedState.order.intentAddr; log( `[WORLD_WITHDRAW] hydrated order ${pay.order?.id} with intent address ${intentAddress}. Polling for payment` ); setModalOptions({ closeOnSuccess, resetOnSuccess }); return intentAddress; }, // eslint-disable-next-line react-hooks/exhaustive-deps [isMiniKitReady, signInWithWorld, pay, log] ); return { withdrawToWorld }; } export { useWithdrawToWorld }; //# sourceMappingURL=useWithdrawToWorld.js.map