Skip to content

Commit

Permalink
currencies transfer erc20 will charge gas (#2073)
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci authored May 4, 2022
1 parent 9417110 commit af16e52
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 6 deletions.
1 change: 1 addition & 0 deletions ecosystem-modules/starport/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ impl module_currencies::Config for Runtime {
type WeightInfo = ();
type AddressMapping = MockAddressMapping;
type EVMBridge = ();
type GasToWeight = ();
type SweepOrigin = EnsureSignedBy<One, AccountId>;
type OnDust = ();
}
Expand Down
13 changes: 9 additions & 4 deletions modules/currencies/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ use orml_traits::{
use primitives::{evm::EvmAddress, CurrencyId};
use sp_io::hashing::blake2_256;
use sp_runtime::{
traits::{CheckedAdd, CheckedSub, MaybeSerializeDeserialize, Saturating, StaticLookup, Zero},
traits::{CheckedAdd, CheckedSub, Convert, MaybeSerializeDeserialize, Saturating, StaticLookup, Zero},
DispatchError, DispatchResult,
};
use sp_std::{fmt::Debug, marker, result, vec::Vec};
use support::{AddressMapping, EVMBridge, InvokeContext};
use support::{evm::limits::erc20, AddressMapping, EVMBridge, InvokeContext};

mod mock;
mod tests;
Expand Down Expand Up @@ -97,6 +97,9 @@ pub mod module {
type AddressMapping: AddressMapping<Self::AccountId>;
type EVMBridge: EVMBridge<Self::AccountId, BalanceOf<Self>>;

/// Convert gas to weight.
type GasToWeight: Convert<u64, Weight>;

/// The AccountId that can perform a sweep dust.
type SweepOrigin: EnsureOrigin<Self::Origin>;

Expand Down Expand Up @@ -150,7 +153,9 @@ pub mod module {
///
/// The dispatch origin for this call must be `Signed` by the
/// transactor.
#[pallet::weight(T::WeightInfo::transfer_non_native_currency())]
#[pallet::weight(T::WeightInfo::transfer_non_native_currency()
.saturating_add(if currency_id.is_erc20_currency_id() { T::GasToWeight::convert(erc20::TRANSFER.gas) } else { 0 })
)]
pub fn transfer(
origin: OriginFor<T>,
dest: <T::Lookup as StaticLookup>::Source,
Expand All @@ -177,7 +182,7 @@ pub mod module {
T::NativeCurrency::transfer(&from, &to, amount)
}

/// update amount of account `who` under `currency_id`.
/// Update amount of account `who` under `currency_id`.
///
/// The dispatch origin of this call must be _Root_.
#[pallet::weight(T::WeightInfo::update_balance_non_native_currency())]
Expand Down
10 changes: 9 additions & 1 deletion modules/currencies/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ ord_parameter_types! {
pub const PublicationFee: u64 = 200;
}

pub struct GasToWeight;
impl Convert<u64, u64> for GasToWeight {
fn convert(a: u64) -> u64 {
a
}
}

impl module_evm::Config for Runtime {
type AddressMapping = MockAddressMapping;
type Currency = PalletBalances;
Expand All @@ -149,7 +156,7 @@ impl module_evm::Config for Runtime {
type PrecompilesType = ();
type PrecompilesValue = ();
type ChainId = ();
type GasToWeight = ();
type GasToWeight = GasToWeight;
type ChargeTransactionPayment = ();
type NetworkContractOrigin = EnsureSignedBy<NetworkContractAccount, AccountId>;
type NetworkContractSource = NetworkContractSource;
Expand Down Expand Up @@ -178,6 +185,7 @@ impl Config for Runtime {
type WeightInfo = ();
type AddressMapping = MockAddressMapping;
type EVMBridge = module_evm_bridge::EVMBridge<Runtime>;
type GasToWeight = GasToWeight;
type SweepOrigin = EnsureSignedBy<CouncilAccount, AccountId>;
type OnDust = crate::TransferDust<Runtime, DustAccount>;
}
Expand Down
30 changes: 29 additions & 1 deletion modules/currencies/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#![cfg(test)]

use super::*;
use frame_support::{assert_noop, assert_ok};
use frame_support::{assert_noop, assert_ok, weights::GetDispatchInfo};
use mock::{
alice, bob, deploy_contracts, erc20_address, eva, AccountId, AdaptedBasicCurrency, CouncilAccount, Currencies,
DustAccount, Event, ExtBuilder, NativeCurrency, Origin, PalletBalances, Runtime, System, Tokens, ALICE_BALANCE,
Expand Down Expand Up @@ -2339,3 +2339,31 @@ fn sweep_dust_erc20_not_allowed() {
);
});
}

#[test]
fn transfer_erc20_will_charge_gas() {
ExtBuilder::default().build().execute_with(|| {
let dispatch_info = module::Call::<Runtime>::transfer {
dest: alice(),
currency_id: CurrencyId::Erc20(erc20_address()),
amount: 1,
}
.get_dispatch_info();
assert_eq!(
dispatch_info.weight,
<Runtime as module::Config>::WeightInfo::transfer_non_native_currency()
+ support::evm::limits::erc20::TRANSFER.gas // mock GasToWeight is 1:1
);

let dispatch_info = module::Call::<Runtime>::transfer {
dest: alice(),
currency_id: DOT,
amount: 1,
}
.get_dispatch_info();
assert_eq!(
dispatch_info.weight,
<Runtime as module::Config>::WeightInfo::transfer_non_native_currency()
);
});
}
1 change: 1 addition & 0 deletions modules/homa-lite/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ impl module_currencies::Config for Runtime {
type WeightInfo = ();
type AddressMapping = MockAddressMapping;
type EVMBridge = ();
type GasToWeight = ();
type SweepOrigin = EnsureSignedBy<Root, AccountId>;
type OnDust = ();
}
Expand Down
1 change: 1 addition & 0 deletions modules/homa-lite/src/mock_no_fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ impl module_currencies::Config for NoFeeRuntime {
type WeightInfo = ();
type AddressMapping = MockAddressMapping;
type EVMBridge = ();
type GasToWeight = ();
type SweepOrigin = EnsureSignedBy<Root, AccountId>;
type OnDust = ();
}
Expand Down
1 change: 1 addition & 0 deletions modules/homa/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ impl module_currencies::Config for Runtime {
type WeightInfo = ();
type AddressMapping = MockAddressMapping;
type EVMBridge = ();
type GasToWeight = ();
type SweepOrigin = EnsureRoot<AccountId>;
type OnDust = ();
}
Expand Down
1 change: 1 addition & 0 deletions modules/honzon-bridge/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl module_currencies::Config for Runtime {
type WeightInfo = ();
type AddressMapping = MockAddressMapping;
type EVMBridge = ();
type GasToWeight = ();
type SweepOrigin = EnsureRoot<AccountId>;
type OnDust = ();
}
Expand Down
1 change: 1 addition & 0 deletions modules/nft/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ impl module_currencies::Config for Runtime {
type WeightInfo = ();
type AddressMapping = MockAddressMapping;
type EVMBridge = ();
type GasToWeight = ();
type SweepOrigin = EnsureSignedBy<One, AccountId>;
type OnDust = ();
}
Expand Down
1 change: 1 addition & 0 deletions modules/transaction-payment/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl module_currencies::Config for Runtime {
type WeightInfo = ();
type AddressMapping = MockAddressMapping;
type EVMBridge = ();
type GasToWeight = ();
type SweepOrigin = EnsureSignedBy<Zero, AccountId>;
type OnDust = ();
}
Expand Down
1 change: 1 addition & 0 deletions runtime/acala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,7 @@ impl module_currencies::Config for Runtime {
type WeightInfo = weights::module_currencies::WeightInfo<Runtime>;
type AddressMapping = EvmAddressMapping<Runtime>;
type EVMBridge = module_evm_bridge::EVMBridge<Runtime>;
type GasToWeight = GasToWeight;
type SweepOrigin = EnsureRootOrOneGeneralCouncil;
type OnDust = module_currencies::TransferDust<Runtime, AcalaTreasuryAccount>;
}
Expand Down
1 change: 1 addition & 0 deletions runtime/common/src/precompile/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ impl module_currencies::Config for Test {
type WeightInfo = ();
type AddressMapping = EvmAddressMapping<Test>;
type EVMBridge = module_evm_bridge::EVMBridge<Test>;
type GasToWeight = ();
type SweepOrigin = EnsureSignedBy<CouncilAccount, AccountId>;
type OnDust = ();
}
Expand Down
1 change: 1 addition & 0 deletions runtime/karura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,7 @@ impl module_currencies::Config for Runtime {
type WeightInfo = weights::module_currencies::WeightInfo<Runtime>;
type AddressMapping = EvmAddressMapping<Runtime>;
type EVMBridge = module_evm_bridge::EVMBridge<Runtime>;
type GasToWeight = GasToWeight;
type SweepOrigin = EnsureRootOrOneGeneralCouncil;
type OnDust = module_currencies::TransferDust<Runtime, KaruraTreasuryAccount>;
}
Expand Down
1 change: 1 addition & 0 deletions runtime/mandala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,7 @@ impl module_currencies::Config for Runtime {
type WeightInfo = weights::module_currencies::WeightInfo<Runtime>;
type AddressMapping = EvmAddressMapping<Runtime>;
type EVMBridge = module_evm_bridge::EVMBridge<Runtime>;
type GasToWeight = GasToWeight;
type SweepOrigin = EnsureRootOrOneGeneralCouncil;
type OnDust = module_currencies::TransferDust<Runtime, TreasuryAccount>;
}
Expand Down

0 comments on commit af16e52

Please sign in to comment.