-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathserver.ts
More file actions
46 lines (40 loc) · 1.18 KB
/
Copy pathserver.ts
File metadata and controls
46 lines (40 loc) · 1.18 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
import {
Transaction,
rpc as SorobanRpc,
scValToNative,
BASE_FEE,
} from "stellar-sdk";
import { NetworkDetails } from "@shared/constants/stellar";
import { getSdk } from "@shared/helpers/stellar";
export const simulateTx = async <ArgType>(
tx: Transaction,
server: SorobanRpc.Server,
): Promise<ArgType> => {
const simulatedTX = await server.simulateTransaction(tx);
if ("result" in simulatedTX && simulatedTX.result !== undefined) {
return scValToNative(simulatedTX.result.retval);
}
throw new Error("Invalid response from simulateTransaction");
};
export const buildSorobanServer = (
serverUrl: string,
networkPassphrase: string,
) => {
const Sdk = getSdk(networkPassphrase);
return new Sdk.rpc.Server(serverUrl, {
allowHttp: serverUrl.startsWith("http://"),
});
};
export const getNewTxBuilder = async (
publicKey: string,
networkDetails: NetworkDetails,
server: SorobanRpc.Server,
fee = BASE_FEE,
) => {
const Sdk = getSdk(networkDetails.networkPassphrase);
const sourceAccount = await server.getAccount(publicKey);
return new Sdk.TransactionBuilder(sourceAccount, {
fee,
networkPassphrase: networkDetails.networkPassphrase,
});
};