本文整理汇总了Python中eth_tester.EthereumTester方法的典型用法代码示例。如果您正苦于以下问题:Python eth_tester.EthereumTester方法的具体用法?Python eth_tester.EthereumTester怎么用?Python eth_tester.EthereumTester使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类eth_tester
的用法示例。
在下文中一共展示了eth_tester.EthereumTester方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_freeze_balance
# 需要导入模块: import eth_tester [as 别名]
# 或者: from eth_tester import EthereumTester [as 别名]
def test_freeze_balance(web3, chain):
stable_coin, _ = chain.provider.get_or_deploy_contract('StableCoin')
t = eth_tester.EthereumTester()
account2 = t.get_accounts()[1]
account3 = t.get_accounts()[2]
set_txn_hash = stable_coin.functions.transfer(account2, 10).transact()
chain.wait.for_receipt(set_txn_hash)
set_txn_hash = stable_coin.functions.transfer(account3, 1).transact({'from': account2})
chain.wait.for_receipt(set_txn_hash)
set_txn_hash = stable_coin.functions.freezeBalance(account2, True).transact()
chain.wait.for_receipt(set_txn_hash)
with pytest.raises(eth_tester.exceptions.TransactionFailed):
stable_coin.functions.transfer(account3, 1).transact({'from': account2})
set_txn_hash = stable_coin.functions.freezeBalance(account2, False).transact()
chain.wait.for_receipt(set_txn_hash)
set_txn_hash = stable_coin.functions.transfer(account3, 1).transact({'from': account2})
chain.wait.for_receipt(set_txn_hash)
开发者ID:PacktPublishing,项目名称:Hands-On-Blockchain-for-Python-Developers,代码行数:26,代码来源:test_stable_token.py
示例2: test_transfer
# 需要导入模块: import eth_tester [as 别名]
# 或者: from eth_tester import EthereumTester [as 别名]
def test_transfer(web3, chain):
erc20_token, _ = chain.provider.get_or_deploy_contract('ERC20Token')
t = eth_tester.EthereumTester()
account2 = t.get_accounts()[1]
old_balance = erc20_token.functions.balanceOf(account2).call()
assert old_balance == 0
set_txn_hash = erc20_token.functions.transfer(account2, 10).transact({'from': web3.eth.coinbase})
chain.wait.for_receipt(set_txn_hash)
sender_new_balance = erc20_token.functions.balanceOf(web3.eth.coinbase).call()
assert sender_new_balance == 999990
destination_new_balance = erc20_token.functions.balanceOf(account2).call()
assert destination_new_balance == 10
开发者ID:PacktPublishing,项目名称:Hands-On-Blockchain-for-Python-Developers,代码行数:18,代码来源:test_erc20_token.py
示例3: test_transfer
# 需要导入模块: import eth_tester [as 别名]
# 或者: from eth_tester import EthereumTester [as 别名]
def test_transfer(web3, chain):
simple_token, _ = chain.provider.get_or_deploy_contract('SimpleToken')
t = eth_tester.EthereumTester()
account2 = t.get_accounts()[1]
old_balance = simple_token.functions.balances(account2).call()
assert old_balance == 0
set_txn_hash = simple_token.functions.transfer(account2, 10).transact({'from': web3.eth.coinbase})
chain.wait.for_receipt(set_txn_hash)
sender_new_balance = simple_token.functions.balances(web3.eth.coinbase).call()
assert sender_new_balance == 9990
destination_new_balance = simple_token.functions.balances(account2).call()
assert destination_new_balance == 10
开发者ID:PacktPublishing,项目名称:Hands-On-Blockchain-for-Python-Developers,代码行数:18,代码来源:test_simple_token.py
示例4: test_purchase_token
# 需要导入模块: import eth_tester [as 别名]
# 或者: from eth_tester import EthereumTester [as 别名]
def test_purchase_token(web3, chain):
crowd_sale_token, _ = chain.provider.get_or_deploy_contract('CrowdSaleToken')
t = eth_tester.EthereumTester()
account2 = t.get_accounts()[1]
account3 = t.get_accounts()[2]
set_txn_hash = crowd_sale_token.functions.__default__().transact({'from': account2, 'value': web3.toWei('1', 'ether')})
chain.wait.for_receipt(set_txn_hash)
set_txn_hash = crowd_sale_token.functions.__default__().transact({'from': account3, 'value': web3.toWei('2', 'ether')})
chain.wait.for_receipt(set_txn_hash)
ethBalance_account2 = crowd_sale_token.functions.ethBalances(account2).call()
balance_account2 = crowd_sale_token.functions.balanceOf(account2).call()
amountRaised = crowd_sale_token.functions.amountRaised().call()
balance_account1 = crowd_sale_token.functions.balanceOf(web3.eth.coinbase).call()
assert ethBalance_account2 == web3.toWei('1', 'ether')
assert balance_account2 == 100
assert amountRaised == web3.toWei('3', 'ether')
assert balance_account1 == (10000 - 100 - 200)
开发者ID:PacktPublishing,项目名称:Hands-On-Blockchain-for-Python-Developers,代码行数:23,代码来源:test_crowd_sale_token.py
示例5: test_manager_account_could_withdraw_money
# 需要导入模块: import eth_tester [as 别名]
# 或者: from eth_tester import EthereumTester [as 别名]
def test_manager_account_could_withdraw_money(web3, chain):
donation, _ = chain.provider.get_or_deploy_contract('Donation')
t = eth_tester.EthereumTester()
account2 = t.get_accounts()[1]
donatur_name = b'Taylor Swift'
set_txn_hash = donation.functions.donate(donatur_name).transact({'from': account2, 'value': web3.toWei('1', 'ether')})
chain.wait.for_receipt(set_txn_hash)
initial_balance = web3.eth.getBalance(web3.eth.coinbase)
set_txn_hash = donation.functions.withdraw_donation().transact({'from': web3.eth.coinbase})
chain.wait.for_receipt(set_txn_hash)
after_withdraw_balance = web3.eth.getBalance(web3.eth.coinbase)
assert abs((after_withdraw_balance - initial_balance) - web3.toWei('1', 'ether')) < web3.toWei('10', 'gwei')
示例6: test_custom_virtual_machines
# 需要导入模块: import eth_tester [as 别名]
# 或者: from eth_tester import EthereumTester [as 别名]
def test_custom_virtual_machines():
if not is_pyevm_available():
pytest.skip("PyEVM is not available")
backend = PyEVMBackend(vm_configuration=(
(0, FrontierVM),
(3, MuirGlacierVM),
))
# This should be a FrontierVM block
VM_at_2 = backend.chain.get_vm_class_for_block_number(2)
# This should be a MuirGlacier block
VM_at_3 = backend.chain.get_vm_class_for_block_number(3)
assert issubclass(VM_at_2, FrontierVM)
assert not issubclass(VM_at_2, MuirGlacierVM)
assert issubclass(VM_at_3, MuirGlacierVM)
# Right now, just test that EthereumTester doesn't crash
# Maybe some more sophisticated test to make sure the VMs are set correctly?
# We should to make sure the VM config translates all the way to the main
# tester, maybe with a custom VM that hard-codes some block value? that can
# be found with tester.get_block_by_number()?
EthereumTester(backend=backend)
示例7: test_override_genesis_state
# 需要导入模块: import eth_tester [as 别名]
# 或者: from eth_tester import EthereumTester [as 别名]
def test_override_genesis_state(self):
state_overrides = {"balance": to_wei(900000, "ether")}
test_accounts = 3
# Initialize PyEVM backend with custom genesis state
genesis_state = PyEVMBackend._generate_genesis_state(
overrides=state_overrides, num_accounts=test_accounts
)
# Test the correct number of accounts are created with the specified balance override
pyevm_backend = PyEVMBackend(genesis_state=genesis_state)
assert len(pyevm_backend.account_keys) == test_accounts
for private_key in pyevm_backend.account_keys:
account = private_key.public_key.to_canonical_address()
balance = pyevm_backend.get_balance(account=account)
assert balance == state_overrides["balance"]
# Test integration with EthereumTester
tester = EthereumTester(backend=pyevm_backend)
for private_key in pyevm_backend.account_keys:
account = private_key.public_key.to_checksum_address()
balance = tester.get_balance(account=account)
assert balance == state_overrides["balance"]
示例8: test_override_genesis_parameters
# 需要导入模块: import eth_tester [as 别名]
# 或者: from eth_tester import EthereumTester [as 别名]
def test_override_genesis_parameters(self):
# Establish a custom gas limit
param_overrides = {"gas_limit": 4750000}
block_one_gas_limit = param_overrides['gas_limit']
# Initialize PyEVM backend with custom genesis parameters
genesis_params = PyEVMBackend._generate_genesis_params(
overrides=param_overrides
)
pyevm_backend = PyEVMBackend(genesis_parameters=genesis_params)
genesis_block = pyevm_backend.get_block_by_number(0)
assert genesis_block["gas_limit"] == param_overrides["gas_limit"]
genesis_block = pyevm_backend.get_block_by_number(1)
assert genesis_block["gas_limit"] == block_one_gas_limit
# Integrate with EthereumTester
tester = EthereumTester(backend=pyevm_backend)
genesis_block = tester.get_block_by_number(0)
assert genesis_block["gas_limit"] == param_overrides["gas_limit"]
genesis_block = tester.get_block_by_number(1)
assert genesis_block["gas_limit"] == block_one_gas_limit
示例9: get_web3
# 需要导入模块: import eth_tester [as 别名]
# 或者: from eth_tester import EthereumTester [as 别名]
def get_web3(eth_tester: EthereumTester, deployer_key: PrivateKey) -> Web3:
"""Returns an initialized Web3 instance"""
provider = EthereumTesterProvider(eth_tester)
web3 = Web3(provider)
# add faucet account to tester
eth_tester.add_account(deployer_key.to_hex())
# make faucet rich
eth_tester.send_transaction(
{
"from": eth_tester.get_accounts()[0],
"to": private_key_to_address(deployer_key.to_hex()),
"gas": 21000,
"value": FAUCET_ALLOWANCE,
}
)
return web3
示例10: web3
# 需要导入模块: import eth_tester [as 别名]
# 或者: from eth_tester import EthereumTester [as 别名]
def web3(ethereum_tester: EthereumTester) -> Web3:
"""Returns an initialized Web3 instance"""
provider = EthereumTesterProvider(ethereum_tester)
web3 = Web3(provider)
# Improve test speed by skipping the gas cost estimation.
web3.eth.estimateGas = lambda txn: int(5.2e6) # type: ignore # pylint: disable=E1101
# add faucet account to tester
ethereum_tester.add_account(FAUCET_PRIVATE_KEY.hex())
# make faucet rich
ethereum_tester.send_transaction(
{
"from": ethereum_tester.get_accounts()[0],
"to": FAUCET_ADDRESS,
"gas": 21000,
"value": FAUCET_ALLOWANCE,
}
)
# Enable strict type checks
web3.enable_strict_bytes_type_checking()
return web3
示例11: create_account
# 需要导入模块: import eth_tester [as 别名]
# 或者: from eth_tester import EthereumTester [as 别名]
def create_account(web3: Web3, ethereum_tester: EthereumTester) -> Callable:
def get(privkey: Optional[PrivateKey] = None) -> HexAddress:
if not privkey:
privkey = get_random_privkey()
address = private_key_to_address(privkey)
if not any((is_same_address(address, x) for x in ethereum_tester.get_accounts())):
# account has not been added to ethereum_tester, yet
ethereum_tester.add_account(privkey.hex())
for faucet in web3.eth.accounts[:10]:
try:
web3.eth.sendTransaction(
{"from": faucet, "to": address, "value": Wei(1 * int(units["finney"]))}
)
break
except TransactionFailed:
continue
return address
return get
示例12: get_provider_from_uri
# 需要导入模块: import eth_tester [as 别名]
# 或者: from eth_tester import EthereumTester [as 别名]
def get_provider_from_uri(node_uri: str):
if node_uri.startswith('http'):
return HTTPProvider(node_uri)
elif node_uri.startswith('ipc'):
path = node_uri.replace('ipc://', '')
return IPCProvider(ipc_path=path)
elif node_uri.startswith('test'):
return EthereumTesterProvider(EthereumTester())
else:
raise ValueError('%s uri is not supported. Must start by http, ipc, or test' % node_uri)
示例13: __init__
# 需要导入模块: import eth_tester [as 别名]
# 或者: from eth_tester import EthereumTester [as 别名]
def __init__(self, provider,
max_workers: int=10, max_batch_requests: int=10, slow_provider_timeout: int=400):
"""
:param node_uri: Node http address. If uri starts with 'test', EthereumTester will be used
:param max_workers: Max workers for multithread calls. 1 -> No multithread
:param max_batch_requests: Max requests in the same batch for RPC
:param self.slow_provider_timeout: Timeout for time lasting requests (like filters)
"""
self.provider = provider
self.max_workers = max_workers
self.max_batch_requests = max_batch_requests
self.slow_provider_timeout = slow_provider_timeout
self.node_uri = self.get_node_uri()
self.web3 = Web3(provider)
self.web3_slow = Web3(self.slow_provider)
self.http_session = requests.session()
# If rinkeby, inject Geth PoA middleware
# http://web3py.readthedocs.io/en/latest/middleware.html#geth-style-proof-of-authority
try:
if int(self.web3.net.version) == RINKEBY_CHAIN_ID:
self.web3.middleware_stack.inject(geth_poa_middleware, layer=0)
# For tests using dummy connections (like IPC)
except (UnhandledRequest, ConnectionError, ConnectionRefusedError, FileNotFoundError):
pass
示例14: setUp
# 需要导入模块: import eth_tester [as 别名]
# 或者: from eth_tester import EthereumTester [as 别名]
def setUp(self):
self.provider = EthereumTesterProvider(EthereumTester())
self.web3 = Web3Service(provider=self.provider).web3
self.web3.eth.defaultAccount = self.web3.eth.coinbase
# Mock web3
self.daemon = DaemonFactory()
self.tx_data = {'from': self.web3.eth.accounts[0],
'gas': 1000000}
# create oracles
centralized_contract_factory = self.web3.eth.contract(abi=centralized_oracle_abi,
bytecode=centralized_oracle_bytecode)
tx_hash = centralized_contract_factory.constructor().transact()
self.centralized_oracle_factory_address = self.web3.eth.getTransactionReceipt(tx_hash).get('contractAddress')
self.centralized_oracle_factory = self.web3.eth.contract(self.centralized_oracle_factory_address,
abi=centralized_oracle_abi)
self.contracts = [
{
'NAME': 'Centralized Oracle Factory',
'EVENT_ABI': centralized_oracle_abi,
'EVENT_DATA_RECEIVER': 'django_eth_events.tests.utils.CentralizedOraclesReceiver',
'ADDRESSES': [self.centralized_oracle_factory_address[2::]]
}
]
EventListener.instance = None
self.listener_under_test = EventListener(contract_map=self.contracts,
provider=self.provider)
CentralizedOracle().reset()
self.assertEqual(CentralizedOracle().length(), 0)
self.assertEqual(1, self.web3.eth.blockNumber)
示例15: test_eth_tester_provider
# 需要导入模块: import eth_tester [as 别名]
# 或者: from eth_tester import EthereumTester [as 别名]
def test_eth_tester_provider(self):
eth_tester_provider = EthereumTesterProvider(EthereumTester())
service1 = Web3ServiceProvider()
self.assertIsInstance(service1.web3.providers[0], HTTPProvider)
service2 = Web3Service(eth_tester_provider)
self.assertIsInstance(service2.web3.providers[0], EthereumTesterProvider)
self.assertEqual(service2.web3.providers[0], eth_tester_provider)