Continuing With Your First Smart Contract

In a previous edition of iCode, I left off at the breadcrumbs external and payable as the keywords to research on.

Answers:
  1. The keyword payable is kind of obvious.  The fallback function in our Smart Contract is payable, that is to say, that whoever is calling this function has to send some ETH (as little as it can be, e.g. 1 Wei [1/18th of 1 ETH]) to this function.  Anyone calling this function without any ETH (i.e. 0) will be rejected.
  2. The keyword external is worthy of some technical research.  Within solidity, each function has to be of a specific visibility type.  Solidity provides the following:
    • external,
    • public,
    • internal, and
    • private

I will not re-invent the wheel.  Here is a snapshot of the documentation says for these visibility types:

Solidity Visibility Types

Now, it is mandatory that a fallback function has only and only external visibility.  This is logical, you do not want this function to be callable from within the contract itself.

Now adding everything together, we understand that Smart Contract lets anyone send ETH to it, and all this Smart Contract will do is that it will send it back to the same ETH address from which the ETH was sent.

Let’s complete this Smart Contract, by deploying this and testing it out.  Our goal after this is to write a Smart Contract that will help us input our ETH and possibly input it into a DeFi product available.

First Step: Let’s Compile the Code
  1. Compilation section: In order to do that, I need you to click on the left-most vertical menu bar and reach out to this symbol Compiling Smart Contract . Do not be surprised if you do not have a green tick mark over there.  That is what we will achieve once we compile the Smart Contract correctly.
  2. Compiler version: Next, we will select a compiler version (read my post Writing Your First Smart Contract) on the state of Solidity and continuous development) for (obviously) compiling our code.

NOTE: At the time of writing this article, Solidity has released its updated version of 0.6.1. There are some breaking changes between 0.5 and 0.6. For the purpose of this article we are still relying on 0.5 version. Should you choose to write your Smart Contract based on 0.6 version you will have to take note of the update in the language and update your Smart Contract accordingly!

This is what we will be selecting: 

Once you have the above selected.  Click the Compile button, and we should have something like this:

We should now have the green tick mark mentioned above and the following section:

Easy to guess, the Bytecode is the code that I mentioned in my previous post, Writing Your First Smart Contract, that will get deployed onto the Blockchain.  You can copy it from here and have a look at it if required.

ABI it is the short form for Application Binary Interface.  In one-liner, it is the JSON based representation of the Smart Contract that will be required for our Front-end code to interact with.  More of this in later posts.

Second Step: Deployment of the SC in JavaScript VM

For this I will need you to visit this section:

Over here, we will first select our environment:

Do note that the Environment has multiple options. In the next article, I will take you through each one of them.

That is it, we are now ready to deploy our SC.  For this, all you have to do is now click Deploy as shown below:

Once you do that, you will see this:

Congrats, you just deployed your SC! Trust me, deploying to any other chain, ie, a real TestNet or the MainNet is a similar process.  All of which we will cover in the next article.

Testing Our Smart Contract

Clicking on the dropdown for our deployed contract, you will see this:

The fallback function is red for a specific reason (this is breadcrumb!).

Let us now go back to our ETH wallet accounts:

Change the value to 15 ether or whatever amount you like and click the red fallback function.  Then head over to this section which says listen on network (which looks like a browser-based terminal):

There, dropdown the last Debug section and you will see this:

The green tick mark on the top left corner is the sign of a successful transaction and all of the above are details of how the transaction was executed.

If you notice your account balance, it would practically be unchanged, as that is what the SC did, it sent the entire amount back to you.  The minor difference, if you were able to notice, will be the execution cost (again a breadcrumb!).

That is it friends.  You have successfully deployed your Smart Contract and tested it out too (manually though).

Hope you liked it!

CategoriesTags

Related Posts