Skip to content

Commit

Permalink
pyiele: compile solidity contract
Browse files Browse the repository at this point in the history
  • Loading branch information
anvacaru committed Oct 19, 2021
1 parent a7f7977 commit 1444d0d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyiele/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3

from .config import *
from .rlpencoder import *
from .rlp import *
from .rpc import *
from .utils import *
1 change: 1 addition & 0 deletions pyiele/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ class config:
gas_price = "0x0"
gas_limit = "0x166f5777"
sleep_time = 3
directory = "contract_artifacts"
File renamed without changes.
1 change: 0 additions & 1 deletion pyiele/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
## RPC Call helpers

def send_rpc(rpc, port):
#print("> ", rpc)
process = Popen(['curl', '--silent', '-H', 'Content-Type: application/json', '--data', rpc, 'http://localhost:' + str(port)], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
return stdout
Expand Down
33 changes: 31 additions & 2 deletions pyiele/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/python3
import json
from .config import config
from .rlpencoder import decode_response, encode_function
from .rlp import decode_response, encode_function, encode_contract
from .rpc import *
from time import sleep
from subprocess import run, PIPE
from os import path

def get_result(input):
try:
Expand All @@ -23,7 +25,8 @@ def transaction_call_data(sender, data, to, gas_limit, gas_price):
return ( "{" f""""from": "{sender}", "to":"{to}", "gasLimit":"{gas_limit}", "gasPrice":"{gas_price}", "data": "{data}" """ "}" )

def deploy_contract(walletId, sender, bytecode):
tx_data = transaction_deploy_data(sender, bytecode, config.gas_limit, config.gas_price)
rlp = encode_contract(bytecode)
tx_data = transaction_deploy_data(sender, rlp, config.gas_limit, config.gas_price)
private_key = send(wallet_getDefaultPrivateAddress(walletId))
tx_hash = send(wallet_callContract(walletId, private_key, config.passphrase, tx_data))
send(qa_mineBlocks(1, "true"))
Expand All @@ -48,3 +51,29 @@ def init_wallet():
send(qa_mineBlocks(1, "true"))
sleep(config.sleep_time)
return walletId

def get_bytecode_of(file_path, contract_name):
file_dict = json.loads(compile_file(file_path))
bin = file_dict['contracts']['erc20.sol:'+contract_name]['bin']
metadatabin=file_dict['contracts']['erc20.sol:'+contract_name]['metadata-bin']
if bin.endswith(metadatabin):
bin = bin[:-len(metadatabin)]
return bin

def compile_file(file_path):
command = ["isolc", file_path, "--combined-json", "asm,bin,metadata-bin,srcmap"]
result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True)
if( result.returncode == 0 ):
write_file(file_path, result.stdout)
return result.stdout
else:
print(result.stderr)
return

def write_file(file_path, content):
if not path.exists(config.directory):
makedirs(config.directory)
base = path.basename(file_path)
file_name = path.splitext(base)[0] + ".json"
with open(config.directory + "/" + file_name, 'w') as f:
f.write(content)

0 comments on commit 1444d0d

Please sign in to comment.