def _get_rinkeby_w3(): logging.debug('Getting rinkeby w3 through infura') w3 = Web3(HTTPProvider( f'https://rinkeby.infura.io/v3/{settings.get_infura_api_key()}')) w3.middleware_stack.inject(geth_poa_middleware, layer=0) return w3
def exception_retry_request_setup(): web3 = Mock() provider = HTTPProvider() errors = (ConnectionError, HTTPError, Timeout, TooManyRedirects) setup = exception_retry_middleware(provider.make_request, web3, errors, 5) setup.web3 = web3 return setup
def main(contract_file, get_shell=False): abi, bytecode = create_deploy_bytecode(contract_file) w3 = Web3(HTTPProvider(RPC_URL)) acct = w3.eth.account.privateKeyToAccount(PRIVATE_KEY) contract_ = w3.eth.contract(abi=abi, bytecode=bytecode) construct_txn = contract_.constructor().buildTransaction({ 'from': acct.address, 'nonce': w3.eth.getTransactionCount(acct.address), 'gas': 2000000, 'gasPrice': w3.toWei('1', 'gwei') }) # Deploy print(f"Account address: {acct.address}") receipt = None signed = acct.signTransaction(construct_txn) tx_hash = w3.eth.sendRawTransaction(signed.rawTransaction) print(f'Deployed Transaction: {tx_hash.hex()}') w3.eth.waitForTransactionReceipt(tx_hash) print('Mined transaction') receipt = w3.eth.waitForTransactionReceipt(tx_hash) print(f'Contract Address: {receipt["contractAddress"]}') print(f"Success?: {receipt['status'] == 1}") print(receipt) if get_shell: import pdb pdb.set_trace()
def run(method, address, game_id): # Web3 INFURA_PROJECT_ID = os.environ['INFURA_PROJECT_ID'] INFURA_PROJECT_SECRET = os.environ['INFURA_PROJECT_SECRET'] headers = {"auth": ("", INFURA_PROJECT_SECRET)} uri = 'https://ropsten.infura.io/v3/%s' % (INFURA_PROJECT_ID) web3 = Web3(HTTPProvider(uri, headers)) # Owner owner = Account.from_key(os.environ['PRIVATE_KEY_0x2048']) # ABI filepath = 'bin/combined.json' with open(filepath) as f: compiled_artifacts = json.load(f) data = compiled_artifacts["contracts"] contract_data = data["solidity/ArcadeProtocol.sol:ArcadeProtocol"] abi = contract_data["abi"] bytecode = contract_data["bin"] # Run interactor = ContractInteractor(web3) if method == 'deploy': interactor.deploy(abi, bytecode, from_addr=owner) elif method == 'add_game': price = 100000000000000 # 0.0001 ETH fee = 1 interactor.set_contract(abi, address) interactor.add_game(game_id, price, fee, from_addr=owner)
def get_web3(network: str = config["network"], provider: str = config["provider"]) -> Web3: """ Get XinFin Web3 instance. :param network: XinFin network, defaults to ``mainnet``. :type network: str :param provider: XinFin network provider, defaults to ``http``. :type provider: str :returns: Web3 -- XinFin Web3 instance. >>> from swap.providers.xinfin.rpc import get_web3 >>> get_web3(network="testnet", provider="http") <web3.main.Web3 object at 0x000001DDECCD0640> """ # Check parameter instances if not is_network(network=network): raise NetworkError( f"Invalid XinFin '{network}' network", "choose only 'mainnet', 'apothem' or 'testnet' networks.") if provider == "http": web3: Web3 = Web3( HTTPProvider(endpoint_uri=URI(config[network]["http"]), request_kwargs={"timeout": config["timeout"]})) return web3 elif provider == "websocket": web3: Web3 = Web3( WebsocketProvider(endpoint_uri=URI(config[network]["websocket"]))) return web3 else: raise ValueError(f"Invalid XinFin '{provider}' provider", "choose only 'http' or 'websocket' providers.")
def move(payload): errors = MovePayload().validate(payload) if errors: raise MovePayloadValidationError # Validate session sessions = redis.Redis.from_url(REDIS_URL) session_id = payload['session_id'] serialized_session = sessions.get(session_id) or '{}' session = json.loads(serialized_session) errors = PaidSession().validate(session) if errors: raise PaidSessionValidationError # Arcade protocol user = session['user'] INFURA_PROJECT_ID = os.environ['INFURA_PROJECT_ID'] INFURA_PROJECT_SECRET = os.environ['INFURA_PROJECT_SECRET'] headers = {"auth": ("", INFURA_PROJECT_SECRET)} uri = 'https://ropsten.infura.io/v3/%s' % (INFURA_PROJECT_ID) provider = HTTPProvider(uri, headers) contract = arcade_protocol.Contract( provider, ADDRESS, ABI, TwentyFortyEight.id, ) game_interface = arcade_protocol.GameInterface( contract, player=user, ) # Move state = session['gamestate'] direction = payload['direction'] next_state = TwentyFortyEight.update(state, direction) signed_score = game_interface.sign(KEY, next_state['score']) # Set session session_data = { 'paid': True, 'gamestate': next_state, 'user': user, } sessions.set(session_id, json.dumps(session_data)) # Set payload payload = { 'board': next_state['board'], 'score': next_state['score'], 'gameover': next_state['gameover'], 'signed_score': signed_score, } return json.dumps(payload)
def get_web3(network: str = config["network"], provider: str = config["provider"], token: Optional[str] = None) -> Web3: """ Get Ethereum Web3 instance. :param network: Ethereum network, defaults to ``mainnet``. :type network: str :param provider: Ethereum network provider, defaults to ``http``. :type provider: str :param token: Infura API endpoint token, defaults to ``4414fea5f7454211956b1627621450b4``. :type token: str :returns: Web3 -- Ethereum Web3 instance. >>> from swap.providers.ethereum.rpc import get_web3 >>> get_web3(network="testnet", provider="http", token="infura endpoint token ...") <web3.main.Web3 object at 0x000001DDECCD0640> """ # Check parameter instances if not is_network(network=network): raise NetworkError(f"Invalid Ethereum '{network}' network", "choose only 'mainnet', 'ropsten', 'kovan', 'rinkeby' or 'testnet' networks.") endpoint: str = "ganache-cli" if network == "testnet" else "infura" token: str = token if token else config[network][endpoint]["token"] if provider == "http": web3: Web3 = Web3(HTTPProvider( URI( f"{config[network]['infura']['http']}/{token}" if token else config[network][endpoint]["http"] ), request_kwargs={ "timeout": config["timeout"] } ) ) return web3 elif provider == "websocket": web3: Web3 = Web3(WebsocketProvider( URI( f"{config[network]['infura']['websocket']}/{token}" if token else config[network][endpoint]["websocket"] ) ) ) return web3 else: raise ValueError(f"Invalid Ethereum '{provider}' provider", "choose only 'http' or 'websocket' providers.")
def load_provider_from_uri(uri_string, headers=None): uri = urlparse(uri_string) if uri.scheme == 'file': return IPCProvider(uri.path) elif uri.scheme in HTTP_SCHEMES: return HTTPProvider(uri_string, headers) elif uri.scheme in WS_SCHEMES: return WebsocketProvider(uri_string) else: raise NotImplementedError( 'Web3 does not know how to connect to scheme %r in %r' % ( uri.scheme, uri_string, ))
def load_provider_from_uri( uri_string: URI, headers: Optional[Dict[str, Tuple[str, str]]] = None) -> BaseProvider: uri = urlparse(uri_string) if uri.scheme == 'file': return IPCProvider(uri.path) elif uri.scheme in HTTP_SCHEMES: return HTTPProvider(uri_string, headers) elif uri.scheme in WS_SCHEMES: return WebsocketProvider(uri_string) else: raise NotImplementedError( f'Web3 does not know how to connect to scheme {uri.scheme!r} in {uri_string!r}' )
def test_user_provided_session(): adapter = HTTPAdapter(pool_connections=20, pool_maxsize=20) session = Session() session.mount('http://', adapter) session.mount('https://', adapter) provider = HTTPProvider(endpoint_uri=URI, session=session) web3 = Web3(provider) assert web3.manager.provider == provider session = request._get_session(URI) adapter = session.get_adapter(URI) assert isinstance(adapter, HTTPAdapter) assert adapter._pool_connections == 20 assert adapter._pool_maxsize == 20
def test_retries(make_post_request_mock): # use short backoff times to make the test run quickly def quick_retry_middleware(make_request, web3): return http_retry_with_backoff_middleware( make_request, web3, retries=5, first_backoff=0.01, ) provider = HTTPProvider() provider.middlewares.replace( 'http_retry_request', quick_retry_middleware, ) w3 = web3.Web3(provider) # log the time since start each time the mock is called start_time = time() retry_times = [] def side_effect(*args, **kwargs): retry_times.append(time() - start_time) raise ConnectionError make_post_request_mock.side_effect = side_effect # the call must fail after the number of retries is exceeded with pytest.raises(ConnectionError): w3.eth.blockNumber() # check timings assert make_post_request_mock.call_count == 5 expected_times = [ 0, 0.01, 0.01 + 0.02, 0.01 + 0.02 + 0.04, 0.01 + 0.02 + 0.04 + 0.08, ] assert retry_times == pytest.approx(expected_times, abs=0.002, rel=0.3) # try again to make sure that each request starts with a clean backoff start_time = time() retry_times = [] with pytest.raises(ConnectionError): w3.eth.blockNumber() assert retry_times == pytest.approx(expected_times, abs=0.002, rel=0.3)
def load_provider_from_environment(): uri_string = os.environ.get('WEB3_PROVIDER_URI', '') if not uri_string: return None uri = urlparse(uri_string) if uri.scheme == 'file': return IPCProvider(uri.path) elif uri.scheme == 'http': return HTTPProvider(uri_string) else: raise NotImplementedError( 'Web3 does not know how to connect to scheme %r in %r' % ( uri.scheme, uri_string, ))
def test_retries(make_post_request_mock): # use short backoff times to make the test run quickly def quick_retry_middleware(make_request, web3): return http_retry_with_backoff_middleware(make_request, web3, retries=5, first_backoff=0.01) provider = HTTPProvider() provider.middlewares.replace("http_retry_request", quick_retry_middleware) # type: ignore web3 = Web3(provider) # log the time since start each time the mock is called start_time = time() retry_times = [] def side_effect(*_args, **_kwargs): retry_times.append(time() - start_time) raise requests.exceptions.ConnectionError make_post_request_mock.side_effect = side_effect # the call must fail after the number of retries is exceeded with pytest.raises(requests.exceptions.ConnectionError): web3.eth.blockNumber # pylint: disable=pointless-statement # check timings assert make_post_request_mock.call_count == 5 expected_times = [ 0, 0.01, 0.01 + 0.02, 0.01 + 0.02 + 0.04, 0.01 + 0.02 + 0.04 + 0.08 ] assert retry_times == pytest.approx(expected_times, abs=0.006, rel=0.3) # try again to make sure that each request starts with a clean backoff start_time = time() retry_times = [] with pytest.raises(requests.exceptions.ConnectionError): web3.eth.blockNumber # pylint: disable=pointless-statement assert retry_times == pytest.approx(expected_times, abs=0.006, rel=0.3)
def __init__(self, key_file=None, private_key=None, account='0x0000000000000000000000000000000000000000', password=None, auth_type=None, node_url=None): """ Devise constructor :param key_file: An encrypted json keystore file, requires a password to decrypt :param private_key: An Ethereum private key to use to sign messages and smart contract transactions :param account: The Ethereum address to use to sign messages and smart contract transactions :param password: The password to decrypt the encrypted key store file. :param auth_type: One of "ledger", or "trezor", "key_file", "private_key", "software". If auth_type is None (the default), we will attempt to use each signing methods until one is found in the order: hardware, private_key, key_file, then software. Note: the software option will attempt the locate the account specified in the local Ethereum Wallet path for the current user. :param node_url: An Ethereum node to connect to """ assert key_file or private_key or account, "Please specify one of: account, key_file or private_key!" assert not ( key_file and private_key ), "Please specify either key_file or private_key, not both!" # logging self.logger = logging.getLogger(__name__) self.logger.setLevel(logging.INFO) self.logger.handlers.clear() self.logger.addHandler(logging.StreamHandler()) # Initialize credentials for transaction and message signing self._init_credentials(key_file, private_key, account, password, auth_type) # Connect to node url if node_url is None: node_url = get_default_node_url() provider = HTTPProvider(node_url) self.w3 = Web3(provider) self._network_id = self._get_network_id() network = NODE_TO_NETWORK.get(node_url, "CUSTOM") if network == "MAINNET": self.logger.info( "!!!!!! WARNING: CONNECTED TO THE MAIN ETHEREUM NETWORK. " "ALL TRANSACTIONS ARE FINAL. !!!!!") elif network == "CUSTOM": self.logger.info( "WARNING: Connected to the Ethereum network corresponding " "to your chosen node (%s)! All transactions are final!" % node_url) else: self.logger.info("INFO: Connected to the %s Ethereum network." % network) # inject the poa compatibility middleware to the innermost layer self.w3.middleware_stack.inject(geth_poa_middleware, layer=0) # Automatically determine necessary gas based on 5min to mine avg time self.w3.eth.setGasPriceStrategy(medium_gas_price_strategy)
import os import sys import time from web3.providers import HTTPProvider from web3 import Web3 from eth_account import Account from migrate.legacy_rep import LEGACY_REP_ADDRESS, LEGACY_REP_ABI from migrate.rep import REP_ADDRESS, REP_ABI CHAIN_ID = 103 GAS_PRICE_IN_GWEI = '5' w3 = Web3(HTTPProvider()) legacyReputationToken = w3.eth.contract(address=LEGACY_REP_ADDRESS, abi=LEGACY_REP_ABI) reputationToken = w3.eth.contract(address=REP_ADDRESS, abi=REP_ABI) def migrate(): private_key = os.environ.get('ETHEREUM_PRIVATE_KEY') if not private_key: print("No value found for required environment variable ETHEREUM_PRIVATE_KEY") sys.exit() account = Account.privateKeyToAccount(private_key) address = account.address nonce = w3.eth.getTransactionCount(address) v1_balance = legacyReputationToken.functions.balanceOf(address).call()
import os import arcade_protocol from game.game import ( TwentyFortyEight, ) from web3.providers import ( HTTPProvider, ) from webserver.config import ( ADDRESS, ABI, ) infura_project_id = os.environ['INFURA_PROJECT_ID'] infura_project_secret = os.environ['INFURA_PROJECT_SECRET'] headers = {"auth": ("", infura_project_secret)} uri = 'https://ropsten.infura.io/v3/%s' % (infura_project_id) provider = HTTPProvider(uri, headers) game_id = TwentyFortyEight.id arcade_contract = lambda: arcade_protocol.Contract( provider, ADDRESS, ABI, game_id, )
import os import sys import time from web3.providers import HTTPProvider from web3 import Web3 from eth_account import Account from migrate.legacy_rep import LEGACY_REP_ADDRESS, LEGACY_REP_ABI from migrate.rep import REP_ADDRESS, REP_ABI w3 = Web3(HTTPProvider(os.environ.get('ETHEREUM_HTTP'))) legacyReputationToken = w3.eth.contract(address=LEGACY_REP_ADDRESS, abi=LEGACY_REP_ABI) reputationToken = w3.eth.contract(address=REP_ADDRESS, abi=REP_ABI) def migrate(): private_key = os.environ.get('ETHEREUM_PRIVATE_KEY') if not private_key: print( "No value found for required environment variable ETHEREUM_PRIVATE_KEY" ) sys.exit() account = Account.privateKeyToAccount(private_key) address = account.address nonce = w3.eth.getTransactionCount(address)
def __init__(self, providers): self.providers = [ HTTPProvider(provider) for provider in providers.split(",") ]
def test_is_strictly_default_http_middleware(): web3 = HTTPProvider() assert 'http_retry_request' in web3.middlewares web3 = IPCProvider() assert 'http_retry_request' not in web3.middlewares
def _get_ropsten_w3(): logging.debug('Getting ropsten w3 through infura') return Web3(HTTPProvider( f'https://ropsten.infura.io/v3/{settings.get_infura_api_key()}'))
def test_no_args(): provider = HTTPProvider() web3 = Web3(provider) assert web3.manager.provider == provider
def _get_parity_w3(): logging.debug('Getting parity w3') return Web3(HTTPProvider(settings.parity_endpoint()))
def test_check_with_all_middlewares(make_post_request_mock): provider = HTTPProvider() w3 = web3.Web3(provider) with pytest.raises(ConnectionError): w3.eth.blockNumber() assert make_post_request_mock.call_count == 5
def test_init_kwargs(): provider = HTTPProvider(endpoint_uri=URI, request_kwargs={'timeout': 60}) web3 = Web3(provider) assert web3.manager.provider == provider