partner_chains_smart_contracts_commands/
reserve.rs

1use crate::{GenesisUtxo, PaymentFilePath, option_to_json, transaction_submitted_json};
2use partner_chains_cardano_offchain::reserve::{
3	create::{ReserveParameters, create_reserve_utxo},
4	deposit::deposit_to_reserve,
5	handover::handover_reserve,
6	init::init_reserve_management,
7	release::release_reserve_funds,
8	update_settings::update_reserve_settings,
9};
10use sidechain_domain::{AssetId, ScriptHash, UtxoId};
11use std::num::NonZero;
12
13#[derive(Clone, Debug, clap::Subcommand)]
14#[allow(clippy::large_enum_variant)]
15/// Command for managing the reserve on the main chain
16pub enum ReserveCmd {
17	/// Initialize the reserve management system for your chain
18	Init(InitReserveCmd),
19	/// Creates the reserve for your chain. `init` and `create` will be merged into a single command in a future release.
20	Create(CreateReserveCmd),
21	/// Deposits tokens from payment key wallet to the reserve
22	Deposit(DepositReserveCmd),
23	/// Update reserve management system settings for your chain
24	UpdateSettings(UpdateReserveSettingsCmd),
25	/// Releases all the remaining funds from the reserve to the illiquid supply
26	Handover(HandoverReserveCmd),
27	/// Releases funds from the reserve to the illiquid supply
28	Release(ReleaseReserveCmd),
29}
30
31impl ReserveCmd {
32	/// Executes the internal command
33	pub async fn execute(self) -> crate::SubCmdResult {
34		match self {
35			Self::Init(cmd) => cmd.execute().await,
36			Self::Create(cmd) => cmd.execute().await,
37			Self::Deposit(cmd) => cmd.execute().await,
38			Self::UpdateSettings(cmd) => cmd.execute().await,
39			Self::Handover(cmd) => cmd.execute().await,
40			Self::Release(cmd) => cmd.execute().await,
41		}
42	}
43}
44
45#[derive(Clone, Debug, clap::Parser)]
46/// Command for initializing the components neccesary for operation of the reserve management system for your chain
47pub struct InitReserveCmd {
48	#[clap(flatten)]
49	common_arguments: crate::CommonArguments,
50	#[clap(flatten)]
51	/// Path to the payment key file
52	payment_key_file: PaymentFilePath,
53	#[clap(flatten)]
54	/// Genesis UTXO
55	genesis_utxo: GenesisUtxo,
56}
57
58impl InitReserveCmd {
59	/// Initializes the components neccesary for operation of the reserve management system for your chain
60	pub async fn execute(self) -> crate::SubCmdResult {
61		let payment_key = self.payment_key_file.read_key()?;
62		let client = self.common_arguments.get_ogmios_client().await?;
63		let result = init_reserve_management(
64			self.genesis_utxo.into(),
65			&payment_key,
66			&client,
67			&self.common_arguments.retries(),
68		)
69		.await?;
70		Ok(serde_json::json!(result))
71	}
72}
73
74#[derive(Clone, Debug, clap::Parser)]
75/// Command for creating the reserve for your chain
76pub struct CreateReserveCmd {
77	#[clap(flatten)]
78	common_arguments: crate::CommonArguments,
79	#[clap(flatten)]
80	/// Path to the payment key file
81	payment_key_file: PaymentFilePath,
82	#[clap(flatten)]
83	/// Genesis UTXO
84	genesis_utxo: GenesisUtxo,
85	#[arg(long)]
86	/// Script hash of the 'total accrued function', also called V-function, that computes how many tokens could be released from the reserve at given moment.
87	total_accrued_function_script_hash: ScriptHash,
88	#[arg(long)]
89	/// Initial amount of tokens to deposit. They must be present in the payment wallet.
90	initial_deposit_amount: u64,
91	#[arg(long)]
92	/// Reserve token asset id encoded in form <policy_id_hex>.<asset_name_hex>.
93	token: AssetId,
94	#[arg(long, default_value = "1")]
95	/// Amount of illiquid circulation supply authority tokens to mint.
96	ics_initial_utxos_amount: NonZero<u64>,
97}
98
99impl CreateReserveCmd {
100	/// Creates the reserve for your chain
101	pub async fn execute(self) -> crate::SubCmdResult {
102		let payment_key = self.payment_key_file.read_key()?;
103		let client = self.common_arguments.get_ogmios_client().await?;
104		let result = create_reserve_utxo(
105			ReserveParameters {
106				total_accrued_function_script_hash: self.total_accrued_function_script_hash,
107				token: self.token,
108				initial_deposit: self.initial_deposit_amount,
109				ics_initial_utxos_amount: self.ics_initial_utxos_amount,
110			},
111			self.genesis_utxo.into(),
112			&payment_key,
113			&client,
114			&self.common_arguments.retries(),
115		)
116		.await?;
117		Ok(serde_json::json!(result))
118	}
119}
120
121#[derive(Clone, Debug, clap::Parser)]
122/// Command for depositing more tokens to an already existing reserve
123pub struct DepositReserveCmd {
124	#[clap(flatten)]
125	common_arguments: crate::CommonArguments,
126	#[clap(flatten)]
127	/// Path to the payment key file
128	payment_key_file: PaymentFilePath,
129	#[clap(flatten)]
130	/// Genesis UTXO
131	genesis_utxo: GenesisUtxo,
132	#[arg(long)]
133	/// Amount of reserve tokens to deposit. They must be present in the payment wallet.
134	amount: u64,
135}
136
137impl DepositReserveCmd {
138	/// Deposits tokens from payment key wallet to the reserve
139	pub async fn execute(self) -> crate::SubCmdResult {
140		let payment_key = self.payment_key_file.read_key()?;
141		let client = self.common_arguments.get_ogmios_client().await?;
142		let result = deposit_to_reserve(
143			self.amount,
144			self.genesis_utxo.into(),
145			&payment_key,
146			&client,
147			&self.common_arguments.retries(),
148		)
149		.await?;
150		Ok(serde_json::json!(result))
151	}
152}
153
154#[derive(Clone, Debug, clap::Parser)]
155/// Command for updating the reserve management system settings for your chain
156pub struct UpdateReserveSettingsCmd {
157	#[clap(flatten)]
158	common_arguments: crate::CommonArguments,
159	#[clap(flatten)]
160	/// Path to the payment key file
161	payment_key_file: PaymentFilePath,
162	#[arg(long, short('c'))]
163	/// Genesis UTXO of the partner-chain.
164	genesis_utxo: UtxoId,
165	#[arg(long)]
166	/// Script hash of the 'total accrued function', also called V-function, that computes how many tokens could be released from the reserve at given moment.
167	total_accrued_function_script_hash: ScriptHash,
168}
169
170impl UpdateReserveSettingsCmd {
171	/// Updates the reserve management system settings for your chain
172	pub async fn execute(self) -> crate::SubCmdResult {
173		let payment_key = self.payment_key_file.read_key()?;
174		let client = self.common_arguments.get_ogmios_client().await?;
175		let result = update_reserve_settings(
176			self.genesis_utxo,
177			&payment_key,
178			self.total_accrued_function_script_hash,
179			&client,
180			&self.common_arguments.retries(),
181		)
182		.await?;
183		Ok(option_to_json(result))
184	}
185}
186
187#[derive(Clone, Debug, clap::Parser)]
188/// Command for handing over the remaining funds from the reserve to the illiquid supply.
189/// This operation ends the lifecycle of the reserve.
190pub struct HandoverReserveCmd {
191	#[clap(flatten)]
192	common_arguments: crate::CommonArguments,
193	#[clap(flatten)]
194	/// Path to the payment key file
195	payment_key_file: PaymentFilePath,
196	#[arg(long, short('c'))]
197	/// Genesis UTXO of the partner-chain.
198	genesis_utxo: UtxoId,
199}
200
201impl HandoverReserveCmd {
202	/// Hands over the remaining funds from the reserve to the illiquid supply.
203	/// This operation ends the lifecycle of the reserve.
204	pub async fn execute(self) -> crate::SubCmdResult {
205		let payment_key = self.payment_key_file.read_key()?;
206		let client = self.common_arguments.get_ogmios_client().await?;
207		let result = handover_reserve(
208			self.genesis_utxo,
209			&payment_key,
210			&client,
211			&self.common_arguments.retries(),
212		)
213		.await?;
214		Ok(serde_json::json!(result))
215	}
216}
217
218#[derive(Clone, Debug, clap::Parser)]
219/// Command for releasing funds from the reserve to the illiquid supply
220pub struct ReleaseReserveCmd {
221	#[clap(flatten)]
222	common_arguments: crate::CommonArguments,
223	#[clap(flatten)]
224	/// Path to the payment key file
225	payment_key_file: PaymentFilePath,
226	#[arg(long, short('c'))]
227	/// Genesis UTXO of the partner-chain.
228	genesis_utxo: UtxoId,
229	#[arg(long, short('r'))]
230	/// Reference UTXO containing the V-Function script
231	reference_utxo: UtxoId,
232	#[arg(long)]
233	/// Amount of reserve tokens to be released to the illiquid supply.
234	amount: NonZero<u64>,
235}
236
237impl ReleaseReserveCmd {
238	/// Releases funds from the reserve to the illiquid supply
239	pub async fn execute(self) -> crate::SubCmdResult {
240		let payment_key = self.payment_key_file.read_key()?;
241		let client = self.common_arguments.get_ogmios_client().await?;
242		let result = release_reserve_funds(
243			self.amount,
244			self.genesis_utxo,
245			self.reference_utxo,
246			&payment_key,
247			&client,
248			&self.common_arguments.retries(),
249		)
250		.await?;
251		Ok(transaction_submitted_json(result))
252	}
253}