1. 程式人生 > >Solidify實現一個智慧合約17(建立BLC幣)

Solidify實現一個智慧合約17(建立BLC幣)

建立資料夾:mkdir BLC

進入後執行:truffle init

再執行:npm init,一直回車

再執行:npm install zeppelin-solidity

[email protected]:/media/hisee/本地磁碟2/MyCodes/BlockChain/BLC$ npm install zeppelin-solidity
npm WARN deprecated [email protected]: This package has been renamed to openzeppelin-solidity. Please update your dependency, or you will no longer receive updates.
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN 
[email protected]
No description npm WARN [email protected] No repository field. + [email protected] added 1 package from 1 contributor in 1.891s [email protected]:/media/hisee/本地磁碟2/MyCodes/BlockChain/BLC$ [email protected]:/media/hisee/本地磁碟2/MyCodes/BlockChain/BLC$ ll 總用量 4 drwxrwxrwx 1 hisee hisee 520 10月 7 14:32 ./ drwxrwxrwx 1 hisee hisee 536 10月 7 14:07 ../ drwxrwxrwx 1 hisee hisee 0 10月 7 14:16 contracts/ drwxrwxrwx 1 hisee hisee 0 10月 7 14:16 migrations/ drwxrwxrwx 1 hisee hisee 168 10月 7 14:32 node_modules/ -rwxrwxrwx 1 hisee hisee 311 10月 7 14:32 package.json* -rwxrwxrwx 1 hisee hisee 334 10月 7 14:32 package-lock.json* drwxrwxrwx 1 hisee hisee 0 10月 7 14:16 test/ -rwxrwxrwx 1 hisee hisee 545 10月 7 14:16 truffle-config.js* -rwxrwxrwx 1 hisee hisee 545 10月 7 14:16 truffle.js*
[email protected]
:/media/hisee/本地磁碟2/MyCodes/BlockChain/BLC$

建立BloggerCoin合約

pragma solidity ^0.4.4;
import "zeppelin-solidity/contracts/token/ERC20/StandardToken.sol";

contract BloggerCoin is StandardToken {

  string public name = "BloggerCoin";
  string public symbol = "BLC";
  uint8 public decimals = 4;
  uint256 public INITIAL_SUPPLY = 10000000;

  constructor() {
    uint256  totalSupply = INITIAL_SUPPLY;
    balances[msg.sender] = INITIAL_SUPPLY;
  }

}

接著執行 truffle develop --->  compile

建立2_deploy_BloggerCoin.js檔案

var BloggerCoin = artifacts.require("./BloggerCoin.sol");

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

再執行:migrate

宣告一個contract變數,並賦值。

truffle(develop)> let contract
undefined
truffle(develop)> contract = BloggerCoin.deployed().then(instance => contract = instance)

進行查詢轉賬等操作

truffle(develop)> contract.name();
'BloggerCoin'
truffle(develop)> contract.symbol()
'BLC'
truffle(develop)> contract.INITIAL_SUPPLY()
BigNumber { s: 1, e: 7, c: [ 10000000 ] }
truffle(develop)> contract.balanceOf(web3.eth.coinbase)
BigNumber { s: 1, e: 7, c: [ 10000000 ] }
truffle(develop)> contract.balanceOf(web3.eth.accounts[1])
BigNumber { s: 1, e: 0, c: [ 0 ] }
truffle(develop)> contract.transfer(web3.eth.accounts[1],1000)
{ tx: '0x0dfe484bd557e4fbdf44769f1bd58ef1dcbd055a0ea37c26147f4e0828bd1d13',
  receipt: 
   { transactionHash: '0x0dfe484bd557e4fbdf44769f1bd58ef1dcbd055a0ea37c26147f4e0828bd1d13',
     transactionIndex: 0,
     blockHash: '0xcc76adb90e1ea1c0226083df7df1072a8811596c3bf75041b79f1e7389789670',
     blockNumber: 5,
     gasUsed: 51633,
     cumulativeGasUsed: 51633,
     contractAddress: null,
     logs: [ [Object] ],
     status: '0x01',
     logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000010000008000000000000000000010000000080000000000000000000000000000000000000000000000000000000000000000010000000000000000000010000000000000000000000000000000000000000010000000002000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000010000000000000' },
  logs: 
   [ { logIndex: 0,
       transactionIndex: 0,
       transactionHash: '0x0dfe484bd557e4fbdf44769f1bd58ef1dcbd055a0ea37c26147f4e0828bd1d13',
       blockHash: '0xcc76adb90e1ea1c0226083df7df1072a8811596c3bf75041b79f1e7389789670',
       blockNumber: 5,
       address: '0x345ca3e014aaf5dca488057592ee47305d9b3e10',
       type: 'mined',
       event: 'Transfer',
       args: [Object] } ] }
truffle(develop)> contract.balanceOf(web3.eth.accounts[1])
BigNumber { s: 1, e: 3, c: [ 1000 ] }
truffle(develop)>