
We are done with the first section where we looked at some tools and concepts , hopefully you were able to access the tools. If not please let me know in the questions and I will help you.
We will divide the rest of the four sections into two parts
In the first two sessions, including this one, we will test solidity concepts using remix and ganache.
In the next one we will replace remix with a web3js client, so there will be a bit of javascript involved.
In the last session we will deploy our onto the kovan testnet, so we can see our smart contract working on a public blockchain
Now let us take a look at a very simple solidity smart contract, this is the same one we had open in the last session, it is called Simple Storage.
The Simple Storage Contract is a very simple state changer, there is a single unsigned integer value
Let us first examine its structure, it starts with a pragma directive, which indicates the range of or minimum version of solidity we are going to on in this contract.
Then it has an import section that indicates other contract files, and just to bring it up I will be using .sol extension for the solidity files.
The core contract code starts with a name contract keyword and which encloses and functions and state.
Now to deploy this contract, first we will connect the remix IDE to our ganache instance, so that we can deploy our contract on it and use the functions.
The ethereum blockchain instance exposes multiple interfaces for connecting with it. The easiest one is RPC or remote procedure call interface. Ganache exposes the RPC interface at port 7545. We can connect to the RPC interface through run Web3 Provider.
Once connected, I can see all the accounts that ganache provides.
Once deployed, I can see the methods, I can call the get method, to see that I get the initial value for the unsigned integer, then I can set it to any value, and retrieve it using the get call.
The read calls like calls can be designated with view or pure.
Two things to bring up here is that I will see a transaction only when I an changing the state of the contract, but when I am reading I will not see a transaction. Also, I only have to pay fee for the transaction when i am changing the state the of the blockchain. Which is the set method call.
I can see the transaction I create with the set call on ganache as well.
Another thing to notice for the state change function call is that it cannot return a value, we can only see the success if this call through a state update, which we did with the unsigned integer value update.
Now let’s take a look at some basics of solidity, the units and globals.
This contract lists some important value types.
There are variants of these types in the reference document depending on the size of the value we want to store in thim
We also have some globals that all contract functions have access to, There is an exhaustive list of these also in the reference document but we will take a look at some important ones
Msg.sender is the the most used one, which has the address of the account calling the function.
Some logic also runs on the block number of the current block. It is available in the block.number global
There are some time based units, like second, days hours days weeks etc. These can be used in conjunction with the now global to execute time based logic.
For example, if we want to execute some logic before certain number of days after contract deployment, we compare the current time with days passed after deployment and a function parameter
To do that we implemented what is a constructor for the smart contract which records the time when the deployment occured.
When we execute the function, we pass the number of seconds and add the time passed since the deployment. Then we compare it with now, which is the timestamp of the current block.
If the deployment is within certain number , say 2, seconds old we execute the logic inside the if.
Since its an write function, it cannot return a value, so we have to see its effects in the read. Another point to note here is that solidity provides a read method by default for state variables that are declared public
With that we will close this session, the solidity files are uploaded, please comment if you get any issues running them.
Hello folks, thank you for joining back, in the last session we saw the contract state, functions, constructors, globals and units.
In this session we will look at basic control structures like loops, conditionals. We will also see complex types like structs arrays, mappings. We will understand inheritance, exceptions and custom function modifiers. We will finish up with concepts typical to ethereum blockchain and solidity like payable functions, deletes, ecrecorver, gas etc.
First lets take a look at simple control structures, these are common in all computing languages.
In our contract method we are able to simply confirm the execution of the loop 10 times
Similarly the if else control is able to confirm the change of state variable based on the value passed.
Now lets take a look at some dynamic types - structs.
Structs are very similar to c structs where we are able to group value types together to create a user defined record like the employee one.
These structs usually work as a collection through arrays or mappings
Arrays are a common construct in most programming languages.
Solidity allows for static and dynamic arrays. You can add elements using push() and access via the index.
Mappings are a map construct that can create a key - value map. The key has to be a native type and value any accepted types struct type.
Mapping is used very frequently in tokens, ERC 20 which is a very popular type of smart contract in ethereum. We will take a look at the token contracts later in last session.
Next up in inheritance. A smart contract can inherit from another contract, and solidity supports multiple inheritance.
The sub contract is able to access both the functions and state of the super contract.
Another useful feature is that we can import one contract file into another.
Next we look at exception and error handling, Solidity uses state-reverting exceptions to handle errors. Such an exception will undo all changes made to the state in the current call (and all its sub-calls) and also flag an error to the caller.
In solidity we can also define custom function modifiers, this is especially useful when we want certain function to be only callable by a creator of the contract.
Here we also see a call to require() which is a convenient way to check for a condition and call revert() if it fails.
Till now the features we saw are found in almost all the programming languages.
Now lets take a look at some of features that are typical to solidity as its a language of programming for blockchain smart contracts.
First lets take a look at payable functions, that have a payable modifier, payable allows a function to receive ether while being called
If you try to send ether to a function without a payable modifier, the transaction will be rejected.
There also a practice to have a default payable function. This function is executed in two cases
this function is executed whenever the contract receives plain Ether (without call data) and add it to the total balance of the contract i,e. The contract address gets the ethers.
This function is also executed if none of the other functions match the given function identifier
You can withdraw the funds by using address.transfer() call.
Here we see three concepts - this and which has the address of the contract, the balance member variable use of two globals of the address type which returns the balance of ethers on the specific address and the transfer() call on the address, which transfers ethers to the caller address from the calling address.
Remix has a limitation where we cannot send funds along with wirte calls, so we will test payable functions through our web3js browser client.
Next up is delete, which is resets a type value to its initial state. It returns a part of the funds in ethers that created the state back to the caller.
In the next session we will start using a web3 client instead of remix, as that is a more real world situation and also we cannot test the rest of the solidity features through remix.
Hello, as promised, in this session we will use actual javascript code to the run some of the features of solidity, we will continue to use ganache for the blockchain and remix only for deploying the contracts on ganache.
First up we will execute a simple read call on the first contract we deployed.
This is a browser version of the web3js client. We will do a quick walkthrough of the code.
First we will take a look at our script imports -first is the web3js import and then we also import a ethereumjs which we will use for creating a signed transaction,
Then we look at our contract connection code - first we define the web3 client by setting the address of the RPC provider - just like we did while connecting remix to ganache.
Then we define our account which will make the call, and contract address of the deployed contract.
Next we list the ABI of the contract, which we can get from remix.
After that we create the instance of the contract passing in the ABI and contract address.
With web3js 1.0 , all the calls are async, so we have to call it using the javascript promise await async construct
We see the updated value in the console of the browser as it is read by the contract.
Now that was simple, it is slightly more complex to call a write function. Since we also have to commit ether funds, so we will have to use the private key of our account to sign the transaction.
Here we are importing an additional library ethereum.js to create a raw transaction object and sign it using the private key of the account,
The method to create the contract instance is the same, we declare the private key of the account address , this private has been copied from ganache accounts.
First we capture the nonce, which is the number of transaction sent form this account.
Then we create the transaction object passing in the some inputs gas price and gas limits are blockchain parameters. We have set them at defaults. The value is the amount of additional ethers we want to send with the call. Is the encoded call to the write or set method.
Then we sign and serialize transaction, then we use the sendSignedtransaction method of the web3js library to send the call.
The return is the transactionHash of the call. We can check the effect of the transaction by reading the contract state through the get call.
These two are the boilerplate methods to call contract methods through web3js.
Now we can also test the payable logic through our payable contract calls.
Next up is Events, Applications can subscribe and listen to these events through the RPC interface of an Ethereum client
If we look at the code for the event it just needs to be defined and then emitted as a function call.
On the client side we create a socket connection on the RPC port and create an instance of the contract and poll for the event
In this session we will try to deploy the code on the kovan ethereum testnet.
The kovan testnet is a public test blockchain to mimic the network of ethereum main blockchain.
To interact with ethereum testnet, we need to have an account with some test ether.
We will create an ERC 20 token contract and deploy it. ERC 20 is a specification using which we can create a token or cryptocurrency on ethereum blockchain.
We will transfer the created contract tokens from one address to another.
If you are looking to start blockchain development quickly and you have some understanding of programming languages, spend the next half an hour here and start writing smart contracts on the ethereum or tron blockchain platforms
1. Remix/Ganache Solidity Development Environment
2. Basic/Intermediate Solidity Concepts (Types / Controls / Events / Payables/ Inheritance / Exception)
3. Deploy on the kovan Network