@nomicfoundation/edr 0.10.0 → 0.12.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,291 +0,0 @@
1
- use std::{
2
- num::NonZeroU64,
3
- path::PathBuf,
4
- time::{Duration, SystemTime},
5
- };
6
-
7
- use edr_eth::HashMap;
8
- use edr_provider::AccountConfig;
9
- use napi::{
10
- bindgen_prelude::{BigInt, Buffer},
11
- Either,
12
- };
13
- use napi_derive::napi;
14
-
15
- use crate::{account::GenesisAccount, block::BlobGas, cast::TryCast, config::SpecId};
16
-
17
- /// Configuration for a chain
18
- #[napi(object)]
19
- pub struct ChainConfig {
20
- /// The chain ID
21
- pub chain_id: BigInt,
22
- /// The chain's supported hardforks
23
- pub hardforks: Vec<HardforkActivation>,
24
- }
25
-
26
- /// Configuration for forking a blockchain
27
- #[napi(object)]
28
- pub struct ForkConfig {
29
- /// The URL of the JSON-RPC endpoint to fork from
30
- pub json_rpc_url: String,
31
- /// The block number to fork from. If not provided, the latest safe block is
32
- /// used.
33
- pub block_number: Option<BigInt>,
34
- /// The HTTP headers to use when making requests to the JSON-RPC endpoint
35
- pub http_headers: Option<Vec<HttpHeader>>,
36
- }
37
-
38
- #[napi(object)]
39
- pub struct HttpHeader {
40
- pub name: String,
41
- pub value: String,
42
- }
43
-
44
- /// Configuration for a hardfork activation
45
- #[napi(object)]
46
- pub struct HardforkActivation {
47
- /// The block number at which the hardfork is activated
48
- pub block_number: BigInt,
49
- /// The activated hardfork
50
- pub spec_id: SpecId,
51
- }
52
-
53
- #[napi(string_enum)]
54
- #[doc = "The type of ordering to use when selecting blocks to mine."]
55
- pub enum MineOrdering {
56
- #[doc = "Insertion order"]
57
- Fifo,
58
- #[doc = "Effective miner fee"]
59
- Priority,
60
- }
61
-
62
- /// Configuration for the provider's mempool.
63
- #[napi(object)]
64
- pub struct MemPoolConfig {
65
- pub order: MineOrdering,
66
- }
67
-
68
- #[napi(object)]
69
- pub struct IntervalRange {
70
- pub min: BigInt,
71
- pub max: BigInt,
72
- }
73
-
74
- /// Configuration for the provider's miner.
75
- #[napi(object)]
76
- pub struct MiningConfig {
77
- pub auto_mine: bool,
78
- pub interval: Option<Either<BigInt, IntervalRange>>,
79
- pub mem_pool: MemPoolConfig,
80
- }
81
-
82
- /// Configuration for a provider
83
- #[napi(object)]
84
- pub struct ProviderConfig {
85
- /// Whether to allow blocks with the same timestamp
86
- pub allow_blocks_with_same_timestamp: bool,
87
- /// Whether to allow unlimited contract size
88
- pub allow_unlimited_contract_size: bool,
89
- /// Whether to return an `Err` when `eth_call` fails
90
- pub bail_on_call_failure: bool,
91
- /// Whether to return an `Err` when a `eth_sendTransaction` fails
92
- pub bail_on_transaction_failure: bool,
93
- /// The gas limit of each block
94
- pub block_gas_limit: BigInt,
95
- /// The directory to cache remote JSON-RPC responses
96
- pub cache_dir: Option<String>,
97
- /// The chain ID of the blockchain
98
- pub chain_id: BigInt,
99
- /// The configuration for chains
100
- pub chains: Vec<ChainConfig>,
101
- /// The address of the coinbase
102
- pub coinbase: Buffer,
103
- /// Enables RIP-7212
104
- pub enable_rip_7212: bool,
105
- /// The configuration for forking a blockchain. If not provided, a local
106
- /// blockchain will be created
107
- pub fork: Option<ForkConfig>,
108
- /// The genesis accounts of the blockchain
109
- pub genesis_accounts: Vec<GenesisAccount>,
110
- /// The hardfork of the blockchain
111
- pub hardfork: SpecId,
112
- /// The initial base fee per gas of the blockchain. Required for EIP-1559
113
- /// transactions and later
114
- pub initial_base_fee_per_gas: Option<BigInt>,
115
- /// The initial blob gas of the blockchain. Required for EIP-4844
116
- pub initial_blob_gas: Option<BlobGas>,
117
- /// The initial date of the blockchain, in seconds since the Unix epoch
118
- pub initial_date: Option<BigInt>,
119
- /// The initial parent beacon block root of the blockchain. Required for
120
- /// EIP-4788
121
- pub initial_parent_beacon_block_root: Option<Buffer>,
122
- /// The minimum gas price of the next block.
123
- pub min_gas_price: BigInt,
124
- /// The configuration for the miner
125
- pub mining: MiningConfig,
126
- /// The network ID of the blockchain
127
- pub network_id: BigInt,
128
- }
129
-
130
- impl TryFrom<ForkConfig> for edr_provider::hardhat_rpc_types::ForkConfig {
131
- type Error = napi::Error;
132
-
133
- fn try_from(value: ForkConfig) -> Result<Self, Self::Error> {
134
- let block_number: Option<u64> = value.block_number.map(TryCast::try_cast).transpose()?;
135
- let http_headers = value.http_headers.map(|http_headers| {
136
- http_headers
137
- .into_iter()
138
- .map(|HttpHeader { name, value }| (name, value))
139
- .collect()
140
- });
141
-
142
- Ok(Self {
143
- json_rpc_url: value.json_rpc_url,
144
- block_number,
145
- http_headers,
146
- })
147
- }
148
- }
149
-
150
- impl From<MemPoolConfig> for edr_provider::MemPoolConfig {
151
- fn from(value: MemPoolConfig) -> Self {
152
- Self {
153
- order: value.order.into(),
154
- }
155
- }
156
- }
157
-
158
- impl From<MineOrdering> for edr_evm::MineOrdering {
159
- fn from(value: MineOrdering) -> Self {
160
- match value {
161
- MineOrdering::Fifo => Self::Fifo,
162
- MineOrdering::Priority => Self::Priority,
163
- }
164
- }
165
- }
166
-
167
- impl TryFrom<MiningConfig> for edr_provider::MiningConfig {
168
- type Error = napi::Error;
169
-
170
- fn try_from(value: MiningConfig) -> Result<Self, Self::Error> {
171
- let mem_pool = value.mem_pool.into();
172
-
173
- let interval = value
174
- .interval
175
- .map(|interval| {
176
- let interval = match interval {
177
- Either::A(interval) => {
178
- let interval = interval.try_cast()?;
179
- let interval = NonZeroU64::new(interval).ok_or_else(|| {
180
- napi::Error::new(
181
- napi::Status::GenericFailure,
182
- "Interval must be greater than 0",
183
- )
184
- })?;
185
-
186
- edr_provider::IntervalConfig::Fixed(interval)
187
- }
188
- Either::B(IntervalRange { min, max }) => edr_provider::IntervalConfig::Range {
189
- min: min.try_cast()?,
190
- max: max.try_cast()?,
191
- },
192
- };
193
-
194
- napi::Result::Ok(interval)
195
- })
196
- .transpose()?;
197
-
198
- Ok(Self {
199
- auto_mine: value.auto_mine,
200
- interval,
201
- mem_pool,
202
- })
203
- }
204
- }
205
-
206
- impl TryFrom<ProviderConfig> for edr_provider::ProviderConfig {
207
- type Error = napi::Error;
208
-
209
- fn try_from(value: ProviderConfig) -> Result<Self, Self::Error> {
210
- let chains = value
211
- .chains
212
- .into_iter()
213
- .map(
214
- |ChainConfig {
215
- chain_id,
216
- hardforks,
217
- }| {
218
- let hardforks = hardforks
219
- .into_iter()
220
- .map(
221
- |HardforkActivation {
222
- block_number,
223
- spec_id,
224
- }| {
225
- let block_number = block_number.try_cast()?;
226
- let spec_id = spec_id.into();
227
-
228
- Ok((block_number, spec_id))
229
- },
230
- )
231
- .collect::<napi::Result<Vec<_>>>()?;
232
-
233
- let chain_id = chain_id.try_cast()?;
234
- Ok((chain_id, edr_eth::spec::HardforkActivations::new(hardforks)))
235
- },
236
- )
237
- .collect::<napi::Result<_>>()?;
238
-
239
- let block_gas_limit =
240
- NonZeroU64::new(value.block_gas_limit.try_cast()?).ok_or_else(|| {
241
- napi::Error::new(
242
- napi::Status::GenericFailure,
243
- "Block gas limit must be greater than 0",
244
- )
245
- })?;
246
-
247
- Ok(Self {
248
- accounts: value
249
- .genesis_accounts
250
- .into_iter()
251
- .map(AccountConfig::try_from)
252
- .collect::<napi::Result<Vec<_>>>()?,
253
- allow_blocks_with_same_timestamp: value.allow_blocks_with_same_timestamp,
254
- allow_unlimited_contract_size: value.allow_unlimited_contract_size,
255
- bail_on_call_failure: value.bail_on_call_failure,
256
- bail_on_transaction_failure: value.bail_on_transaction_failure,
257
- block_gas_limit,
258
- cache_dir: PathBuf::from(
259
- value
260
- .cache_dir
261
- .unwrap_or(String::from(edr_defaults::CACHE_DIR)),
262
- ),
263
- chain_id: value.chain_id.try_cast()?,
264
- chains,
265
- coinbase: value.coinbase.try_cast()?,
266
- enable_rip_7212: value.enable_rip_7212,
267
- fork: value.fork.map(TryInto::try_into).transpose()?,
268
- genesis_accounts: HashMap::new(),
269
- hardfork: value.hardfork.into(),
270
- initial_base_fee_per_gas: value
271
- .initial_base_fee_per_gas
272
- .map(TryCast::try_cast)
273
- .transpose()?,
274
- initial_blob_gas: value.initial_blob_gas.map(TryInto::try_into).transpose()?,
275
- initial_date: value
276
- .initial_date
277
- .map(|date| {
278
- let elapsed_since_epoch = Duration::from_secs(date.try_cast()?);
279
- napi::Result::Ok(SystemTime::UNIX_EPOCH + elapsed_since_epoch)
280
- })
281
- .transpose()?,
282
- initial_parent_beacon_block_root: value
283
- .initial_parent_beacon_block_root
284
- .map(TryCast::try_cast)
285
- .transpose()?,
286
- mining: value.mining.try_into()?,
287
- min_gas_price: value.min_gas_price.try_cast()?,
288
- network_id: value.network_id.try_cast()?,
289
- })
290
- }
291
- }
package/src/subscribe.rs DELETED
@@ -1,63 +0,0 @@
1
- use edr_eth::B256;
2
- use napi::{
3
- bindgen_prelude::BigInt,
4
- threadsafe_function::{
5
- ErrorStrategy, ThreadSafeCallContext, ThreadsafeFunction, ThreadsafeFunctionCallMode,
6
- },
7
- Env, JsFunction,
8
- };
9
- use napi_derive::napi;
10
-
11
- #[derive(Clone)]
12
- pub struct SubscriberCallback {
13
- inner: ThreadsafeFunction<edr_provider::SubscriptionEvent, ErrorStrategy::Fatal>,
14
- }
15
-
16
- impl SubscriberCallback {
17
- pub fn new(env: &Env, subscription_event_callback: JsFunction) -> napi::Result<Self> {
18
- let mut callback = subscription_event_callback.create_threadsafe_function(
19
- 0,
20
- |ctx: ThreadSafeCallContext<edr_provider::SubscriptionEvent>| {
21
- // SubscriptionEvent
22
- let mut event = ctx.env.create_object()?;
23
-
24
- ctx.env
25
- .create_bigint_from_words(false, ctx.value.filter_id.as_limbs().to_vec())
26
- .and_then(|filter_id| event.set_named_property("filterId", filter_id))?;
27
-
28
- let result = match ctx.value.result {
29
- edr_provider::SubscriptionEventData::Logs(logs) => ctx.env.to_js_value(&logs),
30
- edr_provider::SubscriptionEventData::NewHeads(block) => {
31
- let block = edr_rpc_eth::Block::<B256>::from(block);
32
- ctx.env.to_js_value(&block)
33
- }
34
- edr_provider::SubscriptionEventData::NewPendingTransactions(tx_hash) => {
35
- ctx.env.to_js_value(&tx_hash)
36
- }
37
- }?;
38
-
39
- event.set_named_property("result", result)?;
40
-
41
- Ok(vec![event])
42
- },
43
- )?;
44
-
45
- // Maintain a weak reference to the function to avoid the event loop from
46
- // exiting.
47
- callback.unref(env)?;
48
-
49
- Ok(Self { inner: callback })
50
- }
51
-
52
- pub fn call(&self, event: edr_provider::SubscriptionEvent) {
53
- // This is blocking because it's important that the subscription events are
54
- // in-order
55
- self.inner.call(event, ThreadsafeFunctionCallMode::Blocking);
56
- }
57
- }
58
-
59
- #[napi(object)]
60
- pub struct SubscriptionEvent {
61
- pub filter_id: BigInt,
62
- pub result: serde_json::Value,
63
- }