-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change: init governance transaction (#241)
- Loading branch information
1 parent
a2d581a
commit c2f41e5
Showing
7 changed files
with
362 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#[cfg(test)] | ||
mod tests; | ||
|
||
pub(crate) mod transaction; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
use crate::{csl::*, plutus_script::PlutusScript}; | ||
use cardano_serialization_lib::*; | ||
use ogmios_client::types::OgmiosUtxo; | ||
use partner_chains_plutus_data::version_oracle::VersionOracleDatum; | ||
use sidechain_domain::MainchainAddressHash; | ||
|
||
// Script ID of the governance script in the script cache. | ||
// TODO: Use a proper value of raw_scripts::ScripId once we upgrade to a version that has it. | ||
const SCRIPT_ID: u32 = 32; | ||
|
||
#[allow(dead_code)] | ||
pub(crate) fn init_governance_transaction( | ||
multi_sig_policy: &[u8], | ||
version_oracle_validator: &[u8], | ||
version_oracle_policy: &[u8], | ||
governance_authority: MainchainAddressHash, | ||
tx_context: &TransactionContext, | ||
genesis_utxo: OgmiosUtxo, | ||
ex_units: ExUnits, | ||
) -> anyhow::Result<Transaction> { | ||
let multi_sig_policy = | ||
PlutusScript::from_wrapped_cbor(multi_sig_policy, LanguageKind::PlutusV2)? | ||
.apply_uplc_data(multisig_governance_policy_configuration(governance_authority))?; | ||
let version_oracle_validator = | ||
PlutusScript::from_wrapped_cbor(version_oracle_validator, LanguageKind::PlutusV2)? | ||
.apply_uplc_data(genesis_utxo.to_uplc_plutus_data())?; | ||
let version_oracle_policy = | ||
PlutusScript::from_wrapped_cbor(version_oracle_policy, LanguageKind::PlutusV2)? | ||
.apply_uplc_data(genesis_utxo.to_uplc_plutus_data())? | ||
.apply_uplc_data(version_oracle_validator.address_data(tx_context.network)?)?; | ||
let config = crate::csl::get_builder_config(&tx_context)?; | ||
let mut tx_builder = TransactionBuilder::new(&config); | ||
|
||
tx_builder.set_mint_builder(&{ | ||
let mut mint_builder = MintBuilder::new(); | ||
|
||
mint_builder.add_asset( | ||
&mint_witness(&version_oracle_policy, &multi_sig_policy, &ex_units)?, | ||
&version_oracle_asset_name(), | ||
&Int::new_i32(1), | ||
)?; | ||
mint_builder | ||
}); | ||
|
||
tx_builder.add_output(&version_oracle_datum_output( | ||
version_oracle_validator.clone(), | ||
version_oracle_policy.clone(), | ||
multi_sig_policy, | ||
tx_context.network, | ||
&tx_context, | ||
)?)?; | ||
|
||
tx_builder.set_inputs(&{ | ||
let mut tx_input_builder = TxInputsBuilder::new(); | ||
tx_input_builder.add_key_input( | ||
&tx_context.payment_key_hash(), | ||
&genesis_utxo.to_csl_tx_input(), | ||
&Value::new(&Coin::from(genesis_utxo.value.lovelace)), | ||
); | ||
|
||
tx_input_builder | ||
}); | ||
|
||
Ok(tx_builder.balance_update_and_build(&tx_context)?) | ||
} | ||
|
||
fn version_oracle_asset_name() -> AssetName { | ||
AssetName::new(b"Version oracle".to_vec()).expect("Constant asset name should work") | ||
} | ||
|
||
fn mint_witness( | ||
version_oracle_policy: &PlutusScript, | ||
multi_sig_policy: &PlutusScript, | ||
ex_units: &ExUnits, | ||
) -> anyhow::Result<MintWitness> { | ||
Ok(MintWitness::new_plutus_script( | ||
&PlutusScriptSource::new(&version_oracle_policy.to_csl()), | ||
&Redeemer::new( | ||
&RedeemerTag::new_mint(), | ||
&0u32.into(), | ||
&PlutusData::new_constr_plutus_data(&ConstrPlutusData::new(&0u64.into(), &{ | ||
let mut list = PlutusList::new(); | ||
list.add(&PlutusData::new_integer(&SCRIPT_ID.into())); | ||
list.add(&PlutusData::new_bytes(multi_sig_policy.script_hash().to_vec())); | ||
list | ||
})), | ||
&ex_units, | ||
), | ||
)) | ||
} | ||
|
||
fn version_oracle_datum_output( | ||
version_oracle_validator: PlutusScript, | ||
version_oracle_policy: PlutusScript, | ||
multi_sig_policy: PlutusScript, | ||
network: NetworkIdKind, | ||
tx_context: &TransactionContext, | ||
) -> anyhow::Result<cardano_serialization_lib::TransactionOutput> { | ||
let datum: PlutusData = VersionOracleDatum { | ||
version_oracle: SCRIPT_ID, | ||
currency_symbol: version_oracle_policy.policy_id().0.into(), | ||
} | ||
.into(); | ||
|
||
let amount_builder = TransactionOutputBuilder::new() | ||
.with_address(&version_oracle_validator.address(network)) | ||
.with_plutus_data(&datum) | ||
.with_script_ref(&ScriptRef::new_plutus_script(&multi_sig_policy.to_csl())) | ||
.next()?; | ||
let mut ma = MultiAsset::new(); | ||
let mut assets = Assets::new(); | ||
assets.insert(&version_oracle_asset_name(), &1u64.into()); | ||
ma.insert(&version_oracle_policy.policy_id().0.into(), &assets); | ||
let output = amount_builder.with_coin_and_asset(&0u64.into(), &ma).build()?; | ||
let min_ada = MinOutputAdaCalculator::new( | ||
&output, | ||
&DataCost::new_coins_per_byte( | ||
&tx_context.protocol_parameters.min_utxo_deposit_coefficient.into(), | ||
), | ||
) | ||
.calculate_ada()?; | ||
let output = amount_builder.with_coin_and_asset(&min_ada, &ma).build()?; | ||
Ok(output) | ||
} | ||
|
||
// Returns the simplest MultiSig policy configuration plutus data: | ||
// there is one required authority and it is the governance authority from sidechain params. | ||
pub fn multisig_governance_policy_configuration( | ||
governance_authority: MainchainAddressHash, | ||
) -> uplc::PlutusData { | ||
uplc::PlutusData::Array(vec![ | ||
uplc::PlutusData::Array(vec![uplc::PlutusData::BoundedBytes( | ||
governance_authority.0.to_vec().into(), | ||
)]), | ||
uplc::PlutusData::BigInt(uplc::BigInt::Int(1.into())), | ||
]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters