Skip to content

Commit b845ef3

Browse files
authored
fix(cli): parse failed txs (#41)
1 parent 1d8a767 commit b845ef3

File tree

1 file changed

+43
-9
lines changed

1 file changed

+43
-9
lines changed

scripts/debugTransaction.ts

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@ import {
77
fromNano,
88
OpenedContract,
99
Transaction,
10+
TransactionComputePhase,
11+
TransactionDescriptionGeneric,
1012
} from '@ton/core';
1113
import { TonClient } from '@ton/ton';
12-
import { bufferToHexString, depositLogFromCell, GatewayOp, sliceToHexString } from '../types';
14+
import {
15+
bufferToHexString,
16+
DepositLog,
17+
depositLogFromCell,
18+
GatewayOp,
19+
sliceToHexString,
20+
} from '../types';
1321

1422
let isTestnet = false;
1523

@@ -46,10 +54,21 @@ export async function run(provider: NetworkProvider) {
4654
async function fetchLastTransactions(client: TonClient, gw: OpenedContract<Gateway>, limit = 10) {
4755
const txs = await client.getTransactions(gw.address, { limit, archival: true });
4856

57+
const out: any[] = [];
58+
4959
for (const tx of txs) {
50-
const parsed = parseTransaction(tx);
51-
console.log(parsed);
60+
try {
61+
const parsed = parseTransaction(tx);
62+
out.push(parsed);
63+
} catch (error) {
64+
out.push({
65+
hash: tx.hash().toString('hex'),
66+
error: error instanceof Error ? error.message : error,
67+
});
68+
}
5269
}
70+
71+
console.log(JSON.stringify(out, null, 2));
5372
}
5473

5574
async function fetchTransaction(client: TonClient, gw: OpenedContract<Gateway>, txHash: string) {
@@ -106,6 +125,18 @@ function parseTransaction(tx: Transaction) {
106125
function parseInbound(tx: Transaction) {
107126
const info = tx.inMessage!.info as CommonMessageInfoInternal;
108127
const hash = tx.hash().toString('hex');
128+
const exitCode = (tx.description as any).computePhase!.exitCode as number;
129+
const success = exitCode === 0;
130+
131+
const parseLog = (): DepositLog => {
132+
if (!success) {
133+
return { amount: 0n, depositFee: 0n };
134+
}
135+
136+
const body = tx.outMessages.get(0)!.body;
137+
138+
return depositLogFromCell(body);
139+
};
109140

110141
let kv: Record<string, any> = {};
111142

@@ -123,9 +154,9 @@ function parseInbound(tx: Transaction) {
123154
kv.queryId = slice.loadUint(64);
124155
kv.zevmRecipient = bufferToHexString(slice.loadBuffer(20));
125156

126-
const outDeposit = depositLogFromCell(tx.outMessages.get(0)!.body);
127-
kv.depositAmount = formatCoin(outDeposit.amount);
128-
kv.depositFee = formatCoin(outDeposit.depositFee);
157+
const depositLog = parseLog();
158+
kv.depositAmount = formatCoin(depositLog.amount);
159+
kv.depositFee = formatCoin(depositLog.depositFee);
129160

130161
break;
131162
case GatewayOp.DepositAndCall:
@@ -134,9 +165,9 @@ function parseInbound(tx: Transaction) {
134165
kv.zevmRecipient = bufferToHexString(slice.loadBuffer(20));
135166
kv.callData = sliceToHexString(slice.loadRef().asSlice());
136167

137-
const outDepositAndCall = depositLogFromCell(tx.outMessages.get(0)!.body);
138-
kv.depositAmount = formatCoin(outDepositAndCall.amount);
139-
kv.depositFee = formatCoin(outDepositAndCall.depositFee);
168+
const dacLog = parseLog();
169+
kv.depositAmount = formatCoin(dacLog.amount);
170+
kv.depositFee = formatCoin(dacLog.depositFee);
140171

141172
break;
142173
default:
@@ -148,6 +179,7 @@ function parseInbound(tx: Transaction) {
148179
receiver: info.dest.toRawString(),
149180
hash: `${tx.lt}:${hash}`,
150181
timestamp: formatDate(tx.now),
182+
exitCode: exitCode,
151183
txAmount: formatCoin(info.value.coins),
152184
gas: formatCoin(tx.totalFees.coins),
153185
link: common.txLink(hash, isTestnet),
@@ -158,6 +190,7 @@ function parseInbound(tx: Transaction) {
158190
function parseOutbound(tx: Transaction) {
159191
const info = tx.inMessage!.info as CommonMessageInfoExternalIn;
160192
const hash = tx.hash().toString('hex');
193+
const exitCode = (tx.description as any).computePhase!.exitCode as number;
161194

162195
const slice = tx.inMessage!.body.beginParse();
163196

@@ -180,6 +213,7 @@ function parseOutbound(tx: Transaction) {
180213
receiver: info.dest.toRawString(),
181214
hash: `${tx.lt}:${hash}`,
182215
timestamp: formatDate(tx.now),
216+
exitCode: exitCode,
183217
gas: formatCoin(tx.totalFees.coins),
184218
link: common.txLink(hash, isTestnet),
185219
payload: {

0 commit comments

Comments
 (0)