Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add update_currency_metadata function #1259

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions pallets/asset-registry/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,40 @@ benchmarks! {
};
}: {call.dispatch_bypass_filter(origin)?}

update_currency_metadata {
let origin = T::RegisterOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;

assert_ok!(AssetRegistry::<T>::register_token_metadata(
origin.clone(),
Box::new(AssetMetadata {
name: b"Old Token Name".to_vec(),
symbol: b"OTN".to_vec(),
decimals: 10,
minimal_balance: BalanceOf::<T>::unique_saturated_from(1u128),
})
));

let call = Call::<T>::update_currency_metadata {
currency_id: CurrencyId::Token2(0),
asset_name: Some(b"Token Name".to_vec()),
asset_symbol: Some(b"TN".to_vec()),
asset_decimals : Some(12),
asset_minimal_balance : Some(BalanceOf::<T>::unique_saturated_from(1000u128)),
};

}: {call.dispatch_bypass_filter(origin)?}
verify {
assert_eq!(
CurrencyMetadatas::<T>::get(CurrencyId::Token2(0)),
Some(AssetMetadata {
name: b"Token Name".to_vec(),
symbol: b"TN".to_vec(),
decimals: 12,
minimal_balance: BalanceOf::<T>::unique_saturated_from(1000u128),
})
);
}

impl_benchmark_test_suite!(
AssetRegistry,
crate::mock::ExtBuilder::default().build(),
Expand Down
39 changes: 39 additions & 0 deletions pallets/asset-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ pub mod pallet {
AssetUpdated { asset_id: AssetIds, metadata: AssetMetadata<BalanceOf<T>> },
/// The CurrencyId registered.
CurrencyIdRegistered { currency_id: CurrencyId, metadata: AssetMetadata<BalanceOf<T>> },
/// The CurrencyId updated.
CurrencyIdUpdated { currency_id: CurrencyId, metadata: AssetMetadata<BalanceOf<T>> },
/// MultiLocation Force set.
MultiLocationSet { currency_id: CurrencyId, location: MultiLocation, weight: Weight },
}
Expand Down Expand Up @@ -444,6 +446,43 @@ pub mod pallet {

Ok(())
}

#[pallet::call_index(8)]
#[pallet::weight(T::WeightInfo::update_currency_metadata())]
pub fn update_currency_metadata(
origin: OriginFor<T>,
currency_id: CurrencyId,
asset_name: Option<Vec<u8>>,
asset_symbol: Option<Vec<u8>>,
asset_decimals: Option<u8>,
asset_minimal_balance: Option<BalanceOf<T>>,
) -> DispatchResult {
T::RegisterOrigin::ensure_origin(origin)?;

// Check if the currency metadata exists
let mut metadata =
CurrencyMetadatas::<T>::get(currency_id).ok_or(Error::<T>::CurrencyIdNotExists)?;

// Update the metadata fields based on the provided options
if let Some(name) = asset_name {
metadata.name = name;
}
if let Some(symbol) = asset_symbol {
metadata.symbol = symbol;
}
if let Some(decimals) = asset_decimals {
metadata.decimals = decimals;
}
if let Some(minimal_balance) = asset_minimal_balance {
metadata.minimal_balance = minimal_balance;
}

// Store the updated metadata
CurrencyMetadatas::<T>::insert(currency_id, metadata.clone());
Self::deposit_event(Event::<T>::CurrencyIdUpdated { currency_id, metadata });

Ok(())
}
}
}

Expand Down
120 changes: 120 additions & 0 deletions pallets/asset-registry/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,123 @@ fn force_set_multilocation_should_work() {
);
})
}

#[test]
fn update_currency_metadata_should_work() {
ExtBuilder::default().build().execute_with(|| {
let caller = CouncilAccount::get();
let currency_id = CurrencyId::Token2(0);
let name = b"Updated Name".to_vec();
let symbol = b"UN".to_vec();
let decimals: u8 = 10;
let minimal_balance = 1000u32.into();

// Pre-insert a currency_metadata to update
CurrencyMetadatas::<Runtime>::insert(
currency_id,
AssetMetadata {
name: b"Old Name".to_vec(),
symbol: b"ON".to_vec(),
decimals: 8,
minimal_balance: 1u32.into(),
},
);

// Ensure the origin has the required permissions
let origin = RuntimeOrigin::signed(caller);
assert_ok!(AssetRegistry::update_currency_metadata(
origin,
currency_id,
Some(name.clone()),
Some(symbol.clone()),
Some(decimals),
Some(minimal_balance)
));

System::assert_last_event(RuntimeEvent::AssetRegistry(crate::Event::CurrencyIdUpdated {
currency_id,
metadata: AssetMetadata {
name: name.clone(),
symbol: symbol.clone(),
decimals,
minimal_balance,
},
}));

// Verify the updated metadata
let updated_metadata = CurrencyMetadatas::<Runtime>::get(currency_id).unwrap();
assert_eq!(updated_metadata.name, name);
assert_eq!(updated_metadata.symbol, symbol);
assert_eq!(updated_metadata.decimals, decimals);
assert_eq!(updated_metadata.minimal_balance, minimal_balance);
})
}

#[test]
fn update_currency_metadata_should_work_no_change() {
ExtBuilder::default().build().execute_with(|| {
let caller = CouncilAccount::get();
let currency_id = CurrencyId::Token2(0);
let name = None;
let symbol = None;
let decimals = None;
let minimal_balance = None;

let old_metadata = AssetMetadata {
name: b"Old Name".to_vec(),
symbol: b"ON".to_vec(),
decimals: 8,
minimal_balance: 1u32.into(),
};

// Pre-insert a currency_metadata to update
CurrencyMetadatas::<Runtime>::insert(currency_id, old_metadata.clone());

// Ensure the origin has the required permissions
let origin = RuntimeOrigin::signed(caller);
assert_ok!(AssetRegistry::update_currency_metadata(
origin,
currency_id,
name,
symbol,
decimals,
minimal_balance
));

// Verify the event
System::assert_last_event(RuntimeEvent::AssetRegistry(crate::Event::CurrencyIdUpdated {
currency_id,
metadata: old_metadata.clone(),
}));

// Verify the updated metadata
let updated_metadata = CurrencyMetadatas::<Runtime>::get(currency_id).unwrap();
assert_eq!(updated_metadata, old_metadata);
});
}

#[test]
fn update_currency_metadata_nonexistent_currency_id() {
ExtBuilder::default().build().execute_with(|| {
let caller = CouncilAccount::get();
let currency_id = CurrencyId::Token2(1); // Non-existent currency ID
let name = Some(b"Updated Name".to_vec());
let symbol = Some(b"UN".to_vec());
let decimals = Some(10);
let minimal_balance = Some(1000u32.into());

// Ensure the origin has the required permissions
let origin = RuntimeOrigin::signed(caller);
assert_noop!(
AssetRegistry::update_currency_metadata(
origin,
currency_id,
name,
symbol,
decimals,
minimal_balance
),
Error::<Runtime>::CurrencyIdNotExists
);
});
}
12 changes: 12 additions & 0 deletions pallets/asset-registry/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub trait WeightInfo {
fn register_vsbond_metadata() -> Weight;
fn register_multilocation() -> Weight;
fn force_set_multilocation() -> Weight;
fn update_currency_metadata() -> Weight;
}

// For backwards compatibility and tests
Expand Down Expand Up @@ -175,4 +176,15 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(3_u64))
}
/// Storage: `AssetRegistry::CurrencyMetadatas` (r:1 w:1)
/// Proof: `AssetRegistry::CurrencyMetadatas` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn update_currency_metadata() -> Weight {
// Proof Size summary in bytes:
// Measured: `409`
// Estimated: `3874`
// Minimum execution time: 12_000_000 picoseconds.
Weight::from_parts(13_000_000, 3874)
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
}
11 changes: 11 additions & 0 deletions runtime/bifrost-kusama/src/weights/bifrost_asset_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,15 @@ impl<T: frame_system::Config> bifrost_asset_registry::WeightInfo for BifrostWeig
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(3))
}
// Storage: `AssetRegistry::CurrencyMetadatas` (r:1 w:1)
// Proof: `AssetRegistry::CurrencyMetadatas` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn update_currency_metadata() -> Weight {
// Proof Size summary in bytes:
// Measured: `409`
// Estimated: `3874`
// Minimum execution time: 12_000 nanoseconds.
Weight::from_parts(13_000_000, 3874)
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1))
}
}
11 changes: 11 additions & 0 deletions runtime/bifrost-polkadot/src/weights/bifrost_asset_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,15 @@ impl<T: frame_system::Config> bifrost_asset_registry::WeightInfo for BifrostWeig
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(3))
}
// Storage: `AssetRegistry::CurrencyMetadatas` (r:1 w:1)
// Proof: `AssetRegistry::CurrencyMetadatas` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn update_currency_metadata() -> Weight {
// Proof Size summary in bytes:
// Measured: `409`
// Estimated: `3874`
// Minimum execution time: 12_000 nanoseconds.
Weight::from_parts(13_000_000, 3874)
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1))
}
}