Skip to content

Commit

Permalink
add anyhow
Browse files Browse the repository at this point in the history
  • Loading branch information
kroist committed Feb 12, 2024
1 parent 102ffd2 commit 6e22ae9
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions shielder/contract/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions shielder/contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ repository = "https://github.com/Cardinal-Cryptography/zk-apps"
edition = "2021"

[dependencies]
anyhow = { version = "1.0.79", default-features = false }
ink = { version = "5.0.0-rc", default-features = false }

[dev-dependencies]
Expand Down
5 changes: 3 additions & 2 deletions shielder/contract/drink_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use crate::{
drink_tests::utils::{chain::*, ops::*, psp22::*, shielder::*},
test_utils::merkle::MerkleTree,
};
use anyhow::Result;

#[drink::contract_bundle_provider]
pub enum BundleProvider {}

#[drink::test]
fn deploy_single_deposit_single_withdraw(
mut session: Session,
) -> Result<(), Box<dyn std::error::Error>> {
) -> Result<()> {
let alice: AccountId32 = init_alice(&mut session)?;
let bob: AccountId32 = init_bob(&mut session)?;

Expand Down Expand Up @@ -74,7 +75,7 @@ fn deploy_single_deposit_single_withdraw(
#[drink::test]
fn deploy_single_deposit_multiple_withdraw(
mut session: Session,
) -> Result<(), Box<dyn std::error::Error>> {
) -> Result<()> {
let alice: AccountId32 = init_alice(&mut session)?;
session = session.with_actor(alice.clone());

Expand Down
7 changes: 4 additions & 3 deletions shielder/contract/drink_tests/utils/chain.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::drink_tests::utils::ACCOUNT_INITIAL_AMOUNT;
use drink::{runtime::MinimalRuntime, session::Session, AccountId32};
use anyhow::Result;

pub fn init_acc_with_balance(
session: &mut Session<MinimalRuntime>,
acc: &AccountId32,
) -> Result<(), Box<dyn std::error::Error>> {
) -> Result<()> {
session
.sandbox()
.mint_into(acc.clone(), ACCOUNT_INITIAL_AMOUNT)
Expand All @@ -14,15 +15,15 @@ pub fn init_acc_with_balance(

pub fn init_alice(
session: &mut Session<MinimalRuntime>,
) -> Result<AccountId32, Box<dyn std::error::Error>> {
) -> Result<AccountId32> {
let res = AccountId32::new([2u8; 32]);
init_acc_with_balance(session, &res)?;
Ok(res)
}

pub fn init_bob(
session: &mut Session<MinimalRuntime>,
) -> Result<AccountId32, Box<dyn std::error::Error>> {
) -> Result<AccountId32> {
let res = AccountId32::new([3u8; 32]);
init_acc_with_balance(session, &res)?;
Ok(res)
Expand Down
9 changes: 5 additions & 4 deletions shielder/contract/drink_tests/utils/psp22.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use drink::{
session::{Session, NO_ENDOWMENT, NO_SALT},
AccountId32, ContractBundle,
};
use anyhow::Result;

pub fn deploy_test_token(
session: &mut Session<MinimalRuntime>,
supply: u128,
) -> Result<AccountId32, Box<dyn std::error::Error>> {
) -> Result<AccountId32> {
let formatted_supply = format!("{}", supply);

let psp22_bundle =
Expand All @@ -32,7 +33,7 @@ pub fn get_psp22_balance(
session: &mut Session<MinimalRuntime>,
token: &AccountId32,
address: &AccountId32,
) -> Result<u128, Box<dyn std::error::Error>> {
) -> Result<u128> {
let res = session.call_with_address(
token.clone(),
"PSP22::balance_of",
Expand All @@ -47,7 +48,7 @@ pub fn get_psp22_allowance(
token: &AccountId32,
from: &AccountId32,
to: &AccountId32,
) -> Result<u128, Box<dyn std::error::Error>> {
) -> Result<u128> {
let res = session.call_with_address(
token.clone(),
"PSP22::allowance",
Expand All @@ -62,7 +63,7 @@ pub fn psp22_approve(
token: &AccountId32,
to: &AccountId32,
amount: u128,
) -> Result<(), Box<dyn std::error::Error>> {
) -> Result<()> {
let formatted_amount = format!("{}", amount);
session.call_with_address(
token.clone(),
Expand Down
8 changes: 5 additions & 3 deletions shielder/contract/drink_tests/utils/shielder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use crate::{
test_utils::merkle::MerkleTree,
types::Scalar,
};
use anyhow::Result;

pub struct ShielderUserEnv {
pub proof: ZkProof,
pub nullifier: Scalar,
Expand All @@ -25,7 +27,7 @@ pub struct ShielderUserEnv {

pub fn deploy_shielder(
session: &mut Session<MinimalRuntime>,
) -> Result<AccountId32, Box<dyn std::error::Error>> {
) -> Result<AccountId32> {
let res = session.deploy_bundle(
BundleProvider::local()?,
"new",
Expand All @@ -41,7 +43,7 @@ pub fn create_shielder_account(
shielder_address: &AccountId32,
token: &AccountId32,
merkle_tree: &mut MerkleTree,
) -> Result<ShielderUserEnv, Box<dyn std::error::Error>> {
) -> Result<ShielderUserEnv> {
let mut tokens: [Scalar; TOKENS_NUMBER] = [0_u128.into(); TOKENS_NUMBER];
tokens[0] = Scalar::from_bytes(*((*token).as_ref()));

Expand Down Expand Up @@ -80,7 +82,7 @@ pub fn shielder_update(
upd_op: UpdateOperation,
user_shielded_data: ShielderUserEnv,
merkle_tree: &mut MerkleTree,
) -> Result<ShielderUserEnv, Box<dyn std::error::Error>> {
) -> Result<ShielderUserEnv> {
let merkle_root = merkle_tree.root();
let merkle_proof = merkle_tree
.gen_proof(user_shielded_data.tree_leaf_id as usize)
Expand Down

0 comments on commit 6e22ae9

Please sign in to comment.