{ "cells": [ { "cell_type": "code", "execution_count": 105, "id": "feee22ef", "metadata": {}, "outputs": [], "source": [ "from pyhmy import signing, staking_signing, numbers, transaction, account, validator as validator_module, staking_structures, contract\n", "from web3 import Web3" ] }, { "cell_type": "code", "execution_count": 3, "id": "55b8db60", "metadata": {}, "outputs": [], "source": [ "# we need five transactions in conftest\n", "# simple transfer (from localnet address)\n", "# contract creation (from second address)\n", "# cross shard transfer (from second address)\n", "# validator creation (from localnet address)\n", "# delegation (from second address)" ] }, { "cell_type": "markdown", "id": "e104724c", "metadata": {}, "source": [ "### Simple Transfer" ] }, { "cell_type": "code", "execution_count": 144, "id": "d7fa35f8", "metadata": {}, "outputs": [], "source": [ "pk = \"1f84c95ac16e6a50f08d44c7bde7aff8742212fda6e4321fde48bf83bef266dc\"\n", "tx = {\n", " 'from': 'one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3',\n", " # 3c86ac59f6b038f584be1c08fced78d7c71bb55d5655f81714f3cddc82144c65\n", " 'to': 'one1ru3p8ff0wsyl7ncsx3vwd5szuze64qz60upg37',\n", " 'gasPrice': Web3.toWei( 100, 'gwei' ),\n", " 'gas': 21000,\n", " 'chainId': 2, # localnet\n", " 'value': int( numbers.convert_one_to_atto( 503 ) ),\n", " 'nonce': 0,\n", " 'shardID': 0,\n", " 'toShardID': 0,\n", "}\n", "raw_tx = signing.sign_transaction(tx, pk).rawTransaction.hex()\n", "tx_hash = transaction.send_raw_transaction(raw_tx)" ] }, { "cell_type": "code", "execution_count": 145, "id": "ed907d4b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0xf86f8085174876e8008252088080941f2213a52f7409ff4f103458e6d202e0b3aa805a891b4486fafde57c00008027a0d7c0b20207dcc9dde376822dc3f5625eac6f59a7526111695cdba3e29553ca17a05d4ca9a421ae16f89cbf6848186eaea7a800da732446dff9952e7c1e91d414e3\n", "0xc26be5776aa57438bccf196671a2d34f3f22c9c983c0f844c62b2fb90403aa43\n" ] } ], "source": [ "print( raw_tx )\n", "print( tx_hash )" ] }, { "cell_type": "markdown", "id": "1bbee37b", "metadata": {}, "source": [ "### Contract Creation" ] }, { "cell_type": "code", "execution_count": 147, "id": "b143507b", "metadata": {}, "outputs": [], "source": [ "pk = '3c86ac59f6b038f584be1c08fced78d7c71bb55d5655f81714f3cddc82144c65'\n", "data = \"0x6080604052348015600f57600080fd5b50607780601d6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80634936cd3614602d575b600080fd5b604080516001815290519081900360200190f3fea2646970667358221220fa3fa0e8d0267831a59f4dd5edf39a513d07e98461cb06660ad28d4beda744cd64736f6c634300080f0033\"\n", "tx = {\n", " 'gasPrice': Web3.toWei( 100, 'gwei' ),\n", " 'gas': 100000,\n", " 'chainId': 2,\n", " 'nonce': 0,\n", " 'shardID': 0,\n", " 'toShardID': 0,\n", " 'data': data,\n", "}\n", "raw_tx = signing.sign_transaction(tx, pk).rawTransaction.hex()\n", "tx_hash = transaction.send_raw_transaction(raw_tx)" ] }, { "cell_type": "code", "execution_count": 148, "id": "53dbcbff", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0xf8e88085174876e800830186a080808080b8946080604052348015600f57600080fd5b50607780601d6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80634936cd3614602d575b600080fd5b604080516001815290519081900360200190f3fea2646970667358221220fa3fa0e8d0267831a59f4dd5edf39a513d07e98461cb06660ad28d4beda744cd64736f6c634300080f003327a08bf26ee0120c296b17af507f62606abdb5c5f09a65642c3d30b349b8bfbb3d69a03ec7be51c615bcbf2f1d63f6eaa56cf8d7be81671717f90239619830a81ebc9f\n", "0xa605852dd2fa39ed42e101c17aaca9d344d352ba9b24b14b9af94ec9cb58b31f\n" ] } ], "source": [ "print( raw_tx )\n", "print( tx_hash )" ] }, { "cell_type": "code", "execution_count": 130, "id": "6e66392b", "metadata": {}, "outputs": [], "source": [ "contract_address = transaction.get_transaction_receipt( tx_hash ).get( 'contractAddress' )\n", "deployed = contract.get_code( contract_address, 'latest' )" ] }, { "cell_type": "code", "execution_count": 131, "id": "ead2f9d4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0x6080604052348015600f57600080fd5b506004361060285760003560e01c80634936cd3614602d575b600080fd5b604080516001815290519081900360200190f3fea2646970667358221220fa3fa0e8d0267831a59f4dd5edf39a513d07e98461cb06660ad28d4beda744cd64736f6c634300080f0033\n", "0x6080604052348015600f57600080fd5b50607780601d6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80634936cd3614602d575b600080fd5b604080516001815290519081900360200190f3fea2646970667358221220fa3fa0e8d0267831a59f4dd5edf39a513d07e98461cb06660ad28d4beda744cd64736f6c634300080f0033\n" ] } ], "source": [ "print( deployed )\n", "print( data )" ] }, { "cell_type": "code", "execution_count": 132, "id": "453a34d6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3300f080003436c6f63746dc447adeb4d82da06660bc16489e70d315a93fde5dd4f95a1387620d8e0af3af0221228537660796462aef3f091002063009180915092518100615080406b5df080006b575d2064163dc63943608c10e065300067582060163400605b5df08000675f0065108432504060806x0\n", "3300f080003436c6f63746dc447adeb4d82da06660bc16489e70d315a93fde5dd4f95a1387620d8e0af3af0221228537660796462aef3f091002063009180915092518100615080406b5df080006b575d2064163dc63943608c10e065300067582060163400605b5df08000675f0065108432504060806ef3f0006930006d10608770605b5df08000675f0065108432504060806x0\n" ] } ], "source": [ "print( \"\".join( [ deployed[ len( deployed ) - ( i + 1 ) ] for i in range( len( deployed ) ) ] ) )\n", "print( \"\".join( [ data[ len( data ) - ( i + 1 ) ] for i in range( len( data ) ) ] ) )" ] }, { "cell_type": "code", "execution_count": 133, "id": "d251d1bf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"0x6080604052348015600f57600080fd5b506004361060285760003560e01c80634936cd3614602d575b600080fd5b604080516001815290519081900360200190f3fea2646970667358221220fa3fa0e8d0267831a59f4dd5edf39a513d07e98461cb06660ad28d4beda744cd64736f6c634300080f0033\" == deployed" ] }, { "cell_type": "markdown", "id": "812e033c", "metadata": {}, "source": [ "### Cross Shard Transfer" ] }, { "cell_type": "code", "execution_count": 149, "id": "d7c70614", "metadata": {}, "outputs": [], "source": [ "pk = '3c86ac59f6b038f584be1c08fced78d7c71bb55d5655f81714f3cddc82144c65'\n", "tx = {\n", " 'from': 'one1ru3p8ff0wsyl7ncsx3vwd5szuze64qz60upg37',\n", " 'gasPrice': Web3.toWei( 100, 'gwei' ),\n", " 'gas': 21000,\n", " 'chainId': 2,\n", " 'nonce': 1,\n", " 'shardID': 0,\n", " 'toShardID': 1,\n", " 'to': 'one1e8rdglh97t37prtnv7k35ymnh2wazujpzsmzes',\n", " 'value': Web3.toWei( 100, 'gwei' ),\n", "}\n", "raw_tx = signing.sign_transaction(tx, pk).rawTransaction.hex()\n", "tx_hash = transaction.send_raw_transaction(raw_tx)" ] }, { "cell_type": "code", "execution_count": 150, "id": "f20990f1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0xf86b0185174876e800825208800194c9c6d47ee5f2e3e08d7367ad1a1373ba9dd1724185174876e8008027a02501c517220e9499f14e97c20b0a88cd3b7ba80637bba43ed295422e69a3f300a079b8e1213c9506184aed6ac2eb0b2cb00594c3f9fcdd6c088937ce17fe47107c\n", "0xf73ba634cb96fc0e3e2c9d3b4c91379e223741be4a5aa56e6d6caf49c1ae75cf\n" ] } ], "source": [ "print( raw_tx )\n", "print( tx_hash )" ] }, { "cell_type": "code", "execution_count": 153, "id": "66f024b9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 153, "metadata": {}, "output_type": "execute_result" } ], "source": [ "account.get_balance( 'one1e8rdglh97t37prtnv7k35ymnh2wazujpzsmzes', 'http://localhost:9502' )" ] }, { "cell_type": "markdown", "id": "2b2446df", "metadata": {}, "source": [ "### Validator Creation" ] }, { "cell_type": "code", "execution_count": 154, "id": "c3513c37", "metadata": {}, "outputs": [], "source": [ "pk = \"1f84c95ac16e6a50f08d44c7bde7aff8742212fda6e4321fde48bf83bef266dc\"\n", "address = \"one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3\"\n", "info = {\n", " \"name\": \"Alice\",\n", " \"identity\": \"alice\",\n", " \"website\": \"alice.harmony.one\",\n", " \"security-contact\": \"Bob\",\n", " \"details\": \"Are you even reading this?\",\n", " \"min-self-delegation\": int( numbers.convert_one_to_atto( 10000 ) ),\n", " \"max-total-delegation\": int( numbers.convert_one_to_atto( 100000 ) ),\n", " \"rate\": \"0.1\",\n", " \"max-rate\": \"0.9\",\n", " \"max-change-rate\": \"0.05\",\n", " \"bls-public-keys\": [\n", " # private key is b1f2a5029f5f43c8c933a61ce936ced030b2c9379f8e2478fc888fa670cdbc89b8cd1ebc29b5b00a81d3152bb3aaa3a337404f50bee5e434430ca3693a94a1c102a765cf3b0887b8b0bcf5317d33f4bec60a97feae2498a39ab7a1c2\n", " # blspass.txt is empty\n", " \"0xa20e70089664a874b00251c5e85d35a73871531306f3af43e02138339d294e6bb9c4eb82162199c6a852afeaa8d68712\",\n", " ],\n", " \"amount\": int( numbers.convert_one_to_atto( 10000 ) ),\n", " \"bls-key-sigs\": [\n", " \"0xef2c49a2f31fbbd23c21bc176eaf05cd0bebe6832033075d81fea7cff6f9bc1ab42f3b6895c5493fe645d8379d2eaa1413de55a9d3ce412a4f747cb57d52cc4da4754bfb2583ec9a41fe5dd48287f964f276336699959a5fcef3391dc24df00d\",\n", " ]\n", "}\n", "validator = validator_module.Validator( address )\n", "validator.load( info )\n", "raw_tx = validator.sign_create_validator_transaction(\n", " 1,\n", " Web3.toWei( 100, 'gwei' ),\n", " 55000000, # gas limit\n", " pk,\n", " 2 # chain id\n", ").rawTransaction.hex()\n", "tx_hash = transaction.send_raw_staking_transaction(\n", " raw_tx,\n", ")" ] }, { "cell_type": "code", "execution_count": 155, "id": "9b12f75f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0xf9017c80f9012994a5241513da9f4463f1d4874b548dfbac29d91f34f83d85416c69636585616c69636591616c6963652e6861726d6f6e792e6f6e6583426f629a41726520796f75206576656e2072656164696e6720746869733fddc988016345785d8a0000c9880c7d713b49da0000c887b1a2bc2ec500008a021e19e0c9bab24000008a152d02c7e14af6800000f1b0a20e70089664a874b00251c5e85d35a73871531306f3af43e02138339d294e6bb9c4eb82162199c6a852afeaa8d68712f862b860ef2c49a2f31fbbd23c21bc176eaf05cd0bebe6832033075d81fea7cff6f9bc1ab42f3b6895c5493fe645d8379d2eaa1413de55a9d3ce412a4f747cb57d52cc4da4754bfb2583ec9a41fe5dd48287f964f276336699959a5fcef3391dc24df00d8a021e19e0c9bab24000000185174876e8008403473bc028a08c1146305eaef981aa24c2f17c8519664d10c99ee42acedbc258749930d31a7ca031dadf114ee6ab9bd09933208094c65037b66c796bcfc57a70158106b37357b0\n", "0x400e9831d358f5daccd153cad5bf53650a0d413bd8682ec0ffad55367d162968\n" ] } ], "source": [ "print( raw_tx )\n", "print( tx_hash )" ] }, { "cell_type": "markdown", "id": "4c2ff645", "metadata": {}, "source": [ "### Delegation" ] }, { "cell_type": "code", "execution_count": 156, "id": "458d81b8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0xf88302f4941f2213a52f7409ff4f103458e6d202e0b3aa805a94a5241513da9f4463f1d4874b548dfbac29d91f3489056bc75e2d631000000285174876e80082c35028a02c5e953062dcdfa2de9723639b63bab45705eb6dfbfe7f44536ed266c3c7ca20a0742964e646338e7431874f70715565d99c01c762324355c69db34a9ed9de81d7\n", "0xc8177ace2049d9f4eb4a45fd6bd6b16f693573d036322c36774cc00d05a3e24f\n" ] } ], "source": [ "pk = \"3c86ac59f6b038f584be1c08fced78d7c71bb55d5655f81714f3cddc82144c65\"\n", "tx = {\n", " 'directive': staking_structures.Directive.Delegate,\n", " 'delegatorAddress': 'one1ru3p8ff0wsyl7ncsx3vwd5szuze64qz60upg37',\n", " 'validatorAddress': 'one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3',\n", " 'amount': Web3.toWei( 100, 'ether' ),\n", " 'nonce': 2,\n", " 'gasPrice': Web3.toWei( 100, 'gwei' ),\n", " 'gasLimit': 50000,\n", " 'chainId': 2,\n", "}\n", "raw_tx = staking_signing.sign_staking_transaction(\n", " tx,\n", " pk,\n", ").rawTransaction.hex()\n", "tx_hash = transaction.send_raw_staking_transaction(\n", " raw_tx,\n", ")\n", "print( raw_tx )\n", "print( tx_hash )" ] }, { "cell_type": "markdown", "id": "8efa5536", "metadata": {}, "source": [ "### test_transaction.py - transfer 105 ONE to another address" ] }, { "cell_type": "code", "execution_count": 157, "id": "c3295fee", "metadata": {}, "outputs": [], "source": [ "pk = '3c86ac59f6b038f584be1c08fced78d7c71bb55d5655f81714f3cddc82144c65'\n", "tx = {\n", " 'from': 'one1ru3p8ff0wsyl7ncsx3vwd5szuze64qz60upg37',\n", " 'gasPrice': Web3.toWei( 100, 'gwei' ),\n", " 'gas': 21000,\n", " 'chainId': 2,\n", " 'nonce': 3,\n", " 'shardID': 0,\n", " 'toShardID': 0,\n", " 'to': 'one1e8rdglh97t37prtnv7k35ymnh2wazujpzsmzes',\n", " 'value': Web3.toWei( 105, 'ether' ),\n", "}\n", "raw_tx = signing.sign_transaction(tx, pk).rawTransaction.hex()\n", "tx_hash = transaction.send_raw_transaction(raw_tx)" ] }, { "cell_type": "code", "execution_count": 158, "id": "af515c7e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0xf86f0385174876e800825208808094c9c6d47ee5f2e3e08d7367ad1a1373ba9dd172418905b12aefafa80400008027a07a4952b90bf38723a9197179a8e6d2e9b3a86fd6da4e66a9cf09fdc59783f757a053910798b311245525bd77d6119332458c2855102e4fb9e564f6a3b710d18bb0\n", "0x7ccd80f8513f76ec58b357c7a82a12a95e025d88f1444e953f90e3d86e222571\n" ] } ], "source": [ "print( raw_tx )\n", "print( tx_hash )" ] }, { "cell_type": "markdown", "id": "bb546b3e", "metadata": {}, "source": [ "### test_transaction.py - staking transaction" ] }, { "cell_type": "code", "execution_count": 168, "id": "c14e2d6d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0xf88302f494c9c6d47ee5f2e3e08d7367ad1a1373ba9dd1724194a5241513da9f4463f1d4874b548dfbac29d91f3489056bc75e2d631000008085174876e80082c35027a0808ea7d27adf3b1f561e8da4676814084bb75ac541b616bece87c6446e6cc54ea02f19f0b14240354bd42ad60b0c7189873c0be87044e13072b0981a837ca76f64\n", "0xe7d07ef6d9fca595a14ceb0ca917bece7bedb15efe662300e9334a32ac1da629\n" ] } ], "source": [ "pk = \"ff9ef6b00a61672b4b7bedd5ac653439b56ac8ee808c99a1bd871cf51b7d60eb\"\n", "tx = {\n", " 'directive': staking_structures.Directive.Delegate,\n", " 'delegatorAddress': 'one1e8rdglh97t37prtnv7k35ymnh2wazujpzsmzes',\n", " 'validatorAddress': 'one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3',\n", " 'amount': Web3.toWei( 100, 'ether' ),\n", " 'nonce': 0,\n", " 'gasPrice': Web3.toWei( 100, 'gwei' ),\n", " 'gasLimit': 50000,\n", " 'chainId': 2,\n", "}\n", "raw_tx = staking_signing.sign_staking_transaction(\n", " tx,\n", " pk,\n", ").rawTransaction.hex()\n", "tx_hash = transaction.send_raw_staking_transaction(\n", " raw_tx,\n", ")\n", "print( raw_tx )\n", "print( tx_hash )" ] }, { "cell_type": "code", "execution_count": 162, "id": "ebf296aa", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'blockHash': '0xf55f1fb3c9be76fb74370e8a7d8580327797d2d6082040074783207a171e2de6',\n", " 'blockNumber': 34,\n", " 'from': 'one1ru3p8ff0wsyl7ncsx3vwd5szuze64qz60upg37',\n", " 'hash': '0xf73ba634cb96fc0e3e2c9d3b4c91379e223741be4a5aa56e6d6caf49c1ae75cf',\n", " 'shardID': 0,\n", " 'to': 'one1e8rdglh97t37prtnv7k35ymnh2wazujpzsmzes',\n", " 'toShardID': 1,\n", " 'value': 100000000000}" ] }, "execution_count": 162, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transaction.get_cx_receipt_by_hash( '0xf73ba634cb96fc0e3e2c9d3b4c91379e223741be4a5aa56e6d6caf49c1ae75cf', 'http://localhost:9502' )" ] }, { "cell_type": "code", "execution_count": 166, "id": "ff0229ce", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'blockHash': '0x0000000000000000000000000000000000000000000000000000000000000000',\n", " 'blockNumber': None,\n", " 'from': 'one1e8rdglh97t37prtnv7k35ymnh2wazujpzsmzes',\n", " 'gas': 50000,\n", " 'gasPrice': 100000000000,\n", " 'hash': '0x279935278d20d778cbe4fdfa5d51be9eb1eb184053dc9a7cb88ad3365df73060',\n", " 'msg': {'amount': 100000000000000000000,\n", " 'delegatorAddress': 'one1e8rdglh97t37prtnv7k35ymnh2wazujpzsmzes',\n", " 'validatorAddress': 'one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3'},\n", " 'nonce': 2,\n", " 'r': '0x8660a63c10af06f2fb3f24b92cf61d4f319044a1f1931c4f4e54ce986ff563c',\n", " 's': '0x597785559c4283d3ece2df37cbf37077966487a2a2dc0f4cdbbf75a8f20bc1a8',\n", " 'timestamp': 0,\n", " 'transactionIndex': 0,\n", " 'type': 'Delegate',\n", " 'v': '0x27'}" ] }, "execution_count": 166, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transaction.get_staking_transaction_by_hash( \"0x279935278d20d778cbe4fdfa5d51be9eb1eb184053dc9a7cb88ad3365df73060\" )" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.5" } }, "nbformat": 4, "nbformat_minor": 5 }