Skip to content

Commit

Permalink
unsafeSend
Browse files Browse the repository at this point in the history
  • Loading branch information
jordankzf committed Jul 25, 2023
1 parent 819ae85 commit 3a30674
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions packages/sado-connect/src/hooks/useSend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import { Psbt } from "bitcoinjs-lib";

type SendFunction = (
address: string,
satoshis: number
satoshis: number,
feeRate?: number
) => Promise<string | null>;

export function useSend(): [SendFunction, string | null, boolean] {
const { wallet, network, address, publicKey } = useSadoContext();
const { wallet, network, address, publicKey, safeMode } = useSadoContext();
const [error, setError] = useState<string | null>(null);
const [loading, setLoading] = useState<boolean>(false);

const send: SendFunction = async (toAddress, satoshis) => {
const safeSend: SendFunction = async (toAddress, satoshis) => {
setLoading(true);
try {
setError(null);
Expand Down Expand Up @@ -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];
}

0 comments on commit 3a30674

Please sign in to comment.