Skip to content

Commit

Permalink
feat: add transaction info command (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
JackHamer09 authored Feb 8, 2024
1 parent e3e765f commit 4174f70
Show file tree
Hide file tree
Showing 10 changed files with 540 additions and 23 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ Run `npx zksync-cli dev` to see the full list of commands.
- **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)

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

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

### Wallet commands
- `npx zksync-cli wallet transfer`: send funds on L2 to another account
Expand Down
2 changes: 1 addition & 1 deletion docs/contract-interaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The `npx zksync-cli contract read` command executes read-only methods on contrac
### Read Options
You do not need to specify options bellow, you will be prompted to enter them if they are not specified.

- `--chain <chain-name>`: Select the chain to use
- `--chain <chain-name>`: Select the chain to use (e.g., `zksync-mainnet`, `zksync-sepolia`).
- `--rpc <url>`: Provide RPC URL instead of selecting a chain
- `--contract <address>`: Specify contract's address
- `--method <method-signature>`: Defines the contract method to interact with
Expand Down
97 changes: 97 additions & 0 deletions docs/transaction-info.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Transaction information

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.

### Table of contents
- [Options](#options)
- [Examples](#example-usage)
- [Basic usage](#basic-usage)
- [Parsing transaction data](#parsing-transaction-data)
- [Viewing detailed information](#viewing-detailed-information)
- [Displaying raw JSON response](#displaying-raw-json-response)

<br />

---

<br />

### Options
You do not need to specify options bellow, you will be prompted to enter them if they are not specified.

- `--tx <transaction hash>`: Specify the transaction hash to query.
- `--chain <chain-name>`: Select the chain to use (e.g., `zksync-mainnet`, `zksync-sepolia`).
- `--rpc <url>`: Provide RPC URL instead of selecting a chain
- `--full`: Show all available transaction data for comprehensive insights.
- `--raw`: Display the raw JSON response from the node.
- `--abi <path>`: Path to a local ABI file to decode the transaction's input data.

If no options are provided directly, the CLI will prompt the user to enter the necessary information, such as the chain and transaction hash.

<br />

---

<br />

## Example usage

### Basic usage
```bash
npx zksync-cli transaction info
```

You will be prompted to select a chain and transaction hash.
```bash
? Chain to use: zkSync Sepolia Testnet
? Transaction hash: 0x2547ce8219eb7ed5d73e68673b0e4ded83afc732a6c651d43d9dc49bb2f13d40
```

The command will then display detailed information about the transaction, including its status, from/to addresses, value transferred, method signature with arguments, and more:
```
──────────────────── Main info ────────────────────
Transaction hash: 0x2547ce8219eb7ed5d73e68673b0e4ded83afc732a6c651d43d9dc49bb2f13d40
Status: completed
From: 0x56DDd604011c5F8629bd7C2472E3504Bd32c269b
To: 0xBB5c309A3a9347c0135B93CbD53D394Aa84345E5
Value: 0 ETH
Fee: 0.0001503581 ETH | Initial: 0.0004 ETH Refunded: 0.0038496419 ETH
Method: transmit(bytes,bytes32[],bytes32[],bytes32) 0xc9807539
───────────────── Method arguments ─────────────────
[1] bytes: 0x0000000000000000000000fd69e45d6f51e482ac4f8f2e14f2155200008b5f010001020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000007df298c81a0000000000000000000000000000000000000000000000000000007df298c81a0000000000000000000000000000000000000000000000000000007df298c81a
[2] bytes32[]: 0xd737d65b6b610c3f330bcfddbfc08e46d2a628c88bf22ec0d8f25627a3330798,0x9d33be2ba33b731555c13a4e7bf02d3d576fa3115f7523cbf07732321c85cdba
[3] bytes32[]: 0x73d639deda36b781ae049c8eceafba4196ee8ecc1efb74c538a28ea762ff6658,0x37ac79ff2ca902140613b0e51357d8fb218a67b4736bdee0c268c5fd9812e146
[4] bytes32: 0x0101000000000000000000000000000000000000000000000000000000000000
───────────────────── Details ─────────────────────
Date: 2/8/2024, 2:19:54 PM (15 minutes ago)
Block: #364999
Nonce: 50131
```

### Parsing transaction data
By default `zksync-cli` tries to fetch contract verification data from the server.
In case this is not possible it queries the [open signature](https://www.4byte.directory/) database to get signature of the transaction method.
If the method signature is not found, the transaction's data is displayed as a hex string.


Alternatively, you can provide the path to a local ABI file to decode the transaction's input data:
```bash
npx zksync-cli transaction info \
--abi "./Greeter.json"
```

### Viewing detailed information
For an even more detailed overview you can use the `--full` option:

```bash
npx zksync-cli transaction info --full
```

### Displaying raw JSON response
If you prefer to see the raw JSON response from the zkSync node, use the `--raw` option:

```bash
npx zksync-cli transaction info --raw
```
25 changes: 4 additions & 21 deletions src/commands/contract/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ora from "ora";
import { getMethodId } from "./formatters.js";
import { getProxyImplementation } from "./proxy.js";
import { fileOrDirExists } from "../../../utils/files.js";
import { formatSeparator } from "../../../utils/formatters.js";
import Logger from "../../../utils/logger.js";

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

const formatSeparator = (text: string): DistinctChoice => {
const totalLength = 50; // Total length of the line including the text

if (!text) {
return {
type: "separator",
line: "─".repeat(totalLength + 1),
};
}

const textLength = text.length;
const dashLength = (totalLength - textLength) / 2;
const dashes = "─".repeat(dashLength);
return {
type: "separator",
line: `${dashes} ${text} ${dashes}`,
};
};
const formatFragment = (fragment: ethers.utils.FunctionFragment): DistinctChoice => {
let name = fragment.format(ethers.utils.FormatTypes.full);
if ((type === "write" || type === "any") && name.includes(" returns ")) {
Expand All @@ -180,7 +163,7 @@ export const askAbiMethod = async (
noMethods: { type: "separator", line: chalk.white("No methods found") } as DistinctChoice,
contractNotVerified: { type: "separator", line: chalk.white("Contract is not verified") } as DistinctChoice,
};
choices.push(formatSeparator("Provided contract"));
choices.push(formatSeparator("Provided contract") as DistinctChoice);
if (contractInfo.abi) {
const methods = getMethodsFromAbi(contractInfo.abi, type);
if (methods.length) {
Expand All @@ -199,7 +182,7 @@ export const askAbiMethod = async (
}
if (contractInfo?.implementation) {
if (contractInfo.implementation.abi) {
choices.push(formatSeparator("Resolved implementation"));
choices.push(formatSeparator("Resolved implementation") as DistinctChoice);
const implementationMethods = getMethodsFromAbi(contractInfo.implementation.abi, type);
if (implementationMethods.length) {
choices.push(...implementationMethods.map(formatFragment));
Expand All @@ -217,7 +200,7 @@ export const askAbiMethod = async (
}
}

choices.push(formatSeparator(""));
choices.push(formatSeparator("") as DistinctChoice);
choices.push({
name: "Type method manually",
value: "manual",
Expand Down
3 changes: 3 additions & 0 deletions src/commands/transaction/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Program from "../../program.js";

export default Program.command("transaction").description("Transactions related functionality");
3 changes: 3 additions & 0 deletions src/commands/transaction/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "./info.js";

import "./command.js"; // registers all the commands above
Loading

0 comments on commit 4174f70

Please sign in to comment.