forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseRpcPrepareTx.ts
More file actions
56 lines (51 loc) · 1.42 KB
/
Copy pathuseRpcPrepareTx.ts
File metadata and controls
56 lines (51 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* eslint-disable @typescript-eslint/only-throw-error */
import { useMutation } from "@tanstack/react-query";
import { rpc as StellarRpc, TransactionBuilder } from "@stellar/stellar-sdk";
import { isEmptyObject } from "../util/isEmptyObject";
import {
NetworkHeaders,
PrepareRpcErrorResponse,
PrepareRpcResponse,
} from "../types/types";
type PrepareRpcTxProps = {
rpcUrl: string;
transactionXdr: string;
networkPassphrase: string;
headers: NetworkHeaders;
};
// RPC's prepareTransaction method handles both
// simulating and assembling transactions
export const useRpcPrepareTx = () => {
const mutation = useMutation<
PrepareRpcResponse,
PrepareRpcErrorResponse,
PrepareRpcTxProps
>({
mutationFn: async ({
rpcUrl,
transactionXdr,
networkPassphrase,
headers,
}: PrepareRpcTxProps) => {
try {
const transaction = TransactionBuilder.fromXDR(
transactionXdr,
networkPassphrase,
);
const rpcServer = new StellarRpc.Server(rpcUrl, {
headers: isEmptyObject(headers) ? undefined : { ...headers },
allowHttp: new URL(rpcUrl).hostname === "localhost",
});
const preparedTx = await rpcServer.prepareTransaction(transaction);
return {
transactionXdr: preparedTx.toXDR(),
};
} catch (e) {
throw {
result: e,
};
}
},
});
return mutation;
};