[Beginner Guide] Write and deploy your first ICO smart contract under 30 minutes (stage 2)
Write a smart contract that can issue tokens and transfer tokens
This is the second half of the super simple tutorial for writing your first ICO smart contract. Please check out the first half guide here, if you haven’t.
Things you need
- Make sure you have read the first guide (here)
- Optional: Github code source of the tutorial (provided by BEN@HK)
Stay at Remix and let’s write codes
We are going to implement the last feature — Method: Transfer tokens from one user to the other user. And walk you through the procedure of deploying and testing a smart contract.
Add the following codes below the previous features, yet still inside the Token contract body:
Wow it’s such a huge function that we have seen so far. But no worries, I will explain the lines one by one for you.
The function has a name
transfer which takes in TWO variables:
_to (the recipient of the transfer) and
_value (the amount of tokens I wish to transfer). The recipient is with type
address because that’s a user.
Inside the function, let’s first jump to line 4 and 5. The straight-forward idea to transfer money is to reduce my balance and increase your balance. So at line 4, the balance of
msg.sender , which is the sender/caller of the function, has been deducted a certain amount
_value from his or her
balances . Likewise, the
balances of the recipient
_to has been increased by a certain amount
_value . This makes sure the
_value amount of token can flow from me
msg.sender to my friend
It’s all done.
But here comes two problems:
- I can transfer more than I have
- The transfer can cause the recipient’s balance becomes less (overflow)
To prevent the first case, we added a
require statement to enforce that the
balances of the sender
msg.sender MUST larger than or equal to the amount she wishes to transfer.
require(balances[msg.sender] >= _value);
// after the statement is comments. You are free to add anything human-readable after the
// like your moods and weather and whatever.
For the second case, it is a overflow problem. When a number is too large, the variable cannot hold the number properly. Like an odometer below:
When the number is larger than 999,999 in the odometer, it will circle back to 000,000 onward. By applying this to the Token smart contract. When you transfer a value to your friend who is too rich, there is a chance that you can zero out his bank account. (Education purpose…don’t try ;p)
Therefore, we have added the following line 3, to ensure that the final result of the increased wallet amount, is larger than it was.
require(balances[_to] + _value >= balances[_to]);
That’s it! Congrats on finishing your first Token smart contract! Let’s try out our code.
Deploy the smart contract
Head over to the right hand side of your Remix compiler. Make sure you’re logged in and connected to the Metamask (tutorial by CryptoCompare). After you click on the Run tab, you will see the following:
If your Account shows the wallet address that you are currently using, then you are good. If not, you might want to check out the tutorial above. Also make sure you have received some ethers from the faucet already.
Type any number you wish at the placeholder of Deploy, and click on the red Deploy button. I am issuing 1,000 token this time.
Click on the green Submit button of the popped up Metamask window to confirm the deployment.
Wait for 30 seconds until you see the following window appears at the right hand side. This is the easy interface for you to interact with the smart contract you’ve created.
Test and play around with the smart contract
Find your wallet address from Metamask, and paste it to the getBalance blue button to check whether your get the full token issued that I did!
Try to transfer some tokens to your friends (or anyone, e.g. me: 0x5ff2c17ada131e5D9fa0f927395Abe35657e4768). And see whether it succeed.
And the Token smart contract is completed!
In this two tutorials you have learned the following:
- Basic syntax of Solidity and Smart contract programming
- Interact with Ethereum smart contract via Metamask
- Basic ideas of how to write smart contract in a secure way
The code base is here on Gitihub! Feel free to check this out.
Thanks for your time and passion of spending 30 minutes going through all the lines. Please let me know if you came across any question. :)