use alloc::boxed::Box;
use core::marker::PhantomData;
use ethereum_standards::IERC20;
use frame_support::{
defensive_assert,
traits::{
fungible::Inspect,
tokens::imbalance::{
ImbalanceAccounting, UnsafeConstructorDestructor, UnsafeManualAccounting,
},
OriginTrait,
},
};
use frame_system::pallet_prelude::OriginFor;
use pallet_revive::{
precompiles::alloy::{
primitives::{Address, U256 as EU256},
sol_types::SolCall,
},
AddressMapper, ContractResult, ExecConfig, MomentOf, TransactionLimits,
};
use sp_core::{Get, H160, H256, U256};
use sp_runtime::Weight;
use xcm::latest::prelude::*;
use xcm_executor::{
traits::{ConvertLocation, Error as MatchError, MatchesFungibles, TransactAsset},
AssetsInHolding,
};
type BalanceOf<T> = <<T as pallet_revive::Config>::Currency as Inspect<
<T as frame_system::Config>::AccountId,
>>::Balance;
pub struct ERC20Transactor<
T,
Matcher,
AccountIdConverter,
WeightLimit,
StorageDepositLimit,
AccountId,
TransfersCheckingAccount,
>(
PhantomData<(
T,
Matcher,
AccountIdConverter,
WeightLimit,
StorageDepositLimit,
AccountId,
TransfersCheckingAccount,
)>,
);
struct Erc20Credit(u128);
impl UnsafeConstructorDestructor<u128> for Erc20Credit {
fn unsafe_clone(&self) -> Box<dyn ImbalanceAccounting<u128>> {
Box::new(Erc20Credit(self.0))
}
fn forget_imbalance(&mut self) -> u128 {
let amount = self.0;
self.0 = 0;
amount
}
}
impl UnsafeManualAccounting<u128> for Erc20Credit {
fn saturating_subsume(&mut self, mut other: Box<dyn ImbalanceAccounting<u128>>) {
let amount = other.forget_imbalance();
self.0 = self.0.saturating_add(amount);
}
}
impl ImbalanceAccounting<u128> for Erc20Credit {
fn amount(&self) -> u128 {
self.0
}
fn saturating_take(&mut self, amount: u128) -> Box<dyn ImbalanceAccounting<u128>> {
let new = self.0.min(amount);
self.0 = self.0 - new;
Box::new(Erc20Credit(new))
}
}
impl<
AccountId: Eq + Clone,
T: pallet_revive::Config<AccountId = AccountId>,
AccountIdConverter: ConvertLocation<AccountId>,
Matcher: MatchesFungibles<H160, u128>,
WeightLimit: Get<Weight>,
StorageDepositLimit: Get<BalanceOf<T>>,
TransfersCheckingAccount: Get<AccountId>,
> TransactAsset
for ERC20Transactor<
T,
Matcher,
AccountIdConverter,
WeightLimit,
StorageDepositLimit,
AccountId,
TransfersCheckingAccount,
>
where
BalanceOf<T>: Into<U256> + TryFrom<U256>,
MomentOf<T>: Into<U256>,
T::Hash: frame_support::traits::IsType<H256>,
{
fn can_check_in(_origin: &Location, _what: &Asset, _context: &XcmContext) -> XcmResult {
Err(XcmError::Unimplemented)
}
fn check_in(_origin: &Location, _what: &Asset, _context: &XcmContext) {
}
fn can_check_out(_destination: &Location, _what: &Asset, _context: &XcmContext) -> XcmResult {
Err(XcmError::Unimplemented)
}
fn check_out(_destination: &Location, _what: &Asset, _context: &XcmContext) {
}
fn withdraw_asset_with_surplus(
what: &Asset,
who: &Location,
_context: Option<&XcmContext>,
) -> Result<(AssetsInHolding, Weight), XcmError> {
tracing::trace!(
target: "xcm::transactor::erc20::withdraw",
?what, ?who,
);
let (asset_id, amount) = Matcher::matches_fungibles(what)?;
let who = AccountIdConverter::convert_location(who)
.ok_or(MatchError::AccountIdConversionFailed)?;
let checking_account_eth = T::AddressMapper::to_address(&TransfersCheckingAccount::get());
let checking_address = Address::from(Into::<[u8; 20]>::into(checking_account_eth));
let weight_limit = WeightLimit::get();
let data =
IERC20::transferCall { to: checking_address, value: EU256::from(amount) }.abi_encode();
let ContractResult { result, weight_consumed, storage_deposit, .. } =
pallet_revive::Pallet::<T>::bare_call(
OriginFor::<T>::signed(who.clone()),
asset_id,
U256::zero(),
TransactionLimits::WeightAndDeposit {
weight_limit,
deposit_limit: StorageDepositLimit::get(),
},
data,
&ExecConfig::new_substrate_tx(),
);
let surplus = weight_limit.saturating_sub(weight_consumed);
tracing::trace!(target: "xcm::transactor::erc20::withdraw", ?weight_consumed, ?surplus, ?storage_deposit);
if let Ok(return_value) = result {
tracing::trace!(target: "xcm::transactor::erc20::withdraw", ?return_value, "Return value by withdraw_asset");
if return_value.did_revert() {
tracing::debug!(target: "xcm::transactor::erc20::withdraw", "ERC20 contract reverted");
Err(XcmError::FailedToTransactAsset("ERC20 contract reverted"))
} else {
let is_success = IERC20::transferCall::abi_decode_returns_validate(&return_value.data).map_err(|error| {
tracing::debug!(target: "xcm::transactor::erc20::withdraw", ?error, "ERC20 contract result couldn't decode");
XcmError::FailedToTransactAsset("ERC20 contract result couldn't decode")
})?;
if is_success {
tracing::trace!(target: "xcm::transactor::erc20::withdraw", "ERC20 contract was successful");
Ok((
AssetsInHolding::new_from_fungible_credit(
what.id.clone(),
Box::new(Erc20Credit(amount)),
),
surplus,
))
} else {
tracing::debug!(target: "xcm::transactor::erc20::withdraw", "contract transfer failed");
Err(XcmError::FailedToTransactAsset("ERC20 contract transfer failed"))
}
}
} else {
tracing::debug!(target: "xcm::transactor::erc20::withdraw", ?result, "Error");
Err(XcmError::FailedToTransactAsset("ERC20 contract execution errored"))
}
}
fn deposit_asset_with_surplus(
what: AssetsInHolding,
who: &Location,
_context: Option<&XcmContext>,
) -> Result<Weight, (AssetsInHolding, XcmError)> {
tracing::trace!(
target: "xcm::transactor::erc20::deposit",
?what, ?who,
);
defensive_assert!(what.len() == 1, "Trying to deposit more than one asset!");
let maybe = what
.fungible_assets_iter()
.next()
.and_then(|asset| Matcher::matches_fungibles(&asset).ok());
let (asset_contract_id, amount) = match maybe {
Some(inner) => inner,
None => return Err((what, MatchError::AssetNotHandled.into())),
};
let who = match AccountIdConverter::convert_location(who) {
Some(inner) => inner,
None => return Err((what, MatchError::AccountIdConversionFailed.into())),
};
let eth_address = T::AddressMapper::to_address(&who);
let address = Address::from(Into::<[u8; 20]>::into(eth_address));
let data = IERC20::transferCall { to: address, value: EU256::from(amount) }.abi_encode();
let weight_limit = WeightLimit::get();
let ContractResult { result, weight_consumed, storage_deposit, .. } =
pallet_revive::Pallet::<T>::bare_call(
OriginFor::<T>::signed(TransfersCheckingAccount::get()),
asset_contract_id,
U256::zero(),
TransactionLimits::WeightAndDeposit {
weight_limit,
deposit_limit: StorageDepositLimit::get(),
},
data,
&ExecConfig::new_substrate_tx(),
);
let surplus = weight_limit.saturating_sub(weight_consumed);
tracing::trace!(target: "xcm::transactor::erc20::deposit", ?weight_consumed, ?surplus, ?storage_deposit);
if let Ok(return_value) = result {
tracing::trace!(target: "xcm::transactor::erc20::deposit", ?return_value, "Return value");
if return_value.did_revert() {
tracing::debug!(target: "xcm::transactor::erc20::deposit", "Contract reverted");
Err((what, XcmError::FailedToTransactAsset("ERC20 contract reverted")))
} else {
match IERC20::transferCall::abi_decode_returns_validate(&return_value.data) {
Ok(true) => {
tracing::trace!(target: "xcm::transactor::erc20::deposit", "ERC20 contract was successful");
Ok(surplus)
},
Ok(false) => {
tracing::debug!(target: "xcm::transactor::erc20::deposit", "contract transfer failed");
Err((
what,
XcmError::FailedToTransactAsset("ERC20 contract transfer failed"),
))
},
Err(error) => {
tracing::debug!(target: "xcm::transactor::erc20::deposit", ?error, "ERC20 contract result couldn't decode");
Err((
what,
XcmError::FailedToTransactAsset(
"ERC20 contract result couldn't decode",
),
))
},
}
}
} else {
tracing::debug!(target: "xcm::transactor::erc20::deposit", ?result, "Error");
Err((what, XcmError::FailedToTransactAsset("ERC20 contract execution errored")))
}
}
}