The introduction

This article assumes that you already have some knowledge of blockchain. Smart contracts were an idea put forward by Nick Saab in the 1990s. Ethereum first saw the combination of blockchain and smart contracts and published a white paper entitled ethereum: The Next Generation of Smart Contracts and decentralized Application Platform, and has been working to make Ethereum the best smart contract platform. So smart contracts are basically computer programs running on the Ethereum blockchain. This article will help you quickly write and run your first smart contract.

Language and IDE choices

The most popular programming language for smart contracts right now is Solidity, but it’s not just Solidity. To code Solidity we can use Remix, a browser-based Soldity IDE at remix.ethereum.org/. Remix supports writing, testing, and deploying smart contracts. The following figure shows the Remix screen

Write the code

The first program to learn a language is, of course, HelloWorld, so let’s write a Smart contract for HelloWorld. The contract code is as follows.

Pragma solidity ^ 0.4.21; contract HelloWorld { string hello ="Hello World!!!";
    event say(string _value);
    
    functionsayHello() public { emit say(hello); }}Copy the code

The first line of the contract code specifies that the contract uses Solidity version 0.4.21. Solidity features higher than 0.4.21 are not supported.

In Solidity, the contract keyword contains a snippet of code that represents a smart contract and has member variables that represent the contract’s data, such as Hello in our HelloWorld, which we can modify to operate on. It also has functions that can be called by others.

Events are an event and subscription mechanism provided by Solidity. Smart contracts emit events that contract callers can listen for and react to.

This contract doesn’t do much, it just issues a say event when someone calls its sayHello method. Let’s deploy and execute it.

Deploy and run contracts

First we need to Compile this code, on the right side of the Remix there is a Compile TAB, click Start to Compile, Compile success, if failed there will be an error message, correct it.

Then we need to deploy it to the blockchain and switch TAB to Run.

Remix supports three environment running contracts. If it is a JavaScript VM, the contract will be executed in the browser JavaScript sandbox blockchain, which can be understood as Remix using the browser’S JS environment to virtual a blockchain virtual machine. In the case of Injected Provider, Remix will link Matamask or Mist blockchain wallets to indirectly deploy and invoke contracts through them. Finally, for Web3 Provider, Remix links remote zone connection nodes such as Geth to deploy and invoke contracts.

For simplicity, we’ll use a JavaScript VM that will initialize five blockchain accounts for us, using the default account. Set the Environment to the JavaScript VM. The diagram below.

In addition to Environment and Account, we can see the Gas Limit, which is the maximum cost we can accept to perform a Transaction. Value is how much ether we’re going to transfer to the contract account on the next call.

The next HelloWorld represents the contract we will create. Click Create to Create the contract. In the figure below we see that the HelloWorld contract has been created.

conclusion

In this article, we wrote a simple smart contract, deployed it, and ran it. Instead of deploying the contract to the actual chain, we just executed it in the JS sandbox virtual machine. We will continue to explain how contracts can be deployed on chains in future articles.