client: add tx pretty print to rust (#189)

This commit is contained in:
Kirill Fomichev 2023-10-03 18:09:05 +04:00 committed by GitHub
parent f559fa8f60
commit ed4350d316
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 3 deletions

View File

@ -13,6 +13,7 @@ The minor version will be incremented upon a breaking change and the patch versi
### Features
- proto: add mod `convert_to`, `convert_from` ([#190](https://github.com/rpcpool/yellowstone-grpc/pull/190)).
- client: add tx pretty print to rust ([#189](https://github.com/rpcpool/yellowstone-grpc/pull/189)).
### Fixes

2
Cargo.lock generated
View File

@ -4493,7 +4493,9 @@ dependencies = [
"hex",
"log",
"maplit",
"serde_json",
"solana-sdk",
"solana-transaction-status",
"tokio",
"yellowstone-grpc-client",
"yellowstone-grpc-proto",

View File

@ -20,7 +20,9 @@ futures = "0.3.24"
hex = "0.4.3"
log = { version = "0.4.14", features = ["std"] }
maplit = "1.0.2"
serde_json = "1.0.86"
solana-sdk = "=1.16.14"
solana-transaction-status = "=1.16.14"
tokio = { version = "1.21.2", features = ["rt-multi-thread", "macros", "time"] }
yellowstone-grpc-client = { path = "../../yellowstone-grpc-client" }
yellowstone-grpc-proto = { path = "../../yellowstone-grpc-proto" }

View File

@ -3,10 +3,11 @@ use {
clap::{Parser, Subcommand, ValueEnum},
futures::{future::TryFutureExt, sink::SinkExt, stream::StreamExt},
log::{error, info},
solana_sdk::pubkey::Pubkey,
solana_sdk::{pubkey::Pubkey, signature::Signature},
solana_transaction_status::{EncodedTransactionWithStatusMeta, UiTransactionEncoding},
std::{
collections::HashMap,
env,
env, fmt,
sync::{Arc, Mutex},
time::Duration,
},
@ -20,7 +21,7 @@ use {
SubscribeRequestFilterAccountsFilter, SubscribeRequestFilterAccountsFilterMemcmp,
SubscribeRequestFilterBlocks, SubscribeRequestFilterBlocksMeta,
SubscribeRequestFilterEntry, SubscribeRequestFilterSlots,
SubscribeRequestFilterTransactions, SubscribeUpdateAccount,
SubscribeRequestFilterTransactions, SubscribeUpdateAccount, SubscribeUpdateTransaction,
},
tonic::service::Interceptor,
},
@ -353,6 +354,48 @@ impl From<SubscribeUpdateAccount> for AccountPretty {
}
}
#[allow(dead_code)]
pub struct TransactionPretty {
slot: u64,
signature: Signature,
is_vote: bool,
tx: EncodedTransactionWithStatusMeta,
}
impl fmt::Debug for TransactionPretty {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
struct TxWrap<'a>(&'a EncodedTransactionWithStatusMeta);
impl<'a> fmt::Debug for TxWrap<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let serialized = serde_json::to_string(self.0).expect("failed to serialize");
fmt::Display::fmt(&serialized, f)
}
}
f.debug_struct("TransactionPretty")
.field("slot", &self.slot)
.field("signature", &self.signature)
.field("is_vote", &self.is_vote)
.field("tx", &TxWrap(&self.tx))
.finish()
}
}
impl From<SubscribeUpdateTransaction> for TransactionPretty {
fn from(SubscribeUpdateTransaction { transaction, slot }: SubscribeUpdateTransaction) -> Self {
let tx = transaction.expect("should be defined");
Self {
slot,
signature: Signature::try_from(tx.signature.as_slice()).expect("valid signature"),
is_vote: tx.is_vote,
tx: yellowstone_grpc_proto::convert_from::create_tx_with_meta(tx)
.expect("valid tx with meta")
.encode(UiTransactionEncoding::Base64, Some(u8::MAX), true)
.expect("failed to encode"),
}
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
env::set_var(
@ -485,6 +528,14 @@ async fn geyser_subscribe(
);
continue;
}
Some(UpdateOneof::Transaction(tx)) => {
let tx: TransactionPretty = tx.into();
info!(
"new transaction update: filters {:?}, transaction: {:#?}",
msg.filters, tx
);
continue;
}
_ => {}
}
info!("new message: {msg:?}")