forked from StellarRouter/StellarRouter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoke.ts
More file actions
29 lines (27 loc) · 1.1 KB
/
Copy pathinvoke.ts
File metadata and controls
29 lines (27 loc) · 1.1 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
import { Account, StrKey, Transaction, xdr } from "@stellar/stellar-sdk";
export interface InvocationInfo {
contractId: string;
method: string;
args: xdr.ScVal[];
}
/**
* Extract the contract invocation from a (prepared) Soroban transaction:
* contract id, method symbol, and argument ScVals.
*/
export function extractInvocation(tx: Transaction): InvocationInfo | null {
try {
const envelope = tx.toEnvelope();
const operation = envelope.v1().tx().operations()[0];
if (!operation) return null;
const body = operation.body();
if (body.switch().name !== "invokeHostFunction") return null;
const hostFunction = body.invokeHostFunctionOp().hostFunction();
if (hostFunction.switch().name !== "hostFunctionTypeInvokeContract") return null;
const invokeContract = hostFunction.invokeContract();
const contractId = StrKey.encodeContract(Buffer.from(invokeContract.contractAddress().contractId() as unknown as Uint8Array));
const method = invokeContract.functionName().toString();
return { contractId, method, args: invokeContract.args() };
} catch {
return null;
}
}