Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

feat(providers): Log methods for PendingTransaction #884

Merged
merged 3 commits into from
Feb 8, 2022

Conversation

wolflo
Copy link
Contributor

@wolflo wolflo commented Feb 8, 2022

Motivation

I frequently want to print the transaction hash of a pending transaction before it is mined. Currently, this is pretty verbose, because you need a let binding for the transaction:

let tx = token.transfer(recipient, amt);
let pending = tx.send().await?;
println!("Tx hash: {:?}", *pending);
let mined = pending.await?;

Solution

Add two convenience methods on the PendingTransaction<_> type, log() and log_msg().

let mined = token.transfer(recipient, amt).send().await?.log_msg("Tx hash").await?;

Another approach that isn't just for printing might be to add a method to PendingTransaction<_> that returns (TxHash, Self), which would turn the above into:

let (hash, fut) = token.transfer(recipient, amt).send().await?.hash();
println!("Tx hash: {:?}", hash);
let mined = fut.await?;

PR Checklist

  • Added Tests
  • Added Documentation
  • Updated the changelog

@mattsse
Copy link
Collaborator

mattsse commented Feb 8, 2022

I can see how this is useful,

alternatively we could make this akin to the std::iter::inscpect function:

pub fn inspect<F>(self, f: F) -> Self where F: FnMut(Self) {
   f(&self);
   self
}

so you can simply to

let mined = token.transfer(recipient, amt).send().await?.inspect(|tx| println!(".... {}", *tx)).await?;

@wolflo
Copy link
Contributor Author

wolflo commented Feb 8, 2022

I like that too. I probably like the current version the least, because it's obviously use-case specific. Though maybe we want to keep log() and/or log_msg() in conjunction with a more useful version just for the succinctness.

@gakonst gakonst merged commit b657e47 into gakonst:master Feb 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants