Skip to content

Commit

Permalink
Fix proposal deposits
Browse files Browse the repository at this point in the history
  • Loading branch information
rooooooooob committed Jul 9, 2024
1 parent 319668c commit 4bf74bc
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion chain/rust/src/builders/certificate_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub fn add_cert_vkeys(
StakeCredential::PubKey { hash, .. } => {
vkeys.insert(*hash);
}
}
},
Certificate::UnregDrepCert(cert) => match &cert.drep_credential {
StakeCredential::Script { hash, .. } => {
return Err(CertBuilderError::ExpectedKeyHash(*hash))
Expand Down
5 changes: 4 additions & 1 deletion chain/rust/src/builders/proposal_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ impl ProposalBuilder {
}
}

pub fn with_proposal(mut self, proposal: ProposalProcedure) -> Result<Self, ProposalBuilderError> {
pub fn with_proposal(
mut self,
proposal: ProposalProcedure,
) -> Result<Self, ProposalBuilderError> {
if proposal.gov_action.script_hash().is_some() {
return Err(ProposalBuilderError::ProposalIsScript);
}
Expand Down
4 changes: 2 additions & 2 deletions chain/rust/src/builders/redeemer_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ impl RedeemerSetBuilder {
)));
}
RedeemerTag::Proposing => {
let entry = self.proposals.iter_mut().nth(key.index as usize).unwrap();
let entry = self.proposals.get_mut(key.index as usize).unwrap();
*entry = Some(UntaggedRedeemerPlaceholder::Full(UntaggedRedeemer::new(
entry.as_ref().unwrap().data().clone(),
ex_units,
)));
}
RedeemerTag::Voting => {
let entry = self.votes.iter_mut().nth(key.index as usize).unwrap();
let entry = self.votes.get_mut(key.index as usize).unwrap();
*entry = Some(UntaggedRedeemerPlaceholder::Full(UntaggedRedeemer::new(
entry.as_ref().unwrap().data().clone(),
ex_units,
Expand Down
1 change: 1 addition & 0 deletions chain/rust/src/builders/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,7 @@ impl TransactionBuilder {
pub fn get_deposit(&self) -> Result<Coin, TxBuilderError> {
internal_get_deposit(
self.certs.as_deref(),
self.proposals.as_deref(),
self.config.pool_deposit,
self.config.key_deposit,
)
Expand Down
21 changes: 19 additions & 2 deletions chain/rust/src/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use cml_core::ArithmeticError;

use crate::{certs::Certificate, transaction::TransactionBody, Coin, Value, Withdrawals};
use crate::{
certs::Certificate, governance::ProposalProcedure, transaction::TransactionBody, Coin, Value,
Withdrawals,
};

pub fn internal_get_implicit_input(
withdrawals: Option<&Withdrawals>,
Expand Down Expand Up @@ -39,6 +42,7 @@ pub fn internal_get_implicit_input(

pub fn internal_get_deposit(
certs: Option<&[Certificate]>,
proposals: Option<&[ProposalProcedure]>,
pool_deposit: Coin, // // protocol parameter
key_deposit: Coin, // protocol parameter
) -> Result<Coin, ArithmeticError> {
Expand All @@ -58,7 +62,16 @@ pub fn internal_get_deposit(
})
.ok_or(ArithmeticError::IntegerOverflow)?,
};
Ok(certificate_refund)
let proposal_refund = match proposals {
None => 0,
Some(proposals) => proposals
.iter()
.try_fold(0u64, |acc, proposal| acc.checked_add(proposal.deposit))
.ok_or(ArithmeticError::IntegerOverflow)?,
};
certificate_refund
.checked_add(proposal_refund)
.ok_or(ArithmeticError::IntegerOverflow)
}

pub fn get_implicit_input(
Expand All @@ -81,6 +94,10 @@ pub fn get_deposit(
) -> Result<Coin, ArithmeticError> {
internal_get_deposit(
txbody.certs.as_ref().map(|certs| certs.as_ref()),
txbody
.proposal_procedures
.as_ref()
.map(|proposals| proposals.as_ref()),
pool_deposit,
key_deposit,
)
Expand Down
16 changes: 8 additions & 8 deletions chain/wasm/src/builders/proposal_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ impl ProposalBuilder {
Self(cml_chain::builders::proposal_builder::ProposalBuilder::new())
}

pub fn with_vote(&self, proposal: ProposalProcedure) -> Result<ProposalBuilder, JsError> {
pub fn with_proposal(&self, proposal: ProposalProcedure) -> Result<ProposalBuilder, JsError> {
self.0
.clone()
.with_vote(proposal.clone().into())
.with_proposal(proposal.clone().into())
.map(Into::into)
.map_err(Into::into)
}

pub fn with_native_script_vote(
pub fn with_native_script_proposal(
&self,
proposal: ProposalProcedure,
native_script: NativeScript,
witness_info: NativeScriptWitnessInfo,
) -> Result<ProposalBuilder, JsError> {
self.0
.clone()
.with_native_script_vote(
.with_native_script_proposal(
proposal.clone().into(),
native_script.clone().into(),
witness_info.clone().into(),
Expand All @@ -57,7 +57,7 @@ impl ProposalBuilder {
.map_err(Into::into)
}

pub fn with_plutus_vote(
pub fn with_plutus_proposal(
&self,
proposal: &ProposalProcedure,
partial_witness: &PartialPlutusWitness,
Expand All @@ -66,7 +66,7 @@ impl ProposalBuilder {
) -> Result<ProposalBuilder, JsError> {
self.0
.clone()
.with_plutus_vote(
.with_plutus_proposal(
proposal.clone().into(),
partial_witness.clone().into(),
required_signers.clone().into(),
Expand All @@ -76,15 +76,15 @@ impl ProposalBuilder {
.map_err(Into::into)
}

pub fn with_plutus_vote_inline_datum(
pub fn with_plutus_proposal_inline_datum(
&self,
proposal: ProposalProcedure,
partial_witness: PartialPlutusWitness,
required_signers: RequiredSigners,
) -> Result<ProposalBuilder, JsError> {
self.0
.clone()
.with_plutus_vote_inline_datum(
.with_plutus_proposal_inline_datum(
proposal.clone().into(),
partial_witness.clone().into(),
required_signers.clone().into(),
Expand Down

0 comments on commit 4bf74bc

Please sign in to comment.