Liked the previous parts? Want to explore more? If you’re just getting started, my ebook — “Blockchain programming for absolute beginners” is a perfect start. Have a look at it’s contents here!

This is the final tutorial in the token smart contract series. If you haven’t already, do check out the previous tutorial on building a token smart contract! It’s a pre requisite for this one.

In the previous tutorial we had a StandardToken contract, implementing all the functions of the ERC20Contract protocol and also introducing certain necessary state variables. Today we will build the final contract for our token, which should complete the code for the token contract.

LBDCoin Contract

Now let’s create the LBDCoin contract. It’s an acronym for Learn By Doing — a philosophy I believe in and the title of this series! We would inherit from the StandardToken contract as all of the ERC20Contract functions have already been implemented there. We will only add a few more functions, such as the constructor and a fallback function, along with a notification function. We’ll explain these concepts as we go along.

Here is the code for the LBDCoin contract.

interface tokenRecipient

{

function receiveApproval(address from, uint256 tokens, address tokenAddress, bytes extraData);

} contract LBDCoin is StandardToken { string public name;

string public symbol;

uint8 public decimals = 18; // Default, recommend not to change function LBDCoin(uint256 initialSupply, string tokenName, string tokenSymbol)

{

name = tokenName;

symbol = tokenSymbol;

totalSupply = initialSupply * 10 ** decimals;

balances[msg.sender] = totalSupply;

} function approveAndCall(address spenderAddress, uint256 tokens, bytes extraData) public returns (bool success)

{

tokenRecipient spender = tokenRecipient(spenderAddress);

if(approve(spenderAddress, tokens)

{

spender.receiveApproval(msg.sender, tokens, this, extraData); // Called in the spender address contract

}

} }

By inheriting from the StandardToken contract, we have inherited all the functions which we previously implemented in the StandardToken contract code. The only things that remain are the constructor and the approveAndCall function.

This might seem a lot more complex to digest but trust me it’s easy. Let’s go over it step by step. Firstly, the interface.

TokenRecipient

Why did we have to include the tokenRecipient interface in the code here? We do this because we want to call the provided function — receiveApproval on instance of this type. We inform the code about this beforehand by declaring an interface.

Constructor

In the constructor, we pass along variable values specific to this coin, the name, symbol and the intialSupply of coins. The total Supply of coins is pretty straightforward. It is given by the following code snippet.

totalSupply = initialSupply * 10 ** decimals;

Let’s take an example. If you have 1 coin initially, and the number of decimal places is say, 2, you would have 0.xx coins, where x ranges from 0 to 9. Total coins = initialSupply * (10 ^ decimalPlaces) = 1 * 10² = 100. Further more, by setting

balances[msg.sender] = totalSupply;

in the constructor, we are giving all our totalSupply to the contract creator address initially. We can later distribute it to various addresses as we like.

approveAndCall

The approve and call function is something that is called when the caller of this function [the msg.sender] wants to authorise the spenderAddress [passed as parameter] to spend certain amount of tokens on its behalf. If the approval is successful, receiveApproval is then called on the spenderAddress contract. This function is added to contracts by default i.e you don’t have to implement it. In the extraData field, you can pass any data you like. For example, the reason why the spender was approved to spend a certain amount, in case there are multiple reasons for approval. More information on security around this function can be found here.

Conclusion

That’s it! That’s your coin contract! In the next (click) tutorial, we will learn about what GAS is, what GAS price is, and how to deploy your contract on the Ethereum TestNet as well as Mainnet (both of which will be explained in the tutorial). Stay tuned, and don’t forget to share, comment and follow!

Stay updated

Join us on Telegram : https://t.me/joinchat/G4Ls4hLyjTLLNke_3QGygQ

Join the ebook email-list! : https://goo.gl/FtRCuX

Donations

If you enjoyed this tutorial, and would like to help me continue building these in the future, feel free to drop some ETH at the address below!

ETH: 0xE11aaAB8c3a6feD2843e66FaC08C13Cda8Cb0D55