Photo by Jezael Melgoza / Unsplash

Background

俗話說的好 畢圈一日 人間一年..... 開始學 Smart Contract吧, part 2.

New Token

$ mkdir simpletoken
$ cd simpletoken
$ cd contracts
$ truffle create contract SimpleToken

SimpleToken

pragma solidity ^0.4.22;


pragma solidity ^0.4.22;


contract SimpleToken {

    uint256 INITIAL_SUPPLY = 1000000;
    address public minter;
    mapping(address => uint256) balances;

    function SimpleToken() public {
        balances[msg.sender] = INITIAL_SUPPLY;
        minter = msg.sender;
    }

    // transfer token for a specified address
    function sendToken(address _to, uint256 _amount) public {
        require(balances[msg.sender] > _amount);
        balances[msg.sender] -= _amount;
        balances[_to] += _amount;
    }

    // Gets the balance of the specified address
    function balanceOf(address _owner) public constant returns (uint256) {
        return balances[_owner];
    }

}

稍微講解一下 mapping(address => uint256) balances; Mapping 是一種 key-value 的型態. key是address型態, value是unsigned integer.

msg是一個全域(Global)物件, msg.sender 表示用作呼叫當前函式的帳號(address).

$ truffle compile
Compiling ./contracts/Migrations.sol...
Compiling ./contracts/SimpleToken.sol...

Compilation warnings encountered:

/Users/taiker/workspace/smart_contract/simpletoken/contracts/SimpleToken.sol:14:5: Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(...) { ... }" instead.
    function SimpleToken() public {
    ^ (Relevant source part starts here and spans across multiple lines).

Writing artifacts to ./build/contracts

Deploy

$ cd migrations
$ vim 2_deploy_token.js

2_deploy_token.js

var SimpleToken = artifacts.require("SimpleToken");

module.exports = function(deployer) {
  deployer.deploy(SimpleToken);
};

truffle.js

module.exports = {
  // See <http://truffleframework.com/docs/advanced/configuration>
  // to customize your Truffle configuration!
  networks: {
    development: {
      host: "localhost",
      port: 7545,
      network_id: "5777" // Match any network id
    }
  }
};
$ truffle migrate
Using network 'development'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0x6afbe98cb0112372f33e0e3782fa38599e839e68e62c2a33a3d33b06309265ee
  Migrations: 0x36cd1b2e25600f17d7313e10a7204556abec63b1
Saving successful migration to network...
  ... 0xa1cdabe3c4bd69c69331967217bc42c48b031beff19b6547705e6c1721c6a784
Saving artifacts...
Running migration: 2_deploy_token.js
  Deploying SimpleToken...
  ... 0xbb481f2fa9d2e889865bbce554d0b9acedb8e101f900678e35f56a9cb4e2fdca
  SimpleToken: 0x066fb6895a346d651bad53aa71c8b028efb7ad1b
Saving successful migration to network...
  ... 0x301d5c3f9c581ad756e9db980eba6295dcdcb3f0b57a11a63de2f1ad9a6eda74
Saving artifacts...

Interaction

$ truffle console
truffle(development)> let contract
truffle(development)> SimpleToken.deployed().then(instance => contract = instance)
truffle(development)> contract.balanceOf(web3.eth.accounts[0])
BigNumber { s: 1, e: 6, c: [ 1000000 ] }
truffle(development)> contract.balanceOf(web3.eth.accounts[1])
BigNumber { s: 1, e: 0, c: [ 0 ] }
truffle(development)> contract.sendToken(web3.eth.accounts[1], 100)
{ tx: '0x6e2a9db74133161de30523cbba2447d7f4805deb8574a4fa3c15bc7616aab9f6',
  receipt:
   { transactionHash: '0x6e2a9db74133161de30523cbba2447d7f4805deb8574a4fa3c15bc7616aab9f6',
     transactionIndex: 0,
     blockHash: '0x6fb1ff918a7bdf8544092eaebaafdbf7042d05985b739a072d8fb8acf8280cf0',
     blockNumber: 9,
     gasUsed: 49083,
     cumulativeGasUsed: 49083,
     contractAddress: null,
     logs: [],
     status: '0x1',
     logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' },
  logs: [] }
truffle(development)> contract.balanceOf(web3.eth.coinbase)
BigNumber { s: 1, e: 3, c: [ 9900 ] }
truffle(development)> contract.balanceOf(web3.eth.accounts[1])
BigNumber { s: 1, e: 2, c: [ 100 ] }

然後也可以看到 Ganache 中有這筆交易紀錄, 可惜 Ganache 目前還沒辦法 show 出 Token 的資訊.

Screen-Shot-2018-07-22-at-2.21.02-AM

Reference