{"id":26469799,"url":"https://github.com/ocdbytes/python_solidity_integration_simplestorage","last_synced_at":"2025-09-04T02:42:16.589Z","repository":{"id":113749897,"uuid":"455950190","full_name":"ocdbytes/Python_Solidity_Integration_SimpleStorage","owner":"ocdbytes","description":"Solidity contract integrated with python and hosted on a virtual network \"Ganache\". Full working and transacting code with proper indications ","archived":false,"fork":false,"pushed_at":"2022-02-08T13:22:59.000Z","size":2104,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-19T17:09:21.856Z","etag":null,"topics":["python3","solidity","solidity-contracts","web3","web3py"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ocdbytes.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2022-02-05T18:20:27.000Z","updated_at":"2025-02-11T11:39:37.000Z","dependencies_parsed_at":"2023-03-15T11:30:38.254Z","dependency_job_id":null,"html_url":"https://github.com/ocdbytes/Python_Solidity_Integration_SimpleStorage","commit_stats":null,"previous_names":["ocdbytes/python_solidity_integration_simplestorage"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ocdbytes/Python_Solidity_Integration_SimpleStorage","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ocdbytes%2FPython_Solidity_Integration_SimpleStorage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ocdbytes%2FPython_Solidity_Integration_SimpleStorage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ocdbytes%2FPython_Solidity_Integration_SimpleStorage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ocdbytes%2FPython_Solidity_Integration_SimpleStorage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ocdbytes","download_url":"https://codeload.github.com/ocdbytes/Python_Solidity_Integration_SimpleStorage/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ocdbytes%2FPython_Solidity_Integration_SimpleStorage/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273543526,"owners_count":25124336,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-04T02:00:08.968Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python3","solidity","solidity-contracts","web3","web3py"],"created_at":"2025-03-19T17:09:26.189Z","updated_at":"2025-09-04T02:42:16.566Z","avatar_url":"https://github.com/ocdbytes.png","language":"Python","readme":"\u003cimg src=\"Web3%20python%20-%20Simple%20Storage%2059346cc950614329ac52723f5783e7a6/main.png\" width=\"100%\"\u003e\u003c/img\u003e\n\n# Web3 python - Simple Storage\n\n## Compiling the Solidity Code from Python\n\n```python\n# install py-solc-x for compiling solidity file\nimport json\nfrom solcx import compile_standard, install_solc\ninstall_solc(\"0.6.0\")\n\n# Getting file\nwith open(\"./SimpleStorage.sol\", 'r') as file:\n    simple_storage_file = file.read()\n\n# Compile our solidity code\n\ncompiled_sol = compile_standard(\n    {\n        \"language\": \"Solidity\",\n        \"sources\": {\"SimpleStorage.sol\": {\"content\": simple_storage_file}},\n        \"settings\": {\n            \"outputSelection\": {\n                \"*\": {\n                    \"*\": ['abi', 'metadata', 'evm.bytecode', 'evm.sourceMap']\n                }\n            }\n        }\n    },\n    solc_version=\"0.6.0\",\n)\n\nwith open(\"compiled__code.json\", 'w') as file:\n    json.dump(compiled_sol, file)\n```\n\n- First we open the file\n- then compile the code using the above compile_standard syntax\n- Then dump the contents into a new file named “compiled_sol” using json.dump\n\n## Getting our ByteCode and ABI\n\nIn order to deploy our contract we need the byte code and abi and we will get it from our compiled_sol\n\n```python\n# install py-solc-x for compiling solidity file\nimport json\nfrom solcx import compile_standard, install_solc\ninstall_solc(\"0.6.0\")\n\n# Getting file\nwith open(\"./SimpleStorage.sol\", 'r') as file:\n    simple_storage_file = file.read()\n\n# Compile our solidity code\n\ncompiled_sol = compile_standard(\n    {\n        \"language\": \"Solidity\",\n        \"sources\": {\"SimpleStorage.sol\": {\"content\": simple_storage_file}},\n        \"settings\": {\n            \"outputSelection\": {\n                \"*\": {\n                    \"*\": ['abi', 'metadata', 'evm.bytecode', 'evm.sourceMap']\n                }\n            }\n        }\n    },\n    solc_version=\"0.6.0\",\n)\n\nwith open(\"compiled__code.json\", 'w') as file:\n    json.dump(compiled_sol, file)\n\n# get bytecode\n# here we are just extracting the data from our compiled solidity code\nbytecode = compiled_sol[\"contracts\"][\"SimpleStorage.sol\"][\"SimpleStorage\"][\"evm\"][\"bytecode\"][\"object\"]\n\n# get abi\nabi = compiled_sol[\"contracts\"][\"SimpleStorage.sol\"][\"SimpleStorage\"][\"abi\"]\n```\n\n# Now we need something in which we are going to deploy our code virtually so we will do it using “Ganache”\n\n[Truffle Suite - Truffle Suite](https://trufflesuite.com/ganache/)\n\nDownload It\n\n- Now we will click on Quickstart Button in Ganache\n- We will get a screen like this\n\n![Screenshot 2022-02-05 at 7.32.33 PM.png](Web3%20python%20-%20Simple%20Storage%2059346cc950614329ac52723f5783e7a6/Screenshot_2022-02-05_at_7.32.33_PM.png)\n\n- Install [Web3.py](http://Web3.py)\n\n```bash\npip install web3\n```\n\n## Now we will see how we will connect our blockchain contract with a network (Ganache)→\n\n1. Import Web3 Library in our code\n\n```python\nfrom web3 import Web3\n```\n\n1. create a HTTP Provider and connect to blockchain\n\n```python\nw3 = Web3(Web3.HTTPProvider(\"network_link_here\"))\n# Here we are connected to our Ganache network\n```\n\n1. Write your creds\n\n```python\n# Our credentials\nchain_id = 1337\nmy_address = \"your_address\"\nprivate_key = \"your_private_key\"\n```\n\n1. Create a contract in python\n\n```python\n# Create Contract in Python\nSimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)\n# print(SimpleStorage)\n# This will print \u003cclass 'web3._utils.datatypes.Contract'\u003e\n```\n\n1. Get the nonce (It is defined as the total number of transactions done on an account)\n\n```python\n# Get latest transaction\nnonce = w3.eth.getTransactionCount(my_address)\n```\n\n## How to send a transaction →\n\n1. Build a transaction\n2. Sign a transaction\n3. send the transaction\n\n```python\n# transaction building\ntransaction = SimpleStorage.constructor().buildTransaction({\n    \"chainId\": chain_id,\n    \"from\": my_address,\n    \"nonce\": nonce,\n    \"gasPrice\": w3.eth.gas_price\n})\n# print(transaction)\n\n# signing the transaction\nsigned_transaction = w3.eth.account.sign_transaction(\n    transaction, private_key=private_key)\n# print(signed_transaction)\n\n# sending the signed transaction\ntransaction_hash = w3.eth.send_raw_transaction(\n    signed_transaction.rawTransaction)\n\n# wait for block confirmations\ntransaction_recipt = w3.eth.wait_for_transaction_receipt(transaction_hash)\nprint(transaction_recipt)\n```\n\n# Working with the contract →\n\nFor working with contract we need :\n\n- Contract Address\n- Contract ABI\n\n1. Getting the deployed contract Address\n\n```python\n# Getting the deployed contract\nsimple_storage = w3.eth.contract(\n    address=transaction_recipt.contractAddress, abi=abi)\n```\n\n1. We can interact in two ways with our Contract :\n\n- Call → Simulate making the call and getting the value (no state change involved)\n- Transact -\u003e actually make a state change\n\nCall Functions\n\n```python\nprint(simple_storage.functions.retrieve().call())\n```\n\nTransact Functions\n\n```python\n# how to make a transaction on our contract\nstorage_transaction = simple_storage.functions.store(15).buildTransaction({\n    \"chainId\": chain_id,\n    \"from\": my_address,\n    \"nonce\": nonce + 1, # using nonce + 1,beacuse nonce value is already used once so we have to change it in order to make a transaction\n    \"gasPrice\": w3.eth.gas_price\n})\nsigned_storage_transaction = w3.eth.account.sign_transaction(\n    storage_transaction, private_key=private_key\n)\nsend_storage_transaction = w3.eth.send_raw_transaction(\n    signed_storage_transaction.rawTransaction)\nprint(\"Store(15) transaction done ✅\")\n\ntransaction_recipt = w3.eth.wait_for_transaction_receipt(\n    send_storage_transaction)\nprint(\"Transaction Recipt -\u003e\")\nprint(transaction_recipt)\n```\n\n# Final Code →\n\n```python\n# install py-solc-x for compiling solidity file\nimport os\nimport json\nfrom web3 import Web3\nfrom dotenv import load_dotenv\nfrom solcx import compile_standard, install_solc\ninstall_solc(\"0.6.0\")\n\nload_dotenv()\n\n# Getting file\nwith open(\"./SimpleStorage.sol\", 'r') as file:\n    simple_storage_file = file.read()\n\n# Compile our solidity code\n\ncompiled_sol = compile_standard(\n    {\n        \"language\": \"Solidity\",\n        \"sources\": {\"SimpleStorage.sol\": {\"content\": simple_storage_file}},\n        \"settings\": {\n            \"outputSelection\": {\n                \"*\": {\n                    \"*\": ['abi', 'metadata', 'evm.bytecode', 'evm.sourceMap']\n                }\n            }\n        }\n    },\n    solc_version=\"0.6.0\",\n)\n\nwith open(\"compiled__code.json\", 'w') as file:\n    json.dump(compiled_sol, file)\n\n# get bytecode\n# here we are just extracting the data from our compiled solidity code\nbytecode = compiled_sol[\"contracts\"][\"SimpleStorage.sol\"][\"SimpleStorage\"][\"evm\"][\"bytecode\"][\"object\"]\n\n# get abi\nabi = compiled_sol[\"contracts\"][\"SimpleStorage.sol\"][\"SimpleStorage\"][\"abi\"]\n\n# for connecting to ganache\n\n# HTTP Provider (for connecting to blockchain)\nw3 = Web3(Web3.HTTPProvider(\"HTTP://127.0.0.1:7545\"))\nprint(\"Blockchain Connected....\")\n# Our credentials\nchain_id = 1337\nmy_address = os.getenv(\"MY_ADDRESS\")\nprivate_key = os.getenv(\"PRIVATE_KEY\")\n\n# Create Contract in Python\nSimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)\n# print(SimpleStorage)\n# This will print \u003cclass 'web3._utils.datatypes.Contract'\u003e\nprint(\"Contract created using bytecode and abi\")\n# Get latest transaction\nnonce = w3.eth.getTransactionCount(my_address)\n# print(nonce)\n\n# 1. Build a Transaction\n# 2. Sign a Transaction\n# 3. Send a Transaction\n\n# transaction building\ntransaction = SimpleStorage.constructor().buildTransaction({\n    \"chainId\": chain_id,\n    \"from\": my_address,\n    \"nonce\": nonce,\n    \"gasPrice\": w3.eth.gas_price\n})\nprint(\"Transaction build successfull 🎯\")\n\n# signing the transaction\nsigned_transaction = w3.eth.account.sign_transaction(\n    transaction, private_key=private_key)\n# print(signed_transaction)\n\n# sending the signed transaction\ntransaction_hash = w3.eth.send_raw_transaction(\n    signed_transaction.rawTransaction)\n\n# wait for block confirmations\ntransaction_recipt = w3.eth.wait_for_transaction_receipt(transaction_hash)\n# print(transaction_recipt)\nprint(\"Transaction success ✅\")\n\n# Working with the Contract -\u003e\n\n# for working with contract we always need :\n# * Contract Address\n# * Contract ABI\n\n# Getting the deployed contract\nsimple_storage = w3.eth.contract(\n    address=transaction_recipt.contractAddress, abi=abi)\n\nprint(\"Contract for interaction initialised 🗳️\")\n\n# We can interact in two ways with our Contract :\n# Call -\u003e Simulate making the call and getting the value (no state change involved)\n# Transact -\u003e actually make a state change\n\n# initial value of favoriteNumber (from contract)\nprint(\"-------------------------------------------\")\nprint(\"Initial value (Favorite Number) : \")\nprint(simple_storage.functions.retrieve().call())\nprint(\"-------------------------------------------\")\n# print(simple_storage.functions.store(15).call())\n\n# how to make a transaction on our contract\nstorage_transaction = simple_storage.functions.store(15).buildTransaction({\n    \"chainId\": chain_id,\n    \"from\": my_address,\n    \"nonce\": nonce + 1,\n    \"gasPrice\": w3.eth.gas_price\n})\nsigned_storage_transaction = w3.eth.account.sign_transaction(\n    storage_transaction, private_key=private_key\n)\nsend_storage_transaction = w3.eth.send_raw_transaction(\n    signed_storage_transaction.rawTransaction)\nprint(\"Store(15) transaction done ✅\")\n\ntransaction_recipt = w3.eth.wait_for_transaction_receipt(\n    send_storage_transaction)\nprint(\"Transaction Recipt -\u003e\")\nprint(transaction_recipt)\n\nprint(\"-------------------------------------------\")\nprint(\"Final value (Favorite Number) : \")\nprint(simple_storage.functions.retrieve().call())\nprint(\"-------------------------------------------\")\n```\n\n### Output →\n\n```python\nBlockchain Connected....\nContract created using bytecode and abi\nTransaction build successfull 🎯\nTransaction success ✅\nContract for interaction initialised 🗳️\n-------------------------------------------\nInitial value (Favorite Number) :\n0\n-------------------------------------------\nStore(15) transaction done ✅\nTransaction Recipt -\u003e\nAttributeDict({'transactionHash': HexBytes('0x4b6a97ff70a77c5ce62c6e59e9d6d8a02a0af629fb192b7fc32ad48b18b78050'), 'transactionIndex': 0, 'blockHash': HexBytes('0xf6b1bca9c9ca1290798cec30a0b90fd872be0459e3030f63f184b3df9d2f0e84'), 'blockNumber': 16, 'from': '0x3e8683715c1455d051Cd0BFf6f6e087b29823529', 'to': '0xd302C0D225B08f4ed5dEE368902F247353629C45', 'gasUsed': 41518, 'cumulativeGasUsed': 41518, 'contractAddress': None, 'logs': [], 'status': 1, 'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')})\n-------------------------------------------\nFinal value (Favorite Number) :\n15\n-------------------------------------------\n```\n\n![Screenshot 2022-02-05 at 11.42.51 PM.png](Web3%20python%20-%20Simple%20Storage%2059346cc950614329ac52723f5783e7a6/Screenshot_2022-02-05_at_11.42.51_PM.png)\n\n# Yay!! We have integrated our First Contract with Python 🎉🍻\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Focdbytes%2Fpython_solidity_integration_simplestorage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Focdbytes%2Fpython_solidity_integration_simplestorage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Focdbytes%2Fpython_solidity_integration_simplestorage/lists"}