Skip to content

Commit

Permalink
Get native token from asset-registry (#2052)
Browse files Browse the repository at this point in the history
* get native token from asset-registry

* update docker images

* fix benchmarks
  • Loading branch information
zjb0807 authored Apr 23, 2022
1 parent 7b4237e commit 7c16cec
Show file tree
Hide file tree
Showing 16 changed files with 137 additions and 215 deletions.
4 changes: 2 additions & 2 deletions launch/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
relaychain:
image: parity/polkadot:v0.9.17
image: parity/polkadot:v0.9.18
chain: rococo-local
runtimeGenesisConfig:
configuration:
Expand All @@ -18,7 +18,7 @@ relaychain:
- name: charlie

parachains:
- image: acala/karura-node:2.3.1
- image: acala/karura-node:2.5.0
chain:
base: karura-dev
collators:
Expand Down
38 changes: 35 additions & 3 deletions modules/asset-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#![allow(clippy::unused_unit)]

use frame_support::{
assert_ok,
dispatch::DispatchResult,
ensure,
pallet_prelude::*,
Expand Down Expand Up @@ -193,6 +194,37 @@ pub mod module {
#[pallet::without_storage_info]
pub struct Pallet<T>(_);

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub assets: Vec<(CurrencyId, BalanceOf<T>)>,
}

#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
GenesisConfig {
assets: Default::default(),
}
}
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
self.assets.iter().for_each(|(asset, ed)| {
assert_ok!(Pallet::<T>::do_register_native_asset(
*asset,
&AssetMetadata {
name: asset.name().unwrap().as_bytes().to_vec(),
symbol: asset.symbol().unwrap().as_bytes().to_vec(),
decimals: asset.decimals().unwrap(),
minimal_balance: *ed,
}
));
});
}
}

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(T::WeightInfo::register_foreign_asset())]
Expand Down Expand Up @@ -679,7 +711,7 @@ impl<T: Config> Erc20InfoMapping for EvmErc20InfoMapping<T> {
// the EvmAddress must have been mapped.
fn name(currency_id: CurrencyId) -> Option<Vec<u8>> {
let name = match currency_id {
CurrencyId::Token(_) => currency_id.name().map(|v| v.as_bytes().to_vec()),
CurrencyId::Token(_) => AssetMetadatas::<T>::get(AssetIds::NativeAssetId(currency_id)).map(|v| v.name),
CurrencyId::DexShare(symbol_0, symbol_1) => {
let name_0 = match symbol_0 {
DexShare::Token(symbol) => CurrencyId::Token(symbol).name().map(|v| v.as_bytes().to_vec()),
Expand Down Expand Up @@ -755,7 +787,7 @@ impl<T: Config> Erc20InfoMapping for EvmErc20InfoMapping<T> {
// the EvmAddress must have been mapped.
fn symbol(currency_id: CurrencyId) -> Option<Vec<u8>> {
let symbol = match currency_id {
CurrencyId::Token(_) => currency_id.symbol().map(|v| v.as_bytes().to_vec()),
CurrencyId::Token(_) => AssetMetadatas::<T>::get(AssetIds::NativeAssetId(currency_id)).map(|v| v.symbol),
CurrencyId::DexShare(symbol_0, symbol_1) => {
let token_symbol_0 = match symbol_0 {
DexShare::Token(symbol) => CurrencyId::Token(symbol).symbol().map(|v| v.as_bytes().to_vec()),
Expand Down Expand Up @@ -837,7 +869,7 @@ impl<T: Config> Erc20InfoMapping for EvmErc20InfoMapping<T> {
// the EvmAddress must have been mapped.
fn decimals(currency_id: CurrencyId) -> Option<u8> {
match currency_id {
CurrencyId::Token(_) => currency_id.decimals(),
CurrencyId::Token(_) => AssetMetadatas::<T>::get(AssetIds::NativeAssetId(currency_id)).map(|v| v.decimals),
CurrencyId::DexShare(symbol_0, _) => {
// initial dex share amount is calculated based on currency_id_0,
// use the decimals of currency_id_0 as the decimals of lp token.
Expand Down
6 changes: 6 additions & 0 deletions modules/asset-registry/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ impl ExtBuilder {
.build_storage::<Runtime>()
.unwrap();

asset_registry::GenesisConfig::<Runtime> {
assets: vec![(CurrencyId::Token(TokenSymbol::ACA), 1)],
}
.assimilate_storage(&mut t)
.unwrap();

pallet_balances::GenesisConfig::<Runtime> {
balances: self.balances.into_iter().collect::<Vec<_>>(),
}
Expand Down
1 change: 1 addition & 0 deletions node/service/src/chain_spec/acala.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ fn acala_genesis(
collaterals_params: vec![],
global_interest_rate_per_sec: Default::default(),
},
asset_registry: Default::default(),
evm: Default::default(),
dex: DexConfig {
initial_listing_trading_pairs: vec![],
Expand Down
1 change: 1 addition & 0 deletions node/service/src/chain_spec/karura.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ fn karura_genesis(
collaterals_params: vec![],
global_interest_rate_per_sec: Default::default(),
},
asset_registry: Default::default(),
evm: Default::default(),
dex: DexConfig {
initial_listing_trading_pairs: vec![],
Expand Down
46 changes: 33 additions & 13 deletions node/service/src/chain_spec/mandala.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use acala_primitives::{AccountId, Balance, TokenSymbol};
use acala_primitives::{orml_traits::GetByKey, AccountId, Balance, TokenSymbol};
use coins_bip39::{English, Mnemonic, Wordlist};
use elliptic_curve::sec1::ToEncodedPoint;
use hex_literal::hex;
Expand Down Expand Up @@ -308,12 +308,13 @@ fn testnet_genesis(
evm_accounts: Vec<H160>,
) -> mandala_runtime::GenesisConfig {
use mandala_runtime::{
dollar, get_all_module_accounts, BalancesConfig, CdpEngineConfig, CdpTreasuryConfig, CollatorSelectionConfig,
DexConfig, EVMConfig, EnabledTradingPairs, FinancialCouncilMembershipConfig, GeneralCouncilMembershipConfig,
HomaCouncilMembershipConfig, IndicesConfig, NativeTokenExistentialDeposit, OperatorMembershipAcalaConfig,
OrmlNFTConfig, ParachainInfoConfig, PolkadotXcmConfig, RenVmBridgeConfig, SessionConfig, SessionDuration,
SessionKeys, SessionManagerConfig, StarportConfig, SudoConfig, SystemConfig,
TechnicalCommitteeMembershipConfig, TokensConfig, VestingConfig, ACA, AUSD, DOT, LDOT, RENBTC,
dollar, get_all_module_accounts, AssetRegistryConfig, BalancesConfig, CdpEngineConfig, CdpTreasuryConfig,
CollatorSelectionConfig, DexConfig, EVMConfig, EnabledTradingPairs, ExistentialDeposits,
FinancialCouncilMembershipConfig, GeneralCouncilMembershipConfig, HomaCouncilMembershipConfig, IndicesConfig,
NativeTokenExistentialDeposit, OperatorMembershipAcalaConfig, OrmlNFTConfig, ParachainInfoConfig,
PolkadotXcmConfig, RenVmBridgeConfig, SessionConfig, SessionDuration, SessionKeys, SessionManagerConfig,
StarportConfig, SudoConfig, SystemConfig, TechnicalCommitteeMembershipConfig, TokensConfig, VestingConfig, ACA,
AUSD, DOT, LDOT, RENBTC,
};

let existential_deposit = NativeTokenExistentialDeposit::get();
Expand Down Expand Up @@ -431,6 +432,15 @@ fn testnet_genesis(
1_000_000_000_000_000_000u128,
), /* 5% APR */
},
asset_registry: AssetRegistryConfig {
assets: vec![
(ACA, NativeTokenExistentialDeposit::get()),
(AUSD, ExistentialDeposits::get(&AUSD)),
(DOT, ExistentialDeposits::get(&DOT)),
(LDOT, ExistentialDeposits::get(&LDOT)),
(RENBTC, ExistentialDeposits::get(&RENBTC)),
],
},
evm: EVMConfig {
accounts: evm_genesis_accounts,
},
Expand Down Expand Up @@ -501,12 +511,13 @@ fn mandala_genesis(
endowed_accounts: Vec<AccountId>,
) -> mandala_runtime::GenesisConfig {
use mandala_runtime::{
cent, dollar, get_all_module_accounts, BalancesConfig, CdpEngineConfig, CdpTreasuryConfig,
CollatorSelectionConfig, DexConfig, EVMConfig, EnabledTradingPairs, FinancialCouncilMembershipConfig,
GeneralCouncilMembershipConfig, HomaCouncilMembershipConfig, IndicesConfig, NativeTokenExistentialDeposit,
OperatorMembershipAcalaConfig, OrmlNFTConfig, ParachainInfoConfig, PolkadotXcmConfig, RenVmBridgeConfig,
SessionConfig, SessionDuration, SessionKeys, SessionManagerConfig, StarportConfig, SudoConfig, SystemConfig,
TechnicalCommitteeMembershipConfig, TokensConfig, VestingConfig, ACA, AUSD, DOT, LDOT, RENBTC,
cent, dollar, get_all_module_accounts, AssetRegistryConfig, BalancesConfig, CdpEngineConfig, CdpTreasuryConfig,
CollatorSelectionConfig, DexConfig, EVMConfig, EnabledTradingPairs, ExistentialDeposits,
FinancialCouncilMembershipConfig, GeneralCouncilMembershipConfig, HomaCouncilMembershipConfig, IndicesConfig,
NativeTokenExistentialDeposit, OperatorMembershipAcalaConfig, OrmlNFTConfig, ParachainInfoConfig,
PolkadotXcmConfig, RenVmBridgeConfig, SessionConfig, SessionDuration, SessionKeys, SessionManagerConfig,
StarportConfig, SudoConfig, SystemConfig, TechnicalCommitteeMembershipConfig, TokensConfig, VestingConfig, ACA,
AUSD, DOT, LDOT, RENBTC,
};

let existential_deposit = NativeTokenExistentialDeposit::get();
Expand Down Expand Up @@ -621,6 +632,15 @@ fn mandala_genesis(
1_000_000_000_000_000_000u128,
), /* 5% APR */
},
asset_registry: AssetRegistryConfig {
assets: vec![
(ACA, NativeTokenExistentialDeposit::get()),
(AUSD, ExistentialDeposits::get(&AUSD)),
(DOT, ExistentialDeposits::get(&DOT)),
(LDOT, ExistentialDeposits::get(&LDOT)),
(RENBTC, ExistentialDeposits::get(&RENBTC)),
],
},
evm: EVMConfig {
accounts: evm_genesis_accounts,
},
Expand Down
4 changes: 3 additions & 1 deletion runtime/acala/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

#![cfg(feature = "runtime-benchmarks")]

pub mod utils;
pub mod utils {
include!("../../../mandala/src/benchmarking/utils.rs");
}

// module benchmarking
pub mod asset_registry {
Expand Down
75 changes: 0 additions & 75 deletions runtime/acala/src/benchmarking/utils.rs

This file was deleted.

5 changes: 5 additions & 0 deletions runtime/common/src/precompile/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,11 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
module_evm::GenesisConfig::<Test> { accounts }
.assimilate_storage(&mut storage)
.unwrap();
module_asset_registry::GenesisConfig::<Test> {
assets: vec![(ACA, ExistenceRequirement::get()), (RENBTC, 0)],
}
.assimilate_storage(&mut storage)
.unwrap();

let mut ext = sp_io::TestExternalities::new(storage);
ext.execute_with(|| {
Expand Down
11 changes: 11 additions & 0 deletions runtime/integration-tests/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,17 @@ impl ExtBuilder {
)
.unwrap();

module_asset_registry::GenesisConfig::<Runtime> {
assets: vec![
(NATIVE_CURRENCY, existential_deposit),
(LIQUID_CURRENCY, ExistentialDeposits::get(&LIQUID_CURRENCY)),
(RELAY_CHAIN_CURRENCY, ExistentialDeposits::get(&RELAY_CHAIN_CURRENCY)),
(USD_CURRENCY, ExistentialDeposits::get(&USD_CURRENCY)),
],
}
.assimilate_storage(&mut t)
.unwrap();

module_dex::GenesisConfig::<Runtime> {
initial_enabled_trading_pairs: initial_enabled_trading_pairs,
initial_listing_trading_pairs: Default::default(),
Expand Down
4 changes: 3 additions & 1 deletion runtime/karura/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

#![cfg(feature = "runtime-benchmarks")]

pub mod utils;
pub mod utils {
include!("../../../mandala/src/benchmarking/utils.rs");
}

// module benchmarking
pub mod asset_registry {
Expand Down
Loading

0 comments on commit 7c16cec

Please sign in to comment.