Writing a smart contract

Once all the requisite software is installed, we can start writing a smart contract. First, we will create a new directory, and then initialize it with the Truffle development tool:

$ mkdir my_first_smart_contract
$ cd my_first_smart_contract
$ truffle init

The output of the truffle init command is as follows:

This will command Truffle to initialize your directory to be a smart contract development project. A couple of directories are  available to you when developing a smart contract in this project directory:

$ ls
contracts migrations test truffle-config.js

You usually incorporate a smart contract's source code in the contracts folder. The migrations folder holds the files that are used in the deployment of smart contracts, and the test folder holds the test files. You can configure the smart contract deployment settings in the truffle-config.js file. We will create the first smart contract and name it donation.sol using the following code:

pragma solidity ^0.5.0;

contract Donation {
address public donatur;
address payable donatee;
uint public money;
string public useless_variable;

constructor() public {
donatee = msg.sender;
useless_variable = "Donation string";
}

function change_useless_variable(string memory param) public {
useless_variable = param;
}

function donate() public payable {
donatur = msg.sender;
money = msg.value;
}

function receive_donation() public {
donatee.transfer(address(this).balance);
}
}

If you are new to smart contracts, there may be some unfamiliar keywords in the preceding example. In this chapter, we are not going to discuss everything to do with Solidity. Instead, we will only look into the features of Solidity that are necessary for building a smart contract and learning the concept of a smart contract.

But first, let's compile this smart contract written in Solidity to Ethereum bytecode and an application binary interface (abi). To do this, we will run the following command in the Truffle project directory:

$ truffle compile

The result of the compilation can be seen in the build/contracts folder, named Donation.json:

If you open the file, you can see a number of interesting things. This .json file is 1,530 lines long. The json object in this file has 14 keys. You only need to think about two keys for now. The first one is the interface (called abi), and the second one is the binary that can be executed on the Ethereum Virtual Machine (called bytecode). Refer to the code file in the following GitLab link for the code in this section: https://gitlab.com/arjunaskykok/hands-on-blockchain-for-python-developers/blob/master/chapter_02/my_first_smart_contract/build/contracts/Donation.json.

We cannot run this binary file in the same way as when we compile the C code and execute the binary directly. We need to put this binary into the Ethereum virtual machine. The interface itself is needed for us to interact with the smart contract later when we develop a decentralized application. When you deploy a smart contract to Ethereum blockchain, you need the bytecode. When you want to interact with a smart contract already deployed in Ethereum blockchain, you need the abi interface.