Sitelet https://github.com/SharkHand3/examples/commit/28bef7927dd52cdbf659dd268438af9103829759
Skip to content

Commit 28bef79

Browse files
authored
feat: decoding log from a receipt (alloy-rs#183)
1 parent 02aabf5 commit 28bef79

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//! Example depicting how to decode logs present in a transaction receipt.
2+
3+
use alloy::{providers::ProviderBuilder, sol};
4+
5+
sol! {
6+
#[sol(rpc, bytecode = "6080604052348015600e575f80fd5b506101858061001c5f395ff3fe608060405234801561000f575f80fd5b506004361061003f575f3560e01c806306661abd146100435780632baeceb71461005d578063d09de08a14610067575b5f80fd5b61004b5f5481565b60405190815260200160405180910390f35b61006561006f565b005b6100656100c7565b5f545f0361007957565b60015f8082825461008a9190610123565b90915550505f546040519081527f32814a5bdfd1b8c3d76c49c54e043d6e8aa93d197a09e16599b567135503f748906020015b60405180910390a1565b60015f808282546100d8919061013c565b90915550505f546040519081527f51af157c2eee40f68107a47a49c32fbbeb0a3c9e5cd37aa56e88e6be92368a81906020016100bd565b634e487b7160e01b5f52601160045260245ffd5b818103818111156101365761013661010f565b92915050565b808201808211156101365761013661010f56fea2646970667358221220d955d2934ffc8ca69c62bc3b116ad64fca0144d8bd6cf5a635ae1954025d810c64736f6c63430008190033")]
7+
contract Counter {
8+
uint256 public count;
9+
10+
event Increment(uint256 value);
11+
event Decrement(uint256 value);
12+
13+
function increment() public {
14+
count += 1;
15+
emit Increment(count);
16+
}
17+
18+
function decrement() public {
19+
if (count == 0) return;
20+
count -= 1;
21+
emit Decrement(count);
22+
}
23+
}
24+
}
25+
26+
#[tokio::main]
27+
async fn main() -> eyre::Result<()> {
28+
// Create an AnvilProvider
29+
// Ensure `anvil` is available in $PATH.
30+
let provider = ProviderBuilder::new().on_anvil_with_wallet();
31+
32+
// Deploy the `Counter` contract.
33+
let counter = Counter::deploy(&provider).await?;
34+
35+
// Send a transaction to increment the counter.
36+
let increment = counter.increment().send().await?;
37+
38+
// Get the receipt of the transaction.
39+
let receipt = increment.get_receipt().await?;
40+
41+
// Decode the `Increment` event log.
42+
// Returns None, if there were no logs emitted or if the emitted logs cannot be decoded as
43+
// `Increment`.
44+
let maybe_log = receipt.decoded_log::<Counter::Increment>();
45+
46+
let Some(increment_log) = maybe_log else { eyre::bail!("Increment not emitted") };
47+
48+
assert_eq!(increment_log.address, *counter.address());
49+
let Counter::Increment { value } = increment_log.data;
50+
println!("Incremented value: {value}");
51+
52+
// Attempt to decode as a `Decrement` event log.
53+
let decrement_log = receipt.decoded_log::<Counter::Decrement>();
54+
55+
// None, as there is no `Decrement` event emitted.
56+
assert!(decrement_log.is_none());
57+
58+
Ok(())
59+
}

0 commit comments

Comments
 (0)