import {
useFundEscrow,
useSendTransaction,
} from "@trustless-work/escrow/hooks";
import {
FundEscrowPayload
} from "@trustless-work/escrow/types";
export const useFundEscrowForm = () => {
/*
* useFundEscrow
*/
const { fundEscrow } = useFundEscrow();
/*
* useSendTransaction
*/
const { sendTransaction } = useSendTransaction();
/*
* onSubmit function, this could be called by form button
*/
const onSubmit = async (payload: FundEscrowPayload) => {
try {
/**
* API call by using the trustless work hooks
* @Note:
* - We need to pass the payload to the fundEscrow function
* - The result will be an unsigned transaction
*/
const { unsignedTransaction } = await fundEscrow(
payload,
"multi-release"
// or ...
// "single-release"
);
if (!unsignedTransaction) {
throw new Error(
"Unsigned transaction is missing from fundEscrow response."
);
}
/**
* @Note:
* - We need to sign the transaction using your [private key] such as wallet
* - The result will be a signed transaction
*/
const signedXdr = await signTransaction({ /* This method should be provided by the wallet */
unsignedTransaction,
address: walletAddress || "",
});
if (!signedXdr) {
throw new Error("Signed transaction is missing.");
}
/**
* @Note:
* - We need to send the signed transaction to the API
* - The data will be an SendTransactionResponse
*/
const data = await sendTransaction(signedXdr);
/**
* @Responses:
* data.status === "SUCCESS"
* - Escrow funded successfully
* - Show a success toast
*
* data.status == "ERROR"
* - Show an error toast
*/
if (data.status === "SUCCESS") {
toast.success("Escrow Funded");
}
} catch (error: unknown) {
// catch error logic
}
};
}