@@ -11,7 +11,7 @@ use crate::csl::{
11
11
use crate :: plutus_script:: PlutusScript ;
12
12
use anyhow:: anyhow;
13
13
use cardano_serialization_lib:: {
14
- ExUnits , JsError , PlutusData , Transaction , TransactionBuilder , TxInputsBuilder ,
14
+ ExUnits , JsError , PlutusData , ScriptHash , Transaction , TransactionBuilder , TxInputsBuilder ,
15
15
} ;
16
16
use cardano_serialization_lib:: { LanguageKind , TransactionHash , TransactionInput } ;
17
17
use ogmios_client:: {
@@ -20,6 +20,7 @@ use ogmios_client::{
20
20
} ;
21
21
use partner_chains_plutus_data:: d_param:: { d_parameter_to_plutus_data, DParamDatum } ;
22
22
use sidechain_domain:: { DParameter , McTxHash , UtxoId } ;
23
+ use std:: collections:: HashMap ;
23
24
24
25
#[ cfg( test) ]
25
26
mod tests;
@@ -119,13 +120,7 @@ async fn insert_d_param<C: QueryLedgerState + Transactions + QueryNetwork>(
119
120
where
120
121
C : Transactions ,
121
122
{
122
- let zero_ex_units = ScriptExUnits {
123
- mint_ex_units : vec ! [
124
- ExUnits :: new( & 0u64 . into( ) , & 0u64 . into( ) ) ,
125
- ExUnits :: new( & 0u64 . into( ) , & 0u64 . into( ) ) ,
126
- ] ,
127
- spend_ex_units : vec ! [ ] ,
128
- } ;
123
+ let zero_ex_units = ExUnits :: new ( & 0u64 . into ( ) , & 0u64 . into ( ) ) ;
129
124
130
125
let gov_utxo = crate :: init_governance:: get_governance_utxo ( genesis_utxo, client)
131
126
. await
@@ -136,24 +131,38 @@ where
136
131
policy,
137
132
d_parameter,
138
133
& ctx,
134
+ zero_ex_units. clone ( ) ,
139
135
zero_ex_units,
140
136
gov_utxo. clone ( ) ,
141
137
) ?;
138
+
142
139
let evaluate_response = client. evaluate_transaction ( & tx. to_bytes ( ) ) . await . map_err ( |e| {
143
140
anyhow ! (
144
141
"Evaluate insert D-parameter transaction request failed: {}, bytes: {}" ,
145
142
e,
146
143
hex:: encode( tx. to_bytes( ) )
147
144
)
148
145
} ) ?;
149
- let mut mint_witness_ex_units = get_validator_budgets ( evaluate_response) ;
150
- mint_witness_ex_units. mint_ex_units . reverse ( ) ;
146
+
147
+ let ogmios_gov_script =
148
+ gov_utxo. clone ( ) . script . expect ( "Gov UTXO should have a reference script" ) ;
149
+ let gov_policy = PlutusScript :: from_ogmios ( ogmios_gov_script) ?;
150
+ let mint_keys = tx. body ( ) . mint ( ) . expect ( "insert D parameter transaction has two mints" ) . keys ( ) ;
151
+ let script_to_index: HashMap < ScriptHash , usize > =
152
+ vec ! [ ( mint_keys. get( 0 ) , 0 ) , ( mint_keys. get( 1 ) , 1 ) ] . into_iter ( ) . collect ( ) ;
153
+ let mint_ex_units = get_validator_budgets ( evaluate_response) . mint_ex_units ;
154
+ let policy_idx = script_to_index. get ( & policy. csl_script_hash ( ) ) . unwrap ( ) . clone ( ) ;
155
+ let gov_policy_idx = script_to_index. get ( & gov_policy. csl_script_hash ( ) ) . unwrap ( ) . clone ( ) ;
156
+ let policy_ex_units = mint_ex_units. get ( policy_idx) . expect ( "Ogmios response should have entry for d_param policy" ) ;
157
+ let gov_policy_ex_units = mint_ex_units. get ( gov_policy_idx) . expect ( "Ogmios response should have entry for gov policy" ) ;
158
+
151
159
let tx = mint_d_param_token_tx (
152
160
validator,
153
161
policy,
154
162
d_parameter,
155
163
& ctx,
156
- mint_witness_ex_units,
164
+ policy_ex_units. clone ( ) ,
165
+ gov_policy_ex_units. clone ( ) ,
157
166
gov_utxo,
158
167
) ?;
159
168
let signed_tx = ctx. sign ( & tx) . to_bytes ( ) ;
@@ -235,18 +244,13 @@ fn mint_d_param_token_tx(
235
244
policy : & PlutusScript ,
236
245
d_parameter : & DParameter ,
237
246
ctx : & TransactionContext ,
238
- mut ex_units : ScriptExUnits ,
247
+ d_param_policy_ex_units : ExUnits ,
248
+ gov_policy_ex_units : ExUnits ,
239
249
gov_utxo : OgmiosUtxo ,
240
250
) -> Result < Transaction , JsError > {
241
251
let mut tx_builder = TransactionBuilder :: new ( & get_builder_config ( ctx) ?) ;
242
252
// The essence of transaction: mint a governance token, then mint tokena D-Param and set output with it
243
- tx_builder. add_mint_one_script_token (
244
- policy,
245
- ex_units
246
- . mint_ex_units
247
- . pop ( )
248
- . unwrap_or_else ( || panic ! ( "Mint ex units not found" ) ) ,
249
- ) ?;
253
+ tx_builder. add_mint_one_script_token ( policy, d_param_policy_ex_units) ?;
250
254
tx_builder. add_output_with_one_script_token (
251
255
validator,
252
256
policy,
@@ -266,10 +270,7 @@ fn mint_d_param_token_tx(
266
270
tx_builder. add_mint_one_script_token_using_reference_script (
267
271
& gov_policy,
268
272
& gov_tx_input,
269
- ex_units
270
- . mint_ex_units
271
- . pop ( )
272
- . unwrap_or_else ( || panic ! ( "Mint ex units not found" ) ) ,
273
+ gov_policy_ex_units,
273
274
) ?;
274
275
275
276
tx_builder. add_script_reference_input ( & gov_tx_input, gov_policy. bytes . len ( ) ) ;
0 commit comments