5
5
//! `datum` field being `[num_permissioned_candidates, num_registered_candidates]`.
6
6
7
7
use crate :: csl:: {
8
- get_builder_config, get_validator_budgets, InputsBuilderExt , ScriptExUnits ,
8
+ get_builder_config, get_validator_budgets, zero_ex_units , InputsBuilderExt , ScriptExUnits ,
9
9
TransactionBuilderExt , TransactionContext ,
10
10
} ;
11
+ use crate :: init_governance:: { self , GovernanceData } ;
11
12
use crate :: plutus_script:: PlutusScript ;
12
13
use anyhow:: anyhow;
13
14
use cardano_serialization_lib:: {
14
15
ExUnits , JsError , PlutusData , ScriptHash , Transaction , TransactionBuilder , TxInputsBuilder ,
15
16
} ;
16
- use cardano_serialization_lib:: { LanguageKind , TransactionHash , TransactionInput } ;
17
17
use ogmios_client:: {
18
18
query_ledger_state:: QueryLedgerState , query_network:: QueryNetwork , transactions:: Transactions ,
19
- types:: OgmiosScript :: Plutus , types :: OgmiosUtxo ,
19
+ types:: OgmiosUtxo ,
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 } ;
@@ -116,24 +116,17 @@ async fn insert_d_param<C: QueryLedgerState + Transactions + QueryNetwork>(
116
116
ctx : TransactionContext ,
117
117
genesis_utxo : UtxoId ,
118
118
client : & C ,
119
- ) -> anyhow:: Result < McTxHash >
120
- where
121
- C : Transactions ,
122
- {
123
- let zero_ex_units = ExUnits :: new ( & 0u64 . into ( ) , & 0u64 . into ( ) ) ;
124
-
125
- let gov_utxo = crate :: init_governance:: get_governance_utxo ( genesis_utxo, client)
126
- . await
127
- . map_err ( |e| JsError :: from_str ( e. to_string ( ) . as_str ( ) ) ) ?;
119
+ ) -> anyhow:: Result < McTxHash > {
120
+ let gov_data = init_governance:: get_governance_data ( genesis_utxo, client) . await ?;
128
121
129
122
let tx = mint_d_param_token_tx (
130
123
validator,
131
124
policy,
132
125
d_parameter,
126
+ & gov_data,
133
127
& ctx,
134
- zero_ex_units. clone ( ) ,
135
- zero_ex_units,
136
- gov_utxo. clone ( ) ,
128
+ & zero_ex_units ( ) ,
129
+ & zero_ex_units ( ) ,
137
130
) ?;
138
131
139
132
let evaluate_response = client. evaluate_transaction ( & tx. to_bytes ( ) ) . await . map_err ( |e| {
@@ -144,26 +137,27 @@ where
144
137
)
145
138
} ) ?;
146
139
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
140
let mint_keys = tx. body ( ) . mint ( ) . expect ( "insert D parameter transaction has two mints" ) . keys ( ) ;
151
141
let script_to_index: HashMap < ScriptHash , usize > =
152
142
vec ! [ ( mint_keys. get( 0 ) , 0 ) , ( mint_keys. get( 1 ) , 1 ) ] . into_iter ( ) . collect ( ) ;
153
143
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" ) ;
144
+ let policy_idx = * script_to_index. get ( & policy. csl_script_hash ( ) ) . unwrap ( ) ;
145
+ let gov_policy_idx = * script_to_index. get ( & gov_data. policy_script . csl_script_hash ( ) ) . unwrap ( ) ;
146
+ let policy_ex_units = mint_ex_units
147
+ . get ( policy_idx)
148
+ . expect ( "Evaluate transaction response should have entry for d_param policy" ) ;
149
+ let gov_policy_ex_units = mint_ex_units
150
+ . get ( gov_policy_idx)
151
+ . expect ( "Evaluate transaction response should have entry for governance policy" ) ;
158
152
159
153
let tx = mint_d_param_token_tx (
160
154
validator,
161
155
policy,
162
156
d_parameter,
157
+ & gov_data,
163
158
& ctx,
164
- policy_ex_units. clone ( ) ,
165
- gov_policy_ex_units. clone ( ) ,
166
- gov_utxo,
159
+ policy_ex_units,
160
+ gov_policy_ex_units,
167
161
) ?;
168
162
let signed_tx = ctx. sign ( & tx) . to_bytes ( ) ;
169
163
let res = client. submit_transaction ( & signed_tx) . await . map_err ( |e| {
@@ -186,27 +180,22 @@ async fn update_d_param<C: QueryLedgerState + Transactions + QueryNetwork>(
186
180
ctx : TransactionContext ,
187
181
genesis_utxo : UtxoId ,
188
182
client : & C ,
189
- ) -> anyhow:: Result < McTxHash >
190
- where
191
- C : Transactions ,
192
- {
183
+ ) -> anyhow:: Result < McTxHash > {
193
184
let zero_ex_units = ScriptExUnits {
194
- mint_ex_units : vec ! [ ExUnits :: new ( & 0u64 . into ( ) , & 0u64 . into ( ) ) ] ,
195
- spend_ex_units : vec ! [ ExUnits :: new ( & 0u64 . into ( ) , & 0u64 . into ( ) ) ] ,
185
+ mint_ex_units : vec ! [ zero_ex_units ( ) ] ,
186
+ spend_ex_units : vec ! [ zero_ex_units ( ) ] ,
196
187
} ;
197
188
198
- let gov_utxo = crate :: init_governance:: get_governance_utxo ( genesis_utxo, client)
199
- . await
200
- . map_err ( |e| JsError :: from_str ( e. to_string ( ) . as_str ( ) ) ) ?;
189
+ let governance_data = init_governance:: get_governance_data ( genesis_utxo, client) . await ?;
201
190
202
191
let tx = update_d_param_tx (
203
192
validator,
204
193
policy,
205
194
d_parameter,
206
195
current_utxo,
196
+ & governance_data,
207
197
& ctx,
208
198
zero_ex_units,
209
- gov_utxo. clone ( ) ,
210
199
) ?;
211
200
let evaluate_response = client. evaluate_transaction ( & tx. to_bytes ( ) ) . await . map_err ( |e| {
212
201
anyhow ! (
@@ -222,9 +211,9 @@ where
222
211
policy,
223
212
d_parameter,
224
213
current_utxo,
214
+ & governance_data,
225
215
& ctx,
226
216
spend_ex_units,
227
- gov_utxo. clone ( ) ,
228
217
) ?;
229
218
let signed_tx = ctx. sign ( & tx) . to_bytes ( ) ;
230
219
let res = client. submit_transaction ( & signed_tx) . await . map_err ( |e| {
@@ -243,13 +232,13 @@ fn mint_d_param_token_tx(
243
232
validator : & PlutusScript ,
244
233
policy : & PlutusScript ,
245
234
d_parameter : & DParameter ,
235
+ governance_data : & GovernanceData ,
246
236
ctx : & TransactionContext ,
247
- d_param_policy_ex_units : ExUnits ,
248
- gov_policy_ex_units : ExUnits ,
249
- gov_utxo : OgmiosUtxo ,
237
+ d_param_policy_ex_units : & ExUnits ,
238
+ gov_policy_ex_units : & ExUnits ,
250
239
) -> Result < Transaction , JsError > {
251
240
let mut tx_builder = TransactionBuilder :: new ( & get_builder_config ( ctx) ?) ;
252
- // The essence of transaction: mint a governance token, then mint tokena D-Param and set output with it
241
+ // The essence of transaction: mint D-Param token and set output with it, mint a governance token.
253
242
tx_builder. add_mint_one_script_token ( policy, d_param_policy_ex_units) ?;
254
243
tx_builder. add_output_with_one_script_token (
255
244
validator,
@@ -258,23 +247,15 @@ fn mint_d_param_token_tx(
258
247
ctx,
259
248
) ?;
260
249
261
- let gov_policy = match gov_utxo. script {
262
- Some ( Plutus ( ps) ) => PlutusScript :: from_cbor ( & ps. cbor , LanguageKind :: PlutusV2 ) ,
263
- _ => return Err ( JsError :: from_str ( "Governance UTXO script is not PlutusScript" ) ) ,
264
- } ;
265
-
266
- let gov_tx_input = TransactionInput :: new (
267
- & TransactionHash :: from_bytes ( gov_utxo. transaction . id . into ( ) ) ?,
268
- gov_utxo. index . into ( ) ,
269
- ) ;
250
+ let gov_tx_input = governance_data. utxo_id_as_tx_input ( ) ;
270
251
tx_builder. add_mint_one_script_token_using_reference_script (
271
- & gov_policy ,
252
+ & governance_data . policy_script ,
272
253
& gov_tx_input,
273
254
gov_policy_ex_units,
274
255
) ?;
275
256
276
- tx_builder. add_script_reference_input ( & gov_tx_input, gov_policy . bytes . len ( ) ) ;
277
- tx_builder. add_required_signer ( & ctx. payment_key_hash ( ) ) ;
257
+ tx_builder. add_script_reference_input ( & gov_tx_input, governance_data . policy_script . bytes . len ( ) ) ;
258
+ // tx_builder.add_required_signer(&ctx.payment_key_hash());
278
259
tx_builder. balance_update_and_build ( ctx)
279
260
}
280
261
@@ -283,21 +264,20 @@ fn update_d_param_tx(
283
264
policy : & PlutusScript ,
284
265
d_parameter : & DParameter ,
285
266
script_utxo : & OgmiosUtxo ,
267
+ governance_data : & GovernanceData ,
286
268
ctx : & TransactionContext ,
287
- mut ex_units : ScriptExUnits ,
288
- gov_utxo : OgmiosUtxo ,
269
+ ex_units : ScriptExUnits ,
289
270
) -> Result < Transaction , JsError > {
290
- let config = crate :: csl:: get_builder_config ( ctx) ?;
291
- let mut tx_builder = TransactionBuilder :: new ( & config) ;
271
+ let mut tx_builder = TransactionBuilder :: new ( & get_builder_config ( ctx) ?) ;
292
272
293
273
let mut inputs = TxInputsBuilder :: new ( ) ;
294
274
inputs. add_script_utxo_input (
295
275
script_utxo,
296
276
validator,
297
277
ex_units
298
278
. spend_ex_units
299
- . pop ( )
300
- . unwrap_or_else ( || panic ! ( "Spend ex units not found" ) ) ,
279
+ . first ( )
280
+ . ok_or_else ( || JsError :: from_str ( "Spend ex units not found" ) ) ? ,
301
281
) ?;
302
282
tx_builder. set_inputs ( & inputs) ;
303
283
@@ -308,25 +288,17 @@ fn update_d_param_tx(
308
288
ctx,
309
289
) ?;
310
290
311
- let gov_policy = match gov_utxo. script {
312
- Some ( Plutus ( ps) ) => PlutusScript :: from_cbor ( & ps. cbor , LanguageKind :: PlutusV2 ) ,
313
- _ => return Err ( JsError :: from_str ( "Governance UTXO script is not PlutusScript" ) ) ,
314
- } ;
315
-
316
- let gov_tx_input = TransactionInput :: new (
317
- & TransactionHash :: from_bytes ( gov_utxo. transaction . id . into ( ) ) ?,
318
- gov_utxo. index . into ( ) ,
319
- ) ;
291
+ let gov_tx_input = governance_data. utxo_id_as_tx_input ( ) ;
320
292
tx_builder. add_mint_one_script_token_using_reference_script (
321
- & gov_policy ,
293
+ & governance_data . policy_script ,
322
294
& gov_tx_input,
323
295
ex_units
324
296
. mint_ex_units
325
- . pop ( )
326
- . unwrap_or_else ( || panic ! ( "Mint ex units not found") ) ,
297
+ . first ( )
298
+ . ok_or_else ( || JsError :: from_str ( "MInt ex units not found") ) ? ,
327
299
) ?;
328
300
329
- tx_builder. add_script_reference_input ( & gov_tx_input, gov_policy . bytes . len ( ) ) ;
301
+ tx_builder. add_script_reference_input ( & gov_tx_input, governance_data . policy_script . bytes . len ( ) ) ;
330
302
tx_builder. add_required_signer ( & ctx. payment_key_hash ( ) ) ;
331
303
tx_builder. balance_update_and_build ( ctx)
332
304
}
0 commit comments