-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add getContract, getTransactions and deploy contracts in view.py
- Loading branch information
1 parent
7e6a262
commit 862fbf0
Showing
5 changed files
with
278 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
|
||
import pymysql | ||
import time | ||
import re | ||
db = pymysql.connect( | ||
host='127.0.0.1', | ||
port=3306, | ||
user='root', | ||
password='8614', | ||
database='merkle', | ||
charset='utf8', | ||
) | ||
cursor = db.cursor() | ||
#treedata = '{"depth": 5, "default_values": ["23453ee476428c4bd601f5f74b09fabe5af02f20a121803b46e728ad02dc3c"], "layers": [["12768d51ae7923fc132b23f9d91999384720785109715b1a66b12bf4b337b101"], ["0abd670539bfe8eef99ff2ef9dc797a5195ab496d738c6d94855cd7afdd2388a"]]}' | ||
#treedata = pymysql.escape_string(treedata) | ||
#sqlCreate = "create table tree (id INT auto_increment PRIMARY KEY ,treedata TEXT(10000) NOT NULL)" | ||
#sqlInsert = "insert into tree (treedata) values (%s)" | ||
#treedata1 = '{"depth":5}' | ||
#sqlUpdate = "update tree set treedata=%s where id=1" | ||
id = 0 | ||
sqlSearch = "show tables" | ||
cursor.execute(sqlSearch) | ||
results = cursor.fetchall() | ||
print("results: ", results) | ||
tables_list = re.findall('(\'.*?\')',str(results)) | ||
print("tables_list: ", tables_list) | ||
tables_list = [re.sub("'",'',each) for each in tables_list] | ||
print("tables_list: ", tables_list) | ||
db.commit() | ||
sqlCreateMer = "create table mtree (MID int, tree_data text(40000), blockNumber int)" | ||
cursor.execute(sqlCreateMer) | ||
db.commit() | ||
#cursor.execute(sqlSearch, [id]) | ||
#results = cursor.fetchall()[0] | ||
#print("blocknumber: ", results[3]) | ||
cursor.close() | ||
db.close() | ||
|
||
''' | ||
from python_web3.eth_account.account import Account | ||
import json | ||
import ast | ||
with open("./contract/mixer/abi/Groth16Mixer.abi", "r") as abistring: | ||
abistr = abistring.readlines()[0] | ||
#abistr = json.load(abistr) | ||
list_list = ast.literal_eval(abistr) | ||
print("abistring type: ", list_list) | ||
with open("./contract/mixer/abi/Groth16Mixer.bin", "r") as binstring: | ||
binstr = binstring.readlines()[0] | ||
print("binstr type: ", type(binstr)) | ||
print("binstr: ", binstr) | ||
''' | ||
''' | ||
keystore_file = "pyaccount.keystore" | ||
with open(keystore_file, "r") as dump_f: | ||
keytext = json.load(dump_f) | ||
privatekey = Account.decrypt(keytext, '123456') | ||
prikey = ''.join(['%02X' % b for b in privatekey]) | ||
prikey = "0x" + prikey.lower() | ||
print(prikey) | ||
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,103 @@ | ||
from django.shortcuts import render | ||
|
||
from commands.constants import DATABASE_DEFAULT_ADDRESS, DATABASE_DEFAULT_PORT, DATABASE_DEFAULT_USER, DATABASE_DEFAULT_PASSWORD, DATABASE_DEFAULT_DATABASE | ||
from commands.zeth_token_deploy import deploy_asset | ||
from commands.zeth_deploy import deploy | ||
import pymysql | ||
import time | ||
import re | ||
BACTYPE = "bac" | ||
MIXERTYPE = "mixer" | ||
db = pymysql.connect( | ||
host = DATABASE_DEFAULT_ADDRESS, | ||
port = DATABASE_DEFAULT_PORT, | ||
user = DATABASE_DEFAULT_USER, | ||
password = DATABASE_DEFAULT_PASSWORD, | ||
database = DATABASE_DEFAULT_DATABASE, | ||
charset='utf8' | ||
) | ||
cursor = db.cursor() | ||
# Create your views here. | ||
ownerAddr = "0x598cf8fba4dcc36417f4c11497dee7eb23fb1431" | ||
|
||
def index(request): | ||
|
||
"学习笔记的主页" | ||
def create_table(): | ||
print("check whether existed tables") | ||
sqlSearch = "show tables" | ||
cursor.execute(sqlSearch) | ||
tables = cursor.fetchall() | ||
db.commit() | ||
tables_list = re.findall('(\'.*?\')',str(tables)) | ||
tables_list = [re.sub("'",'',each) for each in tables_list] | ||
mertab = "merkletree" | ||
contab = "contract" | ||
tratab = "transactions" | ||
if not mertab in tables_list: | ||
print("create table merkletree") | ||
sqlCreateMer = "create table merkletree (MID int, tree_data text(40000), blockNumber int)" | ||
cursor.execute(sqlCreateMer) | ||
db.commit() | ||
if not contab in tables_list: | ||
print("create table contract") | ||
sqlCreateCon = "create table contract (conName char(20), conType char(20), conAddr text(500), time char(60), owner text(500), totalAmount bigint, shortName char(20))" | ||
cursor.execute(sqlCreateCon) | ||
db.commit() | ||
if not tratab in tables_list: | ||
print("create table transactions") | ||
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() | ||
|
||
return render(request,'zkclientapp/template/index.html') | ||
|
||
def get_html(request): | ||
return render(request, 'zkclientapp/template/get.html') | ||
|
||
def get(request): | ||
context = {} | ||
# 通过request.GET['name']形式获取get表单内容 | ||
# result为重定向到的result.html所使用的变量 | ||
context['result'] = f"你搜索的内容为:{request.GET['q']}" | ||
return render(request, 'zkclientapp/template/result.html', context) | ||
def deploy_contract(): | ||
print("check whether existed bac token contract and mixer contract") | ||
sqlSearchBac = "select * from contract where conType = %s" | ||
cursor.execute(sqlSearchBac, [BACTYPE]) | ||
resultBac = cursor.fetchall() | ||
db.commit() | ||
if resultBac: | ||
sqlSearchMixer = "select * from contract where conType = %s" | ||
cursor.execute(sqlSearchMixer, [MIXERTYPE]) | ||
resultMixer = cursor.fetchall() | ||
db.commit() | ||
if resultMixer: | ||
print("all contract existed") | ||
return | ||
else: | ||
token_address = resultBac[0][2] | ||
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) | ||
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);" | ||
conName = "Groth16Mixer" | ||
shortName = "mixer_test" | ||
cursor.execute(sqlInsertMixer, [conName, MIXERTYPE, mixer_address, timestr, ownerAddr, 0, shortName]) | ||
db.commit() | ||
else: | ||
print("deploy bac token contract") | ||
shortName = "zk-AAA-demo" | ||
minUnit = 18 | ||
totalAmount = 50000000000 | ||
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);" | ||
conName = "BAC001" | ||
cursor.execute(sqlInsertBac, [conName, BACTYPE, token_address, timestr, 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);" | ||
conNameMixer = "Groth16Mixer" | ||
shortNameMixer = "mixer_test" | ||
cursor.execute(sqlInsertMixer, [conNameMixer, MIXERTYPE, mixer_address, timestrMixer, ownerAddr, 0, shortNameMixer]) | ||
db.commit() | ||
|
||
def post_html(request): | ||
# 不能和get一样使用render_to_response必须使用render进行重定向,不然服务端不会设置csrf_token | ||
# return render_to_response('post.html') | ||
return render(request, 'zkclientapp/template/post.html') | ||
create_table() | ||
|
||
def post(request): | ||
context = {} | ||
# 通过request.GET['name']形式获取post表单内容 | ||
# result为重定向到的result.html所使用的变量 | ||
context['result'] = f"你搜索的内容为:{request.POST['q']}" | ||
return render(request, 'zkclientapp/template/result.html', context) | ||
deploy_contract() |