def generate_go_ethereum_fixture(destination_dir): with contextlib.ExitStack() as stack: datadir = stack.enter_context(tempdir()) keystore_dir = os.path.join(datadir, 'keystore') ensure_path_exists(keystore_dir) keyfile_path = os.path.join(keystore_dir, KEYFILE_FILENAME) with open(keyfile_path, 'w') as keyfile: keyfile.write(KEYFILE_DATA) genesis_file_path = os.path.join(datadir, 'genesis.json') with open(genesis_file_path, 'w') as genesis_file: genesis_file.write(json.dumps(GENESIS_DATA)) geth_ipc_path_dir = stack.enter_context(tempdir()) geth_ipc_path = os.path.join(geth_ipc_path_dir, 'geth.ipc') geth_port = get_open_port() geth_binary = get_geth_binary() with get_geth_process( geth_binary=geth_binary, datadir=datadir, genesis_file_path=genesis_file_path, geth_ipc_path=geth_ipc_path, geth_port=geth_port): wait_for_socket(geth_ipc_path) web3 = Web3(Web3.IPCProvider(geth_ipc_path)) chain_data = setup_chain_state(web3) # close geth by exiting context # must be closed before copying data dir verify_chain_state(web3, chain_data) # verify that chain state is still valid after closing # and re-opening geth with get_geth_process( geth_binary=geth_binary, datadir=datadir, genesis_file_path=genesis_file_path, geth_ipc_path=geth_ipc_path, geth_port=geth_port): wait_for_socket(geth_ipc_path) web3 = Web3(Web3.IPCProvider(geth_ipc_path)) verify_chain_state(web3, chain_data) static_data = { 'raw_txn_account': RAW_TXN_ACCOUNT, 'keyfile_pw': KEYFILE_PW, } config = merge(chain_data, static_data) pprint.pprint(config) write_config_json(config, datadir) shutil.make_archive(destination_dir, 'zip', datadir)
def dial(self, target): ''' connects to a node :param url: string (optional) - resource in which to connect. if not provided, will use default for the stage :returns: provider, error ''' if not target: return None, "target network must be specified with -t or --target" url = get_url(self.config, target) try: if url.startswith('ws'): self.w3 = Web3(WebsocketProvider(url)) elif url.startswith('http'): self.w3 = Web3(HTTPProvider(url)) elif url.endswith('ipc'): if url == 'ipc': url = None self.w3 = Web3(Web3.IPCProvider(url)) else: return None, "Invalid Provider URL: {}".format(url) except Exception as e: return None, e return self.w3, None
def __init__(self, settings_directory : Path = getcwd()): path.append(settings_directory) import settings networks = settings.networks infura_settings = settings.infura_settings if infura_settings: environ["WEB3_INFURA_PROJECT_ID"] = infura_settings["project_id"] environ["WEB3_INFURA_API_SECRET"] = infura_settings["project_secret"] development_network = networks["development"] if development_network["mode"]=="HTTP": server = "http://" + development_network["host"] + ":" + str(development_network["port"]) self.w3 = Web3(Web3.HTTPProvider(server)) elif development_network["mode"]=="IPC": ipc_url = development_network["url"] self.w3 = Web3(Web3.IPCProvider(ipc_url)) elif development_network["mode"]=="Websocket": server = "ws://" + development_network["host"] + ":" + str(development_network["port"]) self.w3 = Web3(Web3.WebsocketProvider(server)) elif development_network["mode"]=="Infura": environ["WEB3_INFURA_SCHEME"] = str(development_network["scheme"]) if development_network["endpoints"] == "mainnet": from web3.auto.infura.mainnet import w3 elif development_network["endpoints"] == "ropsten": from web3.auto.infura.ropsten import w3 elif development_network["endpoints"] == "goerli": from web3.auto.infura.goerli import w3 elif development_network["endpoints"] == "rinkeby": from web3.auto.infura.rinkeby import w3 elif development_network["endpoints"] == "kovan": from web3.auto.infura.kovan import w3 self.w3 = w3
def create_contract(): provider = Web3.IPCProvider(os.path.join(os.path.dirname(__file__), '../DesignNode/geth.ipc')) w3 = Web3(provider) with open('../contracts/Designdb.sol', 'r') as source_file: contract_source = source_file.read() compiled_sol = compile_source(contract_source) contract_interface = compiled_sol['<stdin>:DesignDB'] w3.eth.defaultAccount = w3.eth.accounts[0] Agreement = w3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin']) passphrase = getpass.getpass("Enter passphrase: ") w3.personal.unlockAccount(w3.eth.accounts[0], passphrase) w3.miner.start(4) tx_hash = Agreement.constructor().transact() print("\nTransaction hash: ", Web3.toHex(tx_hash)) print("Waiting for transaction to be mined...") tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash) print("Contract successfully deployed!!") print("Contract address: ", tx_receipt.contractAddress) with open('contract_address','w') as file: file.write(tx_receipt.contractAddress) return
def __init__( self, address: str, private_key: str, provider: str, gas: int, gas_price: Wei, ) -> None: self.address = Web3.toChecksumAddress(address) self.private_key = private_key self.provider = provider if self.provider.startswith('https://'): web3_provider = Web3.HTTPProvider(self.provider, request_kwargs={"timeout": 60}) elif self.provider.startswith('wss://'): web3_provider = Web3.WebsocketProvider(self.provider) elif self.provider.startswith('/'): web3_provider = Web3.IPCProvider(self.provider) else: raise PairswapError(f"Unknown provider type '{self.provider}'") self.conn = Web3(web3_provider) if not self.is_connected: raise PairswapError( f"Connection failed to provider '{self.provider}'") self.tx_gas = gas self.tx_gas_price = gas_price
def connectChain(ip): # w3 = Web3(HTTPProvider('https://mainnet.infura.io/ubiNQxis8bBSZFdQbnSs')) # w3 = Web3(HTTPProvider('http://%s:8545' % ip)) w3 = Web3(Web3.IPCProvider("\\\\.\\pipe\\geth.ipc")) print("Connected with latest block: ", w3.eth.blockNumber) print() return w3
def main(ctx, ipc_path, token_address, individuality_address): """ Main command """ web3 = Web3(Web3.IPCProvider(ipc_path=ipc_path)) wait = Wait(web3, timeout=600) token = web3.eth.contract( address=token_address, abi=DEVCON2_TOKEN['abi'], code=DEVCON2_TOKEN['code'], code_runtime=DEVCON2_TOKEN['code_runtime'], ) individuality = web3.eth.contract( address=individuality_address, abi=INDIVIDUALITY_ROOT['abi'], code=INDIVIDUALITY_ROOT['code'], code_runtime=INDIVIDUALITY_ROOT['code_runtime'], ) click.echo("Waiting for ipc connection: {0}".format( web3.currentProvider.ipc_path)) wait_for_ipc(web3.currentProvider.ipc_path) #click.echo("Waiting for account unlock: {0}".format(web3.eth.coinbase)) #wait.for_unlock() ctx.web3 = web3 ctx.wait = wait ctx.token = token ctx.individuality = individuality
def UpdateWillToOnchain(public_key, encrypt_data): print('==== UpdateWillToOnchain start ====') file_ipc = _GetChainConfig('Ethereum', 'file_ipc') w3 = Web3(Web3.IPCProvider(file_ipc)) contract_info = _GetContractInfo() contract_abi = contract_info['abi'] contract_address = contract_info['address'] contract_inst = w3.eth.contract(contract_address, abi=contract_abi, ContractFactoryClass=ConciseContract) tx_hash = contract_inst.Update(public_key, encrypt_data, transact={'from': w3.eth.accounts[0]}) tx_receipt = w3.eth.getTransactionReceipt(tx_hash) w3.miner.start(1) retry_time = 0 while not tx_receipt and retry_time < 10: print(' wait for miner!') time.sleep(MINER_WAIT_TIME) tx_receipt = w3.eth.getTransactionReceipt(tx_hash) retry_time += 1 w3.miner.stop() if not tx_receipt: raise IOError('still cannot get contract result') print(tx_receipt) print('==== UpdateWillToOnchain finish ====')
def run_query(ipc_path, db): provider = Web3.IPCProvider(ipc_path, timeout=60) w3 = Web3(provider) c = db.cursor() query = f""" SELECT address, SUM(c) s FROM ( SELECT creatorAddress AS address, COUNT(*) c FROM contractCreatedContract GROUP BY creatorAddress UNION SELECT txFrom AS address, COUNT(*) c FROM contract, contractTransaction WHERE transactionHash = hash GROUP BY txFrom ) subquery GROUP BY address ORDER BY s DESC LIMIT 100; """ c.execute(query) result = c.fetchone() while result != None: creator_balance = w3.eth.getBalance(Web3.toChecksumAddress(result[0])) created_contracts_balance = get_total_created_contracts_balance( db, w3, result[0]) print("{} (balance: {}) created {} contracts with total balance {}". format(result[0], creator_balance, result[1], created_contracts_balance), flush=True) result = c.fetchone()
def _StartMultipleDeployToChain(config_handler, contract_dict): file_ipc = config_handler.get_chain_config('Ethereum', 'file_ipc') w3 = Web3(Web3.IPCProvider(file_ipc)) contract_tx_hash = {} for contract_name, contract_inst in contract_dict.items(): tx_hash = contract_inst.transact({'from': w3.eth.accounts[0]}) contract_tx_hash[contract_name] = tx_hash tx_receipts = { contract_name: w3.eth.getTransactionReceipt(tx_hash) for contract_name, tx_hash in contract_tx_hash.items() } w3.miner.start(1) retry_time = 0 while None in tx_receipts.values() and retry_time < RETRY_TIME: print(' wait for miner!') time.sleep(2) tx_receipts = { contract_name: w3.eth.getTransactionReceipt(tx_hash) for contract_name, tx_hash in contract_tx_hash.items() } retry_time += 1 print("wait...") w3.miner.stop() if None in tx_receipts.values(): raise IOError('still cannot get contract result') return tx_receipts, w3.eth.accounts[0]
def __init__(self, contract_source="zk_transfer.sol", key_file="allkeys.json", zk_params_dir="librustzk", skip_zk=False): # setup w3 and accounts datadir = os.getenv('DATADIR') self.w3 = Web3(Web3.IPCProvider(datadir + '/gethdata/geth.ipc')) # self.w3 = Web3(Web3.IPCProvider( # '/root/src/chaindata/gethdata/geth.ipc')) self.load_and_unlock_accounts(key_file) self.old_account_index = 0 # the account who lost its key self.new_account_index = len(self.keys) - 1 # the new account self.limit = 100000000000000000000 self.friends_indexes = [1, 2] # list(range(1, self.new_account_index)) self.passphrase = "passphrase" self.threshold = 2 # compile and deploy contract print("compiling and deploying contract {}...".format(contract_source)) compiled_sol = self.compile_source_file(contract_source) _, contract_interface = compiled_sol.popitem() contract_address = self.deploy_contract(contract_interface, skip_zk) print("Deployed {} to: {}\n".format(contract_source, contract_address)) # setup ZkTransfer self.s = ZkTransfer( contract_address, contract_interface['abi'], self.w3, self.addr2keys, zk_params_dir, skip_zk)
def __init__(self, account, provider, address, abi_file): self.w3 = Web3(Web3.IPCProvider(provider)) with open(abi_file) as f: abi = f.read() self.contract = self.w3.eth.contract(address=address, abi=abi) self.w3.eth.defaultAccount = self.w3.eth.accounts[0] self.unlockAccount()
def run_query(ipc_path, user, password): provider = Web3.IPCProvider(ipc_path, timeout=60) w3 = Web3(provider) db = MySQLdb.connect(user=user, passwd=password, db="eth") threshold = 10 * eth_util.eth_in_wei total_number_of_tx = 0 for nr in range(6900000): if nr % 100000 == 0: print("Blocks {} - {}:".format(nr, nr + 99999), flush=True) txs = w3.eth.getBlock(nr).transactions for t in txs: t_data = w3.eth.getTransaction(t) if t_data.value >= threshold: total_number_of_tx += 1 if t_data.to != None: contract_creation_blocknumber = get_block_number_for_contract( db, t_data.to) if contract_creation_blocknumber != None: if contract_creation_blocknumber > nr: # contract was created _after_ more than 10ETH were transferred to it print( "{} ETH were transferred to {} on blocknumber {}, but contract was created at blocknumber {}" .format(t_data.value / eth_util.eth_in_wei, t_data.to, nr, contract_creation_blocknumber), flush=True) print("Total number of transactions with >= {} wei transferred: {}".format( threshold, total_number_of_tx))
def web3(ipc_file, geth_process): ipc_path = str(os.path.abspath(ipc_file)) wait_for_socket(ipc_path) # Injection of poa middleware w3 = Web3(Web3.IPCProvider(ipc_path)) w3.middleware_stack.inject(geth_poa_middleware, layer=0) return w3
def get_web3_client(network: str) -> Web3: """Returns instance of the Web3 client.""" network_config = NETWORKS[network] endpoint = network_config["KEEPER_ETH1_ENDPOINT"] # Prefer WS over HTTP if endpoint.startswith("ws"): w3 = Web3(Web3.WebsocketProvider(endpoint, websocket_timeout=60)) logger.warning(f"[{network}] Web3 websocket endpoint={endpoint}") elif endpoint.startswith("http"): w3 = Web3(Web3.HTTPProvider(endpoint)) logger.warning(f"[{network}] Web3 HTTP endpoint={endpoint}") else: w3 = Web3(Web3.IPCProvider(endpoint)) logger.warning(f"[{network}] Web3 HTTP endpoint={endpoint}") if network_config["IS_POA"]: w3.middleware_onion.inject(geth_poa_middleware, layer=0) logger.warning(f"[{network}] Injected POA middleware") account = w3.eth.account.from_key(network_config["ORACLE_PRIVATE_KEY"]) w3.middleware_onion.add(construct_sign_and_send_raw_middleware(account)) logger.warning( f"[{network}] Injected middleware for capturing transactions and sending as raw" ) w3.eth.default_account = account.address logger.info(f"[{network}] Configured default account {w3.eth.default_account}") return w3
def server(self): if self._server is None: ipc = Web3.IPCProvider( os.path.join(self.datastore_path, 'jsonrpc.ipc')) self._server = Web3(ipc) return self._server
def __init__(self,ipc_socket,redis_handle,signing_key = None): print(ipc_socket) provider = Web3.IPCProvider(ipc_socket) self.w3 = Web3(provider) self.w3.middleware_onion.inject(geth_poa_middleware, layer=0) assert(self.w3.isConnected()) self.signing_key = signing_key self.redis_handle = redis_handle
def run_query(ipc_path, user, password): provider = Web3.IPCProvider(ipc_path, timeout=60) w3 = Web3(provider) db = MySQLdb.connect(user=user, passwd=password, db="eth") generation = 1 while not is_generation_empty(db, generation): run_query_for_generation(db, w3, generation) generation += 1
def __init__(self): # self.w3 = Web3(HTTPProvider('http://localhost:8545')) #use rpc threading.Thread.__init__(self) self.w3 = Web3(Web3.IPCProvider("~/.ethereum/geth.ipc")) #use ipc self.w3.middleware_stack.inject(geth_poa_middleware, layer=0) self.w3.isConnected(), 'connect fail 请打开geth' self.ourAddress = self.w3.toChecksumAddress( "0x4ffa4508e02cc585f5ea209967039ba345effc88") self.key = '0xda17f8c80a071f1fc8c84df601f4c9f0e1d864e7961d93c6b4245b9246874f17'
def main_script(): uri = "bolt://localhost:7687" driver = GraphDatabase.driver(uri, auth=("neo4j", "1234")) web3 = Web3(Web3.IPCProvider('\\\\.\\pipe\\geth.ipc')) def get_computed_fields_from_neo4j(tx): counter = 0 start = time.time() for record in tx.run("match (out)-[r:SENT]-(receiver)\ SET r.value = toFloat(r.value)\ WITH out.AccountID as ACCOUNT_ID, out=endNode(r) as IsReceiving, count(r) as count, r.value as value, r.timestamp as timeOfTransaction \ WITH ACCOUNT_ID, \ SUM(CASE WHEN IsReceiving THEN count ELSE NULL END) as Received_Transactions,\ SUM(CASE WHEN NOT IsReceiving THEN count ELSE NULL END) as Sent_Transactions,\ SUM(CASE WHEN IsReceiving THEN value/1000000000000000000 ELSE 0 END) as Total_Ether_Received,\ SUM(CASE WHEN NOT IsReceiving THEN value/1000000000000000000 ELSE 0 END) as Total_Ether_Sent,\ COLLECT(CASE WHEN IsReceiving THEN value/1000000000000000000 ELSE 0 END) as LIST_OF_ETHER_RECEIVED,\ COLLECT(CASE WHEN NOT IsReceiving THEN value/1000000000000000000 ELSE 0 END) as LIST_OF_ETHER_SENT,\ COLLECT(CASE WHEN IsReceiving THEN timeOfTransaction ELSE 0 END) as LIST_OF_TRANSACTION_TIMES \ UNWIND LIST_OF_ETHER_RECEIVED as ETHER_TRANSACTIONS_RECEIVED \ UNWIND LIST_OF_ETHER_SENT as ETHER_TRANSACTIONS_SENT \ UNWIND LIST_OF_TRANSACTION_TIMES as TRANSACTION_TIMESTAMP_SENT \ WITH ACCOUNT_ID, \ Received_Transactions,\ Sent_Transactions,\ Total_Ether_Received, \ Total_Ether_Sent,\ Total_Ether_Received - Total_Ether_Sent as Resultant_Transaction_Balance,\ CASE WHEN Total_Ether_Received = 0 THEN Total_Ether_Sent*100 Else Total_Ether_Sent/Total_Ether_Received*100 END as Sent_to_Received_Ether_ratio,\ CASE WHEN Received_Transactions = 0 THEN 0 ELSE Total_Ether_Received/Received_Transactions END AS Avg_received_Ether,\ CASE WHEN Sent_Transactions = 0 THEN 0 ELSE Total_Ether_Sent/Sent_Transactions END AS Avg_sent_Ether,\ LIST_OF_ETHER_RECEIVED, \ MIN(ETHER_TRANSACTIONS_RECEIVED) as MIN_RECEIVED_TRANSACTION,\ MAX(ETHER_TRANSACTIONS_RECEIVED) as MAX_RECEIVED_TRANSACTION,\ LIST_OF_ETHER_SENT,\ MIN(ETHER_TRANSACTIONS_SENT) as MIN_SENT_TRANSACTION,\ MAX(ETHER_TRANSACTIONS_SENT) as MAX_SENT_TRANSACTION \ RETURN ACCOUNT_ID,\ Received_Transactions, \ Sent_Transactions, \ Total_Ether_Received,\ Total_Ether_Sent, \ Sent_to_Received_Ether_ratio,\ Resultant_Transaction_Balance,\ Avg_received_Ether,\ Avg_sent_Ether, \ MIN_RECEIVED_TRANSACTION,\ MAX_RECEIVED_TRANSACTION,\ MIN_SENT_TRANSACTION, \ MAX_SENT_TRANSACTION"): counter = counter + 1 end = time.time() print("Total time", end - start, " Total Number of accounts on which stats generated: ", counter) with driver.session() as session: session.read_transaction(get_computed_fields_from_neo4j)
def connectEth(): # Setup out connection to the Ethereum network using IPC. # Note that this assumes we are running on the same system as a network node! # Inject PoA compatibility middleware so that this will work on the Geth network w3 = Web3(Web3.IPCProvider()) w3.middleware_onion.inject(geth_poa_middleware, layer=0) # The message sender is always the institution w3.eth.defaultAccount = INSTITUTION_ADDR return w3
def connect(ipc_path): my_provider = Web3.IPCProvider(ipc_path) w3 = Web3(my_provider) if(w3.isConnected()): print("Connected to geth!") return w3 else: print("Couldn't connect to geth") quit()
def get_web3_provider(endpoint): if endpoint.startswith('ws'): return Web3.WebsocketProvider(endpoint) elif endpoint.startswith('http'): return Web3.HTTPProvider(endpoint) elif endpoint.startswith('ipc'): return Web3.IPCProvider(endpoint) else: raise Exception('Unsupported JSON-RPC endpoint')
def web3_provider(address: str) -> Web3: try: if address.startswith('http'): # HTTP return Web3(Web3.HTTPProvider(address)) if address.startswith('ws'): # WebSocket return Web3(Web3.WebsocketProvider(address)) return Web3(Web3.IPCProvider(address)) except FileNotFoundError: raise ValueError( "Failed to initialize web3 provider (is eth_node set?)") from None
def __init__(self, rpchost='', ipcpath=''): if ipcpath != '': log.debug("using ipc: %s", ipcpath) self.provider = Web3.IPCProvider(os.path.expanduser(ipcpath)) elif rpchost != '': log.debug("using httprpc: %s", rpchost) self.provider = Web3.HTTPProvider(rpchost) else: raise Exception("need either ipc or http") self.w3 = Web3(self.provider)
def __init__(self, rpchost='', ipcpath=''): if rpchost != '': log.info("using httprpc: %s", rpchost) self.providers.append(Web3.HTTPProvider(rpchost)) if ipcpath != '': log.info("using ipc: %s", ipcpath) self.providers.append(Web3.IPCProvider( os.path.expanduser(ipcpath))) if len(self.providers) == 0: raise Exception("need either ipc or http or both") self.w3 = Web3(self.providers)
def get_eth_provider(provider_name): # open a connection to a local ethereum node (ganache geth or simple in memory blockchain) if provider_name == "in_memory_test_rpc": return Web3.TestRPCProvider() eth_providers = { 'local': HTTPProvider('http://localhost:9545'), 'rinkeby': Web3.IPCProvider(settings.RINKEBY_SOCKET_FILE_PATH), } return eth_providers[provider_name]
def connect_w3_custom_ipc(self, path=None): self.cleanout_w3() from web3 import Web3 if not path: path = self.config.ipc_path w3 = Web3(Web3.IPCProvider(path)) if self.is_connected_with(w3, 'Custom IPC', 3): self.config.ipc_path = path self.config.default_method = self.connect_w3_custom_ipc.__name__ return True return False
def __init__(self, ipc_file, contract_address, contract_json, address, keyfile, passwordfile, explorer_format): self.address = Web3.toChecksumAddress(address) self.explorer_format = explorer_format self.client = Web3(Web3.IPCProvider(ipc_file)) self.private_key = self._load_private_key(keyfile, passwordfile) abi = self._generate_contract_abi(contract_json) self.contract = self._load_contract(abi, contract_address)
def connect(self, ipc): try: w3 = Web3(Web3.IPCProvider(ipc)) w3.middleware_onion.inject(web3.middleware.geth_poa_middleware, layer=0) if not w3.isConnected(): raise Exception except: print(f"Could not connect to IPC '{ipc}'") sys.exit(1) return w3