use darkfi::{
tx::{ContractCallLeaf, Transaction, TransactionBuilder},
util::parse::{decode_base10, encode_base10},
zk::{proof::ProvingKey, vm::ZkCircuit, vm_heap::empty_witnesses},
zkas::ZkBinary,
Error, Result,
};
use darkfi_money_contract::{
client::transfer_v1::make_transfer_call, model::TokenId, MoneyFunction,
MONEY_CONTRACT_ZKAS_BURN_NS_V1, MONEY_CONTRACT_ZKAS_FEE_NS_V1, MONEY_CONTRACT_ZKAS_MINT_NS_V1,
};
use darkfi_sdk::{
crypto::{contract_id::MONEY_CONTRACT_ID, FuncId, Keypair, PublicKey},
pasta::pallas,
tx::ContractCall,
};
use darkfi_serial::AsyncEncodable;
use crate::{money::BALANCE_BASE10_DECIMALS, Drk};
impl Drk {
pub async fn transfer(
&self,
amount: &str,
token_id: TokenId,
recipient: PublicKey,
spend_hook: Option<FuncId>,
user_data: Option<pallas::Base>,
half_split: bool,
) -> Result<Transaction> {
let owncoins = self.get_token_coins(&token_id).await?;
if owncoins.is_empty() {
return Err(Error::Custom(format!(
"Did not find any unspent coins with token ID: {token_id}"
)))
}
let amount = decode_base10(amount, BALANCE_BASE10_DECIMALS, false)?;
let mut balance = 0;
for coin in owncoins.iter() {
balance += coin.note.value;
}
if balance < amount {
return Err(Error::Custom(format!(
"Not enough balance for token ID: {token_id}, found: {}",
encode_base10(balance, BALANCE_BASE10_DECIMALS)
)))
}
let secret = self.default_secret().await?;
let keypair = Keypair::new(secret);
let tree = self.get_money_tree().await?;
let zkas_bins = self.lookup_zkas(&MONEY_CONTRACT_ID).await?;
let Some(mint_zkbin) = zkas_bins.iter().find(|x| x.0 == MONEY_CONTRACT_ZKAS_MINT_NS_V1)
else {
return Err(Error::Custom("Mint circuit not found".to_string()))
};
let Some(burn_zkbin) = zkas_bins.iter().find(|x| x.0 == MONEY_CONTRACT_ZKAS_BURN_NS_V1)
else {
return Err(Error::Custom("Burn circuit not found".to_string()))
};
let Some(fee_zkbin) = zkas_bins.iter().find(|x| x.0 == MONEY_CONTRACT_ZKAS_FEE_NS_V1)
else {
return Err(Error::Custom("Fee circuit not found".to_string()))
};
let mint_zkbin = ZkBinary::decode(&mint_zkbin.1)?;
let burn_zkbin = ZkBinary::decode(&burn_zkbin.1)?;
let fee_zkbin = ZkBinary::decode(&fee_zkbin.1)?;
let mint_circuit = ZkCircuit::new(empty_witnesses(&mint_zkbin)?, &mint_zkbin);
let burn_circuit = ZkCircuit::new(empty_witnesses(&burn_zkbin)?, &burn_zkbin);
let fee_circuit = ZkCircuit::new(empty_witnesses(&fee_zkbin)?, &fee_zkbin);
let mint_pk = ProvingKey::build(mint_zkbin.k, &mint_circuit);
let burn_pk = ProvingKey::build(burn_zkbin.k, &burn_circuit);
let fee_pk = ProvingKey::build(fee_zkbin.k, &fee_circuit);
let (params, secrets, spent_coins) = make_transfer_call(
keypair,
recipient,
amount,
token_id,
owncoins,
tree.clone(),
spend_hook,
user_data,
mint_zkbin,
mint_pk,
burn_zkbin,
burn_pk,
half_split,
)?;
let mut data = vec![MoneyFunction::TransferV1 as u8];
params.encode_async(&mut data).await?;
let call = ContractCall { contract_id: *MONEY_CONTRACT_ID, data };
let mut tx_builder =
TransactionBuilder::new(ContractCallLeaf { call, proofs: secrets.proofs }, vec![])?;
let mut tx = tx_builder.build()?;
let sigs = tx.create_sigs(&secrets.signature_secrets)?;
tx.signatures.push(sigs);
let (fee_call, fee_proofs, fee_secrets) =
self.append_fee_call(&tx, &tree, &fee_pk, &fee_zkbin, Some(&spent_coins)).await?;
tx_builder.append(ContractCallLeaf { call: fee_call, proofs: fee_proofs }, vec![])?;
let mut tx = tx_builder.build()?;
let sigs = tx.create_sigs(&secrets.signature_secrets)?;
tx.signatures.push(sigs);
let sigs = tx.create_sigs(&fee_secrets)?;
tx.signatures.push(sigs);
Ok(tx)
}
}