referrerpolicy=no-referrer-when-downgrade

pallet_revive_eth_rpc/client/
storage_api.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use crate::{
19	subxt_client::{
20		self,
21		runtime_types::pallet_revive::storage::{AccountType, ContractInfo},
22		SrcChainConfig,
23	},
24	ClientError, H160,
25};
26use subxt::{storage::Storage, OnlineClient};
27
28/// A wrapper around the Substrate Storage API.
29#[derive(Clone)]
30pub struct StorageApi(Storage<SrcChainConfig, OnlineClient<SrcChainConfig>>);
31
32impl StorageApi {
33	/// Create a new instance of the StorageApi.
34	pub fn new(api: Storage<SrcChainConfig, OnlineClient<SrcChainConfig>>) -> Self {
35		Self(api)
36	}
37
38	/// Get the contract info for the given contract address.
39	pub async fn get_contract_info(
40		&self,
41		contract_address: &H160,
42	) -> Result<ContractInfo, ClientError> {
43		// TODO: remove once subxt is updated
44		let contract_address: subxt::utils::H160 = contract_address.0.into();
45
46		let query = subxt_client::storage().revive().account_info_of(contract_address);
47		let Some(info) = self.0.fetch(&query).await? else {
48			return Err(ClientError::ContractNotFound);
49		};
50
51		let AccountType::Contract(contract_info) = info.account_type else {
52			return Err(ClientError::ContractNotFound);
53		};
54
55		Ok(contract_info)
56	}
57
58	/// Get the contract trie id for the given contract address.
59	pub async fn get_contract_trie_id(&self, address: &H160) -> Result<Vec<u8>, ClientError> {
60		let ContractInfo { trie_id, .. } = self.get_contract_info(address).await?;
61		Ok(trie_id.0)
62	}
63}