jam_tooling/
rpc.rs

1use crate::{CodeInfo, Error};
2use jam_program_blob::{ConventionalMetadata as Metadata, CrateInfo};
3use jam_std_common::{RpcClient, Service};
4use jam_types::{Hash, ServiceId};
5use jsonrpsee::ws_client::WsClient;
6use scale::{Compact, Decode};
7
8pub async fn query_service(
9	rpc: &WsClient,
10	head: Hash,
11	id: ServiceId,
12) -> Result<(Service, CodeInfo<CrateInfo>), anyhow::Error> {
13	use CodeInfo::*;
14	let req = rpc.service_data(head, id).await?.ok_or(Error::ServiceNotFound)?;
15	let info = Service::decode(&mut &req[..]).map_err(Error::InvalidServiceEncoding)?;
16	let maybe_code = rpc.service_preimage(head, id, info.code_hash.0).await?;
17	let meta = match maybe_code {
18		None => CodeNotProvided(info.code_hash),
19		Some(code) => match <(Compact<u32>, Metadata)>::decode(&mut &code[..]).ok().map(|x| x.1) {
20			None => Undefined(info.code_hash),
21			Some(Metadata::Info(crate_info)) => Known(crate_info),
22		},
23	};
24	Ok((info, meta))
25}