If you have delved into Ethereum development, no doubt that you have come across terms such as ICOs, Metamask, Remix, and Smart Contracts.
Some companies use Smart Contracts to try out a network, while others give you a yellow paper to read, and others recommend that you read a truffle suite. This can leave you confused as to where to start.
There’s so much going on in this world. With the name calling among professionals on Twitter, dangerous protocols, standards and tools which have not been tested and still have bugs, there’s still a lot that hasn’t been discovered yet and everyone is still trying to chart their own way. What will happen to banks, institutions and governments in future will be determined by developers. This is so fascinating.
No need for you to fret any longer. I will do my best to explain everything to you through this tutorial. I will explain to you about Smart Contracts and development of dapps and how everything works together.
However, I won’t go into the minutest of details, instead I will link to some articles, so you can decide if you want to explore these links and do some further reading on your own, so that you can understand the principles that have been discussed here in a better way. The main aim of this tutorial is to show you how in a simple way how everything is interconnected.
What is Ethereum?
If you are unfamiliar with the terms blockchain, Bitcoin, Ethereum and cryptocurrency, then listen to this podcast by Tim Ferris interviewing Nick Sabo and Naval Ravikant talk about The Quite Master of Cryptocurrency.
What Are Smart Contracts?
Smart Contracts are scripts that have been set up in Ethereum to handle cash.
The people who are responsible for setting up these contracts and certifying them are known as miners. Miners are many computers that add a contract to a public ledger that is known as a block. When many blocks are put together, this is referred to as blockchains.
Miners are usually paid a Gas. This is payment for running the contract. Anytime you create a smart contract or you carry out any transaction or send money into a different account, you are required to make payment in the form of ether. This is what’s converted into Gas.
What to Know About the Smart Contracts Development
You should understand that this tutorial is not for newbies. You must have some basic understanding of software development and be familiar with JavaScript. If you don’t know any of these languages, then you will struggle to follow along. In this tutorial we will use Solidity. This is a programming language that is almost similar to JavaScript. Keep in mind that most developments that are done on Ethereum use JavaScript and Node.JS. So, if you are familiar with both of these languages, then you will understand a lot of what is going to be discussed here. I am going to give some links for other tutorial, so that you can understand better various concepts that will be discussed here and you can do more research on your own.
The Development of Smart Contracts
Even though Solidity has some similarities with JavaScript, they are different. In Solidity, when it comes to Front End development, you can’t simply put together a JQuery on a code and then expect it to function. You cannot just ignore security. We will go into more details later on when we are taking about security, development and patterns and how to fail-proof code.
The first example that I am going to use is based on the Movie in Time. This is a dystopian movie where people trade in their remaining time for money. There’s something that resembles arm wrestling in the film where you gamble your time. This is what we are going to discuss here, wrestling money for Smart Contract.
I will also provide you the scripts that we will use on Github.
Don’t get too stressed out if you do not understand that we are talking about, especially if it is your first time to deal with them. It takes time and practice to familiarize yourself with everything that you need to know. It will take some research.
Coding
The foundation of the Solidity script is the pragma directive that shows the compiler the particular Solidity version that’s being used and the name of the contract. This looks like a JavaScript script. We will refer to it as ‘Wrestling’ here.
pragma solidity ^0.4.18;
contract Wrestling {
// our code will go here
Since we will be using 2 wrestlers, we will put in two variables
address public wrestler1;
address public wrestler2;
In this game, the wrestlers can add money. Anyone who puts in money that doubles what the other wrestler has put in automatically wins.
bool public wrestler1Played;
bool public wrestler2Played;
uint private wrestler1Deposit;
uint private wrestler2Deposit;
bool public gameFinished;
address public theWinner;
uint gains;
Keep in mind that just because the private/public key is a private variable, it doesn’t mean that no one can access it. Any information that’s stored inside the variable can be found inside the contracts. Since in blockchains information is stored in multiple computers, other people can see it. This is one of the key security concerns that you must consider.
The compiler comes up with getter functions for public variables. If you want users or contracts to edit the public variable value, you have to set up a setter function.
In our game, we want to set up three events as steps in our game. The first stage is where the wrestlers register their names. The next step is during the game, where there are different rounds. The last step is the end, where one wrestler wins. These events are just logs and you can use them for JavaScript callbacks in the interface used by users. They can be referred to as dapps. During the development phase, you can use events to debug since you cannot find the equal of “console.log()” function in Solidity that is available in JavaScript.
event WrestlingStartsEvent(address wrestler1, address wrestler2);
event EndOfRoundEvent(uint wrestler1Deposit, uint wrestler2Deposit);
event EndOfWrestlingEvent(address winner, uint gains);
The next step is to put the Constructor. This will be given the name of the contract and it will only be called once when the contract has been created.
In this case, it is the first wrestler who developed the contract. “msg.sender” is the person who summoned the contract.
function Wrestling() public {
wrestler1 = msg.sender;
}
The next step is to allow another wrestler to register using the function below
function registerAsAnOpponent() public {
require(wrestler2 == address(0));
wrestler2 = msg.sender;
WrestlingStartsEvent(wrestler1, wrestler2);
}
In case a condition isn’t fulfilled, you can use the require function in Solidity to revert back to the initial position. For instance, in this example that we are using, if the variable wrestler2 is the same as the 0x0 address, the game can go on but if the address of wrestler2 differs from the 0x0 address, then it means that the wrestler has registered as an opponent so, you can refuse this registration.
“msg.sender” is the address that’s used for the one that called the function. It will send out signals that the wrestling is starting.
Each wrestler will then call in a function “wrestle()” and put in money. To see who has won, considering that the winner has to 2x the cash that has been put in by the opponent, we use the “payable” keyword, so that the function can accept money. “msg.value” refers to how much Ether was delivered to the contract.
function wrestle() public payable {
require(!gameFinished && (msg.sender == wrestler1 || msg.sender == wrestler2));
if(msg.sender == wrestler1) {
require(wrestler1Played == false);
wrestler1Played = true;
wrestler1Deposit = wrestler1Deposit + msg.value;
} else {
require(wrestler2Played == false);
wrestler2Played = true;
wrestler2Deposit = wrestler2Deposit + msg.value;
}
if(wrestler1Played && wrestler2Played) {
if(wrestler1Deposit >= wrestler2Deposit * 2) {
endOfGame(wrestler1);
} else if (wrestler2Deposit >= wrestler1Deposit * 2) {
endOfGame(wrestler2);
} else {
endOfRound();
}
}
}
You will then include endOfGame () and End of Round () functions. The ‘internal’ keyword will be similar to the private one. The only thing that will differ is that other contracts can inherit internal keywords.
function endOfRound() internal {
wrestler1Played = false;
wrestler2Played = false;
EndOfRoundEvent(wrestler1Deposit, wrestler2Deposit);
}
function endOfGame(address winner) internal {
gameFinished = true;
theWinner = winner;
` gains = wrestler1Deposit + wrestler2Deposit;
EndOfWrestlingEvent(winner, gains);
}
The money is not given directly to the winner. There are cases where there are many users who can withdraw money from the contract. In such cases, the best thing to do would be to have a withdraw pattern in order to avoid re-entry.
To avoid a user withdrawing money multiple times, it is essential to use a nullify function any time a withdrawal is made.
function withdraw() public {
require(gameFinished && theWinner == msg.sender);
uint amount = gains;
gains = 0;
msg.sender.transfer(amount);
}
To get the whole snippet, go here.
IDE Usage
Open RemixIDE in a new browser, then click on the + button at the top, left part of the page and set up a new file called “Wrestling.sol” then copy the snippet code above and then paste it here.
Githu hub does not yet support the Solidity.sol extension.
Check the page’s right part, where you will notice that there are many interesting tabs, like the “Analysis” tab. This tab shows recommendations and errors. You can explore more of this tool on your own, since we are not going to use it. The tools that we are going to use include:
- Truffle: This tool will help you to create Smart Contracts, publish them and test them. You can find more information here.
- Ganache: It was initially known as TestRPC. It was renamed Ganache after it was integrated with Truffle Suite. The work of tis tool is to set up a virtual blockchain and create accounts that will be utilized during development.
- Mist: This is the equivalent of a web browser, such as Chrome or Firefox, except that it is used for dapps. Since it is not secure do not use it with dapps that you do not fully trust.
- Ethereum Wallet: This is like Mist, only that it can be used to open one dapp only: Ethereum Wallet. Since Mist and Ethereum Wallet are just user interfaces, we need something that will connect us to the real Ethereum blockchain.
- Geth: This is the key software that will connect you to the real blockchain. You can even set up a new one. In our example, we will create a local blockchain for testing purposes, create a contract and mine ether.
We will begin by utilizing Truffle with Ganache and then Truffle with Mist and Geth.
Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
https://medium.com/blocktoken/how-to-code-in-ethereum-a-three-part-series-part-1-6697178a1ac1