Skip to content

Commit 4174f70

Browse files
authored
feat: add transaction info command (#122)
1 parent e3e765f commit 4174f70

File tree

10 files changed

+540
-23
lines changed

10 files changed

+540
-23
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,14 @@ Run `npx zksync-cli dev` to see the full list of commands.
4040
- **Scripting**: Automated interactions and advanced zkSync operations using Node.js, with examples of wallet or contract interactions using viem or ethers. [Scripting Templates](https://github.com/matter-labs/zksync-scripting-templates#readme)
4141

4242
### Contract interaction commands
43+
See full documentation and advanced examples [here](./docs/contract-interaction.md).
4344
- `npx zksync-cli contract read`: run read-only contract methods
4445
- `npx zksync-cli contract write`: send transactions to the contract
4546
- `npx zksync-cli contract encode`: get calldata from the contract method
4647

47-
See full documentation and advanced examples [here](./docs/contract-interaction.md).
48+
### Transaction commands
49+
See full documentation and advanced examples [here](./docs/transaction-info.md).
50+
- `npx zksync-cli transaction info`: get information about a transaction
4851

4952
### Wallet commands
5053
- `npx zksync-cli wallet transfer`: send funds on L2 to another account

docs/contract-interaction.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The `npx zksync-cli contract read` command executes read-only methods on contrac
2424
### Read Options
2525
You do not need to specify options bellow, you will be prompted to enter them if they are not specified.
2626

27-
- `--chain <chain-name>`: Select the chain to use
27+
- `--chain <chain-name>`: Select the chain to use (e.g., `zksync-mainnet`, `zksync-sepolia`).
2828
- `--rpc <url>`: Provide RPC URL instead of selecting a chain
2929
- `--contract <address>`: Specify contract's address
3030
- `--method <method-signature>`: Defines the contract method to interact with

docs/transaction-info.md

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Transaction information
2+
3+
The `npx zksync-cli transaction info` command is designed to fetch and display detailed information about a specific transaction. It can be used to check the status, amounts transferred, fees, method signatures, and arguments of transactions on the chain of choice.
4+
5+
### Table of contents
6+
- [Options](#options)
7+
- [Examples](#example-usage)
8+
- [Basic usage](#basic-usage)
9+
- [Parsing transaction data](#parsing-transaction-data)
10+
- [Viewing detailed information](#viewing-detailed-information)
11+
- [Displaying raw JSON response](#displaying-raw-json-response)
12+
13+
<br />
14+
15+
---
16+
17+
<br />
18+
19+
### Options
20+
You do not need to specify options bellow, you will be prompted to enter them if they are not specified.
21+
22+
- `--tx <transaction hash>`: Specify the transaction hash to query.
23+
- `--chain <chain-name>`: Select the chain to use (e.g., `zksync-mainnet`, `zksync-sepolia`).
24+
- `--rpc <url>`: Provide RPC URL instead of selecting a chain
25+
- `--full`: Show all available transaction data for comprehensive insights.
26+
- `--raw`: Display the raw JSON response from the node.
27+
- `--abi <path>`: Path to a local ABI file to decode the transaction's input data.
28+
29+
If no options are provided directly, the CLI will prompt the user to enter the necessary information, such as the chain and transaction hash.
30+
31+
<br />
32+
33+
---
34+
35+
<br />
36+
37+
## Example usage
38+
39+
### Basic usage
40+
```bash
41+
npx zksync-cli transaction info
42+
```
43+
44+
You will be prompted to select a chain and transaction hash.
45+
```bash
46+
? Chain to use: zkSync Sepolia Testnet
47+
? Transaction hash: 0x2547ce8219eb7ed5d73e68673b0e4ded83afc732a6c651d43d9dc49bb2f13d40
48+
```
49+
50+
The command will then display detailed information about the transaction, including its status, from/to addresses, value transferred, method signature with arguments, and more:
51+
```
52+
──────────────────── Main info ────────────────────
53+
Transaction hash: 0x2547ce8219eb7ed5d73e68673b0e4ded83afc732a6c651d43d9dc49bb2f13d40
54+
Status: completed
55+
From: 0x56DDd604011c5F8629bd7C2472E3504Bd32c269b
56+
To: 0xBB5c309A3a9347c0135B93CbD53D394Aa84345E5
57+
Value: 0 ETH
58+
Fee: 0.0001503581 ETH | Initial: 0.0004 ETH Refunded: 0.0038496419 ETH
59+
Method: transmit(bytes,bytes32[],bytes32[],bytes32) 0xc9807539
60+
61+
───────────────── Method arguments ─────────────────
62+
[1] bytes: 0x0000000000000000000000fd69e45d6f51e482ac4f8f2e14f2155200008b5f010001020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000007df298c81a0000000000000000000000000000000000000000000000000000007df298c81a0000000000000000000000000000000000000000000000000000007df298c81a
63+
[2] bytes32[]: 0xd737d65b6b610c3f330bcfddbfc08e46d2a628c88bf22ec0d8f25627a3330798,0x9d33be2ba33b731555c13a4e7bf02d3d576fa3115f7523cbf07732321c85cdba
64+
[3] bytes32[]: 0x73d639deda36b781ae049c8eceafba4196ee8ecc1efb74c538a28ea762ff6658,0x37ac79ff2ca902140613b0e51357d8fb218a67b4736bdee0c268c5fd9812e146
65+
[4] bytes32: 0x0101000000000000000000000000000000000000000000000000000000000000
66+
67+
───────────────────── Details ─────────────────────
68+
Date: 2/8/2024, 2:19:54 PM (15 minutes ago)
69+
Block: #364999
70+
Nonce: 50131
71+
```
72+
73+
### Parsing transaction data
74+
By default `zksync-cli` tries to fetch contract verification data from the server.
75+
In case this is not possible it queries the [open signature](https://www.4byte.directory/) database to get signature of the transaction method.
76+
If the method signature is not found, the transaction's data is displayed as a hex string.
77+
78+
79+
Alternatively, you can provide the path to a local ABI file to decode the transaction's input data:
80+
```bash
81+
npx zksync-cli transaction info \
82+
--abi "./Greeter.json"
83+
```
84+
85+
### Viewing detailed information
86+
For an even more detailed overview you can use the `--full` option:
87+
88+
```bash
89+
npx zksync-cli transaction info --full
90+
```
91+
92+
### Displaying raw JSON response
93+
If you prefer to see the raw JSON response from the zkSync node, use the `--raw` option:
94+
95+
```bash
96+
npx zksync-cli transaction info --raw
97+
```

src/commands/contract/utils/helpers.ts

+4-21
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import ora from "ora";
77
import { getMethodId } from "./formatters.js";
88
import { getProxyImplementation } from "./proxy.js";
99
import { fileOrDirExists } from "../../../utils/files.js";
10+
import { formatSeparator } from "../../../utils/formatters.js";
1011
import Logger from "../../../utils/logger.js";
1112

1213
import type { L2Chain } from "../../../data/chains.js";
@@ -144,24 +145,6 @@ export const askAbiMethod = async (
144145
return "manual";
145146
}
146147

147-
const formatSeparator = (text: string): DistinctChoice => {
148-
const totalLength = 50; // Total length of the line including the text
149-
150-
if (!text) {
151-
return {
152-
type: "separator",
153-
line: "─".repeat(totalLength + 1),
154-
};
155-
}
156-
157-
const textLength = text.length;
158-
const dashLength = (totalLength - textLength) / 2;
159-
const dashes = "─".repeat(dashLength);
160-
return {
161-
type: "separator",
162-
line: `${dashes} ${text} ${dashes}`,
163-
};
164-
};
165148
const formatFragment = (fragment: ethers.utils.FunctionFragment): DistinctChoice => {
166149
let name = fragment.format(ethers.utils.FormatTypes.full);
167150
if ((type === "write" || type === "any") && name.includes(" returns ")) {
@@ -180,7 +163,7 @@ export const askAbiMethod = async (
180163
noMethods: { type: "separator", line: chalk.white("No methods found") } as DistinctChoice,
181164
contractNotVerified: { type: "separator", line: chalk.white("Contract is not verified") } as DistinctChoice,
182165
};
183-
choices.push(formatSeparator("Provided contract"));
166+
choices.push(formatSeparator("Provided contract") as DistinctChoice);
184167
if (contractInfo.abi) {
185168
const methods = getMethodsFromAbi(contractInfo.abi, type);
186169
if (methods.length) {
@@ -199,7 +182,7 @@ export const askAbiMethod = async (
199182
}
200183
if (contractInfo?.implementation) {
201184
if (contractInfo.implementation.abi) {
202-
choices.push(formatSeparator("Resolved implementation"));
185+
choices.push(formatSeparator("Resolved implementation") as DistinctChoice);
203186
const implementationMethods = getMethodsFromAbi(contractInfo.implementation.abi, type);
204187
if (implementationMethods.length) {
205188
choices.push(...implementationMethods.map(formatFragment));
@@ -217,7 +200,7 @@ export const askAbiMethod = async (
217200
}
218201
}
219202

220-
choices.push(formatSeparator(""));
203+
choices.push(formatSeparator("") as DistinctChoice);
221204
choices.push({
222205
name: "Type method manually",
223206
value: "manual",

src/commands/transaction/command.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Program from "../../program.js";
2+
3+
export default Program.command("transaction").description("Transactions related functionality");

src/commands/transaction/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import "./info.js";
2+
3+
import "./command.js"; // registers all the commands above

0 commit comments

Comments
 (0)