Example #1
0
from zkay.examples.scenario import ScenarioBuilder
from zkay.transaction.offchain import RequireException

raiser, backer1, backer2 = 'hospital', 'patient1', 'patient2'
sb = ScenarioBuilder('CrowdFundingSuccess', 'code/CrowdFunding.zkay').set_users(raiser, backer1, backer2)
# Set hospital as owner
sb.set_deployment_transaction(100, 3600, amount=20, owner=raiser)
sb.add_balance_assertion(20)
sb.add_state_assertion('pot_balance', user=raiser, should_decrypt=True, expected_value=20)
sb.add_state_assertion('pledged', raiser, user=raiser, should_decrypt=True, expected_value=20)

# Add some money to the contract
sb.add_transaction('fund', user=backer1, amount=300)
sb.add_transaction('fund', user=backer2, amount=2000)
sb.add_balance_assertion(2320)
sb.add_state_assertion('pot_balance', user=raiser, should_decrypt=True, expected_value=20)
sb.add_state_assertion('funds', backer1, user=backer1, should_decrypt=True, expected_value=300)
sb.add_state_assertion('funds', backer2, user=backer2, should_decrypt=True, expected_value=2000)

# Ooops, correct mistake
sb.add_transaction('retrieve_unpledged_funds', user=backer2)
sb.add_state_assertion('funds', backer2, user=backer2, should_decrypt=True, expected_value=0)
sb.add_balance_assertion(320)
sb.add_transaction('fund', user=backer2, amount=500)
sb.add_state_assertion('funds', backer2, user=backer2, should_decrypt=True, expected_value=500)
sb.add_balance_assertion(820)

# This should fail since nothing is pledged yet
sb.add_transaction('accept_pledge', user=raiser, expected_exception=RequireException)

# Pledge money 1
Example #2
0
admin, user, stock_a, stock_b = 'admin', 'user', 'stock_a', 'stock_b'
sb = ScenarioBuilder('IndexFund',
                     'code/IndexFund.zkay').set_users(admin, user, stock_a,
                                                      stock_b)
sb.set_deployment_transaction(owner=admin)
sb.add_transaction('add_stocks_to_fund', [stock_a, 1, 10], user=admin)
sb.add_transaction('add_stocks_to_fund', [stock_b, 3, 5], user=admin)
sb.add_state_assertion('current_fund_price', expected_value=25)

sb.add_transaction('pay_into', amount=250, user=user)
sb.add_state_assertion('balance',
                       user,
                       user=user,
                       expected_value=250,
                       should_decrypt=True)
sb.add_balance_assertion(250)

sb.add_transaction('buy_shares', [10], user=user)
sb.add_state_assertion('balance',
                       user,
                       user=user,
                       expected_value=0,
                       should_decrypt=True)
sb.add_state_assertion('shares',
                       user,
                       user=user,
                       expected_value=10,
                       should_decrypt=True)
sb.add_state_assertion('total_shares',
                       user=admin,
                       expected_value=10,
Example #3
0
from zkay.examples.scenario import ScenarioBuilder
from zkay.transaction.offchain import RequireException

ngo, lobbying, direct_aid, a, b = 'ngo', 'lobbying', 'direct_aid', 'a', 'b'
sb = ScenarioBuilder('NGO', 'code/Ngo.zkay').set_users(ngo, lobbying, direct_aid, a, b)
sb.set_deployment_transaction(lobbying, direct_aid, owner=ngo)

sb.add_transaction('contribute', amount=100, user=a)
sb.add_transaction('contribute', amount=200, user=b)

sb.add_balance_assertion(300)
sb.add_state_assertion('totalContributions', user=ngo, expected_value=300, should_decrypt=True)

# NGO now anonymously pays out money to lobbying / direct_aid, e.g. using the 'TokenHomomorphic' contract
# lobbying & direct_aid confirm having received the contributions

sb.add_transaction('reportReceivedContributions', [150], user=lobbying)
sb.add_transaction('proveMinContributions', user=ngo, expected_exception=RequireException)  # Not yet correct
sb.add_transaction('reportReceivedContributions', [90], user=direct_aid)
sb.add_transaction('proveMinContributions', user=ngo)  # Now it is

SCENARIO = sb.build()