Skip to content

Commit

Permalink
nodejs: add parse err function (#483)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitry Rybalchenko <[email protected]>
  • Loading branch information
fanatid and dm-rybalchenko authored Dec 3, 2024
1 parent 8513ac4 commit dae08b2
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The minor version will be incremented upon a breaking change and the patch versi
### Features

- proto: add tonic feature ([#474](https://github.com/rpcpool/yellowstone-grpc/pull/474))
- nodejs: add parse err function ([#483](https://github.com/rpcpool/yellowstone-grpc/pull/483))

### Breaking

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ members = [
"yellowstone-grpc-proto", # 4.0.0
]
exclude = [
"yellowstone-grpc-client-nodejs/solana-encoding-wasm", # 1.0.0
"yellowstone-grpc-client-nodejs/solana-encoding-wasm", # 3.0.0
]

[workspace.package]
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
clean: clean-nodejs clean-rust
rm -rf test-ledger

clean-nodejs:
rm -rf examples/typescript/dist
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 23 additions & 5 deletions examples/typescript/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import yargs from "yargs";
import { inspect } from "node:util";
import Client, {
CommitmentLevel,
SubscribeRequest,
SubscribeRequestFilterAccountsFilter,
SubscribeRequestFilterAccountsFilterLamports,
SubscribeUpdateTransactionInfo,
txEncode,
txErrDecode,
} from "@triton-one/yellowstone-grpc";

async function main() {
Expand Down Expand Up @@ -85,13 +87,24 @@ async function subscribeCommand(client, args) {

// Handle updates
stream.on("data", (data) => {
if (data.transaction && args.transactionsParsed) {
if (
data.transaction &&
(args.transactionsParsed || args.transactionsDecodeErr)
) {
const slot = data.transaction.slot;
const message = data.transaction.transaction;
const tx = txEncode.encode(message, txEncode.encoding.Json, 255, true);
console.log(
`TX filters: ${data.filters}, slot#${slot}, tx: ${JSON.stringify(tx)}`
);
if (args.transactionsParsed) {
const tx = txEncode.encode(message, txEncode.encoding.Json, 255, true);
console.log(
`TX filters: ${data.filters}, slot#${slot}, tx: ${JSON.stringify(tx)}`
);
}
if (message.meta.err && args.transactionsDecodeErr) {
const err = txErrDecode.decode(message.meta.err.err);
console.log(
`TX filters: ${data.filters}, slot#${slot}, err: ${inspect(err)}}`
);
}
return;
}

Expand Down Expand Up @@ -391,6 +404,11 @@ function parseCommandLineArgs() {
describe: "parse transaction to json",
type: "boolean",
},
"transactions-decode-err": {
default: false,
describe: "decode transactions errors",
type: "boolean",
},
"transactions-status": {
default: false,
describe: "subscribe on transactionsStatus updates",
Expand Down
26 changes: 18 additions & 8 deletions yellowstone-grpc-client-nodejs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion yellowstone-grpc-client-nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@triton-one/yellowstone-grpc",
"version": "1.2.0",
"version": "1.3.0",
"license": "Apache-2.0",
"author": "Triton One",
"description": "Yellowstone gRPC Geyser Node.js Client",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "yellowstone-grpc-solana-encoding-wasm"
version = "2.0.0"
version = "3.0.0"
authors = ["Triton One"]
edition = "2021"
homepage = "https://triton.one"
Expand Down
15 changes: 13 additions & 2 deletions yellowstone-grpc-client-nodejs/solana-encoding-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use {
solana_transaction_status::{TransactionWithStatusMeta, UiTransactionEncoding},
wasm_bindgen::prelude::*,
yellowstone_grpc_proto::{
convert_from, prelude::SubscribeUpdateTransactionInfo, prost::Message,
convert_from,
prelude::{SubscribeUpdateTransactionInfo, TransactionError as TransactionErrorProto},
prost::Message,
},
};

Expand All @@ -29,7 +31,7 @@ impl From<WasmUiTransactionEncoding> for UiTransactionEncoding {
}

#[wasm_bindgen]
pub fn encode(
pub fn encode_tx(
data: &[u8],
encoding: WasmUiTransactionEncoding,
max_supported_transaction_version: Option<u8>,
Expand All @@ -49,3 +51,12 @@ pub fn encode(
Err(JsError::new("tx with missing metadata"))
}
}

#[wasm_bindgen]
pub fn decode_tx_error(err: Vec<u8>) -> Result<String, JsError> {
match convert_from::create_tx_error(Some(&TransactionErrorProto { err })) {
Ok(Some(err)) => serde_json::to_string(&err).map_err(Into::into),
Ok(None) => Err(JsError::new("unexpected")),
Err(error) => Err(JsError::new(error)),
}
}
19 changes: 15 additions & 4 deletions yellowstone-grpc-client-nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,23 @@ export {

// Import transaction encoding function created in Rust
import * as wasm from "./encoding/yellowstone_grpc_solana_encoding_wasm";
// Import mapper to get return type based on WasmUiTransactionEncoding
import { MapTransactionEncodingToReturnType } from "./types";
import type {
TransactionErrorSolana,
// Import mapper to get return type based on WasmUiTransactionEncoding
MapTransactionEncodingToReturnType,
} from "./types";

export const txEncode = {
encoding: wasm.WasmUiTransactionEncoding,
encode_raw: wasm.encode,
encode_raw: wasm.encode_tx,
encode: <T extends wasm.WasmUiTransactionEncoding>(
message: SubscribeUpdateTransactionInfo,
encoding: T,
max_supported_transaction_version: number | undefined,
show_rewards: boolean
): MapTransactionEncodingToReturnType[T] => {
return JSON.parse(
wasm.encode(
wasm.encode_tx(
SubscribeUpdateTransactionInfo.encode(message).finish(),
encoding,
max_supported_transaction_version,
Expand All @@ -79,6 +83,13 @@ export const txEncode = {
},
};

export const txErrDecode = {
decode_raw: wasm.decode_tx_error,
decode: (buf: Uint8Array): TransactionErrorSolana => {
return JSON.parse(wasm.decode_tx_error(buf));
},
};

export default class Client {
_client: GeyserClient;
_insecureXToken: string | undefined;
Expand Down
5 changes: 4 additions & 1 deletion yellowstone-grpc-client-nodejs/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type GetTransactionApi } from "@solana/rpc-api";
import { type Signature } from "@solana/keys/dist/types";
import { type Signature } from "@solana/keys";
import { type TransactionError } from "@solana/rpc-types";

const fakeGetTransition: GetTransactionApi["getTransaction"] = (
signature,
Expand All @@ -20,3 +21,5 @@ export type MapTransactionEncodingToReturnType = {
3: typeof json;
4: typeof jsonParsed;
};

export type TransactionErrorSolana = TransactionError;

0 comments on commit dae08b2

Please sign in to comment.