Skip to content

Commit

Permalink
add faucet
Browse files Browse the repository at this point in the history
  • Loading branch information
youwenbusi committed Aug 21, 2020
1 parent d253ced commit d065cbb
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 27 deletions.
29 changes: 29 additions & 0 deletions create_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
CREATE TABLE merkletree (
mid int NOT NULL,
tree_data text(40000) NOT NULL,
blockNumber int NOT NULL,
last_modified_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
)ENGINE = InnoDB;

CREATE TABLE contract (
conName char(20) NOT NULL,
conType char(20) NOT NULL,
conAddr text(500) NOT NULL,
owner text(500) NOT NULL,
totalAmount bigint NOT NULL,
shortName char(20) NOT NULL,
last_modified_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
)ENGINE = InnoDB;

CREATE TABLE transactions (
traType char(20) NOT NULL,
username char(20) NOT NULL,
vin int NOT NULL,
vout int NOT NULL,
input_notes char(40),
output_specs text(2000),
last_modified_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
)ENGINE = InnoDB;
18 changes: 18 additions & 0 deletions interface.json
Original file line number Diff line number Diff line change
Expand Up @@ -240,5 +240,23 @@
"balance": "5000000",
}
},
{
"description": "faucet",
"request": {
"method": "POST",
"uri": "/faucet",

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

47 changes: 33 additions & 14 deletions zkclientapp/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ def genAccount(request) -> None:
with open(keystore_file, "r") as dump_f:
keytext = json.load(dump_f)
privatekey = Account.decrypt(keytext, req['password'])
account = Account.privateKeyToAccount(req['privatekey'])
account = Account.privateKeyToAccount(privatekey)
result['fisco_address'] = account.address
zbac_addr = load_zeth_address(username)
zbac_addr = load_zeth_address_public(req['username'])
result['zbac_address'] = str(zbac_addr)
result['status'] = 0
result['text'] = 'account existed'
Expand Down Expand Up @@ -132,9 +132,9 @@ def importFiscoAddr(request) -> None:
with open(keystore_file, "r") as dump_f:
keytext = json.load(dump_f)
privatekey = Account.decrypt(keytext, req['password'])
account = Account.privateKeyToAccount(req['privatekey'])
account = Account.privateKeyToAccount(privatekey)
result['fisco_address'] = account.address
zbac_addr = load_zeth_address(username)
zbac_addr = load_zeth_address_public(req['username'])
result['zbac_address'] = str(zbac_addr)
result['status'] = 0
result['text'] = 'keystore existed'
Expand Down Expand Up @@ -232,14 +232,36 @@ def sendAsset(request) -> None:
print(f"- {req['username']} the transfer of BAC001asset to the Mixer")
value = EtherValue(req['value'])
out, transactionReceipt = asset_instance.send(
req['toAddress'],
req['fiscoAddress'],
value.wei,'')
print("send tranaction output {}", out)
balance = asset_instance.balance(keypair.address)
result['status'] = 0
result['balance'] = balance
return JsonResponse(result)

def faucet(request) -> None:
print("apply for bac tokens")
result = {}
req = json.loads(request.body)
asset_instance = BAC001(req['token_address'])
asset_instance.client = BcosClient()
value = EtherValue(req['value'])
keystore_file = "{}/{}/{}".format(USER_DIR, req['username'], FISCO_ADDRESS_FILE)
with open(keystore_file, "r") as dump_f:
keytext = json.load(dump_f)
privatekey = Account.decrypt(keytext, req['password'])
account = Account.privateKeyToAccount(privatekey)
out, transactionReceipt = asset_instance.send(
account.address,
value.wei,'')
balance = asset_instance.balance(account.address)
print("get tokens: ", balance)
result['status'] = 0
result['balance'] = balance
return JsonResponse(result)


'''
deposit bac to mixer and get two notes with specified value
params:
Expand Down Expand Up @@ -484,19 +506,17 @@ def getContract(request) -> None:
"contractName": resultbac[0],
"contractType": resultbac[1],
"contractAddr": resultbac[2],
"createtime": resultbac[3],
"ownerAddr": resultbac[4],
"totalAmount": resultbac[5],
"shortName": resultbac[6],
"ownerAddr": resultbac[3],
"totalAmount": resultbac[4],
"shortName": resultbac[5],
}
mixerContract = {
"contractName": resultmixer[0],
"contractType": resultmixer[1],
"contractAddr": resultmixer[2],
"createtime": resultmixer[3],
"ownerAddr": resultmixer[4],
"totalAmount": resultmixer[5],
"shortName": resultmixer[6],
"ownerAddr": resultmixer[3],
"totalAmount": resultmixer[4],
"shortName": resultmixer[5],
}
result['contracts'] = {
"bacContract": bacContract,
Expand Down Expand Up @@ -528,7 +548,6 @@ def getTransactions(request) -> None:
"publicOutput": resultTra[3],
"input_notes": resultTra[4],
"output_specs": resultTra[5],
"time": resultTra[6],
}
result['transactions'].append(transacInfo)
result['status'] = 0
Expand Down
1 change: 1 addition & 0 deletions zkclientapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@
url(r'^getTransactions$', routes.getTransactions),
url(r'^getContract$', routes.getContract),
url(r'^getBalance$', routes.getBalance),
url(r'^faucet$', routes.faucet),
]
22 changes: 9 additions & 13 deletions zkclientapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from commands.zeth_token_deploy import deploy_asset
from commands.zeth_deploy import deploy
import pymysql
import time
import re
BACTYPE = "bac"
MIXERTYPE = "mixer"
Expand All @@ -18,7 +17,7 @@
cursor = db.cursor()
# Create your views here.
ownerAddr = "0x598cf8fba4dcc36417f4c11497dee7eb23fb1431"

'''
def create_table():
print("check whether existed tables")
sqlSearch = "show tables"
Expand All @@ -45,7 +44,7 @@ def create_table():
sqlCreateTra = "create table transactions (traType char(20), username char(20), vin int, vout int, input_notes char(40), output_specs text(2000), time char(60))"
cursor.execute(sqlCreateTra)
db.commit()

'''


def deploy_contract():
Expand All @@ -68,11 +67,10 @@ def deploy_contract():
mixer_address = deploy(token_address)
if mixer_address:
print("save mixer contract to database, address: ", mixer_address)
timestr = time.strftime('%Y-%m-%d %H:%M:%S')
sqlInsertMixer = "insert into contract (conName, conType, conAddr, time, owner, totalAmount, shortName) values (%s, %s, %s, %s, %s, %s, %s);"
sqlInsertMixer = "insert into contract (conName, conType, conAddr, owner, totalAmount, shortName) values (%s, %s, %s, %s, %s, %s);"
conName = "Groth16Mixer"
shortName = "mixer_test"
cursor.execute(sqlInsertMixer, [conName, MIXERTYPE, mixer_address, timestr, ownerAddr, 0, shortName])
cursor.execute(sqlInsertMixer, [conName, MIXERTYPE, mixer_address, ownerAddr, 0, shortName])
db.commit()
else:
print("deploy bac token contract")
Expand All @@ -82,22 +80,20 @@ def deploy_contract():
token_address = deploy_asset("bac token test contract", shortName, minUnit, totalAmount)
if token_address:
print("save bac token contract to database, address: ", token_address)
timestr = time.strftime('%Y-%m-%d %H:%M:%S')
sqlInsertBac = "insert into contract (conName, conType, conAddr, time, owner, totalAmount, shortName) values (%s, %s, %s, %s, %s, %s, %s);"
sqlInsertBac = "insert into contract (conName, conType, conAddr, owner, totalAmount, shortName) values (%s, %s, %s, %s, %s, %s);"
conName = "BAC001"
cursor.execute(sqlInsertBac, [conName, BACTYPE, token_address, timestr, ownerAddr, totalAmount, shortName])
cursor.execute(sqlInsertBac, [conName, BACTYPE, token_address, ownerAddr, totalAmount, shortName])
db.commit()
print("deploy mixer contract on bac token contract of: ", token_address)
mixer_address = deploy(token_address)
if mixer_address:
print("save mixer contract to database, address: ", mixer_address)
timestrMixer = time.strftime('%Y-%m-%d %H:%M:%S')
sqlInsertMixer = "insert into contract (conName, conType, conAddr, time, owner, totalAmount, shortName) values (%s, %s, %s, %s, %s, %s, %s);"
sqlInsertMixer = "insert into contract (conName, conType, conAddr, owner, totalAmount, shortName) values (%s, %s, %s, %s, %s, %s);"
conNameMixer = "Groth16Mixer"
shortNameMixer = "mixer_test"
cursor.execute(sqlInsertMixer, [conNameMixer, MIXERTYPE, mixer_address, timestrMixer, ownerAddr, 0, shortNameMixer])
cursor.execute(sqlInsertMixer, [conNameMixer, MIXERTYPE, mixer_address, ownerAddr, 0, shortNameMixer])
db.commit()

create_table()
#create_table()

deploy_contract()

0 comments on commit d065cbb

Please sign in to comment.