Skip to content

Commit 21a1da5

Browse files
Refactor EVM operations to module functions (#6056)
* Refactor EVM operations to module functions * Bump impl version
1 parent 4331228 commit 21a1da5

File tree

1 file changed

+95
-34
lines changed

1 file changed

+95
-34
lines changed

src/lib.rs

Lines changed: 95 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -256,19 +256,14 @@ decl_module! {
256256
let sender = ensure_signed(origin)?;
257257
let source = T::ConvertAccountId::convert_account_id(&sender);
258258

259-
Self::execute_evm(
259+
Self::execute_call(
260260
source,
261+
target,
262+
input,
261263
value,
262264
gas_limit,
263265
gas_price,
264266
nonce,
265-
|executor| ((), executor.transact_call(
266-
source,
267-
target,
268-
value,
269-
input,
270-
gas_limit as usize,
271-
)),
272267
).map_err(Into::into)
273268
}
274269

@@ -291,22 +286,13 @@ decl_module! {
291286
let sender = ensure_signed(origin)?;
292287
let source = T::ConvertAccountId::convert_account_id(&sender);
293288

294-
let create_address = Self::execute_evm(
289+
let create_address = Self::execute_create(
295290
source,
291+
init,
296292
value,
297293
gas_limit,
298294
gas_price,
299-
nonce,
300-
|executor| {
301-
(executor.create_address(
302-
evm::CreateScheme::Legacy { caller: source },
303-
), executor.transact_create(
304-
source,
305-
value,
306-
init,
307-
gas_limit as usize,
308-
))
309-
},
295+
nonce
310296
)?;
311297

312298
Module::<T>::deposit_event(Event::<T>::Created(create_address));
@@ -332,24 +318,14 @@ decl_module! {
332318
let sender = ensure_signed(origin)?;
333319
let source = T::ConvertAccountId::convert_account_id(&sender);
334320

335-
let code_hash = H256::from_slice(Keccak256::digest(&init).as_slice());
336-
let create_address = Self::execute_evm(
321+
let create_address = Self::execute_create2(
337322
source,
323+
init,
324+
salt,
338325
value,
339326
gas_limit,
340327
gas_price,
341-
nonce,
342-
|executor| {
343-
(executor.create_address(
344-
evm::CreateScheme::Create2 { caller: source, code_hash, salt },
345-
), executor.transact_create2(
346-
source,
347-
value,
348-
init,
349-
salt,
350-
gas_limit as usize,
351-
))
352-
},
328+
nonce
353329
)?;
354330

355331
Module::<T>::deposit_event(Event::<T>::Created(create_address));
@@ -391,6 +367,91 @@ impl<T: Trait> Module<T> {
391367
AccountStorages::remove_prefix(address);
392368
}
393369

370+
/// Execute a create transaction on behalf of given sender.
371+
pub fn execute_create(
372+
source: H160,
373+
init: Vec<u8>,
374+
value: U256,
375+
gas_limit: u32,
376+
gas_price: U256,
377+
nonce: Option<U256>
378+
) -> Result<H160, Error<T>> {
379+
Self::execute_evm(
380+
source,
381+
value,
382+
gas_limit,
383+
gas_price,
384+
nonce,
385+
|executor| {
386+
(executor.create_address(
387+
evm::CreateScheme::Legacy { caller: source },
388+
), executor.transact_create(
389+
source,
390+
value,
391+
init,
392+
gas_limit as usize,
393+
))
394+
},
395+
)
396+
}
397+
398+
/// Execute a create2 transaction on behalf of a given sender.
399+
pub fn execute_create2(
400+
source: H160,
401+
init: Vec<u8>,
402+
salt: H256,
403+
value: U256,
404+
gas_limit: u32,
405+
gas_price: U256,
406+
nonce: Option<U256>
407+
) -> Result<H160, Error<T>> {
408+
let code_hash = H256::from_slice(Keccak256::digest(&init).as_slice());
409+
Self::execute_evm(
410+
source,
411+
value,
412+
gas_limit,
413+
gas_price,
414+
nonce,
415+
|executor| {
416+
(executor.create_address(
417+
evm::CreateScheme::Create2 { caller: source, code_hash, salt },
418+
), executor.transact_create2(
419+
source,
420+
value,
421+
init,
422+
salt,
423+
gas_limit as usize,
424+
))
425+
},
426+
)
427+
}
428+
429+
/// Execute a call transaction on behalf of a given sender.
430+
pub fn execute_call(
431+
source: H160,
432+
target: H160,
433+
input: Vec<u8>,
434+
value: U256,
435+
gas_limit: u32,
436+
gas_price: U256,
437+
nonce: Option<U256>,
438+
) -> Result<(), Error<T>> {
439+
Self::execute_evm(
440+
source,
441+
value,
442+
gas_limit,
443+
gas_price,
444+
nonce,
445+
|executor| ((), executor.transact_call(
446+
source,
447+
target,
448+
value,
449+
input,
450+
gas_limit as usize,
451+
)),
452+
)
453+
}
454+
394455
/// Execute an EVM operation.
395456
fn execute_evm<F, R>(
396457
source: H160,

0 commit comments

Comments
 (0)