Ejemplo n.º 1
0
    def __missing__(self, key):
        config: Web3Config = settings.WEB3_CONFIG[key]

        if key not in settings.WEB3_CONFIG:  # pragma: no cover
            raise ImproperlyConfigured(f"Requested Web3 provider {key} but \
            is missing in settings.WEB3_CONFIG")
        if settings.TEST:
            provider = Web3(Web3.EthereumTesterProvider())
        else:  # pragma: no cover
            provider = Web3(Web3.HTTPProvider(config.endpoint))

        escrow_contract_abi = json.loads(contracts.escrow_contract_abi)
        oracle_contract_abi = json.loads(contracts.oracle_contract_abi)
        contract_instance = provider.eth.contract(
            abi=escrow_contract_abi, address=config.escrow_address)
        admin_contract_instance = provider.eth.contract(
            abi=oracle_contract_abi, address=config.oracle_address)
        w3_provider = Web3Provider(web3=provider,
                                   contract=contract_instance,
                                   admin_contract=admin_contract_instance,
                                   currency=config.currency,
                                   chain_id=config.chain_id,
                                   public_key=config.public_key,
                                   private_key=config.private_key,
                                   default_gas_fee=config.default_gas_fee)
        self[key] = w3_provider
        return w3_provider
Ejemplo n.º 2
0
 def _create_w3_instance(self) -> Web3:
     genesis_overrides = {'gas_limit': int(max_gas_limit * 1.2)}
     custom_genesis_params = PyEVMBackend._generate_genesis_params(
         overrides=genesis_overrides)
     self.eth_tester = EthereumTester(backend=PyEVMBackend(
         genesis_parameters=custom_genesis_params))
     w3 = Web3(Web3.EthereumTesterProvider(self.eth_tester))
     return w3
Ejemplo n.º 3
0
def setup_w3():
    genesis_overrides = {"gas_limit": 5500000}
    custom_genesis_params = PyEVMBackend._generate_genesis_params(
        overrides=genesis_overrides)
    pyevm_backend = PyEVMBackend(genesis_parameters=custom_genesis_params)
    t = EthereumTester(backend=pyevm_backend)
    w3 = Web3(Web3.EthereumTesterProvider(ethereum_tester=t))
    w3.eth.defaultAccount = w3.eth.accounts[0]
    w3.eth.defaultContractFactory = LinkableContract
    w3.enable_unstable_package_management_api()
    return w3
Ejemplo n.º 4
0
def test_update_web3(deployed_safe_math, w3):
    new_w3 = Web3(Web3.EthereumTesterProvider())
    original_package, _ = deployed_safe_math
    assert original_package.w3 is w3
    new_package = original_package.update_w3(new_w3)
    assert new_package.w3 is new_w3
    assert original_package is not new_package
    assert original_package.manifest == new_package.manifest
    with pytest.raises(EthPMValidationError,
                       match="Package has no matching URIs on chain."):
        new_package.deployments
Ejemplo n.º 5
0
    def setUp(self):
        super(TestPoseidonEvm, self).setUp()
        w3 = Web3(Web3.EthereumTesterProvider())

        bytecode = poseidon_contract()
        abi = poseidon_abi()

        PoseidonContract = w3.eth.contract(abi=abi, bytecode=bytecode)
        tx_hash = PoseidonContract.constructor().transact()
        tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
        self.contract = w3.eth.contract(address=tx_receipt.contractAddress,
                                        abi=abi)
Ejemplo n.º 6
0
def w3() -> Web3:
    w3 = Web3(Web3.EthereumTesterProvider())
    return w3
Ejemplo n.º 7
0
def connect_via_test(node_index: int):
    return Web3(Web3.EthereumTesterProvider())
Ejemplo n.º 8
0
    function setGreeting(string _greeting) public {
        greeting = _greeting;
    }

    function greet() view public returns (string) {
        return greeting;
    }
}
'''

#compiled_sol = compile_source(contract_source_code) # Compiled source code
#contract_interface = compiled_sol['<stdin>:Greeter']

# web3.py instance
w3 = Web3(Web3.EthereumTesterProvider())

# set pre-funded account as sender
w3.eth.defaultAccount = w3.eth.accounts[0]

# Instantiate and deploy contract
# Greeter = w3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin'])

# Submit the transaction that deploys the contract
# tx_hash = Greeter.constructor().transact()

# Wait for the transaction to be mined, and get the transaction receipt
# tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)

# Create the contract instance with the newly-deployed address
test_address = "0xF2E246BB76DF876Cef8b38ae84130F4F55De395b"
Ejemplo n.º 9
0
def connect_via_test():
    return Web3(Web3.EthereumTesterProvider())
def fresh_w3():
    w3 = Web3(Web3.EthereumTesterProvider())
    w3.eth.defaultAccount = w3.eth.accounts[0]
    w3.eth.defaultContractFactory = LinkableContract
    w3.enable_unstable_package_management_api()
    return w3
Ejemplo n.º 11
0
def test_provider():
    return Web3.EthereumTesterProvider()
Ejemplo n.º 12
0
 def __init__(self):
     w3 = Web3(Web3.EthereumTesterProvider())
     self.eth = w3.eth
Ejemplo n.º 13
0
def web3():
    _web3 = Web3(Web3.EthereumTesterProvider())
    return _web3
Ejemplo n.º 14
0
def w3():
    w3 = Web3(Web3.EthereumTesterProvider())
    w3.eth.defaultAccount = w3.eth.accounts[0]
    return w3
Ejemplo n.º 15
0
 def setUp(self):
     super(TestMiMCEvm, self).setUp()
     w3 = Web3(Web3.EthereumTesterProvider())
     self.contract_e7 = self._deploy_contract(w3, 7)
     self.contract_e5 = self._deploy_contract(w3, 5)
Ejemplo n.º 16
0
def w3(tester):
    w3 = Web3(Web3.EthereumTesterProvider(tester))
    w3.eth.setGasPriceStrategy(lambda web3, params: 0)  # pylint: disable=no-member
    w3.eth.defaultAccount = w3.eth.accounts[0]  # pylint: disable=no-member
    return w3
Ejemplo n.º 17
0
def w3():
    return Web3(Web3.EthereumTesterProvider())
def create_contract(location: str, month: int, precipitation: Optional[float],
                    address: str):

    compiled_sol = compile_standard({
        'language': 'Solidity',
        'sources': {
            'contract.sol': {
                'content':
                '''
                pragma solidity ^ 0.5.2;

    contract WeatherContract {
        string private location;
        address public owner;
        int public mmPrecipitation = 0;
        uint public avgPrecipitation = 0;
        uint public month;

    constructor(string memory _location, uint _month, uint _avgPrecipitation, address _owner) public {
        location = _location;
        month = _month;
        owner = _owner;
        avgPrecipitation = _avgPrecipitation;
        }

    function getStartingMonth() public view returns(uint) {
        return month;
        }

    function setPrecipitation(int _millimeters) public {
        mmPrecipitation = _millimeters;
        }

    function getAvgPrecipitation() public view returns(uint) {
        require(msg.sender == owner);
        return avgPrecipitation;
        }

    function getLocation() public view returns(string memory) {
        require(msg.sender == owner);
        return location;
        }

    function setLocation() public view returns(string memory) {
        require(msg.sender == owner);
        return location;
        }
    }
            '''
            }
        },
        'settings': {
            'outputSelection': {
                '*': {
                    '*':
                    ['metadata', 'evm.bytecode', 'evm.bytecode.sourceMap']
                }
            }
        }
    })

    w3 = Web3(Web3.EthereumTesterProvider())
    # w3.eth.defaultAccount = w3.eth.accounts[0]
    all_accounts = w3.eth.accounts

    bytecode = compiled_sol['contracts']['contract.sol']['WeatherContract'][
        'evm']['bytecode']['object']
    abi = json.loads(compiled_sol['contracts']['contract.sol']
                     ['WeatherContract']['metadata'])['output']['abi']

    contract = w3.eth.contract(abi=abi, bytecode=bytecode)
    tx_hash = contract.constructor(location, month, int(precipitation),
                                   address).transact()
    tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
    weather_contract = w3.eth.contract(address=tx_receipt.contractAddress,
                                       abi=abi)
    month_name = get_month(month)

    congrats = (
        f'Congrats, you just created a new contract that grants you protection for your crops in {location}, '
        f'starting from {month_name}; average precipitation for this month are '
    )

    if precipitation is None or pd.isnull(precipitation):
        precip = 'not available.\n\n'
    else:
        precip = f'{precipitation} mm.\n\n'

    explic = (
        'As soon as the evaluation period will be over, the index will be evaluated and payments to either you '
        'or the investor who subsidized your contract will be sent out automatically.'
    )
    technical_info = (
        f'\n\nIn the meanwhile, please note that your address is {tx_receipt.contractAddress}. Store it '
        f'in a safe location. For this transaction, you used {tx_receipt.gasUsed} gas, and your '
        f'overall gas consumption amounts to {tx_receipt.cumulativeGasUsed}.')
    greet = '\n\nSee you next time!'
    utterance = congrats + precip + explic + technical_info + greet

    return tx_hash, tx_receipt, weather_contract, all_accounts, utterance
def create_new_address():
    w3 = Web3(Web3.EthereumTesterProvider())
    n = random.randint(0, len(w3.eth.accounts) - 1)
    address = w3.eth.accounts[n]
    return address
Ejemplo n.º 20
0
 def __init__(self):
     w3 = Web3(Web3.EthereumTesterProvider())
     self.eth = w3.eth
     self.roulette = self.contract()
Ejemplo n.º 21
0
def test_set_default_web3(all_manifests, w3):
    new_w3 = Web3(Web3.EthereumTesterProvider())
    current_package = Package(all_manifests, w3)
    assert current_package.w3 is w3
    current_package.set_default_w3(new_w3)
    assert current_package.w3 is new_w3
Ejemplo n.º 22
0
def w3(tester):
    w3 = Web3(Web3.EthereumTesterProvider(tester))
    w3.eth.setGasPriceStrategy(lambda web3, params: 0)
    w3.eth.defaultAccount = w3.eth.accounts[0]
    return w3
Ejemplo n.º 23
0
def w3_for_test(eth_tester):
    return Web3(Web3.EthereumTesterProvider(eth_tester))