Skip to content

Commit 1572333

Browse files
committed
refactor(api/fungibles/erc20): charge benchmarked weight
Uses benchmarks from the fungibles precompile which should be exactly the same for these functions.
1 parent a6f1606 commit 1572333

File tree

1 file changed

+33
-13
lines changed
  • pallets/api-vnext/src/fungibles/precompiles

1 file changed

+33
-13
lines changed

pallets/api-vnext/src/fungibles/precompiles/erc20.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ use frame_support::{
55
traits::fungibles::{approvals::Inspect as _, metadata::Inspect as _},
66
};
77
use pallet_assets::precompiles::{AssetIdExtractor, InlineAssetIdExtractor};
8-
use pallet_revive::AddressMapper as _;
8+
use pallet_revive::{precompiles::RuntimeCosts, AddressMapper as _};
99
use AddressMatcher::Prefix;
1010
use IERC20::*;
1111

1212
use super::{
13-
deposit_event, prefixed_address, sol, to_runtime_origin, AddressMapper, AddressMatcher, Assets,
14-
Config, Error, Ext, NonZero, PhantomData, Precompile, SolCall, UintTryFrom, UintTryTo, U256,
13+
deposit_event, prefixed_address, sol, to_runtime_origin, weights::WeightInfo, AddressMapper,
14+
AddressMatcher, Assets, Config, Error, Ext, NonZero, PhantomData, Precompile, SolCall,
15+
UintTryFrom, UintTryTo, U256,
1516
};
1617

1718
sol!("src/fungibles/precompiles/interfaces/IERC20.sol");
@@ -48,18 +49,22 @@ where
4849
match input {
4950
// IERC20
5051
totalSupply(_) => {
51-
// TODO: charge based on benchmarked weight
52+
env.charge(<T as Config<I>>::WeightInfo::total_supply())?;
53+
5254
let total_supply = U256::saturating_from(<Assets<T, I>>::total_supply(token));
55+
5356
Ok(totalSupplyCall::abi_encode_returns(&total_supply))
5457
},
5558
balanceOf(balanceOfCall { account }) => {
56-
// TODO: charge based on benchmarked weight
59+
env.charge(<T as Config<I>>::WeightInfo::balance_of())?;
60+
5761
let account = env.to_account_id(&(*account.0).into());
5862
let balance = U256::saturating_from(<Assets<T, I>>::balance(token, account));
63+
5964
Ok(balanceOfCall::abi_encode_returns(&balance))
6065
},
6166
transfer(transferCall { to, value }) => {
62-
// TODO: charge based on benchmarked weight
67+
env.charge(<T as Config<I>>::WeightInfo::transfer())?;
6368
let from = <AddressMapper<T>>::to_address(env.caller().account_id()?).0.into();
6469

6570
super::transfer::<T, I>(
@@ -73,31 +78,40 @@ where
7378
Ok(transferCall::abi_encode_returns(&true))
7479
},
7580
allowance(allowanceCall { owner, spender }) => {
76-
// TODO: charge based on benchmarked weight
81+
env.charge(<T as Config<I>>::WeightInfo::allowance())?;
82+
7783
let owner = env.to_account_id(&(*owner.0).into());
7884
let spender = env.to_account_id(&(*spender.0).into());
7985
let remaining =
8086
U256::saturating_from(<Assets<T, I>>::allowance(token, &owner, &spender));
87+
8188
Ok(allowanceCall::abi_encode_returns(&remaining))
8289
},
8390
approve(approveCall { spender, value }) => {
84-
// TODO: charge based on benchmarked weight
91+
let charged = env.charge(<T as Config<I>>::WeightInfo::approve(1, 1))?;
8592
let owner = <AddressMapper<T>>::to_address(env.caller().account_id()?).0.into();
8693

87-
super::approve::<T, I>(
94+
let result = super::approve::<T, I>(
8895
to_runtime_origin(env.caller()),
8996
token,
9097
env.to_account_id(&(*spender.0).into()),
9198
value.saturating_to(),
9299
) // TODO: adjust weight
93100
.map_err(|e| e.error)?;
94101

102+
// Adjust weight
103+
if let Some(actual_weight) = result.actual_weight {
104+
// TODO: replace with `env.adjust_gas(charged, result.weight);` once #8693 lands
105+
env.gas_meter_mut()
106+
.adjust_gas(charged, RuntimeCosts::Precompile(actual_weight));
107+
}
108+
95109
let event = Approval { owner, spender: *spender, value: *value };
96110
deposit_event(env, address, event);
97111
Ok(approveCall::abi_encode_returns(&true))
98112
},
99113
transferFrom(transferFromCall { from, to, value }) => {
100-
// TODO: charge based on benchmarked weight
114+
env.charge(<T as Config<I>>::WeightInfo::transfer_from())?;
101115

102116
super::transfer_from::<T, I>(
103117
to_runtime_origin(env.caller()),
@@ -112,20 +126,26 @@ where
112126
},
113127
// IERC20Metadata
114128
name(_) => {
115-
// TODO: charge based on benchmarked weight
129+
env.charge(<T as Config<I>>::WeightInfo::metadata_name())?;
130+
116131
let result = <Assets<T, I>>::name(token);
117132
let result = String::from_utf8_lossy(result.as_slice()).into();
133+
118134
Ok(nameCall::abi_encode_returns(&result))
119135
},
120136
symbol(_) => {
121-
// TODO: charge based on benchmarked weight
137+
env.charge(<T as Config<I>>::WeightInfo::metadata_symbol())?;
138+
122139
let result = <Assets<T, I>>::symbol(token);
123140
let result = String::from_utf8_lossy(result.as_slice()).into();
141+
124142
Ok(symbolCall::abi_encode_returns(&result))
125143
},
126144
decimals(_) => {
127-
// TODO: charge based on benchmarked weight
145+
env.charge(<T as Config<I>>::WeightInfo::metadata_decimals())?;
146+
128147
let result = <Assets<T, I>>::decimals(token);
148+
129149
Ok(decimalsCall::abi_encode_returns(&result))
130150
},
131151
}

0 commit comments

Comments
 (0)