Smart contracts can be very complicated to work with – and even more so when it comes to smart contract development. There are a number of things that can go wrong, a number of elements to consider, and the near-constant focus you need, for hours, to develop. Even the smallest mistakes can lead to wasted time and effort. If you are on the clock, you may end up missing your deadlines.
Docker is an open-source containerization platform that allows developers to package the content of development into executable components known as “containers.” This process is known as containerization of the source code. Each container acts as an independent standardized component. Combined, these containers can run almost any environment – including a smart contract for your cryptocurrency.
This article will go over how you can automate smart contract development with the help of Docker. Not only will it help you improve your overall development procedure, but also the time it takes to create a new contract and improve your cryptocurrency (with respect to the latest development).
Smart Contract Development with Docker
To start the automated development process, you will need:
- A testRPC network. This network will be used primarily for testing.
- Truffle + web3 environment. This will help you establish communication between contract nodes and ultimately help you deploy it.
- Storage to gather and store transaction receipts, usage and development logs, and more.
- Contract linting
- Staging preparation.
In this guide, we will help you reach the beta phase, test it with respect to the environment you are going to run it in, and run the contract within minutes. You can use a range of coins and development suites for this. For the sake of simplicity, we have used Metacoin (MTC) project template that you can find within Trufflebox.
With this automated Docker, you will be able to:
- Run a smart contract test with the testRPC we mentioned above – or any smart contract you develop
- Deploys the smart contracts on any network
- Update running smart contracts via your local systems.
- Collect usage and data logs
- Use the usage and data logs collected for future projects and evaluation
Before getting started, you need to have the following docker applications on your system.
- Node npm
- Docker and
The first step is to create a project directory. This will help you initiate the needed files. Type in:
&& cd dockerized-smart-contracts
mkdir src && cd src
The src folder will open up, and you will need to navigate to your primary smart contract repo. As mentioned above, we are using metacoin for this guide, but you can use any that you like.
truffle unbox metacoin
Now, you need to download the project repo from the truffle. You will need to start package.json from within the src folder. To do that, type in:
The src/package.json will open up. You will need to fill it up. The standard scripts that you will need to use for smart contract development. Either copy/paste from below, use truffle docs from the suite, or write your own.
“description”: “A smart contract demo project”,
“compile”: “truffle compile”,
“networks”: “truffle networks”,
“lint”: “solhint -f table contracts/**/*.sol”,
“lint-auto-fix”: “prettier –write contracts/**/*.sol”,
“coverage”: “truffle run coverage”,
“eslint-auto-fix”: “./node_modules/.bin/eslint –fix .”,
“deploy”: “sh ./scripts/test-contract.sh”,
Now, let’s run some scripts. The goal is to set up network deployment and logging functionalities. Again, feel free to change the script to your liking, copy/paste the one below, or from truffle docs.
mkdir scripts && cd scripts
touch test-contract.sh deploy-rinkeby.sh
As test-contract.sh opens, you will need to write the testing script. The goal here is to create a logfile that will start logging every activity, starting from today’s date. Type in:
echo “[ TIMESTAMP: $(date “+%H:%M-%S”)” ]>>$LOG_FILE
truffle test | tee -a $LOG_FILEecho “———————————————————————————————————-” >> $LOG_FILE
Go back to the dockerized smart contract direction (where the first package.json was). Type:
touch Dockerfile.test docker-compose.yml
This will create a Dockerfile & docker-compose file for the smart contract you are trying to make within the src.
Now, you need to create layers within the smart contract repo. The Dockerfile script next will help you create a virtual image for this project and deploy it whenever you run container(s) from the image created. Type in:
RUN apk add –no-cache make gcc g++ python
RUN apk add –update tzdata
Change it to your timezone. This’ll come handy to monitor logfiles.
# Creating Relevant directories.
# We decide to name our project directory “smart contracts.”
RUN mkdir logs logs/test-network logs/main-network logs/test
# Copy the package.json & lock file & install the relevant packages
COPY src/package.json /smart-contracts/package.json
COPY src/package-lock.json /smart-contracts/package-lock.json
RUN npm install
# Add smart-contract relevant code to directory
COPY src/ /smart-contracts/
# Give executable permission to scripts.
RUN chmod +x ./scripts/*.sh
ENTRYPOINT [“npm”, “run”]CMD [“deploy”]
This step involves setting up the docker-compose.yml. If you haven’t already installed the composer, do so now. It will help you bundle all the containers you create and establish dependencies thereafter.
# Smart contract source
# The .env file should be set with necessary wallet keys and node information
# For bind mounts, make sure to add your absolute path location on your system.
# Mount build folder
# Mount log files – /home/user/dockerize-smart-contracts/logs:/smart-contracts/logs/
Now to add some scripts into the./dockerize-smart-contracts/package.json file. These scripts will help you set up our run containers via docker-compose. Following are the scripts:
“setup”: “docker-compose up –no-start –build”,
“start”: “docker-compose run smart-contracts test && dokcer-compose up –build -d ganache”,
“stop”: “docker-compose stop”,
Final Step for Automating Smart Contract Development with Docker
This step involves getting ready with the smart-contract project and testing it.
npm run setup
You will need to start the containers to be the test on your network. To do that, type:
npm run start
With this, the smart-contract container will start and execute the test. You won’t have to do anything further. Instead, Docker will run automated tests for you easily. All you have to do is change the parameters of your smart contract, run the test, and see the difference.
Contact us for services and solutions related to Automating Smart Contract Development with Docker.
Further blogs within this Automating Smart Contract Development with Docker category.