Skip to content

Commit

Permalink
add getBalance
Browse files Browse the repository at this point in the history
  • Loading branch information
youwenbusi committed Aug 21, 2020
1 parent 171df5b commit d253ced
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 18 deletions.
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ setup:
pip install --upgrade pip --progress-bar off
pip install -e . --progress-bar off
$(MAKE) grpc
python -c "from zeth.contracts import install_sol; \
install_sol()"

check: syntax test

Expand Down
7 changes: 3 additions & 4 deletions commands/event_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import List

import pymysql
from click import command, option, argument


from commands.constants import USER_DIR, WALLET_DIR_DEFAULT, \
DATABASE_DEFAULT_ADDRESS, DATABASE_DEFAULT_PORT, DATABASE_DEFAULT_USER, DATABASE_DEFAULT_PASSWORD, \
Expand Down Expand Up @@ -125,9 +125,8 @@ def on_event(self, eventdata):
wallet.update_and_save_state()


@command()
@argument("mixer_addr")
def event_sync(mixer_addr: str):

def event_sync():

indexed_value = None
try:
Expand Down
19 changes: 18 additions & 1 deletion interface.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,23 @@
"privatekey": "0x3fa34c3f31a42c65c4c7056d05d3130f4d0d29bba81266de06b79be3cf80740e",
"zbac_address": "d3603132dbaf076060d16d911a4adf8d6624b60510a819def82599353a0685ec:8f37a09af7b7469a5b7bef573e941521e8cc8ef6c528b30a16a5b7bb211f7362"
}
}
},
{
"description": "getBalance",
"request": {
"method": "POST",
"uri": "/getBalance",

"body": {
"username": "hehe",
"password": "123456",
"token_address": "0x511003e45fdbfdc993d6a87b6c71dedda2fe5e09"
}
},
"response": {
"status": 0,
"balance": "5000000",
}
},
]

51 changes: 40 additions & 11 deletions zkclientapp/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,16 @@ def genAccount(request) -> None:
result = {}
req = json.loads(request.body)
keystore_file = "{}/{}/{}".format(USER_DIR, req['username'], FISCO_ADDRESS_FILE)
if exists(keystore_file):
result['status'] = 1
result['text'] = 'username existed'
return JsonResponse(result)
addr_file = "{}/{}/{}".format(USER_DIR, req['username'], ADDRESS_FILE_DEFAULT)
if exists(addr_file):
result['status'] = 1
if exists(keystore_file) and exists(addr_file):
with open(keystore_file, "r") as dump_f:
keytext = json.load(dump_f)
privatekey = Account.decrypt(keytext, req['password'])
account = Account.privateKeyToAccount(req['privatekey'])
result['fisco_address'] = account.address
zbac_addr = load_zeth_address(username)
result['zbac_address'] = str(zbac_addr)
result['status'] = 0
result['text'] = 'account existed'
return JsonResponse(result)
(address, publickey, privatekey) = gen_fisco_address(req['username'], req['password'])
Expand All @@ -124,16 +127,23 @@ def importFiscoAddr(request) -> None:
req = json.loads(request.body)
account = Account.privateKeyToAccount(req['privatekey'])
keystore_file = "{}/{}/{}".format(USER_DIR, req['username'], FISCO_ADDRESS_FILE)
addr_file = "{}/{}/{}".format(USER_DIR, req['username'], ADDRESS_FILE_DEFAULT)
if exists(keystore_file):
result['status'] = 1
with open(keystore_file, "r") as dump_f:
keytext = json.load(dump_f)
privatekey = Account.decrypt(keytext, req['password'])
account = Account.privateKeyToAccount(req['privatekey'])
result['fisco_address'] = account.address
zbac_addr = load_zeth_address(username)
result['zbac_address'] = str(zbac_addr)
result['status'] = 0
result['text'] = 'keystore existed'
return JsonResponse(result)
user_dir = "{}/{}/{}".format(USER_DIR, req['username'], WALLET_DIR_DEFAULT)
_ensure_dir(user_dir)
keytext = Account.encrypt(account.privateKey, req['password'])
with open(keystore_file, "w") as dump_f:
json.dump(keytext, dump_f)
addr_file = "{}/{}/{}".format(USER_DIR, req['username'], ADDRESS_FILE_DEFAULT)
zbac_addr = ''
if not exists(addr_file):
zbac_addr = zbac_addr + str(gen_address(req['username']))
Expand Down Expand Up @@ -208,7 +218,7 @@ def deployMixer(request) -> None:
def sendAsset(request) -> None:
result = {}
req = json.loads(request.body)
asset_instance = BAC001(req['assetAddress'])
asset_instance = BAC001(req['token_address'])
keystore_file = "{}/{}/{}".format(USER_DIR, req['username'], FISCO_ADDRESS_FILE)
with open(keystore_file, "r") as dump_f:
keytext = json.load(dump_f)
Expand All @@ -227,7 +237,7 @@ def sendAsset(request) -> None:
print("send tranaction output {}", out)
balance = asset_instance.balance(keypair.address)
result['status'] = 0
result['address'] = balance
result['balance'] = balance
return JsonResponse(result)

'''
Expand Down Expand Up @@ -530,4 +540,23 @@ def getTransactions(request) -> None:
else:
result['status'] = 1
result['text'] = 'your account is not recorded in server, please import it firstly or create a new one'
return JsonResponse(result)
return JsonResponse(result)

def getBalance(request) -> None:
result = {}
req = json.loads(request.body)
bac_instance = BAC001(req['token_address'])
keystore_file = "{}/{}/{}".format(USER_DIR, req['username'], FISCO_ADDRESS_FILE)
with open(keystore_file, "r") as dump_f:
keytext = json.load(dump_f)
privkey = Account.decrypt(keytext, req['password'])
bac_instance.client.ecdsa_account = Account.from_key(privkey)
keypair = BcosKeyPair()
keypair.private_key = bac_instance.client.ecdsa_account.privateKey
keypair.public_key = bac_instance.client.ecdsa_account.publickey
keypair.address = bac_instance.client.ecdsa_account.address
bac_instance.client.keypair = keypair
balance = bac_instance.balance(bac_instance.client.ecdsa_account.address)
result['status'] = 0
result['balance'] = balance
return JsonResponse(result)
1 change: 1 addition & 0 deletions zkclientapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
#url(r'^getMixerContract$', routes.getMixerContract),
url(r'^getTransactions$', routes.getTransactions),
url(r'^getContract$', routes.getContract),
url(r'^getBalance$', routes.getBalance),
]

0 comments on commit d253ced

Please sign in to comment.