test.py (1332B)
1 import web3 2 import eth_tester 3 import json 4 5 6 eth_params = eth_tester.backends.pyevm.main.get_default_genesis_params({ 7 'gas_limit': 9000000, 8 }) 9 backend = eth_tester.PyEVMBackend(eth_params) 10 instance = eth_tester.EthereumTester(backend) 11 provider = web3.Web3.EthereumTesterProvider(instance) 12 w3 = web3.Web3(provider) 13 14 15 f = open('AccountsIndex.bin', 'r') 16 bytecode = f.read() 17 f.close() 18 19 f = open('AccountsIndex.json', 'r') 20 abi = json.load(f) 21 f.close() 22 23 c = w3.eth.contract(abi=abi, bytecode=bytecode) 24 tx_hash = c.constructor().transact({'from': w3.eth.accounts[0]}) 25 26 r = w3.eth.getTransactionReceipt(tx_hash) 27 28 c = w3.eth.contract(abi=abi, address=r.contractAddress) 29 fail = False 30 try: 31 c.functions.add(w3.eth.accounts[2]).transact({'from': w3.eth.accounts[1]}) 32 except: 33 fail = True 34 assert fail 35 36 c.functions.addWriter(w3.eth.accounts[1]).transact({'from': w3.eth.accounts[0]}) 37 c.functions.add(w3.eth.accounts[2]).transact({'from': w3.eth.accounts[1]}) 38 c.functions.add(w3.eth.accounts[3]).transact({'from': w3.eth.accounts[1]}) 39 40 assert c.functions.count().call() == 3 41 assert c.functions.accountsIndex(w3.eth.accounts[3]).call() == 2 42 assert c.functions.accounts(2).call() == w3.eth.accounts[3] 43 44 fail = False 45 try: 46 c.functions.add(w3.eth.accounts[3]).transact({'from': w3.eth.accounts[1]}) 47 except: 48 fail = True 49 assert fail