Skip to content

Commit

Permalink
deposit from web and async every once after deposit, save the merklet…
Browse files Browse the repository at this point in the history
…ree in mysql and every user share the same merkletree from mysql
  • Loading branch information
youwenbusi committed Aug 2, 2020
1 parent ccc6244 commit 62ffd65
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 77 deletions.
21 changes: 11 additions & 10 deletions commands/event_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from zeth.wallet import Wallet, ZethNoteDescription
from commands.utils import load_zeth_address
from typing import List
from zkserverapp.models import merkletree
'''
def usage():
usagetext = '\nUsage:\nparams: contractname address event_name indexed\n' \
Expand Down Expand Up @@ -93,8 +94,7 @@ def on_event(self, eventdata):
print(f"The update_merkle_root in wallet of {wallet.username} is {update_merkle_root}")


@command()
@option("--mixer-addr", help="The Groth16Mixer contract address you want to listen")

def event_sync(mixer_addr: str):

indexed_value = None
Expand Down Expand Up @@ -122,18 +122,19 @@ def event_sync(mixer_addr: str):
"after register LogMix,result:{},all:{}".format(
result['result'], result))

while True:
print("waiting event...")
time.sleep(10)
while(not merkletree.objects.all().count() or not merkletree.objects.all().last().is_new):
print(merkletree.objects.all().count())
time.sleep(1)

if bcos_event.client is not None:
bcos_event.client.finish()

except Exception as e:
print("Exception!")
import traceback
traceback.print_exc()
finally:
print("event callback finished!")
if bcos_event.client is not None:
bcos_event.client.finish()
sys.exit(-1)




if __name__ == "__main__":
Expand Down
6 changes: 2 additions & 4 deletions commands/zeth_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@
from commands.constants import PROVER_SERVER_ENDPOINT_DEFAULT


@command()
#@option("--eth-addr", help="Sender eth address or address filename")
@option("--token-address", help="Address of token contract (if used)")
def deploy(
token_address: Optional[str]
) -> None:
) :
"""
Deploy the zeth contracts and record the instantiation details.
"""
Expand All @@ -50,6 +47,7 @@ def deploy(
mixer_address = mixerTransactionRecipient['contractAddress']
print(f"deploy: mixer_address={mixer_address}")
#mixer_instance = Groth16Mixer(address)
return mixer_address


if __name__ == '__main__':
Expand Down
17 changes: 7 additions & 10 deletions commands/zeth_deposit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,19 @@
from zeth.mixer_client import ZethAddressPub
from zeth.utils import EtherValue, from_zeth_units
from api.zeth_messages_pb2 import ZethNote
from click import command, option, pass_context, ClickException, Context
from click import ClickException
from typing import List, Tuple, Optional
import sys
sys.path.append('../')
from contract.Groth16Mixer import Groth16Mixer
from python_web3.eth_account.account import Account

@command()
@option("--mixer-addr", help="The Groth16Mixer contract address you want to use")
@option("--username", help="The account you want to use")
@option("--password", help="the password of you keystore")
@option("--vin", default="0", help="public in value")
@option("--out", "output_specs", multiple=True, help="<receiver_pub_key>,<value>")

def deposit(
mixer_addr: str,
username: str,
password: str,
vin: str,
output_specs: List[str]
) -> None:
) :
"""
Generic mix function
"""
Expand Down Expand Up @@ -69,6 +62,10 @@ def deposit(
tx_value)

print("receipt status: ", receipt['status'])
if receipt['status'] == '0x0':
return True
else:
return False
#do_sync(wallet, receipt)
if __name__ == '__main__':
deposit()
10 changes: 4 additions & 6 deletions commands/zeth_gen_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@
from zeth.wallet import _ensure_dir
ADDRESS_FILE_DEFAULT = "zeth-address.json"

@command()
@option("--username", prompt='Your name', help="specify a username for you")
#@pass_context
def gen_address(username: str) -> None:

def gen_address(username: str) :
"""
Generate a new Zeth secret key and public address
"""
#client_ctx = ctx.obj
#addr_file_name = get_zeth_address_file(client_ctx)
addr_file = "{}/{}/{}".format(USER_DIR, username, ADDRESS_FILE_DEFAULT)
if exists(addr_file):
raise ClickException(f"ZethAddress file {addr_file} exists")
user_dir = "{}/{}/{}".format(USER_DIR, username, WALLET_DIR_DEFAULT)
_ensure_dir(user_dir)
pub_addr_file = pub_address_file(addr_file)
Expand All @@ -35,5 +31,7 @@ def gen_address(username: str) -> None:
print(f"ZethAddress Secret key written to {addr_file}")
write_zeth_address_public(zeth_address.addr_pk, pub_addr_file)
print(f"Public ZethAddress written to {pub_addr_file}")
return zeth_address.addr_pk

if __name__ == '__main__':
gen_address()
14 changes: 6 additions & 8 deletions commands/zeth_gen_fisco_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@



@command()
@option("--username", prompt='Your name', help="specify a username for you")
@option("--password", prompt='Your password', help="specify a password for you")
#@pass_context
def gen_fisco_address(username: str, password: str) -> None:

def gen_fisco_address(username: str, password: str) :
"""
Generate a new fisco account
"""
account = Account.create(password)
keystore_file = "{}/{}/{}".format(USER_DIR, username, FISCO_ADDRESS_FILE)
if exists(keystore_file):
raise ClickException(f"ZethAddress file {keystore_file} exists")
account = Account.create(password)
user_dir = "{}/{}/{}".format(USER_DIR, username, WALLET_DIR_DEFAULT)
_ensure_dir(user_dir)
keytext = Account.encrypt(account.privateKey, password)
Expand All @@ -35,5 +30,8 @@ def gen_fisco_address(username: str, password: str) -> None:
print(f"{username}'s address: {account.address}")
print(f"{username}'s publickey: {account.publickey}")
print(f"fisco account keypair written to {keystore_file}")
return account.address, account.publickey


if __name__ == '__main__':
gen_fisco_address()
5 changes: 1 addition & 4 deletions commands/zeth_ls_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
from click import Context, command, pass_context, option
from commands.constants import PROVER_SERVER_ENDPOINT_DEFAULT

@command()
#@option("--mixer-addr", help="The Groth16Mixer contract address you want to use")
@option("--username", help="The account you want to use")
@option("--password", help="the password of you keystore")

def ls_commits(username: str, password: str) -> None:
"""
List all commitments in the joinsplit contract
Expand Down
6 changes: 1 addition & 5 deletions commands/zeth_ls_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
sys.path.append('../')
#from contract.Groth16Mixer import Groth16Mixer

@command()
#@option("--mixer-addr", help="The Groth16Mixer contract address you want to use")
@option("--username", help="The account you want to use")
#@option("--balance", is_flag=True, help="Show total balance")
#@option("--spent", is_flag=True, help="Show spent notes")

def ls_notes(username: str) -> None:
"""
List the set of notes owned by this wallet
Expand Down
8 changes: 0 additions & 8 deletions commands/zeth_mix.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@
from contract.Groth16Mixer import Groth16Mixer
from python_web3.eth_account.account import Account

@command()
@option("--mixer-addr", help="The Groth16Mixer contract address you want to use")
@option("--username", help="The account you want to use")
@option("--password", help="the password of you keystore")
@option("--vin", default="0", help="public in value")
@option("--vout", default="0", help="public out value")
@option("--in", "input_notes", multiple=True)
@option("--out", "output_specs", multiple=True, help="<receiver_pub_key>,<value>")
def mix(
mixer_addr: str,
username: str,
Expand Down
18 changes: 4 additions & 14 deletions commands/zeth_token_approve.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
# SPDX-License-Identifier: LGPL-3.0+

from commands.utils import EtherValue
from click import command, argument, option, pass_context, ClickException, Context
import sys
sys.path.append('../')
from click import ClickException
from contract.Groth16Mixer import Groth16Mixer
from contract.ERC20Mintable import ERC20Mintable
from python_web3.eth_account.account import Account
Expand All @@ -14,15 +12,8 @@
import json
from os.path import exists

@command()
@argument("tokens")
#@option("--eth-addr", help="Sender eth address or address filename")
#@option("--wait", is_flag=True, help="Wait for transaction to complete")
@option("--mixer-addr", help="The Groth16Mixer contract address you want to use")
@option("--token-addr", help="The Groth16Mixer contract address you want to use")
@option("--username", help="the username you create before")
@option("--password", help="the password of you keystore")
def token_approve(tokens: str, mixer_addr: str, token_addr: str, username: str, password: str) -> None:

def token_approve(tokens: str, mixer_addr: str, token_addr: str, username: str, password: str) :
"""
Approve the mixer to spend some amount of tokens
"""
Expand All @@ -36,8 +27,6 @@ def token_approve(tokens: str, mixer_addr: str, token_addr: str, username: str,

token_instance = ERC20Mintable(token_addr)
keystore_file = "{}/{}/{}".format(USER_DIR, username, FISCO_ADDRESS_FILE)
if exists(keystore_file) is False:
raise ClickException(f"invalid output spec: {keystore_file}")
with open(keystore_file, "r") as dump_f:
keytext = json.load(dump_f)
privkey = Account.decrypt(keytext, password)
Expand All @@ -53,6 +42,7 @@ def token_approve(tokens: str, mixer_addr: str, token_addr: str, username: str,
approve_value.wei)
outputresult = token_instance.allowance(token_instance.client.ecdsa_account.address, mixer_addr)
print(f"- The allowance for the Mixer from {username} is: {outputresult}")
return outputresult

if __name__ == '__main__':
token_approve()
10 changes: 2 additions & 8 deletions commands/zeth_token_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,15 @@
from click import Context, command, option, pass_context
from typing import Optional
from web3 import Web3
import sys
sys.path.append('../')
from contract.ERC20Mintable import ERC20Mintable
#from test_commands.deploy_test_token import mint_token



@command()
@option("--miner-address", help="Address of miner")
@option("--token-amount", help="amount of token been mint")
@pass_context
def deploy_token(
ctx: Context,
miner_address: Optional[str],
token_amount: Optional[int]
) -> None:
) :
"""
Deploy the zeth contracts and record the instantiation details.
"""
Expand All @@ -41,6 +34,7 @@ def deploy_token(
print("- Initial balances: ")
outputresult = token_instance.balanceOf(miner_address)
print(f" {miner_address} : {outputresult}")
return token_address

if __name__ == '__main__':
deploy_token()

0 comments on commit 62ffd65

Please sign in to comment.