Skip to main content

pop_contracts/utils/
map_account.rs

1// SPDX-License-Identifier: GPL-3.0
2
3use crate::{DefaultEnvironment, errors::Error};
4use contract_extrinsics::{ExtrinsicOpts, MapAccountCommandBuilder, MapAccountExec};
5use pop_common::{DefaultConfig, Keypair};
6use subxt::{ext::scale_encode::EncodeAsType, utils::H160};
7
8/// A helper struct for performing account mapping operations.
9pub struct AccountMapper {
10	map_exec: MapAccountExec<DefaultConfig, DefaultEnvironment, Keypair>,
11}
12
13impl AccountMapper {
14	/// Creates a new `AccountMapper` instance.
15	///
16	/// # Arguments
17	/// * `extrinsic_opts` - Options used to build and submit a contract extrinsic.
18	pub async fn new(
19		extrinsic_opts: &ExtrinsicOpts<DefaultConfig, DefaultEnvironment, Keypair>,
20	) -> Result<Self, Error> {
21		let map_exec = MapAccountCommandBuilder::new(extrinsic_opts.clone()).done().await?;
22		Ok(Self { map_exec })
23	}
24
25	/// Checks whether the account needs to be mapped by performing a dry run.
26	pub async fn needs_mapping(&self) -> Result<bool, Error> {
27		Ok(self.map_exec.map_account_dry_run().await.is_ok())
28	}
29
30	/// Performs the actual account mapping.
31	pub async fn map_account(&self) -> Result<H160, Error> {
32		let result = self
33			.map_exec
34			.map_account()
35			.await
36			.map_err(|e| Error::MapAccountError(e.to_string()))?;
37		Ok(result.address)
38	}
39}
40
41// Create a call to `Revive::map_account`.
42#[derive(Debug, EncodeAsType)]
43#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
44pub(crate) struct MapAccount {}
45
46impl MapAccount {
47	// Construct an empty `MapAccount` payload.
48	pub(crate) fn new() -> Self {
49		Self {}
50	}
51	// Create a call to `Revive::map_account` with no arguments.
52	pub(crate) fn build(self) -> subxt::tx::DefaultPayload<Self> {
53		subxt::tx::DefaultPayload::new("Revive", "map_account", self)
54	}
55}