Skip to content

Commit

Permalink
Introduce precompile v2 utils & minor refactoring (#1081)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard authored Nov 8, 2023
1 parent 8b7214c commit 08a4b7d
Show file tree
Hide file tree
Showing 107 changed files with 11,132 additions and 238 deletions.
209 changes: 153 additions & 56 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ ethers = { version = "2.0.9", default_features = false }
# Substrate
# (wasm)
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43", default-features = false }
sp-core-hashing = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43", default-features = false }
Expand Down Expand Up @@ -301,6 +302,7 @@ assets-chain-extension-types = { path = "./chain-extensions/types/assets", defau
unified-accounts-chain-extension-types = { path = "./chain-extensions/types/unified-accounts", default-features = false }

precompile-utils = { path = "./precompiles/utils", default-features = false }
precompile-utils-v2 = { path = "./precompiles/utils_v2", default-features = false }

local-runtime = { path = "./runtime/local", default-features = false }
shibuya-runtime = { path = "./runtime/shibuya", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion bin/collator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "astar-collator"
version = "5.23.0"
version = "5.24.0"
description = "Astar collator implementation in Rust."
build = "build.rs"
default-run = "astar-collator"
Expand Down
22 changes: 15 additions & 7 deletions precompiles/assets-erc20/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

#![cfg_attr(not(feature = "std"), no_std)]

use fp_evm::{IsPrecompileResult, PrecompileHandle, PrecompileOutput};
use fp_evm::{IsPrecompileResult, PrecompileFailure, PrecompileHandle, PrecompileOutput};
use frame_support::traits::fungibles::approvals::Inspect as ApprovalInspect;
use frame_support::traits::fungibles::metadata::Inspect as MetadataInspect;
use frame_support::traits::fungibles::Inspect;
Expand All @@ -47,8 +47,8 @@ use frame_support::{
};
use pallet_evm::{AddressMapping, PrecompileSet};
use precompile_utils::{
keccak256, succeed, Address, Bytes, EvmData, EvmDataWriter, EvmResult, FunctionModifier,
LogExt, LogsBuilder, PrecompileHandleExt, RuntimeHelper,
keccak256, revert, succeed, Address, Bytes, EvmData, EvmDataWriter, EvmResult,
FunctionModifier, LogExt, LogsBuilder, PrecompileHandleExt, RuntimeHelper,
};
use sp_runtime::traits::{Bounded, Zero};

Expand Down Expand Up @@ -358,12 +358,13 @@ where
input.expect_arguments(2)?;

let to: H160 = input.read::<Address>()?.into();
let amount = input.read::<BalanceOf<Runtime, Instance>>()?;
let amount: U256 = input.read()?;

// Build call with origin.
{
let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);
let to = Runtime::AddressMapping::into_account_id(to);
let amount = Self::u256_to_amount(amount)?;

// Dispatch call (if enough gas).
RuntimeHelper::<Runtime>::try_dispatch(
Expand Down Expand Up @@ -400,13 +401,14 @@ where

let from: H160 = input.read::<Address>()?.into();
let to: H160 = input.read::<Address>()?.into();
let amount = input.read::<BalanceOf<Runtime, Instance>>()?;
let amount: U256 = input.read()?;

{
let caller: Runtime::AccountId =
Runtime::AddressMapping::into_account_id(handle.context().caller);
let from: Runtime::AccountId = Runtime::AddressMapping::into_account_id(from);
let to: Runtime::AccountId = Runtime::AddressMapping::into_account_id(to);
let amount = Self::u256_to_amount(amount)?;

// If caller is "from", it can spend as much as it wants from its own balance.
if caller != from {
Expand Down Expand Up @@ -539,7 +541,7 @@ where
input.expect_arguments(2)?;

let beneficiary: H160 = input.read::<Address>()?.into();
let amount = input.read::<BalanceOf<Runtime, Instance>>()?;
let amount = Self::u256_to_amount(input.read::<U256>()?)?;

let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);
let beneficiary = Runtime::AddressMapping::into_account_id(beneficiary);
Expand All @@ -566,7 +568,7 @@ where
input.expect_arguments(2)?;

let who: H160 = input.read::<Address>()?.into();
let amount = input.read::<BalanceOf<Runtime, Instance>>()?;
let amount = Self::u256_to_amount(input.read::<U256>()?)?;

let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);
let who = Runtime::AddressMapping::into_account_id(who);
Expand All @@ -584,4 +586,10 @@ where

Ok(succeed(EvmDataWriter::new().write(true).build()))
}

fn u256_to_amount(value: U256) -> Result<BalanceOf<Runtime, Instance>, PrecompileFailure> {
value
.try_into()
.map_err(|_| revert("Error processing amount"))
}
}
Loading

0 comments on commit 08a4b7d

Please sign in to comment.