Ethereum Experiment Notes

miZyind
miZyind Singularity
6 min readJun 15, 2018

--

I’m a NodeJS developer, so my development environment has been configured. I would recommend having some background knowledge before reading this article.

Glossary:

  • Ethereum -- a blockchain-based decentralized platform, the smart contract mechanism is its main concept.
  • Solidity -- a contract-oriented programming language for writing smart contracts.
  • OpenZepplein Solidity -- an open source library rich with smart contract using best practices.
  • Truffle -- a light, fast development and testing framework for Ethereum based on NodeJS.
  • Ganache -- a personal Ethereum blockchain simulator, with both GUI and CLI support.
  • MetaMask -- a convenient digital wallet and currency exchange viewer.

Create Project:

# Create folder and change working directory.
$ mkdir tutorial-token && cd tutorial-token
# Using "tutorialtoken" template.
$ truffle unbox tutorialtoken
# Create a smart contract named TutorialToken.
$ truffle create contract TutorialToken

After these steps, open this project and find contracts/TutorialToken.sol through VSCode.

Now we can see the contract’s prototype, but why there are some warning messages?

I believe it’s because Solidity is being updated constantly and the templates cannot keep up.

We can find and fix these problems at design time using VSCode’s Solidity Extension, Syntax Highlighting and Syntax Checking features.

These warning will not impact the compiling and publishing of the contract, but as a beginner in Solidity, understanding all the syntax is very important!

Write Contract:

According to Solidity Changelog, a new constructor syntax was added in 0.4.22.

Let’s refine the code of contract with this new syntax:

Now we can refer to the TutorialToken and start to add content!

We don’t need to reinvent the wheel.

We can import existing contract after installing this package:

# Using Yarn to install openzeppelin-solidity package.
yarn add openzeppelin-solidity

Find the Token contract we need from the OpenZepplein Solidity project:

A term that I’ve never seen appears. ERC.

After further research.

It is basically a contract specification determined by the community.
Giving developers rules to follow.

For example, transfer function is the basic requirement in contracts, etc.

I chose ERC20 to be my token’s standard in this experiment.

Import openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol and then using StandardToken as the base, declaring following properties:

  • name -- name of currency.
  • symbol -- short name of currency.
  • decimals -- minimun exchanging unit of currency.
  • INITIAL_SUPPLY -- how much currency be supplied by default.

Many articles mention adding optional properties like name, symbol and decimals.

But after reading the rules of ERC20, I couldn’t find more details on these properties.

For example, the maximum character limits of symbol, or the maximum length limits of decimals, etc.

I don’t even know where they are from and why they are named like this?

Everything became clear when I found EIP20:

There will be tokens minted that don’t have any requirement for names, symbols or decimals. Like prediction market outcomes or energy meter kwh tokens for example. This should not be at the token layer. All tokens are not automatically a sub-currency or coin (that uses additional information).

In short, ERC20 only defined the minimun requirements for currency circulation.

Developers can derive new currencies based on it to suit any situation.

However, when the currency needs to be traded on the market, giving them a identify is unavoidable.

When there are no limits on the number of characters in the symbol, the name and the symbol of this currency may be the same.

In some situations, the symbol might be longer than the name!

Whether these two properties will lose their meaning?

Maybe it will be a part of the standard on future ERC, so let us just leave it at that.

The completed contract:

Solidity language is easy to learn and read if you have basic understanding of programming languages.

Variable declareing syntax is made up of:

Types Visibilities Constant(optional) Name = Value

How about these two undeclared variable(totalSupply_, balances) in constructor?

I guess that has something to do with StandardToken.

And the StandardToken is inherited from BasicToken:

We can find these two properties in BasicToken:

  • totalSupply_ -- total currency in circulation.
  • balances -- all account balances of this contract.

After understanding the meaning of all variables, this simple contract was done.

Compile Contract:

First, we need to configure how Truffle deploy this contract.

Create a new file migrations/2_deploy_contracts.js and put it into deployment settings:

Run the truffle compile command to compile the contract.

And then we can see the files has been save in build folder:

Publish Contracts:

Publishing a contract is to call a node in the blockchain to executing a program written by us.

In Ethereum’s mechanism, the required computational power (Gas) is calculated based on the complexity of the contract.

And we need to give the node compensation for running our program.

You can use Ganache to create your own private blockchain for testing.

Turning your computer into the only node, resolves the contract without paying a fee.

After installation and startup you can see the following screen:

Ganache will automatically creates multiple wallets for testing (you can adjust the number of wallets in the settings)

The default address is http://127.0.0.1:7545

Using the truffle migrate command to publish the contract to the private chain:

Now we can see the consumed Gas and the released contract on Transactions tab.

Call Contract, Transfer Currency:

Using the built-in Web Server and front-end interface for transfering.

Start the server through yarn dev command and it will start running at localhost:3000:

Then use MetaMask to create a connection with the private chain and import the private key of the testing wallet.

You can quickly switch wallets and view status:

Select the current wallet, you will see the amount of currency that was defined at the beginning of the contract:

Enter the target address and the amount to transfer through the webpage:

Ganache will show that the contract has been triggered immediately.

Then use MetaMask to switch to the target wallet to see if it successfully received 2000 TT:

Summary:

Through this experiment, we can learn about the deployment and publishing process of contracts.

Although it may seem easy to write and publish, using these mechanisms to develop decentralized applications and implementing business logic is what makes this so advanced and interesting!

--

--