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).
SERVICE DISABLED VETERAN OWNED SMALL BUSINESS (SDVOSB)
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
Getting Started
Before getting started, you need to have the following docker applications on your system.
- Node npm
- Docker and
- Docker-compose
Unlock the future of intelligent applications with our cutting-edge Generative AI integration services!
Step 1
The first step is to create a project directory. This will help you initiate the needed files. Type in:
mkdir dockerized-smart-contracts
&& cd dockerized-smart-contracts
npm init
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.
Type in:
truffle unbox metacoin
Step 2
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:
npm init
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.
{
“name”: “truffle-metacoin-smart-contracts”,
“version”: “0.1.0”,
“description”: “A smart contract demo project”,
“scripts”: {
“test”:”truffle test”,
“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”,
“deploy-rinkeby”:”sh ./scripts/deploy-rinkeby.sh”,
“deploy-mainnet”:”sh ./scripts/deploy-mainnet.sh”
},
“engines”: {
“node”: “12.0.0”,
“npm”: “6.9.0”
},
“devDependencies”: {
“chai”: “^4.2.0”,
“dotenv”: “^6.2.0”,
“eslint”: “^5.16.0”,
“ganache-cli”: “^6.9.1”,
“prettier”: “^2.0.5”,
“prettier-plugin-solidity”: “^1.0.0-alpha.56”,
“solhint”: “^3.2.0”,
“solhint-plugin-prettier”: “0.0.4”,
“solidity-coverage”: “^0.7.7”,
“standard”: “^12.0.1”,
“truffle”: “^5.0.30”,
“@truffle/hdwallet-provider”: “^1.0.0-web3one.5”
}}
Step 3
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.
Type in:
mkdir logs
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:
LOG_FILE=./logs/$(date “+%Y-%m-%d”)-test-log.txt
echo “[ TIMESTAMP: $(date “+%H:%M-%S”)” ]>>$LOG_FILE
truffle test | tee -a $LOG_FILEecho “———————————————————————————————————-” >> $LOG_FILE
Step 4
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.
Step 5
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:
FROM mhart/alpine-node:12
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.
ENV TZ=Asia/Kolkata
# Creating Relevant directories.
# We decide to name our project directory “smart contracts.”
WORKDIR /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”]
Small Disadvantaged Business
Small Disadvantaged Business (SDB) provides access to specialized skills and capabilities contributing to improved competitiveness and efficiency.
Step 6
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.
Type in:
version: ‘3’
services:
# Ganache-cli
ganache:
image: trufflesuite/ganache-cli
# Smart contract source
smart-contracts:
build:
context: .
dockerfile: Dockerfile.test
# The .env file should be set with necessary wallet keys and node information
env_file:
– ./.env
depends_on:
– ganache
# For bind mounts, make sure to add your absolute path location on your system.
volumes:
# Mount build folder
– /home/user/dockerize-smart-contracts/src/build:/smart-contracts/build/
# Mount log files – /home/user/dockerize-smart-contracts/logs:/smart-contracts/logs/
Step 7
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:
…
“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.