Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add init-reserve-management smart contract execution #336

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions toolkit/offchain/src/csl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,15 @@ impl ScriptExUnits {

pub(crate) fn get_validator_budgets(
mut responses: Vec<OgmiosEvaluateTransactionResponse>,
) -> Result<ScriptExUnits, JsError> {
LGLO marked this conversation as resolved.
Show resolved Hide resolved
) -> ScriptExUnits {
responses.sort_by_key(|r| r.validator.index);
let (mint_ex_units, spend_ex_units) = responses
.into_iter()
.partition::<Vec<_>, _>(|response| response.validator.purpose == "mint");
let mint_ex_units = mint_ex_units.into_iter().map(ex_units_from_response).collect();
let spend_ex_units = spend_ex_units.into_iter().map(ex_units_from_response).collect();

Ok(ScriptExUnits { mint_ex_units, spend_ex_units })
ScriptExUnits { mint_ex_units, spend_ex_units }
}

fn ex_units_from_response(resp: OgmiosEvaluateTransactionResponse) -> ExUnits {
Expand All @@ -208,6 +208,10 @@ pub(crate) fn empty_asset_name() -> AssetName {
AssetName::new(vec![]).expect("Hardcoded empty asset name is valid")
}

pub fn zero_ex_units() -> ExUnits {
ExUnits::new(&BigNum::zero(), &BigNum::zero())
}

pub(crate) trait OgmiosUtxoExt {
fn to_csl_tx_input(&self) -> TransactionInput;
fn to_csl_tx_output(&self) -> Result<TransactionOutput, JsError>;
Expand Down Expand Up @@ -706,8 +710,7 @@ mod tests {
validator: OgmiosValidatorIndex::new(2, "spend"),
budget: OgmiosBudget::new(12, 22),
},
])
.expect("Should succeed");
]);

let expected = ScriptExUnits {
mint_ex_units: vec![
Expand Down
8 changes: 2 additions & 6 deletions toolkit/offchain/src/d_param/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ where
hex::encode(tx.to_bytes())
)
})?;
let mut mint_witness_ex_units = get_validator_budgets(evaluate_response)?;
let mut mint_witness_ex_units = get_validator_budgets(evaluate_response);
mint_witness_ex_units.mint_ex_units.reverse();
let tx = mint_d_param_token_tx(
validator,
Expand Down Expand Up @@ -206,7 +206,7 @@ where
hex::encode(tx.to_bytes())
)
})?;
let spend_ex_units = get_validator_budgets(evaluate_response)?;
let spend_ex_units = get_validator_budgets(evaluate_response);

let tx = update_d_param_tx(
validator,
Expand Down Expand Up @@ -272,8 +272,6 @@ fn mint_d_param_token_tx(
.unwrap_or_else(|| panic!("Mint ex units not found")),
)?;

let tx_hash = TransactionHash::from_bytes(gov_utxo.transaction.id.into())?;
let gov_tx_input = TransactionInput::new(&tx_hash, gov_utxo.index.into());
tx_builder.add_script_reference_input(&gov_tx_input, gov_policy.bytes.len());
tx_builder.add_required_signer(&ctx.payment_key_hash());
tx_builder.balance_update_and_build(ctx)
Expand Down Expand Up @@ -327,8 +325,6 @@ fn update_d_param_tx(
.unwrap_or_else(|| panic!("Mint ex units not found")),
)?;

let tx_hash = TransactionHash::from_bytes(gov_utxo.transaction.id.into())?;
let gov_tx_input = TransactionInput::new(&tx_hash, gov_utxo.index.into());
tx_builder.add_script_reference_input(&gov_tx_input, gov_policy.bytes.len());
tx_builder.add_required_signer(&ctx.payment_key_hash());
tx_builder.balance_update_and_build(ctx)
Expand Down
19 changes: 15 additions & 4 deletions toolkit/offchain/src/init_governance/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::csl::OgmiosUtxoExt;
use crate::plutus_script;
use crate::scripts_data;
use crate::{
await_tx::{AwaitTx, FixedDelayRetries},
Expand Down Expand Up @@ -164,13 +165,20 @@ pub async fn get_governance_utxo<T: QueryLedgerState + Transactions + QueryNetwo
}

pub(crate) struct GovernanceData {
policy_script: PlutusScript,
utxo_id: UtxoId,
pub(crate) policy_script: plutus_script::PlutusScript,
pub(crate) utxo_id: UtxoId,
}

impl GovernanceData {
pub(crate) fn policy_script_hash(&self) -> ScriptHash {
self.policy_script.hash()
self.policy_script.csl_script_hash()
}

pub(crate) fn utxo_id_as_tx_input(&self) -> TransactionInput {
TransactionInput::new(
&TransactionHash::from_bytes(self.utxo_id.tx_hash.0.to_vec()).unwrap(),
self.utxo_id.index.0.into(),
)
}
}

Expand All @@ -181,7 +189,10 @@ pub(crate) async fn get_governance_data<T: QueryLedgerState + Transactions + Que
let utxo = get_governance_utxo(genesis_utxo, client).await?;
let utxo_id = utxo.to_domain();
if let Some(OgmiosScript::Plutus(ps)) = utxo.script.clone() {
Ok(GovernanceData { policy_script: PlutusScript::new_v2(ps.cbor), utxo_id })
Ok(GovernanceData {
policy_script: plutus_script::PlutusScript::from_cbor(&ps.cbor, LanguageKind::PlutusV2),
utxo_id,
})
} else {
Err(anyhow!("Programmatic Error: Governance UTXO script is not PlutusScript"))
}
Expand Down
1 change: 1 addition & 0 deletions toolkit/offchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod permissioned_candidates;
mod plutus_script;
/// Supports candidate registration
pub mod register;
pub mod reserve;
/// Provides synthetized scripts data
pub mod scripts_data;
#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion toolkit/offchain/src/plutus_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl PlutusScript {
}

/// This function is needed to create [PlutusScript] from scripts in [raw_scripts],
/// which are encoded as a cibor byte string containing the cbor of the script
/// which are encoded as a cbor byte string containing the cbor of the script
/// itself. This function removes this layer of wrapping.
pub fn from_wrapped_cbor(cbor: &[u8], language: LanguageKind) -> anyhow::Result<Self> {
Ok(Self::from_cbor(&unwrap_one_layer_of_cbor(cbor)?, language))
Expand Down
Loading
Loading