How to Write a Smart Contract

Image for post
Image for post
picture from smartledger

Introduction

This article talks about how to develop smart contract using truffle, the most active smart contract development framework on the market. Truffle is the most popular Ethereum development framework, and its mission is to make your life easier).

learning Target

1. Understanding smart contracts

2. Simple environment setup

3. Ability to write Hello World contracts using Solidity

4. Contract deployment

5. Contract interaction

What is Smart Contract?

A contract is a collection of specific address codes and data on the Ethereum blockchain, and contract accounts can pass information and actual calculations between them. The contract is stored on the blockchain in an Ethereum-specific binary format called the Ethereum Virtual Machine (EVM) bytecode. Contracts are usually written in some high-level language such as solidity, and then compiled into bytecode and uploaded to the blockchain.

Environment setup

1. Make sure Node.js is installed and the version is above 5.0

2. Install truffle

Image for post
Image for post

3. Choose an Ethereum client

It is recommended to use GANACHE (a personal blockchain for Ethereum development). With GANACHE, you can quickly understand how your application affects the blockchain and get details such as your account, balance, contract creation and gas costs.

Steps to write smart contract

1. Create a project

Open a new terminal and enter the following command

Image for post
Image for post

Project structure:

contract: The directory where smart contracts are stored

migrations: Directory for publishing smart contract scripts

test: The directory where test applications and contract files are stored

truffle.js: truffle configuration file

truffle-config.js: truffle configuration file which is used to resolve namespace conflicts under Windows.

2. Create HelloWorld contract

There are multiple ways to create a contract, here we use the truffle create command to create it. You can also create it in the contracts directory.

Image for post
Image for post

Write HelloWorld.sol as follows:

Image for post
Image for post

3. Compile the contract

Compile by using the truffle compile command:

Image for post
Image for post

4. Deployment contract

Before deployment, we start GANACHE first.

Image for post
Image for post

The system initializes 10 accounts by default, each account has 100eth. After starting GANACHE, we use the truffle create migration command to create the migration file:

Image for post
Image for post

1524738208_helloworld_migration.js in the picture is the migration file we generated through the truffle command. You can also manually create it under the migrations file. Then we modify the content of 1524738208_helloworld_migration.js as follows:

Image for post
Image for post

artifacts.require (“HelloWorld”) returns the contract abstraction that subsequent deployment scripts interact with. Be careful that HelloWorld here is the contract name, not the contract file name. Since the default configuration port of truffle is 8454 and the port of GANACHE we use is 7545, so we modify truffle.js as follows:

Image for post
Image for post

Then we deploy using the truffle migrate command:

Image for post
Image for post

After the deployment is complete, we can see that we have changed on GANACHE.

Image for post
Image for post

At this point the contract deployment is complete.

About contract interaction

truffle provides two types of interaction: truffle console and truffle develop. Here we use truffle console.

Image for post
Image for post

When you see the content in the above picture, it means that we have successfully run the truffle console in development mode, and then we get the contract instance:

Image for post
Image for post

Finally we use the sayHello method through the instance

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store