-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<SendResponse>; | ||
|
||
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<boolean>(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 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters