| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- /* This file is part of DarkFi (https://dark.fi)
- *
- * Copyright (C) 2020-2026 Dyne.org foundation
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- use darkfi::{
- tx::{ContractCallLeaf, Transaction, TransactionBuilder},
- Result,
- };
- use darkfi_deployooor_contract::{client::deploy_v1::DeployCallBuilder, DeployFunction};
- use darkfi_money_contract::{client::OwnCoin, model::MoneyFeeParamsV1};
- use darkfi_sdk::{
- crypto::contract_id::DEPLOYOOOR_CONTRACT_ID, deploy::DeployParamsV1, ContractCall,
- };
- use darkfi_serial::Encodable;
- use super::{Holder, TestHarness};
- impl TestHarness {
- /// Create a `Deployooor::Deploy` transaction with the given WASM bincode.
- ///
- /// Returns the [`Transaction`], and necessary parameters.
- pub async fn deploy_contract(
- &mut self,
- holder: &Holder,
- wasm_bincode: Vec<u8>,
- block_height: u32,
- ) -> Result<(Transaction, DeployParamsV1, Option<MoneyFeeParamsV1>)> {
- let wallet = self.wallet(holder);
- let deploy_keypair = wallet.contract_deploy_authority;
- // Build the contract call
- let builder = DeployCallBuilder { deploy_keypair, wasm_bincode, deploy_ix: vec![] };
- let debris = builder.build()?;
- // Encode the call
- let mut data = vec![DeployFunction::DeployV1 as u8];
- debris.params.encode(&mut data)?;
- let call = ContractCall { contract_id: *DEPLOYOOOR_CONTRACT_ID, data };
- let mut tx_builder =
- TransactionBuilder::new(ContractCallLeaf { call, proofs: vec![] }, vec![])?;
- // If we have tx fees enabled, make an offering
- let mut fee_params = None;
- let mut fee_signature_secrets = None;
- if self.verify_fees {
- let mut tx = tx_builder.build()?;
- let sigs = tx.create_sigs(&[deploy_keypair.secret])?;
- tx.signatures = vec![sigs];
- let (fee_call, fee_proofs, fee_secrets, _spent_fee_coins, fee_call_params) =
- self.append_fee_call(holder, tx, block_height, &[]).await?;
- // Append the fee call to the transaction
- tx_builder.append(ContractCallLeaf { call: fee_call, proofs: fee_proofs }, vec![])?;
- fee_signature_secrets = Some(fee_secrets);
- fee_params = Some(fee_call_params);
- }
- // Now build the actual transaction and sign it with necessary keys.
- let mut tx = tx_builder.build()?;
- let sigs = tx.create_sigs(&[deploy_keypair.secret])?;
- tx.signatures = vec![sigs];
- if let Some(fee_signature_secrets) = fee_signature_secrets {
- let sigs = tx.create_sigs(&fee_signature_secrets)?;
- tx.signatures.push(sigs);
- }
- Ok((tx, debris.params, fee_params))
- }
- /// Execute the transaction created by `deploy_contract()` for a given [`Holder`].
- ///
- /// Returns any found [`OwnCoin`]s.
- pub async fn execute_deploy_tx(
- &mut self,
- holder: &Holder,
- tx: Transaction,
- _params: &DeployParamsV1,
- fee_params: &Option<MoneyFeeParamsV1>,
- block_height: u32,
- append: bool,
- ) -> Result<Vec<OwnCoin>> {
- let wallet = self.holders.get_mut(holder).unwrap();
- wallet.add_transaction("deploy::deploy", tx, block_height).await?;
- if !append {
- return Ok(vec![]);
- }
- Ok(wallet.process_fee(fee_params, holder))
- }
- }
|