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 token_exists(collection: CollectionId, token: TokenId) -> Result<bool, DispatchError> {18 dispatch_unique_runtime!(collection.token_exists(token))19 }2021 fn token_owner(collection: CollectionId, token: TokenId) -> Result<Option<CrossAccountId>, DispatchError> {22 dispatch_unique_runtime!(collection.token_owner(token))23 }24 fn const_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>, DispatchError> {25 dispatch_unique_runtime!(collection.const_metadata(token))26 }27 fn variable_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>, DispatchError> {28 dispatch_unique_runtime!(collection.variable_metadata(token))29 }3031 fn collection_tokens(collection: CollectionId) -> Result<u32, DispatchError> {32 dispatch_unique_runtime!(collection.collection_tokens())33 }34 fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result<u32, DispatchError> {35 dispatch_unique_runtime!(collection.account_balance(account))36 }37 fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<u128, DispatchError> {38 dispatch_unique_runtime!(collection.balance(account, token))39 }40 fn allowance(41 collection: CollectionId,42 sender: CrossAccountId,43 spender: CrossAccountId,44 token: TokenId,45 ) -> Result<u128, DispatchError> {46 dispatch_unique_runtime!(collection.allowance(sender, spender, token))47 }4849 fn adminlist(collection: CollectionId) -> Result<Vec<CrossAccountId>, DispatchError> {50 Ok(<pallet_common::Pallet<Runtime>>::adminlist(collection))51 }52 fn allowlist(collection: CollectionId) -> Result<Vec<CrossAccountId>, DispatchError> {53 Ok(<pallet_common::Pallet<Runtime>>::allowlist(collection))54 }55 fn allowed(collection: CollectionId, user: CrossAccountId) -> Result<bool, DispatchError> {56 Ok(<pallet_common::Pallet<Runtime>>::allowed(collection, user))57 }58 fn last_token_id(collection: CollectionId) -> Result<TokenId, DispatchError> {59 dispatch_unique_runtime!(collection.last_token_id())60 }61 fn collection_by_id(collection: CollectionId) -> Result<Option<Collection<AccountId>>, DispatchError> {62 Ok(<pallet_common::CollectionById<Runtime>>::get(collection))63 }64 fn collection_stats() -> Result<CollectionStats, DispatchError> {65 Ok(<pallet_common::Pallet<Runtime>>::collection_stats())66 }67 fn next_sponsored(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<Option<u64>, DispatchError> {68 Ok(<pallet_unique::UniqueSponsorshipPredict<Runtime> as69 pallet_unique::SponsorshipPredict<Runtime>>::predict(70 collection,71 account,72 token))73 }7475 fn effective_collection_limits(collection: CollectionId) -> Result<Option<CollectionLimits>, DispatchError> {76 Ok(<pallet_common::Pallet<Runtime>>::effective_collection_limits(collection))77 }78 }7980 impl sp_api::Core<Block> for Runtime {81 fn version() -> RuntimeVersion {82 VERSION83 }8485 fn execute_block(block: Block) {86 Executive::execute_block(block)87 }8889 fn initialize_block(header: &<Block as BlockT>::Header) {90 Executive::initialize_block(header)91 }92 }9394 impl sp_api::Metadata<Block> for Runtime {95 fn metadata() -> OpaqueMetadata {96 OpaqueMetadata::new(Runtime::metadata().into())97 }98 }99100 impl sp_block_builder::BlockBuilder<Block> for Runtime {101 fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyExtrinsicResult {102 Executive::apply_extrinsic(extrinsic)103 }104105 fn finalize_block() -> <Block as BlockT>::Header {106 Executive::finalize_block()107 }108109 fn inherent_extrinsics(data: sp_inherents::InherentData) -> Vec<<Block as BlockT>::Extrinsic> {110 data.create_extrinsics()111 }112113 fn check_inherents(114 block: Block,115 data: sp_inherents::InherentData,116 ) -> sp_inherents::CheckInherentsResult {117 data.check_extrinsics(&block)118 }119120 121 122 123 }124125 impl sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block> for Runtime {126 fn validate_transaction(127 source: TransactionSource,128 tx: <Block as BlockT>::Extrinsic,129 hash: <Block as BlockT>::Hash,130 ) -> TransactionValidity {131 Executive::validate_transaction(source, tx, hash)132 }133 }134135 impl sp_offchain::OffchainWorkerApi<Block> for Runtime {136 fn offchain_worker(header: &<Block as BlockT>::Header) {137 Executive::offchain_worker(header)138 }139 }140141 impl fp_rpc::EthereumRuntimeRPCApi<Block> for Runtime {142 fn chain_id() -> u64 {143 <Runtime as pallet_evm::Config>::ChainId::get()144 }145146 fn account_basic(address: H160) -> EVMAccount {147 EVM::account_basic(&address)148 }149150 fn gas_price() -> U256 {151 <Runtime as pallet_evm::Config>::FeeCalculator::min_gas_price()152 }153154 fn account_code_at(address: H160) -> Vec<u8> {155 EVM::account_codes(address)156 }157158 fn author() -> H160 {159 <pallet_evm::Pallet<Runtime>>::find_author()160 }161162 fn storage_at(address: H160, index: U256) -> H256 {163 let mut tmp = [0u8; 32];164 index.to_big_endian(&mut tmp);165 EVM::account_storages(address, H256::from_slice(&tmp[..]))166 }167168 #[allow(clippy::redundant_closure)]169 fn call(170 from: H160,171 to: H160,172 data: Vec<u8>,173 value: U256,174 gas_limit: U256,175 max_fee_per_gas: Option<U256>,176 max_priority_fee_per_gas: Option<U256>,177 nonce: Option<U256>,178 estimate: bool,179 access_list: Option<Vec<(H160, Vec<H256>)>>,180 ) -> Result<pallet_evm::CallInfo, sp_runtime::DispatchError> {181 let config = if estimate {182 let mut config = <Runtime as pallet_evm::Config>::config().clone();183 config.estimate = true;184 Some(config)185 } else {186 None187 };188189 <Runtime as pallet_evm::Config>::Runner::call(190 CrossAccountId::from_eth(from),191 to,192 data,193 value,194 gas_limit.low_u64(),195 max_fee_per_gas,196 max_priority_fee_per_gas,197 nonce,198 access_list.unwrap_or_default(),199 config.as_ref().unwrap_or_else(|| <Runtime as pallet_evm::Config>::config()),200 ).map_err(|err| err.into())201 }202203 #[allow(clippy::redundant_closure)]204 fn create(205 from: H160,206 data: Vec<u8>,207 value: U256,208 gas_limit: U256,209 max_fee_per_gas: Option<U256>,210 max_priority_fee_per_gas: Option<U256>,211 nonce: Option<U256>,212 estimate: bool,213 access_list: Option<Vec<(H160, Vec<H256>)>>,214 ) -> Result<pallet_evm::CreateInfo, sp_runtime::DispatchError> {215 let config = if estimate {216 let mut config = <Runtime as pallet_evm::Config>::config().clone();217 config.estimate = true;218 Some(config)219 } else {220 None221 };222223 <Runtime as pallet_evm::Config>::Runner::create(224 CrossAccountId::from_eth(from),225 data,226 value,227 gas_limit.low_u64(),228 max_fee_per_gas,229 max_priority_fee_per_gas,230 nonce,231 access_list.unwrap_or_default(),232 config.as_ref().unwrap_or_else(|| <Runtime as pallet_evm::Config>::config()),233 ).map_err(|err| err.into())234 }235236 fn current_transaction_statuses() -> Option<Vec<TransactionStatus>> {237 Ethereum::current_transaction_statuses()238 }239240 fn current_block() -> Option<pallet_ethereum::Block> {241 Ethereum::current_block()242 }243244 fn current_receipts() -> Option<Vec<pallet_ethereum::Receipt>> {245 Ethereum::current_receipts()246 }247248 fn current_all() -> (249 Option<pallet_ethereum::Block>,250 Option<Vec<pallet_ethereum::Receipt>>,251 Option<Vec<TransactionStatus>>252 ) {253 (254 Ethereum::current_block(),255 Ethereum::current_receipts(),256 Ethereum::current_transaction_statuses()257 )258 }259260 fn extrinsic_filter(xts: Vec<<Block as sp_api::BlockT>::Extrinsic>) -> Vec<pallet_ethereum::Transaction> {261 xts.into_iter().filter_map(|xt| match xt.0.function {262 Call::Ethereum(pallet_ethereum::Call::transact { transaction }) => Some(transaction),263 _ => None264 }).collect()265 }266267 fn elasticity() -> Option<Permill> {268 None269 }270 }271272 impl fp_rpc::ConvertTransactionRuntimeApi<Block> for Runtime {273 fn convert_transaction(transaction: pallet_ethereum::Transaction) -> <Block as BlockT>::Extrinsic {274 UncheckedExtrinsic::new_unsigned(275 pallet_ethereum::Call::<Runtime>::transact { transaction }.into(),276 )277 }278 }279280 impl sp_session::SessionKeys<Block> for Runtime {281 fn decode_session_keys(282 encoded: Vec<u8>,283 ) -> Option<Vec<(Vec<u8>, KeyTypeId)>> {284 SessionKeys::decode_into_raw_public_keys(&encoded)285 }286287 fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {288 SessionKeys::generate(seed)289 }290 }291292 impl sp_consensus_aura::AuraApi<Block, AuraId> for Runtime {293 fn slot_duration() -> sp_consensus_aura::SlotDuration {294 sp_consensus_aura::SlotDuration::from_millis(Aura::slot_duration())295 }296297 fn authorities() -> Vec<AuraId> {298 Aura::authorities().to_vec()299 }300 }301302 impl cumulus_primitives_core::CollectCollationInfo<Block> for Runtime {303 fn collect_collation_info(header: &<Block as BlockT>::Header) -> cumulus_primitives_core::CollationInfo {304 ParachainSystem::collect_collation_info(header)305 }306 }307308 impl frame_system_rpc_runtime_api::AccountNonceApi<Block, AccountId, Index> for Runtime {309 fn account_nonce(account: AccountId) -> Index {310 System::account_nonce(account)311 }312 }313314 impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance> for Runtime {315 fn query_info(uxt: <Block as BlockT>::Extrinsic, len: u32) -> RuntimeDispatchInfo<Balance> {316 TransactionPayment::query_info(uxt, len)317 }318 fn query_fee_details(uxt: <Block as BlockT>::Extrinsic, len: u32) -> FeeDetails<Balance> {319 TransactionPayment::query_fee_details(uxt, len)320 }321 }322323 324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 #[cfg(feature = "runtime-benchmarks")]365 impl frame_benchmarking::Benchmark<Block> for Runtime {366 fn benchmark_metadata(extra: bool) -> (367 Vec<frame_benchmarking::BenchmarkList>,368 Vec<frame_support::traits::StorageInfo>,369 ) {370 use frame_benchmarking::{list_benchmark, Benchmarking, BenchmarkList};371 use frame_support::traits::StorageInfoTrait;372373 let mut list = Vec::<BenchmarkList>::new();374375 list_benchmark!(list, extra, pallet_evm_migration, EvmMigration);376 list_benchmark!(list, extra, pallet_unique, Unique);377 list_benchmark!(list, extra, pallet_inflation, Inflation);378 list_benchmark!(list, extra, pallet_fungible, Fungible);379 list_benchmark!(list, extra, pallet_refungible, Refungible);380 list_benchmark!(list, extra, pallet_nonfungible, Nonfungible);381 382383 let storage_info = AllPalletsReversedWithSystemFirst::storage_info();384385 return (list, storage_info)386 }387388 fn dispatch_benchmark(389 config: frame_benchmarking::BenchmarkConfig390 ) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {391 use frame_benchmarking::{Benchmarking, BenchmarkBatch, add_benchmark, TrackedStorageKey};392393 let allowlist: Vec<TrackedStorageKey> = vec![394 395 hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac").to_vec().into(),396 397 hex_literal::hex!("c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80").to_vec().into(),398 399 hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7ff553b5a9862a516939d82b3d3d8661a").to_vec().into(),400 401 hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850").to_vec().into(),402 403 hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7").to_vec().into(),404 ];405406 let mut batches = Vec::<BenchmarkBatch>::new();407 let params = (&config, &allowlist);408409 add_benchmark!(params, batches, pallet_evm_migration, EvmMigration);410 add_benchmark!(params, batches, pallet_unique, Unique);411 add_benchmark!(params, batches, pallet_inflation, Inflation);412 add_benchmark!(params, batches, pallet_fungible, Fungible);413 add_benchmark!(params, batches, pallet_refungible, Refungible);414 add_benchmark!(params, batches, pallet_nonfungible, Nonfungible);415 416417 if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }418 Ok(batches)419 }420 }421 }422 }423}