def __init__(self): max_deploy_gas = 0 # Initialize web3 object web3 = Web3(HTTPProvider('http://localhost:8545')) #Ganache default host + port # Path for Solidity source files contract_dir = os.path.abspath('./contracts') # Initialize interface interface = ContractInterface(web3, 'TEST', contract_dir) # Compile contracts & deploy interface.compile_source_files() interface.deploy_contract() # Estimate gas usage. Can be used to deploy if it's below X value # TODO What class is this in? deployment_estimate = deployment.constructor().estimateGas(transaction = deployment_params) if deployment_estimate < self.max_deploy_gas: tx_hash = deployment.constructor().transact(transaction = deployment_params) tx_receipt = self.web3.eth.waitForTransactionReceipt(tx_hash) contract_address = tx_receipt['contractAddress'] vars = { 'contract_address': contract_address, 'contract_abi': deployment_compiled['abi'] }
class TestInterface(unittest.TestCase): def setUp(self): # Set blockcahin provider self.w3 = Web3(HTTPProvider('http://10.10.10.61:7545')) self.contract_dir = os.path.abspath('./contracts/') self.greeter_interface = ContractInterface(self.w3, 'Greeter', self.contract_dir) def test_1_init(self): self.assertEqual(self.greeter_interface.web3.eth.defaultAccount, self.w3.eth.accounts[0], 'Default account not set correctly') def test_2_compile(self): self.greeter_interface.compile_source_files() self.assertEqual(len(self.greeter_interface.all_compiled_contracts), 3) def test_3_deploy(self): self.greeter_interface.deploy_contract() self.assertTrue( os.path.isfile(self.greeter_interface.deployment_vars_path)) def test_4_get_instance(self): self.greeter_interface.get_instance() self.assertEqual(self.greeter_interface.contract_instance.address, self.greeter_interface.contract_address) def test_5_change_greeting(self): event = 'GreetingChange' new_greeting = 'Hola'.encode('utf-8') expected_logs = { 'changer': self.w3.eth.accounts[0], '_from': 'Hello', '_to': new_greeting, 'event': event } self.greeter_interface.get_instance() receipt, indexed_events = self.greeter_interface.send('setGreeting', new_greeting, event=event) self.assertTrue(receipt['blockNumber'] > 0) self.assertEqual( indexed_events['changer'], expected_logs['changer'], "Logging output for {} inconsistent".format('changer')) self.assertEqual(indexed_events['_from'], expected_logs['_from'], "Logging output for {} inconsistent".format('_from')) self.assertEqual( indexed_events['changer'], expected_logs['changer'], "Logging output for {} inconsistent".format('changer')) def test_6_call_greeting(self): expected_greeting = 'Hola' # Although the test suite starts with a clean state, the test above # changes the contract's greeting on the blockchain, which is what # is fetched. self.greeter_interface.get_instance() actual_greeting = self.greeter_interface.retrieve('greeting') self.assertEqual(expected_greeting, actual_greeting, 'Unexpected greeting returned')
def load_contracts(): contract_dir = PATH + "/deployment_vars" deploy_dir = PATH + "/deployment_vars" # setup fake stablecoin path = PATH + "/contracts/TestContracts.sol:FakeUSD" StableCoin = ContractInterface( w3, path, contract_dir, deployment_vars_path=deploy_dir ) StableCoin.deploy_contract(path) # setup price feed path = PATH + "/contracts/TestContracts.sol:FakePriceProvider" PriceProvider = ContractInterface( w3, path, contract_dir, deployment_vars_path=deploy_dir ) PriceProvider.deploy_contract(path, [20000000000]) # setup exchange path = PATH + "/contracts/TestContracts.sol:FakeExchange" Exchange = ContractInterface( w3, path, contract_dir, deployment_vars_path=deploy_dir ) Exchange.deploy_contract( path, [PriceProvider.vars["contract_address"], StableCoin.vars["contract_address"]], ) # setup CallOptions path = PATH + "/contracts/HegicCallOptions.sol:HegicCallOptions" HegicCallOptions = ContractInterface( w3, path, contract_dir, deployment_vars_path=deploy_dir ) HegicCallOptions.deploy_contract(path, [PriceProvider.vars["contract_address"]]) # setup PutOptions path = PATH + "/contracts/HegicPutOptions.sol:HegicPutOptions" HegicPutOptions = ContractInterface( w3, path, contract_dir, deployment_vars_path=deploy_dir ) HegicPutOptions.deploy_contract( path, [ StableCoin.vars["contract_address"], PriceProvider.vars["contract_address"], Exchange.vars["contract_address"], ], ) # setupERCPool path = PATH + "/contracts/HegicERCPool.sol:HegicERCPool" HegicERCPool = ContractInterface( w3, path, contract_dir, deployment_vars_path=deploy_dir ) HegicERCPool.deploy_contract(path, [StableCoin.vars["contract_address"]]) # setupETHPool path = PATH + "/contracts/HegicETHPool.sol:HegicETHPool" HegicETHPool = ContractInterface( w3, path, contract_dir, deployment_vars_path=deploy_dir ) HegicETHPool.deploy_contract(path, [StableCoin.vars["contract_address"]]) print("All Deployed!") return { "StableCoin": StableCoin, "PriceFeed": PriceProvider, "Exchange": Exchange, "HegicCallOptions": HegicCallOptions, "HegicPutOptions": HegicPutOptions, "HegicERCPool": HegicERCPool, "HegicETHPool": HegicETHPool, }
from web3 import Web3, HTTPProvider from interface import ContractInterface # Initialize your web3 object w3 = Web3(HTTPProvider('http://127.0.0.1:8545')) ser = serial.Serial('/dev/ttyUSB1', 115200) ser.flushInput() print("from:", w3.eth.accounts[0]) # Create a path object to your Solidity source files contract_dir = os.path.abspath('./contracts/') greeter_interface = ContractInterface(w3, 'Greeter', contract_dir) # Initialize your interface greeter_interface.compile_source_files() greeter_interface.deploy_contract() instance = greeter_interface.get_instance() def strToHex32(zBytes): len1 = len(zBytes) if len1 > 32: zBytes32 = zBytes[:32] else: zBytes32 = zBytes.ljust(32, '0') return bytes(zBytes32, 'utf-8') while True: ser_bytes = ser.readline()
# -*- coding: utf-8 -*- """ Created on Sun Mar 1 17:06:31 2020 @author: peter """ import os from web3 import Web3, HTTPProvider from interface import ContractInterface w3 = Web3(HTTPProvider("http://127.0.0.1:8545")) contractDir = os.path.abspath("./contracts/") soapBoxInterface = ContractInterface(w3, "SoapBox", contractDir) soapBoxInterface.compile_source_files() soapBoxInterface.deploy_contract() instance = soapBoxInterface.get_instance() print("before we pay:", instance.functions.isApproved(w3.eth.accounts[0]).call()) instance.functions.pay().transact({'value': w3.toWei(0.03, 'ether')}) print("after we pay:", instance.functions.isApproved(w3.eth.accounts[0]).call()) instance.functions.broadcastOpinion( 'You can never be overdressed or overeducated.').transact() print(instance.functions.getCurrentOpinion().call())