forked from hyperledger-solang/solang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_helpers.js
64 lines (56 loc) · 2.87 KB
/
test_helpers.js
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
import * as StellarSdk from '@stellar/stellar-sdk';
export async function call_contract_function(method, server, keypair, contract, ...params) {
let res = null;
try {
let builtTransaction = new StellarSdk.TransactionBuilder(await server.getAccount(keypair.publicKey()), {
fee: StellarSdk.BASE_FEE,
networkPassphrase: StellarSdk.Networks.TESTNET,
}).addOperation(contract.call(method, ...params)).setTimeout(30).build();
let preparedTransaction = await server.prepareTransaction(builtTransaction);
// Sign the transaction with the source account's keypair.
preparedTransaction.sign(keypair);
let sendResponse = await server.sendTransaction(preparedTransaction);
if (sendResponse.status === "PENDING") {
let getResponse = await server.getTransaction(sendResponse.hash);
// Poll `getTransaction` until the status is not "NOT_FOUND"
while (getResponse.status === "NOT_FOUND") {
console.log("Waiting for transaction confirmation...");
// Wait one second
await new Promise((resolve) => setTimeout(resolve, 1000));
// See if the transaction is complete
getResponse = await server.getTransaction(sendResponse.hash);
}
if (getResponse.status === "SUCCESS") {
// Ensure the transaction's resultMetaXDR is not empty
if (!getResponse.resultMetaXdr) {
throw "Empty resultMetaXDR in getTransaction response";
}
// Extract and return the return value from the contract
let transactionMeta = getResponse.resultMetaXdr;
let returnValue = transactionMeta.v3().sorobanMeta().returnValue();
console.log(`Transaction result: ${returnValue.value()}`);
res = returnValue.value();
} else {
throw `Transaction failed: ${getResponse.resultXdr}`;
}
} else if (sendResponse.status === "FAILED") {
// Handle expected failure and return the error message
if (sendResponse.errorResultXdr) {
const errorXdr = StellarSdk.xdr.TransactionResult.fromXDR(sendResponse.errorResultXdr, 'base64');
const errorRes = errorXdr.result().results()[0].tr().invokeHostFunctionResult().code().value;
console.log(`Transaction error: ${errorRes}`);
res = errorRes;
} else {
throw "Transaction failed but no errorResultXdr found";
}
} else {
throw sendResponse.errorResultXdr;
}
} catch (err) {
// Return the error as a string instead of failing the test
console.log("Transaction processing failed");
console.log(err);
res = err.toString();
}
return res;
}