|
| 1 | +import unittest |
| 2 | +import os |
| 3 | +import rlp |
| 4 | +from web3 import Web3 |
| 5 | +from solcx import install_solc |
| 6 | + |
| 7 | +# install_solc(version='latest') |
| 8 | +install_solc(version='0.7.0') |
| 9 | +from solcx import compile_source |
| 10 | + |
| 11 | +proxy_url = os.environ.get('PROXY_URL', 'http://127.0.0.1:9090/solana') |
| 12 | +proxy = Web3(Web3.HTTPProvider(proxy_url)) |
| 13 | +eth_account = proxy.eth.account.create() |
| 14 | +proxy.eth.default_account = eth_account.address |
| 15 | + |
| 16 | + |
| 17 | +STORAGE_SOLIDITY_SOURCE = ''' |
| 18 | +pragma solidity >=0.7.0 <0.9.0; |
| 19 | +
|
| 20 | +contract Storage { |
| 21 | + uint256 number; |
| 22 | + /** |
| 23 | + * @dev Store value in variable |
| 24 | + * @param num value to store |
| 25 | + */ |
| 26 | + function store(uint256 num) public { |
| 27 | + number = num; |
| 28 | + } |
| 29 | + /** |
| 30 | + * @dev Return value |
| 31 | + * @return value of 'number' |
| 32 | + */ |
| 33 | + function retrieve() public view returns (uint256){ |
| 34 | + return number; |
| 35 | + } |
| 36 | +} |
| 37 | +''' |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +class Test_createAccountBlock(unittest.TestCase): |
| 42 | + @classmethod |
| 43 | + def setUpClass(cls): |
| 44 | + print("\n\nhttps://github.com/neonlabsorg/proxy-model.py/issues/147") |
| 45 | + print('eth_account.address:', eth_account.address) |
| 46 | + print('eth_account.key:', eth_account.key.hex()) |
| 47 | + print('balance:', proxy.eth.get_balance(eth_account.address)) |
| 48 | + |
| 49 | + # Create caller account in NeonEVM |
| 50 | + cls.deploy_contract(cls) |
| 51 | + |
| 52 | + def deploy_contract(self): |
| 53 | + compiled_sol = compile_source(STORAGE_SOLIDITY_SOURCE) |
| 54 | + contract_id, contract_interface = compiled_sol.popitem() |
| 55 | + storage = proxy.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin']) |
| 56 | + trx_deploy = proxy.eth.account.sign_transaction(dict( |
| 57 | + nonce=proxy.eth.get_transaction_count(eth_account.address), |
| 58 | + chainId=proxy.eth.chain_id, |
| 59 | + gas=987654321, |
| 60 | + gasPrice=0, |
| 61 | + to='', |
| 62 | + value=0, |
| 63 | + data=storage.bytecode), |
| 64 | + eth_account.key |
| 65 | + ) |
| 66 | + |
| 67 | + trx_deploy_hash = proxy.eth.send_raw_transaction(trx_deploy.rawTransaction) |
| 68 | + return proxy.eth.wait_for_transaction_receipt(trx_deploy_hash) |
| 69 | + |
| 70 | + def transfer(self, target_account, value): |
| 71 | + trx_transfer = proxy.eth.account.sign_transaction(dict( |
| 72 | + nonce=proxy.eth.get_transaction_count(eth_account.address), |
| 73 | + chainId=proxy.eth.chain_id, |
| 74 | + gas=987654321, |
| 75 | + gasPrice=0, |
| 76 | + to=bytes(target_account), |
| 77 | + value=value), |
| 78 | + eth_account.key |
| 79 | + ) |
| 80 | + |
| 81 | + transfer_hash = proxy.eth.send_raw_transaction(trx_transfer.rawTransaction) |
| 82 | + return proxy.eth.wait_for_transaction_receipt(transfer_hash) |
| 83 | + |
| 84 | + def test_blockAccount(self): |
| 85 | + nonce = proxy.eth.get_transaction_count(eth_account.address) |
| 86 | + expected_contract_address = proxy.keccak(rlp.encode((bytes.fromhex(eth_account.address[2:]), nonce + 1)))[-20:] |
| 87 | + |
| 88 | + # Create expected contract account |
| 89 | + transfer_receipt = self.transfer(expected_contract_address, 1_000_000_000) |
| 90 | + self.assertEqual(transfer_receipt["status"], 1) |
| 91 | + |
| 92 | + # Try to deploy to expected contract account |
| 93 | + deploy_receipt = self.deploy_contract() |
| 94 | + self.assertEqual(deploy_receipt["status"], 1) |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == '__main__': |
| 98 | + unittest.main() |
0 commit comments