Web3 Solidity Connector

Overview

Web3 Solidity Connector

With this project, you can compile your sol files and create new transactions including creating contract and calling the state changer functions. You can integrate integrate your sol files with Python and you can call functions with using Python.

Program Life Cycle

  1. Compile the Solidity(.sol) file
  2. Deploy the contract which is in Solidity file
  3. Manipulate the main.py file for calling and executing relevant functions in contract even with parameters via the help of Web3

Folder Structure

To assure the program is working, there are folder structure rules to follow.

  1. This projects points to sol_files folder for your Solidity files. This means sol_files folder must contain your .sol extensioned files. You should select one of the sol file in this directory to be compiled.

  2. After you execute compile.py, "compilation_files_out" folder will be created which contains your output files. "compiled_abi.json" and "compiled_bytecode.txt" files should not be deleted or overwritten! You can examine your compiled code in "compiled_code.json" file.

  3. global_variables.py file contains your default paths for compilation files and the sol files that will be compiled. You can change this structure any way you want.

GLOBAL_COMPILATION_PATH = "./compilation_files_out"  # folder that contains output files
GLOBAL_SOL_PATH = "./sol_files"  # folder that contains sol file

Running the Program

  1. Clone the repository
git clone https://github.com/TekyaygilFethi/ContractDeploment.git
  1. Create an .env file on current folder that contains your address(with MY_ADDRESS key), private key(with PRIVATE_KEY key), rinkeby rpc url(with RINKEBY_RPC_URL key) and chain id(with CHAIN_ID key) values. Your .env file should look like this:
PRIVATE_KEY ="0x{YOUR PRIVATE KEY}"
RINKEBY_RPC_URL = "{YOUR RINKEBY RPC URL}"
MY_ADDRESS = "{YOUR ADDRESS}"
CHAIN_ID = "{YOUR CHAIN ID}"
  1. Install the dependencies from requirements.txt file.
pip install -r requirements.txt
  1. After setting the .env file, to run the program, you first need to go to the project directory and run:
python compile.py {YOUR_SOL_FILE} // python compile.py SimpleContract.sol

! Please note that your sol files must be in the folder sol_files folder by default or in the folder you specified custom in global_variables.py file by assigning to GLOBAL_SOL_PATH.

  1. After compilation you should see screen like this:
Compilation folder created!
Compiled successfully!
  1. When you check your folders, you can see compilation_files_out folder is created. If you changed the folder path and name from global_variables you may see different folder. This folder be based on when deploying your contracts and running your Solidity functions!

  2. For next step, you must deploy your compiled contract. To do this, you must run:

python deploy.py

This command will creates a transaction for contract creation based on your compiled Solidity file. This command will output the success message, transaction receipt and contract address. To use this deployed contract and it's functions, you must copy the address of this deployed contract. You should see response like this (Please note that receipt and address may differ)

New Contract Transaction has been created!

AttributeDict({'transactionHash': HexBytes('0x19f1237cd0bf13bf1112f7e60b9dd7570dcca38c18718368e09c462e01482272'), 'transactionIndex': 0, 'blockHash': HexBytes('0xa47912b38dec2fdecfed283da5fd6a7d778def3f62bc2c629373903cbd5f59bc'), 'blockNumber': 34, 'from': '0x2DAc2487DD401D9E5C757eb03B8928b70FFaFe6e', 'to': None, 'gasUsed': 640222, 'cumulativeGasUsed': 640222, 'contractAddress': '0x874E06Aff5a1031Bd5AE07100A7A518D0C72b8E2', 'logs': [], 'status': 1, 'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')})

Contract Address: 0x874E06Aff5a1031Bd5AE07100A7A518D0C72b8E2 //This address you should copy.
  1. Edit your main.py content according to your functions. For example, I have addHero function in my compiled Solidity:
struct Hero {
    string name;
    string lightsaberColor;
    uint256 age;
}

Hero[] heroes;

function addHero(
        string memory _name,
        string memory _lightsaberColor,
        uint256 _age
    ) public {
        heroes.push(Hero(_name, _lightsaberColor, _age));
        uint256 idx = heroes.length - 1;
        nameToIndex[_name] = idx;
    }

You can call this function from my main.py file with parameters like this:

# write functions with their parameters if any after this line inside of executeContractFunction method.
contractOps.executeContractFunction(
    # write your contract functions as contract.functions.{your function}, your private key
    contract.functions.addHero("Obi-Wan Kenobi", "Blue", 29),
    private_key,
)

Here, contractOps is an object that allows you to perform contract operations such as creating, deploying, gathering contracts or executing a function inside a contract. And executeContractFunction is a special function that allow you to execute a functions. It creates, signs, sends and gets the receipt for transaction automatically.

  • If you have a function that is not changing a state in Solidity file you also can call it. For example here's the function that is not changing state in my Solidity file:
function getInfoByName(string memory name)
        public
        view
        returns (Hero memory)
    {
        uint256 idx = nameToIndex[name];
        return heroes[idx];
    }

function getAllHeroes() public view returns (Hero[] memory) {
        return heroes;
    }

You can call the getAllHeroes function like this:

print(contract.functions.getAllHeroes().call())

You can call the getInfoByName function which takes parameter like this:

print(contract.functions.getInfoByName("Obi-Wan Kenobi").call())

Please note that we had to use .call() at the end of the function call to gather response and make the function call.

  1. To run main.py file, you need to supply contract address. You should use the contracty address you copied at Step 6.
python main.py {ContractAddress}

Here is an example:

python main.py 0x874E06Aff5a1031Bd5AE07100A7A518D0C72b8E2

And you can see the results when you execute this command: Result

And you're done! Congratulations!

Owner
Fethi Tekyaygil
.NET Core Backend & @google Certified #tensorflow Developer - Flutter & Solidity #padawan - Animal Person
Fethi Tekyaygil
hey, this repo is the backend of the sociio project

sociio backend Hey, this repository is a part of sociio project , In this repo we are working to create an independent server for everything you can i

2 Jun 09, 2022
Powerful Assistant

Delta-Assistant Hi I'm Phoenix This project is a smart assistant This is the 1.0 version of this project I am currently working on the third version o

1 Nov 17, 2021
Hotpile: High Order Turing Machine Language Compiler

Hotpile: High Order Turing Machine Language Compiler Build and Run Requirements: Python 3.6+, bison, flex, and GCC installed. Needs to be run under UN

Jiang Weihao 4 Dec 29, 2021
Draw random mazes in python

a-maze Draw random mazes in python This program generates and draws a rectangular maze, with an entrance on one side and one on the opposite side. The

Andrea Pasquali 1 Nov 21, 2021
Python Programmma DarkMap.py

DarkMap Python Programmma DarkMap.py O'rganish va rasmlarni ko'riosh https://drive.google.com/drive/folders/1l1zybs_0Zy9z_trZYz5R72WrwsE6mFOh?usp=shar

Og'abek 0 May 06, 2022
This is a spamming selfbot that has custom spammed message and @everyone spam.

This is a spamming selfbot that has custom spammed message and @everyone spam.

astro1212 1 Jul 31, 2022
Freeze your objects in python

gelidum Freeze your objects in python. Latin English Caelum est hieme frigidum et gelidum; myrtos oleas quaeque alia assiduo tepore laetantur, asperna

Diego J. 51 Dec 22, 2022
Practice10 - Operasi String With Python

Operasi String MY SOSIAL MEDIA : Apa itu Python String ? String adalah urutan si

Maulana Reza Badrudin 1 Jan 05, 2022
A fast python implementation of DTU MVS 2014 evaluation

DTUeval-python A python implementation of DTU MVS 2014 evaluation. It only takes 1min for each mesh evaluation. And the gap between the two implementa

82 Dec 27, 2022
Possible solutions to Wordscapes, a mobile game for the android operating system, downloadable from the play store

Possible solutions to Wordscapes, a mobile game for the android operating system, downloadable from the play store

Clifford Onyonka 2 Feb 23, 2022
Python flexible slugify function

Python flexible slugify function

Dmitry Voronin 471 Dec 20, 2022
The Google Assistant on a rotary phone

Google Assistant Rotary Phone Shoutout to my dad who had this idea a year ago and I'm only now getting around to doing it. Notes This is the code used

rydercalmdown 10 Nov 04, 2022
TallerStereoVision Convencion Python Chile 2021

TallerStereoVision Convencion Python Chile 2021 Taller Stereo Vision & Python PyCon.cl 2021 Instalación Se recomienta utilizar Virtual Environment pyt

2 Oct 20, 2022
A gamey, snakey esoteric programming language

Snak Snak is an esolang based on the classic snake game. Installation You will need python3. To use the visualizer, you will need the curses module. T

David Rutter 3 Oct 10, 2022
Basic repository showing how to use Hydra + Hydra launchers on SLURM cluster

Slurm-Hydra-Submitit This repository is a minimal working example on how to: setup Hydra setup batch of slurm jobs on top of Hydra via submitit-launch

Raphael Meudec 2 Jul 25, 2022
Sudoku-Solver

Sudoku-Solver This is a personal project, that put all my today knowledges to the test, is a project that im developing alone with a lot of effort and

Carlos Ismael Gitto Bernales 5 Nov 08, 2021
Random Turkish name generator with realistic probabilities.

trnames Random Turkish name generator with realistic probabilities. Based on Trey Hunner's names package. Installation The package can be installed us

Kaan Öztürk 20 Jan 02, 2023
Python library for creating and parsing HSReplay XML files

python-hsreplay A python module for HSReplay support. https://hearthsim.info/hsreplay/ Installation The library is available on PyPI. pip install hsre

HearthSim 45 Mar 28, 2022
Notifies server owners of mod updates, also notifies of player deaths and player joins through Discord.

ProjectZomboid-ServerAssistant Notifies server owners of mod updates, also notifies of player deaths and player joins through Discord. A Python based

3 Sep 30, 2022
A normal phoneNumber tracker made with python.

A normal phoneNumber tracker made with python.

CLAYZANE 2 Dec 30, 2021