partner_chains_smart_contracts_commands/
bridge.rs

1use crate::{GenesisUtxo, PaymentFilePath, transaction_submitted_json};
2use partner_chains_cardano_offchain::bridge::{
3	create_validator_utxos, deposit_with_ics_spend, deposit_without_ics_input, init_ics_scripts,
4};
5use sidechain_domain::AssetId;
6use sp_runtime::AccountId32;
7use std::num::NonZero;
8
9#[derive(Clone, Debug, clap::Subcommand)]
10/// Command to initialize and make deposits to the bridge
11pub enum BridgeCmd {
12	/// Initialize Bridge Smart Conctracts in the Versioning System
13	Init(BridgeInitCmd),
14	/// Create UTXOs with special tokens at the Bridge Validator. These tokens are used to help with coin selection problem.
15	CreateUtxos(BridgeCreateUtxosCmd),
16	/// Deposits tokens from payment key wallet to the reserve
17	Deposit(BridgeDepositCmd),
18}
19
20impl BridgeCmd {
21	/// Executes the internal command
22	pub async fn execute(self) -> crate::SubCmdResult {
23		match self {
24			Self::Init(cmd) => cmd.execute().await,
25			Self::CreateUtxos(cmd) => cmd.execute().await,
26			Self::Deposit(cmd) => cmd.execute().await,
27		}
28	}
29}
30
31#[derive(Clone, Debug, clap::Parser)]
32/// Initializes bridge smart contracts in the versioning system.
33pub struct BridgeInitCmd {
34	#[clap(flatten)]
35	common_arguments: crate::CommonArguments,
36	#[clap(flatten)]
37	/// Path to the payment key file
38	payment_key_file: PaymentFilePath,
39	#[clap(flatten)]
40	/// Genesis UTXO
41	genesis_utxo: GenesisUtxo,
42}
43
44impl BridgeInitCmd {
45	pub async fn execute(self) -> crate::SubCmdResult {
46		let payment_key = self.payment_key_file.read_key()?;
47		let client = self.common_arguments.get_ogmios_client().await?;
48		let result = init_ics_scripts(
49			self.genesis_utxo.into(),
50			&payment_key,
51			&client,
52			&self.common_arguments.retries(),
53		)
54		.await?;
55		Ok(serde_json::json!(result))
56	}
57}
58
59#[derive(Clone, Debug, clap::Parser)]
60/// Create UTXOs with special tokens at the Bridge Validator. These tokens are used to help with coin selection problem.
61pub struct BridgeCreateUtxosCmd {
62	#[clap(flatten)]
63	common_arguments: crate::CommonArguments,
64	#[clap(flatten)]
65	/// Path to the payment key file
66	payment_key_file: PaymentFilePath,
67	#[clap(flatten)]
68	/// Genesis UTXO
69	genesis_utxo: GenesisUtxo,
70	#[arg(long)]
71	/// Number of UTXOs to create
72	amount: NonZero<u64>,
73}
74
75impl BridgeCreateUtxosCmd {
76	pub async fn execute(self) -> crate::SubCmdResult {
77		let payment_key = self.payment_key_file.read_key()?;
78		let client = self.common_arguments.get_ogmios_client().await?;
79		let result = create_validator_utxos(
80			self.genesis_utxo.into(),
81			self.amount,
82			&payment_key,
83			&client,
84			&self.common_arguments.retries(),
85		)
86		.await?;
87		Ok(serde_json::json!(result))
88	}
89}
90
91#[derive(Clone, Debug, clap::Parser)]
92/// Command for sending a native token bridge transfer.
93pub struct BridgeDepositCmd {
94	#[clap(flatten)]
95	common_arguments: crate::CommonArguments,
96	#[arg(long)]
97	/// AssetId of tokens to transfer
98	token: AssetId,
99	#[arg(long)]
100	/// Number of tokens to transfer
101	amount: NonZero<u64>,
102	#[arg(long)]
103	/// Address in the partner chain to transfer the tokens, in hex format.
104	pc_address: AccountId32,
105	#[arg(long)]
106	/// When true, the transaction won't spend an UTXO from ICS Validator. This costs more ada, but is simpler.
107	simple: bool,
108	#[clap(flatten)]
109	/// Path to the payment key file
110	payment_key_file: PaymentFilePath,
111	#[clap(flatten)]
112	/// Genesis UTXO
113	genesis_utxo: GenesisUtxo,
114}
115
116impl BridgeDepositCmd {
117	/// Deposits user token in the Bridge.
118	pub async fn execute(self) -> crate::SubCmdResult {
119		let payment_key = self.payment_key_file.read_key()?;
120		let client = self.common_arguments.get_ogmios_client().await?;
121		let tx_hash = if self.simple {
122			deposit_without_ics_input(
123				self.genesis_utxo.into(),
124				self.token,
125				self.amount,
126				self.pc_address.as_ref(),
127				&payment_key,
128				&client,
129				&self.common_arguments.retries(),
130			)
131			.await?
132		} else {
133			deposit_with_ics_spend(
134				self.genesis_utxo.into(),
135				self.token,
136				self.amount,
137				self.pc_address.as_ref(),
138				&payment_key,
139				&client,
140				&self.common_arguments.retries(),
141			)
142			.await?
143		};
144		Ok(transaction_submitted_json(tx_hash))
145	}
146}