From 7781a7c34ad6e5aac2315d17902a1de43121ed98 Mon Sep 17 00:00:00 2001 From: veralygit <57183851+veralygit@users.noreply.github.com> Date: Wed, 15 May 2024 15:33:31 +0800 Subject: [PATCH] feat(useSendV2): add useSendV2 hook (#304) * feat(useSendV2): add useSendV2 hook * fix: revert lint * fix: lint * chore: update to named params * fix: useSendV2 function params --- packages/ord-connect/src/hooks/useSendV2.tsx | 88 ++++++++++++++++++++ packages/ord-connect/src/index.ts | 1 + 2 files changed, 89 insertions(+) create mode 100644 packages/ord-connect/src/hooks/useSendV2.tsx diff --git a/packages/ord-connect/src/hooks/useSendV2.tsx b/packages/ord-connect/src/hooks/useSendV2.tsx new file mode 100644 index 00000000..96e165f3 --- /dev/null +++ b/packages/ord-connect/src/hooks/useSendV2.tsx @@ -0,0 +1,88 @@ +import { useCallback, useState } from "react"; +import { JsonRpcDatasource, PSBTBuilder } from "@ordzaar/ordit-sdk"; + +import signPsbt from "../lib/signPsbt"; +import { useOrdConnect } from "../providers/OrdConnectProvider"; + +type SendFunction = (sendParams: SendParams) => Promise; + +type SendParams = { + toAddress: string; + satoshis: number; + feeRate: number; + relay?: boolean; +}; + +type SendResponse = { + txId?: string; + signedPsbtHex?: string; + error?: string; +}; + +export function useSendV2() { + const { wallet, network, address, publicKey } = useOrdConnect(); + const [isLoading, setIsLoading] = useState(false); + + const send: SendFunction = useCallback( + async ({ toAddress, satoshis, feeRate, relay = true }: SendParams) => { + setIsLoading(true); + + try { + if ( + !address || + !address.payments || + !publicKey || + !publicKey.payments || + !wallet + ) { + throw new Error("No wallet is connected"); + } + + const psbtBuilder = new PSBTBuilder({ + address: address.payments, + feeRate, + network, + publicKey: publicKey.payments, + outputs: [ + { + address: toAddress, + value: satoshis, + }, + ], + }); + + await psbtBuilder.prepare(); + + const signedPsbt = await signPsbt({ + address: address.payments, + wallet, + network, + psbt: psbtBuilder.toPSBT(), + }); + + if (relay) { + const datasource = new JsonRpcDatasource({ network }); + const txId = await datasource.relay({ hex: signedPsbt.hex }); + setIsLoading(false); + return { + txId, + }; + } + + setIsLoading(false); + + return { + signedPsbtHex: signedPsbt.hex, + }; + } catch (err) { + setIsLoading(false); + return { + error: (err as Error).message, + }; + } + }, + [address, network, publicKey, wallet], + ); + + return { send, isLoading }; +} diff --git a/packages/ord-connect/src/index.ts b/packages/ord-connect/src/index.ts index 8d8618e2..ba5f1a8f 100644 --- a/packages/ord-connect/src/index.ts +++ b/packages/ord-connect/src/index.ts @@ -5,6 +5,7 @@ export { OrdConnectKit } from "./components/OrdConnectKit"; export { SelectWalletModal } from "./components/SelectWalletModal"; export { useBalance } from "./hooks/useBalance"; export { useSend } from "./hooks/useSend"; +export { useSendV2 } from "./hooks/useSendV2"; export { useSign } from "./hooks/useSign"; export { useSignMessage } from "./hooks/useSignMessage"; export type {