2. Deploying Smart Contract

You can use Remix Online IDE or use Truffle to deploy MyERC20 smart contract.

2.1 Deploying smart contract using Remix Online IDE

After deploying, you can invoke balanceOf with your account, which was used to deploy the contract. You will find there are 10000000000000 tokens available in your account as below. Because you set decimal as 8 when deploying the contract above, it minted a fixed number of 100000 tokens in the constructor, with one token having a decimal value of 10^8. totalSupply method will return the total supply of tokens minted which should be also 10000000000000.

MyERC20 is now live !

2.2 Deploying smart contract using truffle

You should have installed node.js in your environment. Please take a look at Installing Node.js via package manager to install node.js using package manager in various environment.

$ mkdir klaytn
$ cd klaytn
$ npm init # initialize npm at the erc20token directory
$ npm install truffle@4.1.15
$ npm install caver-js@latest # installing caver-js
$ ln -s node_modules/truffle/build/cli.bundled.js truffle
$ export PATH=`pwd`:$PATH

Now you have installed truffle and caver-js which are required to deploy smart contract.

Let's prepare truffle and a smart contract MyERC20.sol.

$ mkdir myerc20
$ cd myerc20
$ truffle init

Now you will have following directory structures.

.
├── contracts
│   ├── Migrations.sol
├── migrations
│   └── 1_initial_migration.js
└── truffle-config.js

Now write MyERC20.sol and locate it to contracts directory.

Also edit 1_initial_migration.js as below to deploys MyERC20 contract with initial parameters of BAOBABTOKEN, BAO and 8. The token name is set to BAOBABTOKEN and the token symbol to BAO. Token has decimal value 10^8. Note that when you query the totalSupply of BAOBABTOKEN for example, it returns 10^13, not 10^5, because solidity does not support floating point, the number of tokens always represented as a natural number in the smallest denomination.

const Migrations = artifacts.require("./Migrations.sol");
const MyERC20 = artifacts.require("./MyERC20.sol");
module.exports = function(deployer) {
  deployer.deploy(Migrations);
  deployer.deploy(MyERC20, 'BAOBABTOKEN', 'BAO', 8);
};

You also have to edit truffle-config.js as shown below to deploy a smart contract to Klaytn network. This is the same step described in Deploying a Smart Contract using Truffle.

// truffle-config.js
module.exports = {
    networks: {
        baobab: {
            host: '127.0.0.1',
            port: 8551,
            from: '0xabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd', // enter your account address
            network_id: '1001', // Baobab network id
            gas: 20000000, // transaction gas limit
            gasPrice: 250000000000, // gasPrice of Baobab is 250 ston
        },
    },
    compilers: {
      solc: {
        version: "0.5.12"    // Specify compiler's version to 0.5.12
      }
  }
};

Now you are all ready and can deploy MyERC20.sol as below.

$ truffle deploy --network baobab --reset
Compiling ./contracts/MyERC20.sol...
Writing artifacts to ./build/contracts

Using network 'baobab'.

Running migration: 1_initial_migration.js
  Replacing Migrations...
  ... 0x5a947f076f4570dff8ff18b1ae3557e27dd69c92ce38a3c97fad8f5355914066
  Migrations: 0x0d737e9865e5fc4c1ff53744fd2c13c52a44b9bc
  Deploying MyERC20...
  ... 0x1571e80552dab1d67260e8914e06d9b16ccae16fb698c750f6a09aab12517bc1
  MyERC20: 0xc4c8257ED9B4eB6422fDe29B1eCe5Ce301e637e1
Saving successful migration to network...
  ... 0x5b984b3f79c425d80470a96d5badb857fc05e7f31d94423044ae3119c639aa77
Saving artifacts...

It shows that the transaction hash for deploying MyERC20 is 0x1571e80552dab1d67260e8914e06d9b16ccae16fb698c750f6a09aab12517bc1 and the address of MyERC20 is 0xc4c8257ED9B4eB6422fDe29B1eCe5Ce301e637e1.

Now MyERC20 is live !

Last updated