This repository has been archived by the owner on Nov 15, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added ability to deploy smart contracts
- Loading branch information
1 parent
7c858e8
commit 93f127f
Showing
13 changed files
with
385 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ UnitTestChain | |
# C extensions | ||
*.so | ||
|
||
Contracts | ||
|
||
# Distribution / packaging | ||
.Python | ||
|
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
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,130 @@ | ||
|
||
import os | ||
import binascii | ||
from neo.Prompt.Utils import parse_param | ||
from neo.Core.FunctionCode import FunctionCode | ||
from prompt_toolkit import prompt | ||
import json | ||
from neo.VM.ScriptBuilder import ScriptBuilder | ||
from neo.Core.TX.InvocationTransaction import InvocationTransaction | ||
from neo.VM import OpCode | ||
|
||
def LoadContract(args): | ||
|
||
if len(args) < 4: | ||
print("please specify contract to load like such: 'import contract {path} {params} {return_type} {needs_storage}'") | ||
return | ||
|
||
path = args[0] | ||
params = parse_param(args[1], ignore_int=True, prefer_hex=False) | ||
|
||
if type(params) is str: | ||
params = params.encode('utf-8') | ||
|
||
return_type = parse_param(args[2], ignore_int=True, prefer_hex=False) | ||
|
||
if type(return_type) is str: | ||
return_type = return_type.encode('utf-8') | ||
|
||
needs_storage = bool(parse_param(args[3])) | ||
|
||
script = None | ||
|
||
with open(path, 'rb') as f: | ||
|
||
content = f.read() | ||
|
||
try: | ||
content = binascii.unhexlify(content) | ||
except Exception as e: | ||
pass | ||
|
||
script = content | ||
|
||
|
||
if script is not None: | ||
|
||
function_code = FunctionCode(script=script,param_list=bytearray(binascii.unhexlify(params)), return_type=binascii.unhexlify(return_type), needs_storage=needs_storage) | ||
|
||
return function_code | ||
|
||
|
||
print("error loading contract for path %s" % path) | ||
return None | ||
|
||
|
||
def GatherContractDetails(function_code, prompter): | ||
|
||
name = None | ||
version = None | ||
author = None | ||
email = None | ||
description = None | ||
|
||
print("Please fill out the following contract details:") | ||
name = prompt("[Contract Name] > ", | ||
completer=prompter.completer, | ||
history=prompter.history, | ||
get_bottom_toolbar_tokens=prompter.get_bottom_toolbar, | ||
style=prompter.token_style) | ||
|
||
|
||
version = prompt("[Contract Version] > ", | ||
completer=prompter.completer, | ||
history=prompter.history, | ||
get_bottom_toolbar_tokens=prompter.get_bottom_toolbar, | ||
style=prompter.token_style) | ||
|
||
|
||
author = prompt("[Contract Author] > ", | ||
completer=prompter.completer, | ||
history=prompter.history, | ||
get_bottom_toolbar_tokens=prompter.get_bottom_toolbar, | ||
style=prompter.token_style) | ||
|
||
|
||
|
||
email = prompt("[Contract Email] > ", | ||
completer=prompter.completer, | ||
history=prompter.history, | ||
get_bottom_toolbar_tokens=prompter.get_bottom_toolbar, | ||
style=prompter.token_style) | ||
|
||
|
||
description = prompt("[Contract Description] > ", | ||
completer=prompter.completer, | ||
history=prompter.history, | ||
get_bottom_toolbar_tokens=prompter.get_bottom_toolbar, | ||
style=prompter.token_style) | ||
|
||
|
||
print("Creating smart contract....") | ||
print(" Name: %s " % name) | ||
print(" Version: %s" % version) | ||
print(" Author: %s " % author) | ||
print(" Email: %s " % email) | ||
print(" Description: %s " % description) | ||
print("Needs Storage: %s " % function_code.NeedsStorage) | ||
print(json.dumps(function_code.ToJson(), indent=4)) | ||
|
||
return generate_deploy_script(name, version, author, email, description, | ||
function_code.NeedsStorage, function_code.ReturnType, | ||
function_code.ParameterList, function_code.Script) | ||
|
||
def generate_deploy_script(name, version, author, email, description, needs_storage, return_type, parameter_list, script): | ||
sb = ScriptBuilder() | ||
|
||
sb.push(binascii.hexlify(description.encode('utf-8'))) | ||
sb.push(binascii.hexlify(email.encode('utf-8'))) | ||
sb.push(binascii.hexlify(author.encode('utf-8'))) | ||
sb.push(binascii.hexlify(version.encode('utf-8'))) | ||
sb.push(binascii.hexlify(name.encode('utf-8'))) | ||
sb.WriteBool(needs_storage) | ||
sb.push(ord(return_type)) | ||
sb.push(binascii.hexlify(parameter_list)) | ||
sb.WriteVarData(script) | ||
sb.EmitSysCall("Neo.Contract.Create") | ||
|
||
script = sb.ToArray() | ||
|
||
return script |
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,32 @@ | ||
import binascii | ||
from neo.BigInteger import BigInteger | ||
|
||
|
||
def parse_param(p, ignore_int=False, prefer_hex=True): | ||
|
||
if not ignore_int: | ||
try: | ||
val = int(p) | ||
out = BigInteger(val) | ||
return out | ||
except Exception as e: | ||
pass | ||
|
||
try: | ||
val = eval(p) | ||
|
||
if type(val) is bytearray: | ||
return val.hex() | ||
|
||
return val | ||
except Exception as e: | ||
pass | ||
|
||
if type(p) is str: | ||
if prefer_hex: | ||
return binascii.hexlify( p.encode('utf-8')) | ||
else: | ||
return p.encode('utf-8') | ||
|
||
|
||
return p |
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
Oops, something went wrong.