-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtransaction.rs
83 lines (72 loc) · 2.67 KB
/
transaction.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use crate::{
csl::*, governance::SimpleAtLeastN, plutus_script::PlutusScript, scripts_data::version_oracle,
};
use cardano_serialization_lib::*;
use ogmios_client::types::OgmiosUtxo;
use partner_chains_plutus_data::version_oracle::VersionOracleDatum;
use sidechain_domain::MainchainKeyHash;
pub(crate) fn init_governance_transaction(
governance_authority: MainchainKeyHash,
genesis_utxo: OgmiosUtxo,
costs: Costs,
ctx: &TransactionContext,
) -> anyhow::Result<Transaction> {
let multi_sig_policy =
SimpleAtLeastN { threshold: 1, key_hashes: vec![governance_authority.0] }
.to_csl_native_script();
let version_oracle = version_oracle(genesis_utxo.to_domain(), ctx.network)?;
let config = crate::csl::get_builder_config(ctx)?;
let mut tx_builder = TransactionBuilder::new(&config);
tx_builder.add_mint_one_script_token(
&version_oracle.policy,
&version_oracle_asset_name(),
&mint_redeemer(&multi_sig_policy),
&costs.get_mint(&version_oracle.policy.clone().into()),
)?;
tx_builder.add_output(&version_oracle_datum_output(
version_oracle.validator.clone(),
version_oracle.policy.clone(),
multi_sig_policy,
ctx.network,
ctx,
)?)?;
tx_builder.set_inputs(&{
TxInputsBuilder::with_key_inputs(&[genesis_utxo], &ctx.payment_key_hash())?
});
Ok(tx_builder.balance_update_and_build(ctx)?)
}
fn version_oracle_asset_name() -> AssetName {
AssetName::new(b"Version oracle".to_vec()).expect("Constant asset name should work")
}
fn mint_redeemer(multi_sig_policy: &NativeScript) -> PlutusData {
PlutusData::new_constr_plutus_data(&ConstrPlutusData::new(&0u64.into(), &{
let mut list = PlutusList::new();
list.add(&PlutusData::new_integer(
&(raw_scripts::ScriptId::GovernancePolicy as u32).into(),
));
list.add(&PlutusData::new_bytes(multi_sig_policy.hash().to_bytes()));
list
}))
}
pub(crate) fn version_oracle_datum_output(
version_oracle_validator: PlutusScript,
version_oracle_policy: PlutusScript,
multi_sig_policy: NativeScript,
network: NetworkIdKind,
tx_context: &TransactionContext,
) -> anyhow::Result<cardano_serialization_lib::TransactionOutput> {
let datum: PlutusData = VersionOracleDatum {
version_oracle: raw_scripts::ScriptId::GovernancePolicy as u32,
currency_symbol: version_oracle_policy.policy_id().0,
}
.into();
let amount_builder = TransactionOutputBuilder::new()
.with_address(&version_oracle_validator.address(network))
.with_plutus_data(&datum)
.with_script_ref(&ScriptRef::new_native_script(&multi_sig_policy))
.next()?;
let ma = MultiAsset::new()
.with_asset_amount(&version_oracle_policy.asset(version_oracle_asset_name())?, 1u64)?;
let output = amount_builder.with_minimum_ada_and_asset(&ma, tx_context)?.build()?;
Ok(output)
}