Types in Solidity

Booleans

A boolean is declared with the keyword bool as two possible values true and false.

 

Integers

An integer is declared with the keyword int or uint for unsigned integers. Keywords uint8 to uint256 in steps of 8 (unsigned of 8 up to 256 bits) and int8 to int256uint and int are aliases for uint256 and int256, respectively.

The operators that you can use are similar to traditional programing languages:

  • Comparisons: <=<, ==, !=, >=, > (evaluate to bool)
  • Bit operators: &, |, ^ (bitwise exclusive or), ~ (bitwise negation)
  • Arithmetic operators: +, -, unary -, unary +, *, /, % (remainder), ** (exponentiation), << (left shift), >> (right shift)

Remember that arithmetic operations can lead to integer overflow or underflow, therefore we suggest you use the SafeMath library.

Addresses

An address is declared with the keyword address. An address define where you can find a contract or an account on the blockchain.

When you interact with a contract, you can know who interacts with the contracts by accessing msg.sender.

See how we use an address to add basic right privileges to a contract.

Enums

An enum is declared with the keyword enum. It’s a way to create a user-defined type in Solidity. They are explicitly convertible to and from all integer types but implicit conversion is not allowed. The explicit conversions check the value ranges at runtime and a failure causes an exception.

 

 

Ethereum smart contracts lifecycle

In the previous article we saw how to deploy our first smart contract to the blockchain. In this article we’ll learn a little bit more about smart contracts in Ethereum.

Birth of a contract

When we write a smart contract we might have some operations to do when it is deployed to the blockchain. For example set some variables. Same as classes in an object oriented programing language, smart contracts have also a constructor . The constructor is a function with the same name as the contract.

If you try to deploy this contract, you will see that you have to provide the two arguments to the transactions.

If we deploy the contract to the blockchain, we’ll be able to see that the contract executed the code and when we read the value stored it is equal to our power.

The constructor  is really helpful to customize your contracts at the time you deploy it. A traditional class has most of the the time a destructor which is called when the object is destructed. It’s also possible with contracts.

 

Who’s who?

When you create a smart contract, you might need to know who is interacting with it at runtime. In the Ethereum blockchain, actors (smart contracts or wallets) are identified by their address . If you want to know the address  that called a function you can access it by using msg.sender . Storing addresses can let you implement logic depending on the transaction creator.

Let’s say we want to improve our Counter contract from the Hello World tutorial and only allow the creator of the contract to be able to update the counter. We’ll need to add two steps, a constructor that will store the address of the creator and a condition in the increment function to make sure it is the creator that call the function.

Here is our Counter contract updated:

So we secured our contract to only allow the initial creator to increment the counter. You can learn more about solidity data types like address here.

 

Death of a contract

Even the best things have an end and smart contracts can die too! When a contract is killed using the kill function it will not be possible to interact with it anymore. To kill a contract you need to to call selfdestruct(address) . Providing an address as the parameter let you transfer the remaining founds stored in the contract to the address.

When you implement a kill function to destroy a contract, checking the identity of the caller let you protect the contract from getting destroyed by anyone.

In some tutorials you can see people using suicide()  instead of selfdestruct() . The suicide()  function has been renamed in order to have a more healthy language.

So let’s call our kill function:

After few seconds if you try to access your contract, you’ll see that you can no longer interact with it anymore.

We learnt how to write really simple smart contracts. In the next tutorial we’ll see how to create a lottery contract and how to store more data thanks to mapping type.

Solidity Hello World with Ethereum Wallet

come to the classic Helloworld Solidity contract class using Ethereum Wallet. In this class we’ll see how to write a simple contract and deploy it on the block chain. We’ll also see how to interact with our contract by sending and reading data.

The syntax for a contract in Solidity is really similar to a class in oriented object programming languages. A contract have functions we can call and variables we can store and read.

Our Counter contract will store the number of times it has been called and make the value available for everyone to read on the blockchain.

 

To publish our contract on the blockchain, open Ethereum Wallet and go to My contracts.

Click Deploy a new contract.

In the code text area past our Counter contract code.

On the right select the contract you want to publish: our Counter contract.

Enter your password and press Send transaction. The gas price is the amount of Ethereum that will be required to publish your contract to the block chain.

You can see the counter value is equal to 0. On the blockchain, it does not cost anything to read a value, that’s why you can see the value displayed here.

Now if you execute our increment function guess what will happen? Our counter value will be equal to 1. That may take some time as the execution of the code must be written in the blockchain when the next block is mined.

If you execute the increment function another time you’ll see the counter value changing!

Congratulations, you published and interacted with your first contract. In the next tutorial we will see how we can improve our contract so only the owner (you) can interact with it.

Install Ethereum Wallet

If you don’t have a clue about what is Ethereum or a smart contract, you should first read what is Ethereum.

The fastest way to get started with Ethereum smart contract programing is Ethereum Wallet. It’s a native application available for Windows, MacOSX and Linux. You can download it from the Github project.

 

Before anything, Ethereum Wallet needs to connect to the block chain and synchronize.

 

 

Once the sync is done choose the test network. On the test network any transaction will be done with test Ethereum so it does not cost you anything to develop. Keep in mind that while you are on the test network your contracts won’t be accessible from the normal Ethereum network.

 

Then you have to choose a password and don’t forget it. You don’t have to worry too much as the password will only affect the test network.

 

Once this is done you should see the Ethereum Wallet main screen displaying your account address and current balance which should be 0.

 

On the test network you can easily get Ethereum by selecting in the menu Start mining.

 

Let the app run a little and you’ll see Ethereum in your account. It can takes around an hour to get your Ethereum. In the future, you will need a small amount of Ethereum to deploy your smart contracts to the blockchain and interact with them. You can now start writing your first smart contract.