@@ -6,7 +6,8 @@ use crate::{
66} ;
77use anyhow:: anyhow;
88use cardano_serialization_lib:: {
9- ExUnits , JsError , PlutusScriptSource , PlutusWitness , Redeemer , RedeemerTag , TxInputsBuilder ,
9+ BigInt , ExUnits , JsError , PlutusData , PlutusScriptSource , PlutusWitness , Redeemer , RedeemerTag ,
10+ TxInputsBuilder ,
1011} ;
1112use init:: find_script_utxo;
1213use ogmios_client:: { query_ledger_state:: QueryLedgerState , types:: OgmiosUtxo } ;
@@ -26,6 +27,7 @@ pub(crate) struct ReserveData {
2627 pub ( crate ) auth_policy_version_utxo : OgmiosUtxo ,
2728 pub ( crate ) validator_version_utxo : OgmiosUtxo ,
2829 pub ( crate ) illiquid_circulation_supply_validator_version_utxo : OgmiosUtxo ,
30+ pub ( crate ) illiquid_circulation_supply_authority_token_policy_version_utxo : OgmiosUtxo ,
2931}
3032
3133#[ derive( Clone , Debug ) ]
@@ -75,12 +77,23 @@ impl ReserveData {
7577 . ok_or_else ( || {
7678 anyhow ! ( "Illiquid Circulation Supply Validator Version Utxo not found, is the Reserve Token Management initialized?" )
7779 } ) ?;
80+ let illiquid_circulation_supply_authority_token_policy_version_utxo = find_script_utxo (
81+ raw_scripts:: ScriptId :: IlliquidCirculationSupplyAuthorityTokenPolicy as u32 ,
82+ & version_oracle,
83+ ctx,
84+ client,
85+ )
86+ . await ?
87+ . ok_or_else ( || {
88+ anyhow ! ( "Illiquid Circulation Supply Authority Token Policy Version Utxo not found, is the Reserve Token Management initialized?" )
89+ } ) ?;
7890 let scripts = scripts_data:: reserve_scripts ( genesis_utxo, ctx. network ) ?;
7991 Ok ( ReserveData {
8092 scripts,
8193 auth_policy_version_utxo,
8294 validator_version_utxo,
8395 illiquid_circulation_supply_validator_version_utxo,
96+ illiquid_circulation_supply_authority_token_policy_version_utxo,
8497 } )
8598 }
8699
@@ -113,6 +126,33 @@ impl ReserveData {
113126
114127 Ok ( ReserveUtxo { utxo : reserve_utxo, datum : reserve_settings } )
115128 }
129+
130+ pub ( crate ) async fn get_illiquid_circulation_supply_utxo < T : QueryLedgerState > (
131+ & self ,
132+ ctx : & TransactionContext ,
133+ client : & T ,
134+ ) -> Result < OgmiosUtxo , anyhow:: Error > {
135+ let validator_address = self
136+ . scripts
137+ . illiquid_circulation_supply_validator
138+ . address ( ctx. network )
139+ . to_bech32 ( None ) ?;
140+ let validator_utxos = client. query_utxos ( & [ validator_address] ) . await ?;
141+
142+ let auth_token_asset_id = AssetId {
143+ policy_id : self . scripts . illiquid_circulation_supply_auth_token_policy . policy_id ( ) ,
144+ asset_name : AssetName :: empty ( ) ,
145+ } ;
146+
147+ let ics_utxo = validator_utxos
148+ . into_iter ( )
149+ . find ( |utxo| utxo. get_asset_amount ( & auth_token_asset_id) == 1u64 )
150+ . ok_or_else ( || {
151+ anyhow ! ( "Could not find any UTXO with ICS Auth token at ICS Validator, is the Reserve Token Management initialized?" )
152+ } ) ?;
153+
154+ Ok ( ics_utxo. clone ( ) )
155+ }
116156}
117157
118158pub ( crate ) struct TokenAmount {
@@ -127,6 +167,23 @@ pub(crate) fn reserve_utxo_input_with_validator_script_reference(
127167 cost : & ExUnits ,
128168) -> Result < TxInputsBuilder , JsError > {
129169 let mut inputs = TxInputsBuilder :: new ( ) ;
170+ add_reserve_utxo_input_with_validator_script_reference (
171+ & mut inputs,
172+ reserve_utxo,
173+ reserve,
174+ redeemer,
175+ cost,
176+ ) ?;
177+ Ok ( inputs)
178+ }
179+
180+ pub ( crate ) fn add_reserve_utxo_input_with_validator_script_reference (
181+ inputs : & mut TxInputsBuilder ,
182+ reserve_utxo : & OgmiosUtxo ,
183+ reserve : & ReserveData ,
184+ redeemer : ReserveRedeemer ,
185+ cost : & ExUnits ,
186+ ) -> Result < ( ) , JsError > {
130187 let input = reserve_utxo. to_csl_tx_input ( ) ;
131188 let amount = reserve_utxo. value . to_csl ( ) ?;
132189 let script = & reserve. scripts . validator ;
@@ -140,5 +197,32 @@ pub(crate) fn reserve_utxo_input_with_validator_script_reference(
140197 & Redeemer :: new ( & RedeemerTag :: new_spend ( ) , & 0u32 . into ( ) , & redeemer. into ( ) , cost) ,
141198 ) ;
142199 inputs. add_plutus_script_input ( & witness, & input, & amount) ;
143- Ok ( inputs)
200+ Ok ( ( ) )
201+ }
202+
203+ pub ( crate ) fn add_ics_utxo_input_with_validator_script_reference (
204+ inputs : & mut TxInputsBuilder ,
205+ ics_utxo : & OgmiosUtxo ,
206+ reserve : & ReserveData ,
207+ cost : & ExUnits ,
208+ ) -> Result < ( ) , JsError > {
209+ let input = ics_utxo. to_csl_tx_input ( ) ;
210+ let amount = ics_utxo. value . to_csl ( ) ?;
211+ let script = & reserve. scripts . illiquid_circulation_supply_validator ;
212+ let witness = PlutusWitness :: new_with_ref_without_datum (
213+ & PlutusScriptSource :: new_ref_input (
214+ & script. csl_script_hash ( ) ,
215+ & reserve. illiquid_circulation_supply_validator_version_utxo . to_csl_tx_input ( ) ,
216+ & script. language ,
217+ script. bytes . len ( ) ,
218+ ) ,
219+ & Redeemer :: new (
220+ & RedeemerTag :: new_spend ( ) ,
221+ & 0u32 . into ( ) ,
222+ & PlutusData :: new_integer ( & BigInt :: zero ( ) ) ,
223+ cost,
224+ ) ,
225+ ) ;
226+ inputs. add_plutus_script_input ( & witness, & input, & amount) ;
227+ Ok ( ( ) )
144228}
0 commit comments