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 eth_contract_code(account: H160) -> Option<Vec<u8>> {50 <pallet_unique::UniqueErcSupport<Runtime>>::get_code(&account)51 .or_else(|| <pallet_evm_migration::OnMethodCall<Runtime>>::get_code(&account))52 .or_else(|| <pallet_evm_contract_helpers::HelpersOnMethodCall<Self>>::get_code(&account))53 }54 fn adminlist(collection: CollectionId) -> Result<Vec<CrossAccountId>, DispatchError> {55 Ok(<pallet_common::Pallet<Runtime>>::adminlist(collection))56 }57 fn allowlist(collection: CollectionId) -> Result<Vec<CrossAccountId>, DispatchError> {58 Ok(<pallet_common::Pallet<Runtime>>::allowlist(collection))59 }60 fn allowed(collection: CollectionId, user: CrossAccountId) -> Result<bool, DispatchError> {61 Ok(<pallet_common::Pallet<Runtime>>::allowed(collection, user))62 }63 fn last_token_id(collection: CollectionId) -> Result<TokenId, DispatchError> {64 dispatch_unique_runtime!(collection.last_token_id())65 }66 fn collection_by_id(collection: CollectionId) -> Result<Option<Collection<AccountId>>, DispatchError> {67 Ok(<pallet_common::CollectionById<Runtime>>::get(collection))68 }69 fn collection_stats() -> Result<CollectionStats, DispatchError> {70 Ok(<pallet_common::Pallet<Runtime>>::collection_stats())71 }72 fn next_sponsored(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<Option<u64>, DispatchError> {73 Ok(<pallet_unique::UniqueSponsorshipPredict<Runtime> as74 pallet_unique::SponsorshipPredict<Runtime>>::predict(75 collection,76 account,77 token))78 }79 }8081 impl sp_api::Core<Block> for Runtime {82 fn version() -> RuntimeVersion {83 VERSION84 }8586 fn execute_block(block: Block) {87 Executive::execute_block(block)88 }8990 fn initialize_block(header: &<Block as BlockT>::Header) {91 Executive::initialize_block(header)92 }93 }9495 impl sp_api::Metadata<Block> for Runtime {96 fn metadata() -> OpaqueMetadata {97 OpaqueMetadata::new(Runtime::metadata().into())98 }99 }100101 impl sp_block_builder::BlockBuilder<Block> for Runtime {102 fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyExtrinsicResult {103 Executive::apply_extrinsic(extrinsic)104 }105106 fn finalize_block() -> <Block as BlockT>::Header {107 Executive::finalize_block()108 }109110 fn inherent_extrinsics(data: sp_inherents::InherentData) -> Vec<<Block as BlockT>::Extrinsic> {111 data.create_extrinsics()112 }113114 fn check_inherents(115 block: Block,116 data: sp_inherents::InherentData,117 ) -> sp_inherents::CheckInherentsResult {118 data.check_extrinsics(&block)119 }120121 122 123 124 }125126 impl sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block> for Runtime {127 fn validate_transaction(128 source: TransactionSource,129 tx: <Block as BlockT>::Extrinsic,130 hash: <Block as BlockT>::Hash,131 ) -> TransactionValidity {132 Executive::validate_transaction(source, tx, hash)133 }134 }135136 impl sp_offchain::OffchainWorkerApi<Block> for Runtime {137 fn offchain_worker(header: &<Block as BlockT>::Header) {138 Executive::offchain_worker(header)139 }140 }141142 impl fp_rpc::EthereumRuntimeRPCApi<Block> for Runtime {143 fn chain_id() -> u64 {144 <Runtime as pallet_evm::Config>::ChainId::get()145 }146147 fn account_basic(address: H160) -> EVMAccount {148 EVM::account_basic(&address)149 }150151 fn gas_price() -> U256 {152 <Runtime as pallet_evm::Config>::FeeCalculator::min_gas_price()153 }154155 fn account_code_at(address: H160) -> Vec<u8> {156 EVM::account_codes(address)157 }158159 fn author() -> H160 {160 <pallet_evm::Pallet<Runtime>>::find_author()161 }162163 fn storage_at(address: H160, index: U256) -> H256 {164 let mut tmp = [0u8; 32];165 index.to_big_endian(&mut tmp);166 EVM::account_storages(address, H256::from_slice(&tmp[..]))167 }168169 #[allow(clippy::redundant_closure)]170 fn call(171 from: H160,172 to: H160,173 data: Vec<u8>,174 value: U256,175 gas_limit: U256,176 max_fee_per_gas: Option<U256>,177 max_priority_fee_per_gas: Option<U256>,178 nonce: Option<U256>,179 estimate: bool,180 access_list: Option<Vec<(H160, Vec<H256>)>>,181 ) -> Result<pallet_evm::CallInfo, sp_runtime::DispatchError> {182 let config = if estimate {183 let mut config = <Runtime as pallet_evm::Config>::config().clone();184 config.estimate = true;185 Some(config)186 } else {187 None188 };189190 <Runtime as pallet_evm::Config>::Runner::call(191 from,192 to,193 data,194 value,195 gas_limit.low_u64(),196 max_fee_per_gas,197 max_priority_fee_per_gas,198 nonce,199 access_list.unwrap_or_default(),200 config.as_ref().unwrap_or_else(|| <Runtime as pallet_evm::Config>::config()),201 ).map_err(|err| err.into())202 }203204 #[allow(clippy::redundant_closure)]205 fn create(206 from: H160,207 data: Vec<u8>,208 value: U256,209 gas_limit: U256,210 max_fee_per_gas: Option<U256>,211 max_priority_fee_per_gas: Option<U256>,212 nonce: Option<U256>,213 estimate: bool,214 access_list: Option<Vec<(H160, Vec<H256>)>>,215 ) -> Result<pallet_evm::CreateInfo, sp_runtime::DispatchError> {216 let config = if estimate {217 let mut config = <Runtime as pallet_evm::Config>::config().clone();218 config.estimate = true;219 Some(config)220 } else {221 None222 };223224 <Runtime as pallet_evm::Config>::Runner::create(225 from,226 data,227 value,228 gas_limit.low_u64(),229 max_fee_per_gas,230 max_priority_fee_per_gas,231 nonce,232 access_list.unwrap_or_default(),233 config.as_ref().unwrap_or_else(|| <Runtime as pallet_evm::Config>::config()),234 ).map_err(|err| err.into())235 }236237 fn current_transaction_statuses() -> Option<Vec<TransactionStatus>> {238 Ethereum::current_transaction_statuses()239 }240241 fn current_block() -> Option<pallet_ethereum::Block> {242 Ethereum::current_block()243 }244245 fn current_receipts() -> Option<Vec<pallet_ethereum::Receipt>> {246 Ethereum::current_receipts()247 }248249 fn current_all() -> (250 Option<pallet_ethereum::Block>,251 Option<Vec<pallet_ethereum::Receipt>>,252 Option<Vec<TransactionStatus>>253 ) {254 (255 Ethereum::current_block(),256 Ethereum::current_receipts(),257 Ethereum::current_transaction_statuses()258 )259 }260261 fn extrinsic_filter(xts: Vec<<Block as sp_api::BlockT>::Extrinsic>) -> Vec<pallet_ethereum::Transaction> {262 xts.into_iter().filter_map(|xt| match xt.0.function {263 Call::Ethereum(pallet_ethereum::Call::transact { transaction }) => Some(transaction),264 _ => None265 }).collect()266 }267268 fn elasticity() -> Option<Permill> {269 None270 }271 }272273 impl sp_session::SessionKeys<Block> for Runtime {274 fn decode_session_keys(275 encoded: Vec<u8>,276 ) -> Option<Vec<(Vec<u8>, KeyTypeId)>> {277 SessionKeys::decode_into_raw_public_keys(&encoded)278 }279280 fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {281 SessionKeys::generate(seed)282 }283 }284285 impl sp_consensus_aura::AuraApi<Block, AuraId> for Runtime {286 fn slot_duration() -> sp_consensus_aura::SlotDuration {287 sp_consensus_aura::SlotDuration::from_millis(Aura::slot_duration())288 }289290 fn authorities() -> Vec<AuraId> {291 Aura::authorities().to_vec()292 }293 }294295 impl cumulus_primitives_core::CollectCollationInfo<Block> for Runtime {296 fn collect_collation_info(header: &<Block as BlockT>::Header) -> cumulus_primitives_core::CollationInfo {297 ParachainSystem::collect_collation_info(header)298 }299 }300301 impl frame_system_rpc_runtime_api::AccountNonceApi<Block, AccountId, Index> for Runtime {302 fn account_nonce(account: AccountId) -> Index {303 System::account_nonce(account)304 }305 }306307 impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance> for Runtime {308 fn query_info(uxt: <Block as BlockT>::Extrinsic, len: u32) -> RuntimeDispatchInfo<Balance> {309 TransactionPayment::query_info(uxt, len)310 }311 fn query_fee_details(uxt: <Block as BlockT>::Extrinsic, len: u32) -> FeeDetails<Balance> {312 TransactionPayment::query_fee_details(uxt, len)313 }314 }315316 317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 #[cfg(feature = "runtime-benchmarks")]358 impl frame_benchmarking::Benchmark<Block> for Runtime {359 fn benchmark_metadata(extra: bool) -> (360 Vec<frame_benchmarking::BenchmarkList>,361 Vec<frame_support::traits::StorageInfo>,362 ) {363 use frame_benchmarking::{list_benchmark, Benchmarking, BenchmarkList};364 use frame_support::traits::StorageInfoTrait;365366 let mut list = Vec::<BenchmarkList>::new();367368 list_benchmark!(list, extra, pallet_evm_migration, EvmMigration);369 list_benchmark!(list, extra, pallet_unique, Unique);370 list_benchmark!(list, extra, pallet_inflation, Inflation);371 list_benchmark!(list, extra, pallet_fungible, Fungible);372 list_benchmark!(list, extra, pallet_refungible, Refungible);373 list_benchmark!(list, extra, pallet_nonfungible, Nonfungible);374 375376 let storage_info = AllPalletsReversedWithSystemFirst::storage_info();377378 return (list, storage_info)379 }380381 fn dispatch_benchmark(382 config: frame_benchmarking::BenchmarkConfig383 ) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {384 use frame_benchmarking::{Benchmarking, BenchmarkBatch, add_benchmark, TrackedStorageKey};385386 let allowlist: Vec<TrackedStorageKey> = vec![387 388 hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac").to_vec().into(),389 390 hex_literal::hex!("c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80").to_vec().into(),391 392 hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7ff553b5a9862a516939d82b3d3d8661a").to_vec().into(),393 394 hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850").to_vec().into(),395 396 hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7").to_vec().into(),397 ];398399 let mut batches = Vec::<BenchmarkBatch>::new();400 let params = (&config, &allowlist);401402 add_benchmark!(params, batches, pallet_evm_migration, EvmMigration);403 add_benchmark!(params, batches, pallet_unique, Unique);404 add_benchmark!(params, batches, pallet_inflation, Inflation);405 add_benchmark!(params, batches, pallet_fungible, Fungible);406 add_benchmark!(params, batches, pallet_refungible, Refungible);407 add_benchmark!(params, batches, pallet_nonfungible, Nonfungible);408 409410 if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }411 Ok(batches)412 }413 }414 }415 }416}