referrerpolicy=no-referrer-when-downgrade

pallet_revive_eth_rpc/
example.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Example utilities
use crate::{EthRpcClient, ReceiptInfo};
use anyhow::Context;
use pallet_revive::evm::{
	Account, BlockTag, Bytes, GenericTransaction, TransactionLegacyUnsigned, H160, H256, U256,
};
use std::sync::Arc;

/// Transaction builder.
pub struct TransactionBuilder<Client: EthRpcClient + Sync + Send> {
	client: Arc<Client>,
	signer: Account,
	value: U256,
	input: Bytes,
	to: Option<H160>,
	mutate: Box<dyn FnOnce(&mut TransactionLegacyUnsigned)>,
}

#[derive(Debug)]
pub struct SubmittedTransaction<Client: EthRpcClient + Sync + Send> {
	tx: GenericTransaction,
	hash: H256,
	client: Arc<Client>,
}

impl<Client: EthRpcClient + Sync + Send> SubmittedTransaction<Client> {
	/// Get the hash of the transaction.
	pub fn hash(&self) -> H256 {
		self.hash
	}

	/// The gas sent with the transaction.
	pub fn gas(&self) -> U256 {
		self.tx.gas.unwrap()
	}

	/// Wait for the receipt of the transaction.
	pub async fn wait_for_receipt(&self) -> anyhow::Result<ReceiptInfo> {
		let hash = self.hash();
		for _ in 0..30 {
			tokio::time::sleep(std::time::Duration::from_secs(2)).await;
			let receipt = self.client.get_transaction_receipt(hash).await?;
			if let Some(receipt) = receipt {
				if receipt.is_success() {
					assert!(
						self.gas() > receipt.gas_used,
						"Gas used should be less than gas estimated."
					);
					return Ok(receipt)
				} else {
					anyhow::bail!("Transaction failed")
				}
			}
		}

		anyhow::bail!("Timeout, failed to get receipt")
	}
}

impl<Client: EthRpcClient + Send + Sync> TransactionBuilder<Client> {
	pub fn new(client: &Arc<Client>) -> Self {
		Self {
			client: Arc::clone(client),
			signer: Account::default(),
			value: U256::zero(),
			input: Bytes::default(),
			to: None,
			mutate: Box::new(|_| {}),
		}
	}
	/// Set the signer.
	pub fn signer(mut self, signer: Account) -> Self {
		self.signer = signer;
		self
	}

	/// Set the value.
	pub fn value(mut self, value: U256) -> Self {
		self.value = value;
		self
	}

	/// Set the input.
	pub fn input(mut self, input: Vec<u8>) -> Self {
		self.input = Bytes(input);
		self
	}

	/// Set the destination.
	pub fn to(mut self, to: H160) -> Self {
		self.to = Some(to);
		self
	}

	/// Set a mutation function, that mutates the transaction before sending.
	pub fn mutate(mut self, mutate: impl FnOnce(&mut TransactionLegacyUnsigned) + 'static) -> Self {
		self.mutate = Box::new(mutate);
		self
	}

	/// Call eth_call to get the result of a view function
	pub async fn eth_call(self) -> anyhow::Result<Vec<u8>> {
		let TransactionBuilder { client, signer, value, input, to, .. } = self;

		let from = signer.address();
		let result = client
			.call(
				GenericTransaction {
					from: Some(from),
					input: input.into(),
					value: Some(value),
					to,
					..Default::default()
				},
				None,
			)
			.await
			.with_context(|| "eth_call failed")?;
		Ok(result.0)
	}

	/// Send the transaction.
	pub async fn send(self) -> anyhow::Result<SubmittedTransaction<Client>> {
		let TransactionBuilder { client, signer, value, input, to, mutate } = self;

		let from = signer.address();
		let chain_id = Some(client.chain_id().await?);
		let gas_price = client.gas_price().await?;
		let nonce = client
			.get_transaction_count(from, BlockTag::Latest.into())
			.await
			.with_context(|| "Failed to fetch account nonce")?;

		let gas = client
			.estimate_gas(
				GenericTransaction {
					from: Some(from),
					input: input.clone().into(),
					value: Some(value),
					gas_price: Some(gas_price),
					to,
					..Default::default()
				},
				None,
			)
			.await
			.with_context(|| "Failed to fetch gas estimate")?;

		println!("Gas estimate: {gas:?}");
		let mut unsigned_tx = TransactionLegacyUnsigned {
			gas,
			nonce,
			to,
			value,
			input,
			gas_price,
			chain_id,
			..Default::default()
		};

		mutate(&mut unsigned_tx);

		let signed_tx = signer.sign_transaction(unsigned_tx.into());
		let bytes = signed_tx.signed_payload();

		let hash = client
			.send_raw_transaction(bytes.into())
			.await
			.with_context(|| "transaction failed")?;

		Ok(SubmittedTransaction {
			tx: GenericTransaction::from_signed(signed_tx, Some(from)),
			hash,
			client,
		})
	}
}