Ethereum Development Crash Course Part Three:

In part three we will start to looking at how to work with your enviroment by using remix and compiling the contract, so we can deploy and interact with the smart contract when it is on the blockchain. If you have just joined please see article 1 https://medium.com/bitfwd/ethereum-development-crash-course-part-one-327dee16878b

Welcome,

We are going to start by BUIDLing a simple smart contract that creates a value and transfers the value while also triggering an event on the blockchain.

Please see: https://vitalik.ca/2017-09-15-prehistory.html for some more information on Ethereum smart contracts.

Before we start to develop our smart contracts we are going to touch on the fundamentals such as data types and variables. Data types and variables in solidity come in two types elementary(value) types which are booleans, integers, addresses, bytes arrays and enums. And complex(reference) types which are arrays, mappings and structs. Some of these data types can also work together to form more robust structures for your smart contract or application logic, we will design some of these throughout the course of the tutorials.

Elementary (value) types 
- Booleans are value that is either true or false and are defined using bool
- Integers are number of positive or negative value. int, int8
- Unsigned integers are numbers of only positive value. uint256
- Addresses are 32bytes hexadecimal strings that allow for interact between networks participants and smart contracts.
- Bytes arrays are defined as bytes, bytes2, bytes3 and so on up untill bytes32.
- Enums can be used with the syntax enum {go, stop, jump, squat}
Complex (reference) types 
- Arrays can store objects in the contracts state, to use an array try something like uint[] public numbers.
- Mappings can be used to link multiple values together like mapping(uint => address) public balances.
- Structs are handy for objects with multiple values like a bid, or membership. Use define a struct by using the struct keywork and then initializing the variables inside the body.

Now we have outlined and defined the variables that can be used in solidity smart contracts please head over to http://remix.ethereum.org so we can start to develop our smart contract.

At the top of the smart contract please type pragma solidity ^0.4.24; to specify which version of the compiler the virtual machine should use. We have chosen to use 0.4.24 above thanks to ^ NOTE: every smart contract must have the pragma specification at the top the code on line 1.

The next few pieces of code will be the contract declaration, state variables and data types.

contract SimpleTransfer {
   address public owner;
uint public tokens;
   mapping (address => uint) public tokenBalances;
} 

In the above example we have chosen two state variables that can be stored in the contract state upon creation and we have also given the contract a mapping. The mapping will store the balances of each address that holds tokens from this contract. If you also see we have defined both of the variables and the mapping public this will allow anyone to view the values or information lodged against the data types.

Visibility modifiers 
Public - means anyone can view the data or call the function
Private - can only be called from with inside the contract 
Internal - can only be called inside the contract or contracts that derive the state of the contract where the function is stored. 
External - can only be called outside of the interface or library. 

Next up in our smart contract will be coding in the event that will be called once someone calls the function in our smart contract.

Use the keyword "event" 
event transfer(address from, address to, uint total);
Your screen should mimic the above.

Once you have the basic logic of your smart contract built, it’s time to start adding in functions and giving the contract more purpose of being on the blockchain. The first function we will be writing is the constructor function 💪. This function will be run we the contract deployed to the blockchain and never again. The constructor function does not live on the blockchain , it sets the logic that you want the smart contract to start off with. In our case we will be giving the smart contract an owner and also a billion tokens.

constructor () public {
    owner = msg.sender;
tokens = 1000000000;
tokenBalances[owner] = tokenBalances[owner] + tokens;
}

As you can see we have attached the msg.sender to the owner variable, 1,000,000,000 is now the value of tokens and also we have incremented the tokenBalances of the owner address by the amount of tokens

Now time to add some functionality

function transfer (address to, uint total) public returns (bool) {
require(tokenBalances[msg.sender] >= total);
tokenBalances[msg.sender] -= total;
tokenBalances[to] += total;
require(tokenBalances[to] >= total);
emit transfer(msg.sender, to, total);
return true;
}
is your screen like mine ?

In the Transfer function you can see that we have couple of requirements and balances being manipulated along with the word “emit” close to the bottom of the function. This is because at the start of the function the code checks to see if the address calling the function has enough tokens in their balance to submit a valid transaction. If that requirement is met the tokenBalance of the msg.sender is deducted by the total which is given to the to address. Before the transfer event is called the function checks to see if balance of to has been updated, if successful the transfer event is fired. Notice the emit keyword this is because of a recent compiler update that was in favor of cleaning up smart contracts and making them more readable for developers. Once the whole function has been processed we have a return statement that calls a boolean variable to be expressed as true which is also another form of verifaction that the transaction was successful.

Before we move on to deploying the contract theres just one more function that needs to be added

function kill() public {
if (msg.sender == owner) {
selfdestruct(address(this));
}
}

This function will allow us too ?…

NO! Not live like james bond… but destroy the contract “yes” 😈 😈 😈

contract SNAP !

Contract deployment and interaction

To start interacting with the smart contract on remix all you do is click “run” in the top right hand corner of the page then click the red “deploy” the contract is then deployed to the blockchain and you get the public functions of the contract to interact with.

The owner of the contract is the first account in the list of accounts given to you by remix and the amount of tokens is equal to what was specified in the smart contract constructor function.

To use the transfer function all you have to do is

select the second account in the list.
add the address to your clipboard by clicking the page to the right.
then pass the address into the transfer function as a parameter and an amount of tokens.

Oh by the way! I forgot to say that you can check the transactions that are running locally on the blockchain, there is a grey console at the bottom of the text editor. You may have seen it already, what this gives you are the transactions you are sending when interacting with the smart contract. You can see when you’ve made a call to the blockchain and when you are sending a transaction that changes the state of the blockchain.

what you see here is the transaction that called the transfer function has been successful. Also if you copy and paste the second address again but put it into the “tokenBalances” mapping as a parameter you will get the amount of tokens sent with the transaction.

Now’s your time! kill the god damn f🤩🤬😋king contract,

then try to call tokenBalances or tokens

you will see that the contract’s state is unable to accessed once the “kill” function has been called by the owner.
NOTE: only the owner of the contract can call the kill function due to the logic "if(msg.sender == owner)" which only allows the owner address to call the function. The "==" acts as a check to say "if something is equal to something" 
you can also have "!=" which means "if something is not equal to something". 
These are arithmetic operators and are documented in solidity.readthedocs.io
function kill() public {
if (msg.sender == owner) {
selfdestruct(address(this));
}
}

Well Done! That was remix, to follow the next step we are going to need a local dev blockchain set up in the terminal

geth --dev console

check the account you have been given has got a balance

web3.fromWei(eth.getBalance(eth.accounts[0]))

now, if your all good pop back over to remix and click the “compile” tab next to “run” and then “details” which is underneath auto-compile on the right hand side of the page. What you will see is a blue interface pop up, if you scroll down the interface to web3 deploy.

and then add it too your clipboard ready to paste into the local instance of the blockchain

once pasted into the terminal you should hit “enter” on your keyboard and be presented by a screen very similar to the one above.

You have now deployed the contract to your local instance of the blockchain🎉🚀🤩

To interact with the smart contract use:

simpletransfer.Transfer(to, value, {from, gas})

Congratulations 👏 
You’re now well on your way to becoming a wizard in the world of Ethereum development. As always thanks for reading and please leave a clap and or review.

PEACE ✌️