-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.ts
345 lines (321 loc) · 14.1 KB
/
tx.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import { parseError, parseResult } from '@blend-capital/blend-sdk';
import {
Account,
Keypair,
Operation,
SorobanDataBuilder,
SorobanRpc,
TimeoutInfinite,
Transaction,
TransactionBuilder, scValToNative,
xdr,
} from '@stellar/stellar-sdk';
import { config } from './env_config.js';
import { getContractCodeLedgerEntry, getLedgerKeyWasmId } from './contract.js';
export type TxParams = {
account: Account;
signerFunction: (txXdr: string) => Promise<string>;
txBuilderOptions: TransactionBuilder.TransactionBuilderOptions;
};
/**
* Signs a Stellar transaction with a given Keypair.
* @param {string} txXdr - The transaction in XDR format.
* @param {string} passphrase - The network passphrase.
* @param {Keypair} source - The Keypair to sign the transaction with.
* @returns {Promise<string>} The signed transaction in XDR format.
*/
export async function signWithKeypair(
txXdr: string,
passphrase: string,
source: Keypair
): Promise<string> {
const tx = new Transaction(txXdr, passphrase);
// Retrieve the transaction hash used for signatures.
const txHash = tx.hash();
console.log(`txhash in signer: ${txHash.toString('hex')}`);
const sourceKeypair = Keypair.fromPublicKey(tx.source);
tx.sign(source);
const signed = tx.signatures.some((signature) => {
// Verify the signature with the source account's public key.
return sourceKeypair.verify(txHash, signature.signature());
});
console.log(`Was it signed in the signer function? ${signed}`);
return tx.toXDR();
}
/**
* Sends a signed Stellar transaction and returns the result after parsing.
* @template T The type of the expected result.
* @param {Transaction} transaction - The transaction to send.
* @param {(result: string) => T} parser - A function to parse the result.
* @returns {Promise<T | undefined>} The parsed result of the transaction.
*/
export async function sendTransaction<T>(
transaction: Transaction,
parser: (result: string) => T
): Promise<T | undefined> {
try {
let send_tx_response = await config.rpc.sendTransaction(transaction);
const curr_time = Date.now();
while (send_tx_response.status === 'TRY_AGAIN_LATER' && Date.now() - curr_time < 20000) {
await new Promise((resolve) => setTimeout(resolve, 4000));
send_tx_response = await config.rpc.sendTransaction(transaction);
}
if (send_tx_response.status !== 'PENDING') {
const error = parseError(send_tx_response);
console.error('Transaction failed to send:', send_tx_response.hash, 'Error:', error);
throw error;
}
let get_tx_response = await config.rpc.getTransaction(send_tx_response.hash);
while (get_tx_response.status === 'NOT_FOUND') {
await new Promise((resolve) => setTimeout(resolve, 1000));
get_tx_response = await config.rpc.getTransaction(send_tx_response.hash);
}
if (get_tx_response.status === 'SUCCESS') {
const result = parseResult(get_tx_response, parser);
console.log(
'Transaction successfully submitted with hash:',
send_tx_response.hash,
'Result:',
result
);
return result;
} else {
console.log('Transaction failed:', get_tx_response.status);
const error = parseError(get_tx_response);
console.error('Transaction failure detail:', error);
throw error; // Rethrow to ensure calling code can handle it
}
} catch (error) {
console.error('An exception occurred while sending the transaction:', error);
throw error; // Ensure that errors are propagated
}
}
/**
* Simulates a restoration transaction to determine if restoration is needed.
* This function first checks the ledger entry for the given WASM hash. If the entry is found and has expired,
* it attempts a restoration. If the entry hasn't expired yet but the TTL needs extension, it proceeds with TTL extension.
* @param wasmHash - The hash of the WASM to check.
* @param txParams - The transaction parameters including account and signer.
* @returns A promise that resolves to a simulation response, indicating whether restoration or TTL extension is needed.
*/
export async function simulateRestorationIfNeeded(
wasmHash: Buffer,
txParams: TxParams
): Promise<SorobanRpc.Api.SimulateTransactionRestoreResponse | string | undefined> {
try {
const account = await config.rpc.getAccount(txParams.account.accountId());
const ledgerKey = getLedgerKeyWasmId(wasmHash);
const response = await config.rpc.getLedgerEntries(...[ledgerKey]);
if (response.entries && response.entries.length > 0 && response.entries[0].liveUntilLedgerSeq) {
const expirationLedger = response.entries[0].liveUntilLedgerSeq;
const desiredLedgerSeq = response.latestLedger + 500000;
let extendLedgers = desiredLedgerSeq - expirationLedger;
if (extendLedgers < 10000) {
extendLedgers = 10000;
}
console.log('Expiration:', expirationLedger);
console.log('Desired TTL:', desiredLedgerSeq);
const sorobanData = new SorobanDataBuilder().setReadWrite([ledgerKey]).build();
const restoreTx = new TransactionBuilder(account, txParams.txBuilderOptions)
.setSorobanData(sorobanData)
.addOperation(Operation.restoreFootprint({})) // The actual restore operation
.build();
// Simulate a transaction with a restoration operation to check if it's necessary
const restorationSimulation: SorobanRpc.Api.SimulateTransactionResponse =
await config.rpc.simulateTransaction(restoreTx);
const restoreNeeded = SorobanRpc.Api.isSimulationRestore(restorationSimulation);
const resSimSuccess = SorobanRpc.Api.isSimulationSuccess(restorationSimulation);
console.log(`restoration needed: ${restoreNeeded}\nSimulation Success: ${resSimSuccess}`);
// Check if the simulation indicates a need for restoration
if (restoreNeeded) {
return restorationSimulation as SorobanRpc.Api.SimulateTransactionRestoreResponse;
} else {
console.log('No restoration needed., bumping the ttl.');
const account1 = await config.rpc.getAccount(txParams.account.accountId());
const bumpTTLtx = new TransactionBuilder(account1, txParams.txBuilderOptions)
.setSorobanData(new SorobanDataBuilder().setReadWrite([ledgerKey]).build())
.addOperation(
Operation.extendFootprintTtl({
extendTo: desiredLedgerSeq,
})
) // The actual TTL extension operation
.build();
const ttlSimResponse: SorobanRpc.Api.SimulateTransactionResponse =
await config.rpc.simulateTransaction(bumpTTLtx);
const assembledTx = SorobanRpc.assembleTransaction(bumpTTLtx, ttlSimResponse).build();
const signedTx = new Transaction(
await txParams.signerFunction(assembledTx.toXDR()),
config.passphrase
);
// submit the assembled and signed transaction to bump it.
try {
const response = await sendTransaction(signedTx, (result) => {
console.log(`bump ttl for contract result: ${result}`);
return result;
});
return response;
} catch (error) {
console.error('Transaction submission failed with error:', error);
throw error;
}
}
} else {
console.log('No ledger entry found for the given WASM hash.');
}
} catch (error) {
console.error('Failed to simulate restoration:', error);
throw error;
}
return undefined;
}
/**
* Handles the restoration of a Soroban contract.
* @param {SorobanRpc.Api.SimulateTransactionRestoreResponse} simResponse - The simulation response containing restoration information.
* @param {TxParams} txParams - The transaction parameters.
* @returns {Promise<void>} A promise that resolves when the restoration transaction has been submitted.
*/
export async function handleRestoration(
simResponse: SorobanRpc.Api.SimulateTransactionRestoreResponse,
txParams: TxParams
): Promise<void> {
const restorePreamble = simResponse.restorePreamble;
console.log('Restoring for account:', txParams.account.accountId());
const account = await config.rpc.getAccount(txParams.account.accountId());
// Construct the transaction builder with the necessary parameters
const restoreTx = new TransactionBuilder(account, {
...txParams.txBuilderOptions,
fee: restorePreamble.minResourceFee, // Update fee based on the restoration requirement
})
.setSorobanData(restorePreamble.transactionData.build()) // Set Soroban Data from the simulation
.addOperation(Operation.restoreFootprint({})) // Add the RestoreFootprint operation
.build(); // Build the transaction
const simulation: SorobanRpc.Api.SimulateTransactionResponse =
await config.rpc.simulateTransaction(restoreTx);
const assembledTx = SorobanRpc.assembleTransaction(restoreTx, simulation).build();
const signedTx = new Transaction(
await txParams.signerFunction(assembledTx.toXDR()),
config.passphrase
);
console.log('Submitting restoration transaction');
try {
// Submit the transaction to the network
const response = await config.rpc.sendTransaction(signedTx);
console.log('Restoration transaction submitted successfully:', response.hash);
} catch (error) {
console.error('Failed to submit restoration transaction:', error);
throw new Error('Restoration transaction failed');
}
}
/**
* Invokes a Stellar Soroban operation and returns the parsed result.
* @template T The type of the expected result.
* @param {string} operation - The operation to invoke in base64 XDR format.
* @param {(result: string) => T} parser - A function to parse the result.
* @param {TxParams} txParams - Transaction parameters.
* @param {xdr.SorobanTransactionData} [sorobanData] - Optional Soroban transaction data.
* @returns {Promise<T | undefined>} The parsed result of the operation.
*/
export async function invokeSorobanOperation<T>(
operation: string,
parser: (result: string) => T,
txParams: TxParams,
sorobanData?: xdr.SorobanTransactionData
): Promise<T | undefined> {
console.log('invoking soroban operation');
const account = await config.rpc.getAccount(txParams.account.accountId());
console.log('the account is', account);
const txBuilder = new TransactionBuilder(account, txParams.txBuilderOptions)
.addOperation(xdr.Operation.fromXDR(operation, 'base64'))
.setTimeout(TimeoutInfinite);
if (sorobanData) {
txBuilder.setSorobanData(sorobanData);
}
// After building the transaction
const transaction = txBuilder.build();
console.log('Transaction built with sequence number:', transaction.sequence);
// Simulate the transaction
const simulation: SorobanRpc.Api.SimulateTransactionResponse =
await config.rpc.simulateTransaction(transaction);
//console.log('Simulation events result:', simulation.events);
//console.log('simulation stringified', JSON.stringify(simulation));
// After the simulation check, if restoration is needed
if (SorobanRpc.Api.isSimulationRestore(simulation)) {
console.log('Restoration needed for successful simulation.');
const restorePreamble = simulation.restorePreamble;
// Checking and logging the minimum resource fee required for restoration
if (restorePreamble && restorePreamble.minResourceFee) {
console.log(`Minimum resource fee needed: ${restorePreamble.minResourceFee}`);
}
// Processing the transaction data needed for restoration
if (restorePreamble && restorePreamble.transactionData) {
const sorobanDataBuilder = restorePreamble.transactionData;
// Get the read-only and read-write keys using methods from SorobanDataBuilder
const readOnlyKeys = sorobanDataBuilder.getReadOnly();
const readWriteKeys = sorobanDataBuilder.getReadWrite();
// Log read-only entries
if (readOnlyKeys.length > 0) {
console.log('Read-only entries that may need restoration:');
readOnlyKeys.forEach((key, index) => {
console.log(`Entry ${index + 1}:`, key);
});
}
// Log read-write entries
if (readWriteKeys.length > 0) {
console.log('Read-write entries that may need restoration:');
readWriteKeys.forEach((key, index) => {
console.log(`Entry ${index + 1}:`, key);
});
}
}
await handleRestoration(simulation, txParams);
}
if (SorobanRpc.Api.isSimulationError(simulation)) {
console.log('Simulation error with details:', simulation);
throw new Error(simulation.error);
}
const assembledTx = SorobanRpc.assembleTransaction(transaction, simulation).build();
const signedTx = new Transaction(
await txParams.signerFunction(assembledTx.toXDR()),
config.passphrase
);
// Check if transaction is correctly signed
const sourceKeypair = Keypair.fromPublicKey(txParams.account.accountId());
const txHash = signedTx.hash();
const isSignedCorrectly = signedTx.signatures.some((signature) =>
sourceKeypair.verify(txHash, signature.signature())
);
console.log('Is transaction correctly signed by source?', isSignedCorrectly);
// Submit the transaction
try {
const response = await sendTransaction(signedTx, parser);
return response;
} catch (error) {
console.error('Transaction submission failed with error:', error);
throw error;
}
}
/**
* Submits a classic Stellar operation.
* @param {string} operation - The operation to submit in base64 XDR format.
* @param {TxParams} txParams - Transaction parameters.
*/
export async function invokeClassicOp(operation: string, txParams: TxParams) {
const account = await config.rpc.getAccount(txParams.account.accountId());
const txBuilder = new TransactionBuilder(account, txParams.txBuilderOptions)
.addOperation(xdr.Operation.fromXDR(operation, 'base64'))
.setTimeout(TimeoutInfinite);
const transaction = txBuilder.build();
const signedTx = new Transaction(
await txParams.signerFunction(transaction.toXDR()),
config.passphrase
);
console.log(
`Submitting classic operation with transaction hash: ${signedTx.hash().toString('hex')}`
);
try {
await sendTransaction(signedTx, () => undefined);
} catch (e) {
console.error('Error submitting classic operation: ', e);
throw Error('failed to submit classic op TX');
}
}