This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat: pallet asset-rate #13608
Merged
paritytech-processbot
merged 21 commits into
paritytech:master
from
wischli:feat/treasury-oracle
Apr 19, 2023
Merged
feat: pallet asset-rate #13608
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
8996e05
poc
wischli 8a07bc0
fix: remove AssetIdParameter
wischli 47443ec
tests: add
wischli 8d3ddee
docs: add pallet description
wischli 109a321
feat: add benches
wischli 4a0fca4
refactor: UnknownAssetId
wischli d78f6cb
Merge remote-tracking branch 'origin/master' into feat/treasury-oracle
wischli 719364f
fix: normalize mock cfg
wischli 0adf951
fix: benchmarks
wischli ffb6d95
chore: add weights
wischli 301ddb2
refactor: remove storage getter
wischli 7153a2c
chore: apply suggestions from code review
wischli fdeefbd
docs: add native balance to calls
wischli 65df23e
chore: apply suggestions from code review
wischli acf046b
Merge remote-tracking branch 'origin/master' into feat/treasury-oracle
58cc428
chore: apply ConversionFromAssetBalance
wischli 44a9f6a
tests: update balance mock
wischli 03acddb
chore: apply suggestions from code review
wischli 1f14c39
ci: set publish to false
wischli 329b846
docs: fix missing rustdoc
wischli 57c5c6f
Merge remote-tracking branch 'origin/master' into feat/treasury-oracle
wischli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
tests: add
- Loading branch information
commit 47443ec7f97505657c777cc23180c96ed45d1db0
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // This file is part of Substrate. | ||
|
|
||
| // Copyright (C) Parity Technologies (UK) Ltd. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! The crate's mock. | ||
|
|
||
| use crate as pallet_treasury_oracle; | ||
| use frame_support::traits::{ConstU16, ConstU64}; | ||
| use sp_core::H256; | ||
| use sp_runtime::{ | ||
| testing::Header, | ||
| traits::{BlakeTwo256, IdentityLookup}, | ||
| }; | ||
|
|
||
| type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>; | ||
| type Block = frame_system::mocking::MockBlock<Test>; | ||
|
|
||
| frame_support::construct_runtime!( | ||
| pub enum Test where | ||
| Block = Block, | ||
| NodeBlock = Block, | ||
| UncheckedExtrinsic = UncheckedExtrinsic, | ||
| { | ||
| System: frame_system, | ||
| TreasuryOracle: pallet_treasury_oracle, | ||
| Balances: pallet_balances, | ||
| } | ||
| ); | ||
|
|
||
| impl frame_system::Config for Test { | ||
| type BaseCallFilter = frame_support::traits::Everything; | ||
| type BlockWeights = (); | ||
| type BlockLength = (); | ||
| type DbWeight = (); | ||
| type RuntimeOrigin = RuntimeOrigin; | ||
| type RuntimeCall = RuntimeCall; | ||
| type Index = u64; | ||
| type BlockNumber = u64; | ||
| type Hash = H256; | ||
| type Hashing = BlakeTwo256; | ||
| type AccountId = u64; | ||
| type Lookup = IdentityLookup<Self::AccountId>; | ||
| type Header = Header; | ||
| type RuntimeEvent = RuntimeEvent; | ||
| type BlockHashCount = ConstU64<250>; | ||
| type Version = (); | ||
| type PalletInfo = PalletInfo; | ||
| type AccountData = pallet_balances::AccountData<u64>; | ||
| type OnNewAccount = (); | ||
| type OnKilledAccount = (); | ||
| type SystemWeightInfo = (); | ||
| type SS58Prefix = ConstU16<42>; | ||
| type OnSetCode = (); | ||
| type MaxConsumers = frame_support::traits::ConstU32<16>; | ||
| } | ||
|
|
||
| impl pallet_balances::Config for Test { | ||
| type Balance = u64; | ||
| type DustRemoval = (); | ||
| type RuntimeEvent = RuntimeEvent; | ||
| type ExistentialDeposit = ConstU64<1>; | ||
| type AccountStore = System; | ||
| type WeightInfo = (); | ||
| type MaxLocks = (); | ||
| type MaxReserves = (); | ||
| type ReserveIdentifier = [u8; 8]; | ||
| } | ||
|
|
||
| impl pallet_treasury_oracle::Config for Test { | ||
| type WeightInfo = (); | ||
| type RuntimeEvent = RuntimeEvent; | ||
| type CreateOrigin = frame_system::EnsureRoot<u64>; | ||
| type RemoveOrigin = frame_system::EnsureSigned<u64>; | ||
| type UpdateOrigin = frame_system::EnsureSigned<u64>; | ||
| type Balance = u64; | ||
| type Currency = Balances; | ||
| type AssetId = u32; | ||
| } | ||
|
|
||
| // Build genesis storage according to the mock runtime. | ||
| pub fn new_test_ext() -> sp_io::TestExternalities { | ||
| frame_system::GenesisConfig::default().build_storage::<Test>().unwrap().into() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| // This file is part of Substrate. | ||
|
|
||
| // Copyright (C) Parity Technologies (UK) Ltd. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! The crate's tests. | ||
|
|
||
| use super::*; | ||
| use crate::pallet as pallet_treasury_oracle; | ||
| use frame_support::{assert_noop, assert_ok}; | ||
| use mock::{new_test_ext, RuntimeOrigin, Test, TreasuryOracle}; | ||
| use sp_runtime::FixedU128; | ||
|
|
||
| const ASSET_ID: u32 = 42; | ||
|
|
||
| #[test] | ||
| fn create_works() { | ||
| new_test_ext().execute_with(|| { | ||
| assert!(TreasuryOracle::conversion_rate_to_native(ASSET_ID).is_none()); | ||
| assert_ok!(TreasuryOracle::create( | ||
| RuntimeOrigin::root(), | ||
| ASSET_ID, | ||
| FixedU128::from_float(0.1) | ||
| )); | ||
|
|
||
| assert_eq!( | ||
| TreasuryOracle::conversion_rate_to_native(ASSET_ID), | ||
| Some(FixedU128::from_float(0.1)) | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn remove_works() { | ||
| new_test_ext().execute_with(|| { | ||
| assert_ok!(TreasuryOracle::create( | ||
| RuntimeOrigin::root(), | ||
| ASSET_ID, | ||
| FixedU128::from_float(0.1) | ||
| )); | ||
|
|
||
| assert_ok!(TreasuryOracle::remove(RuntimeOrigin::signed(1), ASSET_ID,)); | ||
| assert!(TreasuryOracle::conversion_rate_to_native(ASSET_ID).is_none()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn remove_unknown_throws() { | ||
| new_test_ext().execute_with(|| { | ||
| assert_noop!( | ||
| TreasuryOracle::remove(RuntimeOrigin::signed(1), ASSET_ID,), | ||
| Error::<Test>::Unknown | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn update_works() { | ||
| new_test_ext().execute_with(|| { | ||
| assert_ok!(TreasuryOracle::create( | ||
| RuntimeOrigin::root(), | ||
| ASSET_ID, | ||
| FixedU128::from_float(0.1) | ||
| )); | ||
| assert_ok!(TreasuryOracle::update( | ||
| RuntimeOrigin::signed(1), | ||
| ASSET_ID, | ||
| FixedU128::from_float(0.5) | ||
| )); | ||
|
|
||
| assert_eq!( | ||
| TreasuryOracle::conversion_rate_to_native(ASSET_ID), | ||
| Some(FixedU128::from_float(0.5)) | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn update_unknown_throws() { | ||
| new_test_ext().execute_with(|| { | ||
| assert_noop!( | ||
| TreasuryOracle::update(RuntimeOrigin::signed(1), ASSET_ID, FixedU128::from_float(0.5)), | ||
| Error::<Test>::Unknown | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn convert_works() { | ||
| new_test_ext().execute_with(|| { | ||
| assert_ok!(TreasuryOracle::create( | ||
| RuntimeOrigin::root(), | ||
| ASSET_ID, | ||
| FixedU128::from_float(2.51) | ||
| )); | ||
|
|
||
| let conversion = <TreasuryOracle as BalanceConversion< | ||
| BalanceOf<Test>, | ||
| <Test as pallet_treasury_oracle::Config>::AssetId, | ||
| BalanceOf<Test>, | ||
| >>::to_asset_balance(10, ASSET_ID); | ||
| assert_eq!(conversion.expect("Conversion rate exists for asset"), 25); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn convert_unknown_works() { | ||
| new_test_ext().execute_with(|| { | ||
| let conversion = <TreasuryOracle as BalanceConversion< | ||
| BalanceOf<Test>, | ||
| <Test as pallet_treasury_oracle::Config>::AssetId, | ||
| BalanceOf<Test>, | ||
| >>::to_asset_balance(10, ASSET_ID); | ||
| assert!(conversion.is_err()); | ||
| }); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.