-
Notifications
You must be signed in to change notification settings - Fork 0
/
soroban.ts
104 lines (93 loc) · 3.59 KB
/
soroban.ts
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { SorobanRpc, xdr } from 'stellar-sdk';
import { DEFAULT_NETWORK, NetworkDetails } from '../lib/network';
import {
BASE_FEE,
PLACEHOLDER_ACCOUNT,
getEstimatedFee,
getServer,
initaliseSorosanTransactionBuilder,
} from '../lib/soroban';
export class Soroban {
/**
* @ignore
* The currently selected network details.
*/
selectedNetwork: NetworkDetails;
/**
* @ignore
* The Soroban server instance.
*/
server: SorobanRpc.Server;
/**
* @ignore
* The active public key.
*/
publicKey: string;
constructor(selectedNetwork: NetworkDetails, activePublicKey?: string) {
this.selectedNetwork = selectedNetwork || DEFAULT_NETWORK;
this.server = getServer(selectedNetwork);
this.publicKey = activePublicKey || "";
}
/**
* @ignore
* Initialize a transaction builder.
*
* @param {string} [pubKey] - The public key to use for the transaction. If not provided, the activePublicKey will be used.
* @param {string} [fee] - The fee for the transaction. If not provided, the BASE_FEE will be used.
* @returns {Promise<TxBuilder>} A Promise that resolves to a transaction builder.
*/
protected async initaliseTransactionBuilder(publicKey?: string, fee?: string) {
// Use the provided pubKey or the activePublicKey if not provided
const usedPublicKey = publicKey || this.publicKey;
// Get the network information
const network = await this.server.getNetwork();
// Get the transaction builder
return await initaliseSorosanTransactionBuilder(
usedPublicKey,
fee ? fee.toString() : BASE_FEE,
this.server,
network.passphrase,
);
}
/**
* Helper function to estimate the gas cost of a contract call. This estimation is done by creating a
* transaction builder and simulating the transaction using the Soroban network. The gas cost is
* calculated as the sum of the base fee and the fee obtained from the simulated transaction.
*
* @param {string} contractAddress - The contract address to estimate gas for.
* @param {string} method - The name of the method to be called on the contract.
* @param {xdr.ScVal[]} args - An array of ScVal arguments to pass to the method.
*
* @returns {Promise<string>} A promise that resolves to a string representation of the estimated gas
* cost for the contract call.
*
* @example
* const gasEstimation = await sdk.estimateGas(
* "CCV3ODCHRVCUQTWJZ7F7SLKHGT3JLYWUVHAWMKIYQVSCKMGSOCOJ3AUO",
* "init",
* [xdr.scVal.scvString("Hello World"), new Address("GB...").toScAddress(), ...]
* );
*
* const gasCostInStroops: number = parseInt(gasEstimation); // Convert to a number if needed.
*/
protected async calculateEstimateGas(
contractAddress: string,
method: string,
args: xdr.ScVal[]
): Promise<string> {
// Create a transaction builder with a placeholder account (no actual account used).
const txBuilder = await this.initaliseTransactionBuilder(PLACEHOLDER_ACCOUNT);
// Get the estimated gas cost by simulating the transaction.
return await getEstimatedFee(
contractAddress,
txBuilder,
this.server,
"",
method,
args
);
}
protected set setPublicKey(publicKey: string) {
this.publicKey = publicKey;
}
}