-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathexplainTransactionWasm.ts
More file actions
84 lines (73 loc) · 2.93 KB
/
explainTransactionWasm.ts
File metadata and controls
84 lines (73 loc) · 2.93 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
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
/**
* WASM-based TON transaction explanation.
*
* Built on @bitgo/wasm-ton's parseTransaction(). Derives transaction types,
* extracts outputs/inputs, and maps to BitGoJS TransactionExplanation format.
* This is BitGo-specific business logic that lives outside the wasm package.
*/
import { Transaction as WasmTonTransaction, parseTransaction } from '@bitgo/wasm-ton';
import type { TonTransactionType, ParsedTransaction as WasmParsedTransaction } from '@bitgo/wasm-ton';
import { TransactionExplanation } from './iface';
export interface ExplainTonTransactionWasmOptions {
txBase64: string;
/** When false, use the original bounce-flag-respecting address format. Defaults to true (bounceable EQ...). */
toAddressBounceable?: boolean;
}
function mapTransactionType(wasmType: TonTransactionType): string {
return wasmType;
}
function extractOutputs(
parsed: WasmParsedTransaction,
toAddressBounceable: boolean
): {
outputs: { address: string; amount: string }[];
outputAmount: string;
withdrawAmount: string | undefined;
} {
const outputs: { address: string; amount: string }[] = [];
let withdrawAmount: string | undefined;
for (const action of parsed.sendActions) {
if (action.jettonTransfer) {
outputs.push({
address: action.jettonTransfer.destination,
amount: String(action.jettonTransfer.amount),
});
} else {
// destinationBounceable is always EQ... (bounceable)
// destination respects the original bounce flag (UQ... when bounce=false)
outputs.push({
address: toAddressBounceable ? action.destinationBounceable : action.destination,
amount: String(action.amount),
});
}
// withdrawAmount comes from the body payload parsed by WASM (not the message TON value)
if (action.withdrawAmount !== undefined) {
withdrawAmount = String(action.withdrawAmount);
}
}
const outputAmount = outputs.reduce((sum, o) => sum + BigInt(o.amount), 0n);
return { outputs, outputAmount: String(outputAmount), withdrawAmount };
}
/**
* Standalone WASM-based transaction explanation for TON.
*
* Parses the transaction via `parseTransaction(tx)` from @bitgo/wasm-ton,
* then derives the transaction type, extracts outputs/inputs, and maps
* to BitGoJS TransactionExplanation format.
*/
export function explainTonTransaction(params: ExplainTonTransactionWasmOptions): TransactionExplanation {
const toAddressBounceable = params.toAddressBounceable !== false;
const tx = WasmTonTransaction.fromBytes(Buffer.from(params.txBase64, 'base64'));
const parsed: WasmParsedTransaction = parseTransaction(tx);
const { outputs, outputAmount, withdrawAmount } = extractOutputs(parsed, toAddressBounceable);
return {
displayOrder: ['id', 'outputs', 'outputAmount', 'changeOutputs', 'changeAmount', 'fee', 'withdrawAmount'],
id: tx.id,
outputs,
outputAmount,
changeOutputs: [],
changeAmount: '0',
fee: { fee: 'UNKNOWN' },
withdrawAmount,
};
}