HaloBlock
Published in

HaloBlock

Tutorial: Deploy your own ICO token using Geth (Part II)

Hi everyone, good to see you again. This post is a following blog for our last post. Talk is cheap, show me the "code". Let's dive deep into our topic! After setting up all the local requirements, now you can develop and test smart contract locally using Geth. Now, using the standard ERC20 token, we can use Geth to compile the solidity source code, write contracts onto a blockchain, publish them, and mine them. Just follow our instructions, and you will have a much better understanding of the blockchain world. Here we go!

Prerequisites

  1. Install Solidity

Solidity is a contract-oriented, high-level language for implementing smart contracts. On a Mac, run the following in Terminal to install solidity:

brew update
brew upgrade
brew tap ethereum/ethereum
brew install solidity

2. Follow our last post on setting up private blockchain with Geth.

Compile a Contract

We will be using the HelloCoin.sol contract from our personal GitHub, which is a standard ERC20 token that has basic functionalities of a cryptocurrency. Once you have the HelloCoin.sol script ready, run the following to compile it:

echo "var HelloCoinOutput=`solc --optimize --combined-json abi,bin,interface HelloCoin.sol`" > HelloCoin.js

Basically, what this command does is to compile HelloCoin.sol in .json format, assigns the data to a Javascript variable HelloCoinOutput and sends the output into a HelloCoin.js file. If we do a cat for this file, we can see the output data after compiling, including our newly generated bytecodeand abi of the HelloCoin.sol .

When we write our smart contract to a blockchain, we are actually publishing the compiled bytecode, since the EVM will interpret and execute instructions in the bytecode. bytecode is the code that lives on the blockchain, and abi is the code that provides human-readable interface for users to interact with smart contract’s functions. Without using abi , we have to use the hexadecimal encoding of functions, which makes life much harder. :(

Output after compiling(abi)
Output after compiling(bytecode)

Publish the contract

After compiling the source code and stored the output file, we need to load it in Geth console for further interactions with the smart contract. The following steps take place from the console running from Node 1, so making sure you follow our previous step correctly.

In Geth, load the contents of HelloCoin.js :

$ geth --datadir "~/Libarary/LocalNode1" --networkid 1234 --port 11111 --nodiscover console...
> loadScript("HelloCoin.js")
true
> HelloCoinOutput
...
loading compiled data

As you see, the compiled smart contract has been loaded onto Geth. Then run:

> HelloCoinContract = web3.eth.contract(JSON.parse(HelloCoinOutput.contracts[“HelloCoin.sol:HelloCoin”].abi));

If everything works well, Geth will interpret the data we provided as a JSON file and print it as below:

HelloCoin JSON

And then, run:

> personal.unlockAccount(eth.accounts[0])...
> var HelloCoinInst = HelloCoinContract.new(
{
from: eth.accounts[0],
data: "0x"+HelloCoinOutput.contracts["HelloCoin.sol:HelloCoin"].bin,
gas:4700000
},
function (e, contract) { console.log(e, contract); if (typeof contract.address !== 'undefined') { console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash); } });

This section of code would create a Javascript instance to generate a contract, set the contract's owner to be eth.accounts[0], send the transaction to the network, and then wait for the contract to be mined by other miners. If it is mined, it will add a log (Mining means that miners would execute the bytecode of our smart contract, create a contract creation transaction and writing it onto the blockchain. If there is no miner mining this transaction to the blockchain, the transaction will keep waiting in the transaction pool on miners’ systems until someone picks it up).

publishing the contract

Note that the contract address above will be immutable once it is published on the blockchain. The contract address is generated in part by the creator’s address (in this case, eth.accounts[0]) as well as a nonce. Therefore, after deploying a contract to the blockchain, anyone who wants to interact with this contract has to provide its address.

Mining Simulation

Since the blockchain and the smart contract we deployed are all running on our local environment, we need to do the mining by ourself. On the other node, let's use node 2, we run the following command:

> miner.start()
Miner mined the Contract Creation transcation

And then, wait for a couple of seconds and then run:

> miner.stop()

Now, we are all set! Our smart contract has just been published onto the blockchain. Switch to node 1 and try the following:

> HelloCoinTxHash = HelloCoinInst.transactionHash 
> eth.getTransactionReceipt(HelloCoinTxHash)

You will get a receipt showing our contract was deployed to blockchain successfully. Cheers!

Conclusion

We have introduced different Ethereum clients and development frameworks for smart contract and blockchain tech enthusiasts. When you start writing a contract, Remix would be a good choice for beginners. If you have been coding in solidity for a while, you might want to use Truffle for creating development environment where your smart contracts run on a blockchain. Alternatively, you can use Geth, a command line tool as we discussed above, to dive deep into how the blockchain and your smart contract work.

By Zhiyuan Li and Quan Sun, Security Researchers from HaloBlock.io

--

--

--

Security audits for smart contracts and cryptocurrency exchanges

Recommended from Medium

What is DAOventures (DVG) | What is DAOventures token | What is DVG token

InsuranceDAO, new gem onDEFI insurance space

Aleph Zero to Sponsor ZK Hack: Blockchain Privacy

APAC Insider — Best Business Blockchain Service Provider 2022

Harmony on NKN — AMA Part 1

An Introduction to Solidity and Remix 👾

Crown Sterling AMA

How to buy land in metaverse: 8 Best articles

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
HaloBlock Official

HaloBlock Official

Security Audits for Smart Contracts and Crypto Exchanges

More from Medium

How integration with KYVE will help blockchains

Dialed in with Switchboard: 4–18–22

Introducing Odos

Reviewing Rollup Architecture: Optimistic vs. Zero Knowledge