contract_deploy.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use darkfi::{
  19. tx::{ContractCallLeaf, Transaction, TransactionBuilder},
  20. Result,
  21. };
  22. use darkfi_deployooor_contract::{client::deploy_v1::DeployCallBuilder, DeployFunction};
  23. use darkfi_money_contract::{client::OwnCoin, model::MoneyFeeParamsV1};
  24. use darkfi_sdk::{
  25. crypto::contract_id::DEPLOYOOOR_CONTRACT_ID, deploy::DeployParamsV1, ContractCall,
  26. };
  27. use darkfi_serial::Encodable;
  28. use super::{Holder, TestHarness};
  29. impl TestHarness {
  30. /// Create a `Deployooor::Deploy` transaction with the given WASM bincode.
  31. ///
  32. /// Returns the [`Transaction`], and necessary parameters.
  33. pub async fn deploy_contract(
  34. &mut self,
  35. holder: &Holder,
  36. wasm_bincode: Vec<u8>,
  37. block_height: u32,
  38. ) -> Result<(Transaction, DeployParamsV1, Option<MoneyFeeParamsV1>)> {
  39. let wallet = self.wallet(holder);
  40. let deploy_keypair = wallet.contract_deploy_authority;
  41. // Build the contract call
  42. let builder = DeployCallBuilder { deploy_keypair, wasm_bincode, deploy_ix: vec![] };
  43. let debris = builder.build()?;
  44. // Encode the call
  45. let mut data = vec![DeployFunction::DeployV1 as u8];
  46. debris.params.encode(&mut data)?;
  47. let call = ContractCall { contract_id: *DEPLOYOOOR_CONTRACT_ID, data };
  48. let mut tx_builder =
  49. TransactionBuilder::new(ContractCallLeaf { call, proofs: vec![] }, vec![])?;
  50. // If we have tx fees enabled, make an offering
  51. let mut fee_params = None;
  52. let mut fee_signature_secrets = None;
  53. if self.verify_fees {
  54. let mut tx = tx_builder.build()?;
  55. let sigs = tx.create_sigs(&[deploy_keypair.secret])?;
  56. tx.signatures = vec![sigs];
  57. let (fee_call, fee_proofs, fee_secrets, _spent_fee_coins, fee_call_params) =
  58. self.append_fee_call(holder, tx, block_height, &[]).await?;
  59. // Append the fee call to the transaction
  60. tx_builder.append(ContractCallLeaf { call: fee_call, proofs: fee_proofs }, vec![])?;
  61. fee_signature_secrets = Some(fee_secrets);
  62. fee_params = Some(fee_call_params);
  63. }
  64. // Now build the actual transaction and sign it with necessary keys.
  65. let mut tx = tx_builder.build()?;
  66. let sigs = tx.create_sigs(&[deploy_keypair.secret])?;
  67. tx.signatures = vec![sigs];
  68. if let Some(fee_signature_secrets) = fee_signature_secrets {
  69. let sigs = tx.create_sigs(&fee_signature_secrets)?;
  70. tx.signatures.push(sigs);
  71. }
  72. Ok((tx, debris.params, fee_params))
  73. }
  74. /// Execute the transaction created by `deploy_contract()` for a given [`Holder`].
  75. ///
  76. /// Returns any found [`OwnCoin`]s.
  77. pub async fn execute_deploy_tx(
  78. &mut self,
  79. holder: &Holder,
  80. tx: Transaction,
  81. _params: &DeployParamsV1,
  82. fee_params: &Option<MoneyFeeParamsV1>,
  83. block_height: u32,
  84. append: bool,
  85. ) -> Result<Vec<OwnCoin>> {
  86. let wallet = self.holders.get_mut(holder).unwrap();
  87. wallet.add_transaction("deploy::deploy", tx, block_height).await?;
  88. if !append {
  89. return Ok(vec![]);
  90. }
  91. Ok(wallet.process_fee(fee_params, holder))
  92. }
  93. }