1#[macro_export]2macro_rules! impl_common_runtime_apis {3 (4 $(5 #![custom_apis]67 $($custom_apis:tt)+8 )?9 ) => {10 impl_runtime_apis! {11 $($($custom_apis)+)?1213 impl up_rpc::UniqueApi<Block, CrossAccountId, AccountId> for Runtime {14 fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Result<Vec<TokenId>, DispatchError> {15 dispatch_unique_runtime!(collection.account_tokens(account))16 }17 fn collection_tokens(collection: CollectionId) -> Result<Vec<TokenId>, DispatchError> {18 dispatch_unique_runtime!(collection.collection_tokens())19 }20 fn token_exists(collection: CollectionId, token: TokenId) -> Result<bool, DispatchError> {21 dispatch_unique_runtime!(collection.token_exists(token))22 }2324 fn token_owner(collection: CollectionId, token: TokenId) -> Result<Option<CrossAccountId>, DispatchError> {25 dispatch_unique_runtime!(collection.token_owner(token))26 }27 fn topmost_token_owner(collection: CollectionId, token: TokenId) -> Result<Option<CrossAccountId>, DispatchError> {28 let budget = up_data_structs::budget::Value::new(5);2930 Ok(Some(<pallet_structure::Pallet<Runtime>>::find_topmost_owner(collection, token, &budget)?))31 }32 fn const_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>, DispatchError> {33 dispatch_unique_runtime!(collection.const_metadata(token))34 }3536 fn collection_properties(37 collection: CollectionId,38 keys: Option<Vec<Vec<u8>>>39 ) -> Result<Vec<Property>, DispatchError> {40 let keys = keys.map(41 |keys| pallet_common::Pallet::<Runtime>::bytes_keys_to_property_keys(keys)42 ).transpose()?;4344 pallet_common::Pallet::<Runtime>::filter_collection_properties(collection, keys)45 }4647 fn token_properties(48 collection: CollectionId,49 token_id: TokenId,50 keys: Option<Vec<Vec<u8>>>51 ) -> Result<Vec<Property>, DispatchError> {52 let keys = keys.map(53 |keys| pallet_common::Pallet::<Runtime>::bytes_keys_to_property_keys(keys)54 ).transpose()?;5556 dispatch_unique_runtime!(collection.token_properties(token_id, keys))57 }5859 fn property_permissions(60 collection: CollectionId,61 keys: Option<Vec<Vec<u8>>>62 ) -> Result<Vec<PropertyKeyPermission>, DispatchError> {63 let keys = keys.map(64 |keys| pallet_common::Pallet::<Runtime>::bytes_keys_to_property_keys(keys)65 ).transpose()?;6667 pallet_common::Pallet::<Runtime>::filter_property_permissions(collection, keys)68 }6970 fn token_data(71 collection: CollectionId,72 token_id: TokenId,73 keys: Option<Vec<Vec<u8>>>74 ) -> Result<TokenData<CrossAccountId>, DispatchError> {75 let token_data = TokenData {76 const_data: Self::const_metadata(collection, token_id)?,77 properties: Self::token_properties(collection, token_id, keys)?,78 owner: Self::token_owner(collection, token_id)?79 };8081 Ok(token_data)82 }8384 fn total_supply(collection: CollectionId) -> Result<u32, DispatchError> {85 dispatch_unique_runtime!(collection.total_supply())86 }87 fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result<u32, DispatchError> {88 dispatch_unique_runtime!(collection.account_balance(account))89 }90 fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<u128, DispatchError> {91 dispatch_unique_runtime!(collection.balance(account, token))92 }93 fn allowance(94 collection: CollectionId,95 sender: CrossAccountId,96 spender: CrossAccountId,97 token: TokenId,98 ) -> Result<u128, DispatchError> {99 dispatch_unique_runtime!(collection.allowance(sender, spender, token))100 }101102 fn adminlist(collection: CollectionId) -> Result<Vec<CrossAccountId>, DispatchError> {103 Ok(<pallet_common::Pallet<Runtime>>::adminlist(collection))104 }105 fn allowlist(collection: CollectionId) -> Result<Vec<CrossAccountId>, DispatchError> {106 Ok(<pallet_common::Pallet<Runtime>>::allowlist(collection))107 }108 fn allowed(collection: CollectionId, user: CrossAccountId) -> Result<bool, DispatchError> {109 Ok(<pallet_common::Pallet<Runtime>>::allowed(collection, user))110 }111 fn last_token_id(collection: CollectionId) -> Result<TokenId, DispatchError> {112 dispatch_unique_runtime!(collection.last_token_id())113 }114 fn collection_by_id(collection: CollectionId) -> Result<Option<RpcCollection<AccountId>>, DispatchError> {115 Ok(<pallet_common::Pallet<Runtime>>::rpc_collection(collection))116 }117 fn collection_stats() -> Result<CollectionStats, DispatchError> {118 Ok(<pallet_common::Pallet<Runtime>>::collection_stats())119 }120 fn next_sponsored(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<Option<u64>, DispatchError> {121 Ok(<$crate::sponsoring::UniqueSponsorshipPredict<Runtime> as122 $crate::sponsoring::SponsorshipPredict<Runtime>>::predict(123 collection,124 account,125 token))126 }127128 fn effective_collection_limits(collection: CollectionId) -> Result<Option<CollectionLimits>, DispatchError> {129 Ok(<pallet_common::Pallet<Runtime>>::effective_collection_limits(collection))130 }131 }132133 impl sp_api::Core<Block> for Runtime {134 fn version() -> RuntimeVersion {135 VERSION136 }137138 fn execute_block(block: Block) {139 Executive::execute_block(block)140 }141142 fn initialize_block(header: &<Block as BlockT>::Header) {143 Executive::initialize_block(header)144 }145 }146147 impl sp_api::Metadata<Block> for Runtime {148 fn metadata() -> OpaqueMetadata {149 OpaqueMetadata::new(Runtime::metadata().into())150 }151 }152153 impl sp_block_builder::BlockBuilder<Block> for Runtime {154 fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyExtrinsicResult {155 Executive::apply_extrinsic(extrinsic)156 }157158 fn finalize_block() -> <Block as BlockT>::Header {159 Executive::finalize_block()160 }161162 fn inherent_extrinsics(data: sp_inherents::InherentData) -> Vec<<Block as BlockT>::Extrinsic> {163 data.create_extrinsics()164 }165166 fn check_inherents(167 block: Block,168 data: sp_inherents::InherentData,169 ) -> sp_inherents::CheckInherentsResult {170 data.check_extrinsics(&block)171 }172173 174 175 176 }177178 impl sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block> for Runtime {179 fn validate_transaction(180 source: TransactionSource,181 tx: <Block as BlockT>::Extrinsic,182 hash: <Block as BlockT>::Hash,183 ) -> TransactionValidity {184 Executive::validate_transaction(source, tx, hash)185 }186 }187188 impl sp_offchain::OffchainWorkerApi<Block> for Runtime {189 fn offchain_worker(header: &<Block as BlockT>::Header) {190 Executive::offchain_worker(header)191 }192 }193194 impl fp_rpc::EthereumRuntimeRPCApi<Block> for Runtime {195 fn chain_id() -> u64 {196 <Runtime as pallet_evm::Config>::ChainId::get()197 }198199 fn account_basic(address: H160) -> EVMAccount {200 EVM::account_basic(&address)201 }202203 fn gas_price() -> U256 {204 <Runtime as pallet_evm::Config>::FeeCalculator::min_gas_price()205 }206207 fn account_code_at(address: H160) -> Vec<u8> {208 EVM::account_codes(address)209 }210211 fn author() -> H160 {212 <pallet_evm::Pallet<Runtime>>::find_author()213 }214215 fn storage_at(address: H160, index: U256) -> H256 {216 let mut tmp = [0u8; 32];217 index.to_big_endian(&mut tmp);218 EVM::account_storages(address, H256::from_slice(&tmp[..]))219 }220221 #[allow(clippy::redundant_closure)]222 fn call(223 from: H160,224 to: H160,225 data: Vec<u8>,226 value: U256,227 gas_limit: U256,228 max_fee_per_gas: Option<U256>,229 max_priority_fee_per_gas: Option<U256>,230 nonce: Option<U256>,231 estimate: bool,232 access_list: Option<Vec<(H160, Vec<H256>)>>,233 ) -> Result<pallet_evm::CallInfo, sp_runtime::DispatchError> {234 let config = if estimate {235 let mut config = <Runtime as pallet_evm::Config>::config().clone();236 config.estimate = true;237 Some(config)238 } else {239 None240 };241242 let is_transactional = false;243 <Runtime as pallet_evm::Config>::Runner::call(244 CrossAccountId::from_eth(from),245 to,246 data,247 value,248 gas_limit.low_u64(),249 max_fee_per_gas,250 max_priority_fee_per_gas,251 nonce,252 access_list.unwrap_or_default(),253 is_transactional,254 config.as_ref().unwrap_or_else(|| <Runtime as pallet_evm::Config>::config()),255 ).map_err(|err| err.into())256 }257258 #[allow(clippy::redundant_closure)]259 fn create(260 from: H160,261 data: Vec<u8>,262 value: U256,263 gas_limit: U256,264 max_fee_per_gas: Option<U256>,265 max_priority_fee_per_gas: Option<U256>,266 nonce: Option<U256>,267 estimate: bool,268 access_list: Option<Vec<(H160, Vec<H256>)>>,269 ) -> Result<pallet_evm::CreateInfo, sp_runtime::DispatchError> {270 let config = if estimate {271 let mut config = <Runtime as pallet_evm::Config>::config().clone();272 config.estimate = true;273 Some(config)274 } else {275 None276 };277278 let is_transactional = false;279 <Runtime as pallet_evm::Config>::Runner::create(280 CrossAccountId::from_eth(from),281 data,282 value,283 gas_limit.low_u64(),284 max_fee_per_gas,285 max_priority_fee_per_gas,286 nonce,287 access_list.unwrap_or_default(),288 is_transactional,289 config.as_ref().unwrap_or_else(|| <Runtime as pallet_evm::Config>::config()),290 ).map_err(|err| err.into())291 }292293 fn current_transaction_statuses() -> Option<Vec<TransactionStatus>> {294 Ethereum::current_transaction_statuses()295 }296297 fn current_block() -> Option<pallet_ethereum::Block> {298 Ethereum::current_block()299 }300301 fn current_receipts() -> Option<Vec<pallet_ethereum::Receipt>> {302 Ethereum::current_receipts()303 }304305 fn current_all() -> (306 Option<pallet_ethereum::Block>,307 Option<Vec<pallet_ethereum::Receipt>>,308 Option<Vec<TransactionStatus>>309 ) {310 (311 Ethereum::current_block(),312 Ethereum::current_receipts(),313 Ethereum::current_transaction_statuses()314 )315 }316317 fn extrinsic_filter(xts: Vec<<Block as sp_api::BlockT>::Extrinsic>) -> Vec<pallet_ethereum::Transaction> {318 xts.into_iter().filter_map(|xt| match xt.0.function {319 Call::Ethereum(pallet_ethereum::Call::transact { transaction }) => Some(transaction),320 _ => None321 }).collect()322 }323324 fn elasticity() -> Option<Permill> {325 None326 }327 }328329 impl fp_rpc::ConvertTransactionRuntimeApi<Block> for Runtime {330 fn convert_transaction(transaction: pallet_ethereum::Transaction) -> <Block as BlockT>::Extrinsic {331 UncheckedExtrinsic::new_unsigned(332 pallet_ethereum::Call::<Runtime>::transact { transaction }.into(),333 )334 }335 }336337 impl sp_session::SessionKeys<Block> for Runtime {338 fn decode_session_keys(339 encoded: Vec<u8>,340 ) -> Option<Vec<(Vec<u8>, KeyTypeId)>> {341 SessionKeys::decode_into_raw_public_keys(&encoded)342 }343344 fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {345 SessionKeys::generate(seed)346 }347 }348349 impl sp_consensus_aura::AuraApi<Block, AuraId> for Runtime {350 fn slot_duration() -> sp_consensus_aura::SlotDuration {351 sp_consensus_aura::SlotDuration::from_millis(Aura::slot_duration())352 }353354 fn authorities() -> Vec<AuraId> {355 Aura::authorities().to_vec()356 }357 }358359 impl cumulus_primitives_core::CollectCollationInfo<Block> for Runtime {360 fn collect_collation_info(header: &<Block as BlockT>::Header) -> cumulus_primitives_core::CollationInfo {361 ParachainSystem::collect_collation_info(header)362 }363 }364365 impl frame_system_rpc_runtime_api::AccountNonceApi<Block, AccountId, Index> for Runtime {366 fn account_nonce(account: AccountId) -> Index {367 System::account_nonce(account)368 }369 }370371 impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance> for Runtime {372 fn query_info(uxt: <Block as BlockT>::Extrinsic, len: u32) -> RuntimeDispatchInfo<Balance> {373 TransactionPayment::query_info(uxt, len)374 }375 fn query_fee_details(uxt: <Block as BlockT>::Extrinsic, len: u32) -> FeeDetails<Balance> {376 TransactionPayment::query_fee_details(uxt, len)377 }378 }379380 381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 #[cfg(feature = "runtime-benchmarks")]422 impl frame_benchmarking::Benchmark<Block> for Runtime {423 fn benchmark_metadata(extra: bool) -> (424 Vec<frame_benchmarking::BenchmarkList>,425 Vec<frame_support::traits::StorageInfo>,426 ) {427 use frame_benchmarking::{list_benchmark, Benchmarking, BenchmarkList};428 use frame_support::traits::StorageInfoTrait;429430 let mut list = Vec::<BenchmarkList>::new();431432 list_benchmark!(list, extra, pallet_evm_migration, EvmMigration);433 list_benchmark!(list, extra, pallet_unique, Unique);434 list_benchmark!(list, extra, pallet_structure, Structure);435 list_benchmark!(list, extra, pallet_inflation, Inflation);436 list_benchmark!(list, extra, pallet_fungible, Fungible);437 list_benchmark!(list, extra, pallet_refungible, Refungible);438 list_benchmark!(list, extra, pallet_nonfungible, Nonfungible);439 440441 let storage_info = AllPalletsReversedWithSystemFirst::storage_info();442443 return (list, storage_info)444 }445446 fn dispatch_benchmark(447 config: frame_benchmarking::BenchmarkConfig448 ) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {449 use frame_benchmarking::{Benchmarking, BenchmarkBatch, add_benchmark, TrackedStorageKey};450451 let allowlist: Vec<TrackedStorageKey> = vec![452 453 hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac").to_vec().into(),454 455 hex_literal::hex!("c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80").to_vec().into(),456 457 hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7ff553b5a9862a516939d82b3d3d8661a").to_vec().into(),458 459 hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850").to_vec().into(),460 461 hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7").to_vec().into(),462463 464 hex_literal::hex!("3a7472616e73616374696f6e5f6c6576656c3a").to_vec().into(),465 ];466467 let mut batches = Vec::<BenchmarkBatch>::new();468 let params = (&config, &allowlist);469470 add_benchmark!(params, batches, pallet_evm_migration, EvmMigration);471 add_benchmark!(params, batches, pallet_unique, Unique);472 add_benchmark!(params, batches, pallet_structure, Structure);473 add_benchmark!(params, batches, pallet_inflation, Inflation);474 add_benchmark!(params, batches, pallet_fungible, Fungible);475 add_benchmark!(params, batches, pallet_refungible, Refungible);476 add_benchmark!(params, batches, pallet_nonfungible, Nonfungible);477 478479 if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }480 Ok(batches)481 }482 }483484 #[cfg(feature = "try-runtime")]485 impl frame_try_runtime::TryRuntime<Block> for Runtime {486 fn on_runtime_upgrade() -> (Weight, Weight) {487 log::info!("try-runtime::on_runtime_upgrade unique-chain.");488 let weight = Executive::try_runtime_upgrade().unwrap();489 (weight, RuntimeBlockWeights::get().max_block)490 }491492 fn execute_block_no_check(block: Block) -> Weight {493 Executive::execute_block_no_check(block)494 }495 }496 }497 }498}