Published July 6, 2024 | Version v2
Software Open

FunRedisp Artifact

  • 1. ROR icon Nanjing University of Science and Technology

Description

# FunRedisp
FunRedisp is a function dispatch refactoring tool to reduce the overall invocation gas consumption of Solidity smart contracts. There is a [video](https://youtu.be/NgX8z8n3up4
) showing how to use FunRedisp in detail.
- Section [Functionality guide](#functionality-guide) provides the steps to apply FunRedisp on a dataset contains 50 real-world smart contracts, which validates the functionality of FunRedisp. For time-efficiency, it also provides [A demo for quick test](#a-demo-for-quick-test) which contains only one contract.
- Section [Reusability guide](#reusability-guide) provides the steps for developers to use FunRedisp to refactor their Solidity smart contracts.

## Contents
- [Hardware dependencies](#hardware-dependencies)
- [Software dependencies](#software-dependencies)
- [Getting started](#getting-started)
- [Reusability guide](#reusability-guide)
  - [Step by step](#step01)
- [Functionality guide](#functionality-guide)
  - [Dependencies](#dependencies)
  - [Step by step](#step02)
  - [A demo for quick test](#a-demo-for-quick-test)
  - [Outputs](#outputs)
- [Troubleshooting](#troubleshooting)
- [Security](#security)

## Hardware dependencies
To evaluate FunRedisp, the following hardware dependencies are required:
- A computer with at least 4 GB of RAM and a modern multi-core processor.
- Approximately 500 MB of free disk space.


## Software dependencies
- FunRedisp is easy to use with **PyCharm IDE** in the **Python 3.8** environment.
- The recommended operating system for running FunRedisp is Windows 10.

## Getting started

- Import the project of FunRedisp to PyCharm.
- Enter the terminal of PyCharm, input and run the command `pip3 install -r requirements.txt` to install all the required packages.
- There is already an example contract under  `FunRedisp/for_developers/contracts2refactor`, run the file `contract_refactor.py` and you can get its refactored bytecode under `FunRedisp/for_developers/refactored_contracts` after about two seconds.


## Reusability guide
Developers who own Solidity smart contract source code can use FunRedisp to refactor the contract for saving invocation gas fees. They just need to follow the steps below.

### Step-by-step <a id="step01"></a>

1. Copy the contract source code file to the directory: `FunRedisp/for_developers/contracts2refactor`, it should be in the form of `.txt` file and **the name of the file need to be as same as the main contract name**.

2. Run file `contract_refactor.py` which is under `FunRedisp/for_developers/`.

3. After about two seconds, the refactored bytecode will be generated in the directory `FunRedisp/for_developers/refactored_contracts`, which can be deployed directly. The name of the bytecode file is same as the source code file.


## Functionality guide
For contracts that have already been deployed on Ethereum with transaction histories, FunRedisp can refactor them, simulate deployment and transaction invocation to compare the savings of gas consumption.
**Here we present a dataset of 50 real-world Solidity smart contracts to validate the functionality of FunRedisp.** It took us more than ten hours to run the transaction records of all these contracts and conduct statistics.
If you don't have that much time to run it, we also provide [A demo for quick test](#a-demo-for-quick-test). The results for these 50 contracts can be refered to [Outputs](#outputs), and you can check them directly in the artifact.


### Dependencies

Due to the large number of historical transactions of these contracts and their continuous growth, we need to access their transaction histories through the Internet. There are some specific dependencies and steps.

#### Ganache  
Install Ganache from  [here](https://archive.trufflesuite.com/ganache/).

#### Infura account  
Acquire Infura account from [here](https://infura.io/).

#### Etherscan api key
Acquire Etherscan api key from [here](https://etherscan.io/apis).  

### Step-by-step <a id="step02"></a>

1. One needs to modify **apikey**, **Infura https**, and **proxies** in the following files:  
  - `Data_process.py`
  - `Function_rearrangement.py` ( This file does not require apikey. )  
  - `Running_simulation.py` ( Note that the HTTP node that needs to be modified in this file should be as same as the port node of Ganache to ensure successful connection with it. )  

2. In order to link Ganache to test the performance of FunRedisp, it is also necessary to modify the variable **private_key** in file `Runing_simulation.py` to be the private key of the first account in your Ganache.

3. Make sure you connect to the internet before running FunRedisp.  

4. Run the file `main.py`.

We have already run it in advance and present the result of gas consumption for each contract. For detailed results, please refer to [Outputs](#outputs).

### A demo for quick test  
Due to too many transaction histories for these 50 contracts, it will result in a long testing time. Therefore, we give a demo that uses contract **ICashToken** (address: 0x2F7e7E8029E1c4d4280122Fe74F7453BeE0dDEE1) to **quickly validate the functionality of FunRedisp**.

- After importing the project of FunRedisp and the dependencies are configured as same as [Step by step](#step02), please run the file `Demo.py`.

### Outputs
The outputs of FunRedisp are stored in some folders, which includes the following sub-folders:  

- The folder `deployment_gas` lists the deployment gas consumption for each contract in the experiment before optimization.  

- The folder `new_deployment_gas` lists the deployment gas consumption of each contract in the experiment after optimization.

- The folder `Gas_consumption` contains the gas consumption of each transaction for each contract in the experiment before and after optimization, where each file contains the gas consumption before optimization (first line), the gas consumption after optimization (second line), and the gas savings (third line) of the corresponding contract.


## Troubleshooting  
- If you encounter an error while using the "pip install" command to install web3 suit on Windows, it may be because you haven't installed gcc. To resolve this issue, you can first install Microsoft Visual C++.

- One can also try to install web3 suit from [here](https://pypi.org/project/web3/#files).

## Security
For security reasons, it is recommended that users apply their own Infura account and apikey before running FunRedisp.





Files

FunRedisp.zip

Files (41.9 MB)

Name Size Download all
md5:0b9d579584a43608d81c570ff2ec696b
41.9 MB Preview Download