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}
95
96impl CreateReserveCmd {
97	/// Creates the reserve for your chain
98	pub async fn execute(self) -> crate::SubCmdResult {
99		let payment_key = self.payment_key_file.read_key()?;
100		let client = self.common_arguments.get_ogmios_client().await?;
101		let result = create_reserve_utxo(
102			ReserveParameters {
103				total_accrued_function_script_hash: self.total_accrued_function_script_hash,
104				token: self.token,
105				initial_deposit: self.initial_deposit_amount,
106			},
107			self.genesis_utxo.into(),
108			&payment_key,
109			&client,
110			&self.common_arguments.retries(),
111		)
112		.await?;
113		Ok(serde_json::json!(result))
114	}
115}
116
117#[derive(Clone, Debug, clap::Parser)]
118/// Command for depositing more tokens to an already existing reserve
119pub struct DepositReserveCmd {
120	#[clap(flatten)]
121	common_arguments: crate::CommonArguments,
122	#[clap(flatten)]
123	/// Path to the payment key file
124	payment_key_file: PaymentFilePath,
125	#[clap(flatten)]
126	/// Genesis UTXO
127	genesis_utxo: GenesisUtxo,
128	#[arg(long)]
129	/// Amount of reserve tokens to deposit. They must be present in the payment wallet.
130	amount: u64,
131}
132
133impl DepositReserveCmd {
134	/// Deposits tokens from payment key wallet to the reserve
135	pub async fn execute(self) -> crate::SubCmdResult {
136		let payment_key = self.payment_key_file.read_key()?;
137		let client = self.common_arguments.get_ogmios_client().await?;
138		let result = deposit_to_reserve(
139			self.amount,
140			self.genesis_utxo.into(),
141			&payment_key,
142			&client,
143			&self.common_arguments.retries(),
144		)
145		.await?;
146		Ok(serde_json::json!(result))
147	}
148}
149
150#[derive(Clone, Debug, clap::Parser)]
151/// Command for updating the reserve management system settings for your chain
152pub struct UpdateReserveSettingsCmd {
153	#[clap(flatten)]
154	common_arguments: crate::CommonArguments,
155	#[clap(flatten)]
156	/// Path to the payment key file
157	payment_key_file: PaymentFilePath,
158	#[arg(long, short('c'))]
159	/// Genesis UTXO of the partner-chain.
160	genesis_utxo: UtxoId,
161	#[arg(long)]
162	/// 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.
163	total_accrued_function_script_hash: ScriptHash,
164}
165
166impl UpdateReserveSettingsCmd {
167	/// Updates the reserve management system settings for your chain
168	pub async fn execute(self) -> crate::SubCmdResult {
169		let payment_key = self.payment_key_file.read_key()?;
170		let client = self.common_arguments.get_ogmios_client().await?;
171		let result = update_reserve_settings(
172			self.genesis_utxo,
173			&payment_key,
174			self.total_accrued_function_script_hash,
175			&client,
176			&self.common_arguments.retries(),
177		)
178		.await?;
179		Ok(option_to_json(result))
180	}
181}
182
183#[derive(Clone, Debug, clap::Parser)]
184/// Command for handing over the remaining funds from the reserve to the illiquid supply.
185/// This operation ends the lifecycle of the reserve.
186pub struct HandoverReserveCmd {
187	#[clap(flatten)]
188	common_arguments: crate::CommonArguments,
189	#[clap(flatten)]
190	/// Path to the payment key file
191	payment_key_file: PaymentFilePath,
192	#[arg(long, short('c'))]
193	/// Genesis UTXO of the partner-chain.
194	genesis_utxo: UtxoId,
195}
196
197impl HandoverReserveCmd {
198	/// Hands over the remaining funds from the reserve to the illiquid supply.
199	/// This operation ends the lifecycle of the reserve.
200	pub async fn execute(self) -> crate::SubCmdResult {
201		let payment_key = self.payment_key_file.read_key()?;
202		let client = self.common_arguments.get_ogmios_client().await?;
203		let result = handover_reserve(
204			self.genesis_utxo,
205			&payment_key,
206			&client,
207			&self.common_arguments.retries(),
208		)
209		.await?;
210		Ok(serde_json::json!(result))
211	}
212}
213
214#[derive(Clone, Debug, clap::Parser)]
215/// Command for releasing funds from the reserve to the illiquid supply
216pub struct ReleaseReserveCmd {
217	#[clap(flatten)]
218	common_arguments: crate::CommonArguments,
219	#[clap(flatten)]
220	/// Path to the payment key file
221	payment_key_file: PaymentFilePath,
222	#[arg(long, short('c'))]
223	/// Genesis UTXO of the partner-chain.
224	genesis_utxo: UtxoId,
225	#[arg(long, short('r'))]
226	/// Reference UTXO containing the V-Function script
227	reference_utxo: UtxoId,
228	#[arg(long)]
229	/// Amount of reserve tokens to be released to the illiquid supply.
230	amount: NonZero<u64>,
231}
232
233impl ReleaseReserveCmd {
234	/// Releases funds from the reserve to the illiquid supply
235	pub async fn execute(self) -> crate::SubCmdResult {
236		let payment_key = self.payment_key_file.read_key()?;
237		let client = self.common_arguments.get_ogmios_client().await?;
238		let result = release_reserve_funds(
239			self.amount,
240			self.genesis_utxo,
241			self.reference_utxo,
242			&payment_key,
243			&client,
244			&self.common_arguments.retries(),
245		)
246		.await?;
247		Ok(transaction_submitted_json(result))
248	}
249}