From 3a30674ef1276bd86db9b206244c1d2db02330b6 Mon Sep 17 00:00:00 2001 From: Jordan Kee Date: Tue, 25 Jul 2023 16:14:58 +0800 Subject: [PATCH] unsafeSend --- packages/sado-connect/src/hooks/useSend.tsx | 35 ++++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/packages/sado-connect/src/hooks/useSend.tsx b/packages/sado-connect/src/hooks/useSend.tsx index f38e08c5..2694cd20 100644 --- a/packages/sado-connect/src/hooks/useSend.tsx +++ b/packages/sado-connect/src/hooks/useSend.tsx @@ -9,15 +9,16 @@ import { Psbt } from "bitcoinjs-lib"; type SendFunction = ( address: string, - satoshis: number + satoshis: number, + feeRate?: number ) => Promise; export function useSend(): [SendFunction, string | null, boolean] { - const { wallet, network, address, publicKey } = useSadoContext(); + const { wallet, network, address, publicKey, safeMode } = useSadoContext(); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); - const send: SendFunction = async (toAddress, satoshis) => { + const safeSend: SendFunction = async (toAddress, satoshis) => { setLoading(true); try { setError(null); @@ -79,5 +80,31 @@ export function useSend(): [SendFunction, string | null, boolean] { } }; - return [send, error, loading]; + const unsafeSend: SendFunction = async (toAddress, satoshis, feeRate) => { + setLoading(true); + try { + setError(null); + if (!address || !publicKey) throw new Error("No wallet is connected"); + + let txId; + if (wallet === Wallet.UNISAT) { + txId = await window.unisat.sendBitcoin(toAddress, satoshis, { + feeRate, + }); + } else if (wallet === Wallet.XVERSE) { + // TO-DO + } else { + throw new Error("No wallet selected"); + } + + setLoading(false); + return txId; + } catch (err: any) { + setError(err.message); + setLoading(false); + return null; + } + }; + + return [safeMode ? safeSend : unsafeSend, error, loading]; }