Skip to content

Commit 5495c52

Browse files
committed
refactor(api/fungibles/benchmarking): add copy trait bound to asset id
to eliminate manual clones
1 parent 11a8222 commit 5495c52

File tree

2 files changed

+74
-73
lines changed

2 files changed

+74
-73
lines changed

pallets/api-vnext/src/fungibles/benchmarking.rs

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type TokenId<T, I> = <T as pallet_assets::Config<I>>::AssetId;
5757
Time: Time<Moment: Into<U256>>
5858
>,
5959
// Fungibles
60-
T: pallet_assets::Config<I, AssetId: Default + From<u32> + Into<u32>>,
60+
T: pallet_assets::Config<I, AssetId: Default + From<u32> + Into<u32> + Copy>,
6161
alloy::U256: UintTryFrom<AssetsBalance<T, I>> + UintTryTo<AssetsBalance<T, I>>
6262
)]
6363
mod benchmarks {
@@ -74,21 +74,21 @@ mod benchmarks {
7474
call_setup.set_origin(Origin::Signed(owner.clone()));
7575
let mut ext = call_setup.ext().0;
7676
let input = IFungiblesCalls::transfer(IFungibles::transferCall {
77-
token: token.clone().into(),
77+
token: token.into(),
7878
to: <AddressMapper<T>>::to_address(&to).0.into(),
7979
value: alloy::U256::from(value),
8080
});
8181

82-
<Assets<T, I>>::set_balance(token.clone(), &owner, value);
83-
assert_eq!(<Assets<T, I>>::balance(token.clone(), &owner), value);
84-
assert_eq!(<Assets<T, I>>::balance(token.clone(), &to), 0u8.into());
82+
<Assets<T, I>>::set_balance(token, &owner, value);
83+
assert_eq!(<Assets<T, I>>::balance(token, &owner), value);
84+
assert_eq!(<Assets<T, I>>::balance(token, &to), 0u8.into());
8585

8686
#[block]
8787
{
8888
assert_ok!(call_precompile::<Fungibles<T, I>, _, ()>(&mut ext, &ADDRESS, &input));
8989
}
9090

91-
assert_eq!(<Assets<T, I>>::balance(token.clone(), &owner), 0u8.into());
91+
assert_eq!(<Assets<T, I>>::balance(token, &owner), 0u8.into());
9292
assert_eq!(<Assets<T, I>>::balance(token, &to), value);
9393
}
9494

@@ -104,27 +104,27 @@ mod benchmarks {
104104
call_setup.set_origin(Origin::Signed(spender.clone()));
105105
let mut ext = call_setup.ext().0;
106106
let input = IFungiblesCalls::transferFrom(IFungibles::transferFromCall {
107-
token: token.clone().into(),
107+
token: token.into(),
108108
from: <AddressMapper<T>>::to_address(&owner).0.into(),
109109
to: <AddressMapper<T>>::to_address(&to).0.into(),
110110
value: alloy::U256::from(value),
111111
});
112112

113-
<Assets<T, I>>::set_balance(token.clone(), &owner, value);
114-
assert_eq!(<Assets<T, I>>::balance(token.clone(), &owner), value);
115-
assert_eq!(<Assets<T, I>>::balance(token.clone(), &spender), 0u8.into());
116-
assert_eq!(<Assets<T, I>>::balance(token.clone(), &to), 0u8.into());
117-
assert_ok!(<Assets<T, I>>::approve(token.clone(), &owner, &spender, value));
118-
assert_eq!(<Assets<T, I>>::allowance(token.clone(), &owner, &spender), value);
113+
<Assets<T, I>>::set_balance(token, &owner, value);
114+
assert_eq!(<Assets<T, I>>::balance(token, &owner), value);
115+
assert_eq!(<Assets<T, I>>::balance(token, &spender), 0u8.into());
116+
assert_eq!(<Assets<T, I>>::balance(token, &to), 0u8.into());
117+
assert_ok!(<Assets<T, I>>::approve(token, &owner, &spender, value));
118+
assert_eq!(<Assets<T, I>>::allowance(token, &owner, &spender), value);
119119

120120
#[block]
121121
{
122122
assert_ok!(call_precompile::<Fungibles<T, I>, _, ()>(&mut ext, &ADDRESS, &input));
123123
}
124124

125-
assert_eq!(<Assets<T, I>>::balance(token.clone(), &owner), 0u8.into());
126-
assert_eq!(<Assets<T, I>>::balance(token.clone(), &spender), 0u8.into());
127-
assert_eq!(<Assets<T, I>>::balance(token.clone(), &to), value);
125+
assert_eq!(<Assets<T, I>>::balance(token, &owner), 0u8.into());
126+
assert_eq!(<Assets<T, I>>::balance(token, &spender), 0u8.into());
127+
assert_eq!(<Assets<T, I>>::balance(token, &to), value);
128128
assert_eq!(<Assets<T, I>>::allowance(token, &owner, &spender), 0u8.into());
129129
}
130130

@@ -140,7 +140,7 @@ mod benchmarks {
140140
// Set the `current_allowance`.
141141
<Balances<T>>::set_balance(&owner, u32::MAX.into());
142142
assert_ok!(<Assets<T, I> as approvals::Mutate<T::AccountId>>::approve(
143-
token.clone(),
143+
token,
144144
&owner,
145145
&spender,
146146
current_allowance,
@@ -161,7 +161,7 @@ mod benchmarks {
161161
call_setup.set_origin(Origin::Signed(owner.clone()));
162162
let mut ext = call_setup.ext().0;
163163
let input = IFungiblesCalls::approve(IFungibles::approveCall {
164-
token: token.clone().into(),
164+
token: token.into(),
165165
spender: <AddressMapper<T>>::to_address(&spender).0.into(),
166166
value: alloy::U256::from(approval_value),
167167
});
@@ -171,11 +171,11 @@ mod benchmarks {
171171
assert_ok!(call_precompile::<Fungibles<T, I>, _, ()>(&mut ext, &ADDRESS, &input));
172172
}
173173

174-
assert_eq!(<Assets<T, I>>::allowance(token.clone(), &owner, &spender), approval_value);
174+
assert_eq!(<Assets<T, I>>::allowance(token, &owner, &spender), approval_value);
175175
if c == 1 {
176176
assert_has_event::<T, I>(
177177
pallet_assets::Event::ApprovalCancelled {
178-
asset_id: token.clone(),
178+
asset_id: token,
179179
owner: owner.clone(),
180180
delegate: spender.clone(),
181181
}
@@ -232,18 +232,17 @@ mod benchmarks {
232232
let mut call_setup = set_up_call();
233233
call_setup.set_origin(Origin::Signed(owner.clone()));
234234
let mut ext = call_setup.ext().0;
235-
let input = IFungiblesCalls::startDestroy(IFungibles::startDestroyCall {
236-
token: token.clone().into(),
237-
});
235+
let input =
236+
IFungiblesCalls::startDestroy(IFungibles::startDestroyCall { token: token.into() });
238237

239-
assert_eq!(<Asset<T, I>>::get(token.clone()).unwrap().status, AssetStatus::Live);
238+
assert_eq!(<Asset<T, I>>::get(token).unwrap().status, AssetStatus::Live);
240239

241240
#[block]
242241
{
243242
assert_ok!(call_precompile::<Fungibles<T, I>, _, ()>(&mut ext, &ADDRESS, &input));
244243
}
245244

246-
assert_eq!(<Asset<T, I>>::get(token.clone()).unwrap().status, AssetStatus::Destroying);
245+
assert_eq!(<Asset<T, I>>::get(token).unwrap().status, AssetStatus::Destroying);
247246
}
248247

249248
#[benchmark]
@@ -259,7 +258,7 @@ mod benchmarks {
259258
call_setup.set_origin(Origin::Signed(owner.clone()));
260259
let mut ext = call_setup.ext().0;
261260
let input = IFungiblesCalls::setMetadata(IFungibles::setMetadataCall {
262-
token: token.clone().into(),
261+
token: token.into(),
263262
name: String::from_utf8_lossy(&name).to_string(),
264263
symbol: String::from_utf8_lossy(&symbol).to_string(),
265264
decimals,
@@ -270,8 +269,8 @@ mod benchmarks {
270269
assert_ok!(call_precompile::<Fungibles<T, I>, _, ()>(&mut ext, &ADDRESS, &input));
271270
}
272271

273-
assert_eq!(<Assets<T, I>>::name(token.clone()), name);
274-
assert_eq!(<Assets<T, I>>::symbol(token.clone()), symbol);
272+
assert_eq!(<Assets<T, I>>::name(token), name);
273+
assert_eq!(<Assets<T, I>>::symbol(token), symbol);
275274
assert_eq!(<Assets<T, I>>::decimals(token), decimals);
276275
}
277276

@@ -282,17 +281,16 @@ mod benchmarks {
282281
let mut call_setup = set_up_call();
283282
call_setup.set_origin(Origin::Signed(owner.clone()));
284283
let mut ext = call_setup.ext().0;
285-
let input = IFungiblesCalls::clearMetadata(IFungibles::clearMetadataCall {
286-
token: token.clone().into(),
287-
});
284+
let input =
285+
IFungiblesCalls::clearMetadata(IFungibles::clearMetadataCall { token: token.into() });
288286

289287
#[block]
290288
{
291289
assert_ok!(call_precompile::<Fungibles<T, I>, _, ()>(&mut ext, &ADDRESS, &input));
292290
}
293291

294-
assert!(<Assets<T, I>>::name(token.clone()).is_empty());
295-
assert!(<Assets<T, I>>::symbol(token.clone()).is_empty());
292+
assert!(<Assets<T, I>>::name(token).is_empty());
293+
assert!(<Assets<T, I>>::symbol(token).is_empty());
296294
assert_eq!(<Assets<T, I>>::decimals(token), 0);
297295
}
298296

@@ -307,7 +305,7 @@ mod benchmarks {
307305
call_setup.set_origin(Origin::Signed(owner.clone()));
308306
let mut ext = call_setup.ext().0;
309307
let input = IFungiblesCalls::mint(IFungibles::mintCall {
310-
token: token.clone().into(),
308+
token: token.into(),
311309
account: <AddressMapper<T>>::to_address(&account).0.into(),
312310
value: alloy::U256::from(value),
313311
});
@@ -331,12 +329,12 @@ mod benchmarks {
331329
call_setup.set_origin(Origin::Signed(owner.clone()));
332330
let mut ext = call_setup.ext().0;
333331
let input = IFungiblesCalls::burn(IFungibles::burnCall {
334-
token: token.clone().into(),
332+
token: token.into(),
335333
account: <AddressMapper<T>>::to_address(&account).0.into(),
336334
value: alloy::U256::from(value),
337335
});
338336

339-
<Assets<T, I>>::set_balance(token.clone(), &account, value);
337+
<Assets<T, I>>::set_balance(token, &account, value);
340338

341339
#[block]
342340
{
@@ -457,21 +455,24 @@ fn assert_has_event<T: pallet_assets::Config<I>, I>(
457455
frame_system::Pallet::<T>::assert_has_event(event.into());
458456
}
459457

460-
fn create<T: Config<I> + pallet_assets::Config<I, AssetId: Default> + pallet_revive::Config, I>(
458+
fn create<
459+
T: Config<I> + pallet_assets::Config<I, AssetId: Copy + Default> + pallet_revive::Config,
460+
I,
461+
>(
461462
owner: T::AccountId,
462463
) -> TokenId<T, I> {
463464
let token = NextAssetId::<T, I>::get().unwrap_or_default();
464465
<Balances<T>>::set_balance(&owner, u32::MAX.into());
465466
assert_ok!(<Assets<T, I> as Create<T::AccountId>>::create(
466-
token.clone(),
467+
token,
467468
owner.clone(),
468469
true,
469470
1u32.into()
470471
));
471472

472473
let max = AssetsStringLimit::<T, I>::get() as usize;
473474
assert_ok!(<Assets<T, I> as metadata::Mutate<T::AccountId>>::set(
474-
token.clone(),
475+
token,
475476
&owner,
476477
vec![255u8; max],
477478
vec![255u8; max],

0 commit comments

Comments
 (0)