git.delta.rocks / unique-network / refs/commits / 9c3f0003132c

difftreelog

fix return TokenData with owner for old runtimes (#957)

Gregory Simonov2023-06-28parent: #65414d6.patch.diff
in: master
* fix: return TokenData with owner for old runtimes

* fix: code review requests

* fix: code review requests

---------

1 file changed

modifiedclient/rpc/src/lib.rsdiffbeforeafterboth
432 keys: Option<Vec<String>>432 keys: Option<Vec<String>>
433 ) -> Vec<PropertyKeyPermission>, unique_api);433 ) -> Vec<PropertyKeyPermission>, unique_api);
434434
435 pass_method!(
436 token_data(435 fn token_data(
436 &self,
437 collection: CollectionId,437 collection: CollectionId,
438 token_id: TokenId,438 token_id: TokenId,
439
440 #[map = string_keys_to_bytes_keys]
441 keys: Option<Vec<String>>,439 keys: Option<Vec<String>>,
440 at: Option<<Block as BlockT>::Hash>,
442 ) -> TokenData<CrossAccountId>, unique_api;441 ) -> Result<TokenData<CrossAccountId>> {
443 changed_in 3, token_data_before_version_3(collection, token_id, string_keys_to_bytes_keys(keys)) => |value| Ok(value.into())442 token_data_internal(self.client.clone(), collection, token_id, keys, at)
444 );443 }
445444
446 pass_method!(adminlist(collection: CollectionId) -> Vec<CrossAccountId>, unique_api);445 pass_method!(adminlist(collection: CollectionId) -> Vec<CrossAccountId>, unique_api);
447 pass_method!(allowlist(collection: CollectionId) -> Vec<CrossAccountId>, unique_api);446 pass_method!(allowlist(collection: CollectionId) -> Vec<CrossAccountId>, unique_api);
497 .collect::<Vec<_>>(), app_promotion_api);496 .collect::<Vec<_>>(), app_promotion_api);
498}497}
498
499fn token_data_internal<Block, Client, AccountId, CrossAccountId>(
500 client: Arc<Client>,
501 collection: CollectionId,
502 token_id: TokenId,
503 keys: Option<Vec<String>>,
504 at: Option<<Block as BlockT>::Hash>,
505) -> Result<TokenData<CrossAccountId>>
506where
507 AccountId: Decode,
508 Block: BlockT,
509 Client: ProvideRuntimeApi<Block> + HeaderBackend<Block>,
510 Client::Api: UniqueRuntimeApi<Block, CrossAccountId, AccountId>,
511 CrossAccountId: pallet_evm::account::CrossAccountId<AccountId>,
512{
513 let api = client.runtime_api();
514 let at = at.unwrap_or_else(|| client.info().best_hash);
515 let api_version = if let Ok(Some(api_version)) =
516 api.api_version::<dyn UniqueRuntimeApi<Block, CrossAccountId, AccountId>>(at)
517 {
518 api_version
519 } else {
520 return Err(anyhow!("api is not available").into());
521 };
522 let result = if api_version >= 3 {
523 api.token_data(at, collection, token_id, string_keys_to_bytes_keys(keys))
524 } else {
525 #[allow(deprecated)]
526 api.token_data_before_version_3(at, collection, token_id, string_keys_to_bytes_keys(keys))
527 .map(
528 |r: sc_service::Result<
529 up_data_structs::TokenDataVersion1<CrossAccountId>,
530 sp_runtime::DispatchError,
531 >| r.and_then(|value| Ok(value.into())),
532 )
533 .or_else(|_| {
534 Ok(api
535 .token_owner(at, collection, token_id)?
536 .map(|owner| TokenData {
537 properties: Vec::new(),
538 owner,
539 pieces: 0,
540 }))
541 })
542 };
543 Ok(result
544 .map_err(|e| anyhow!("unable to query: {e}"))?
545 .map_err(|e| anyhow!("runtime error: {e:?}"))?)
546}
499547
500fn string_keys_to_bytes_keys(keys: Option<Vec<String>>) -> Option<Vec<Vec<u8>>> {548fn string_keys_to_bytes_keys(keys: Option<Vec<String>>) -> Option<Vec<Vec<u8>>> {
501 keys.map(|keys| keys.into_iter().map(|key| key.into_bytes()).collect())549 keys.map(|keys| keys.into_iter().map(|key| key.into_bytes()).collect())