Skip to content

Commit

Permalink
Prototype minting circuits for token contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Sep 8, 2023
1 parent 9b7107b commit 556d531
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/blockchain/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,6 @@ pub enum BlockchainError {
ValidatorProofNotGiven,
#[error("epoch randomness can only be changed with vrf-proofs on 0th attempt")]
RandomnessChangeNotPermitted,
#[error("contract is not a token")]
ContractNotToken,
}
40 changes: 40 additions & 0 deletions src/blockchain/ops/apply_tx/update_contract/mint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use super::*;

pub fn mint<K: KvStore>(
chain: &mut KvStoreChain<K>,
contract_id: &ContractId,
contract: &zk::ZkContract,
function_id: &u32,
amount: &Amount,
executor_fees: &mut Vec<Money>,
) -> Result<(zk::ZkVerifierKey, zk::ZkCompressedState), BlockchainError> {
if let Some(token) = &contract.token {
/*let mut bal = chain.get_contract_balance(contract_id.clone(), *token_id)?;
if bal + *amount < bal || token.token.supply + *amount < token.token.supply {
return Err(BlockchainError::TokenSupplyOverflow);
}
bal += *amount;
token.supply += *amount;
chain
.database
.update(&[WriteOp::Put(keys::token(token_id), (&token).into())])?;
chain.database.update(&[WriteOp::Put(
keys::account_balance(&tx_src, *token_id),
bal.into(),
)])?;*/

let func = token
.mint_functions
.get(*function_id as usize)
.ok_or(BlockchainError::ContractFunctionNotFound)?;
let circuit = &func.verifier_key;
let mut state_builder = zk::ZkStateBuilder::<CoreZkHasher>::new(zk::ZkStateModel::Scalar);
state_builder.batch_set(&zk::ZkDeltaPairs(
[(zk::ZkDataLocator(vec![]), Some((*amount).into()))].into(),
))?;
let aux_data = state_builder.compress()?;
Ok((circuit.clone(), aux_data))
} else {
Err(BlockchainError::ContractNotToken)
}
}
11 changes: 10 additions & 1 deletion src/blockchain/ops/apply_tx/update_contract/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod deposit;
mod function_call;
mod mint;
mod withdraw;

use super::*;
Expand Down Expand Up @@ -83,7 +84,15 @@ pub fn update_contract<K: KvStore>(
(circuit, aux_data)
}
ContractUpdateData::Mint { amount } => {
unimplemented!();
let (circuit, aux_data) = mint::mint(
chain,
&contract_id,
&contract,
&update.circuit_id,
amount,
&mut executor_fees,
)?;
(circuit, aux_data)
}
};

Expand Down

0 comments on commit 556d531

Please sign in to comment.