forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseSimulateTx.ts
More file actions
57 lines (52 loc) · 1.34 KB
/
Copy pathuseSimulateTx.ts
File metadata and controls
57 lines (52 loc) · 1.34 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
57
/* eslint-disable @typescript-eslint/only-throw-error */
/* eslint-disable @typescript-eslint/no-unsafe-return */
import { useMutation } from "@tanstack/react-query";
import { NetworkHeaders } from "../types/types";
type SimulateTxProps = {
rpcUrl: string;
transactionXdr: string;
instructionLeeway?: string;
headers: NetworkHeaders;
};
export const useSimulateTx = () => {
const mutation = useMutation({
mutationFn: async ({
rpcUrl,
transactionXdr,
instructionLeeway,
headers,
}: SimulateTxProps) => {
try {
const res = await fetch(rpcUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
...headers,
},
body: JSON.stringify({
jsonrpc: "2.0",
id: 1,
method: "simulateTransaction",
params: {
xdrFormat: "json",
transaction: transactionXdr,
...(instructionLeeway
? {
resourceConfig: {
instructionLeeway: Number(instructionLeeway),
},
}
: {}),
},
}),
});
return await res.json();
} catch (e) {
throw {
result: e,
};
}
},
});
return mutation;
};