基於以太坊的Token開發步驟

消失的風發表於2018-03-30

Token開發步驟

一、準備工具
1.安裝以太坊
brew tap ethereum/ethereum
brew install ethereum
2.node:brew install nodejs
3.安裝依賴庫:npm install -g ganache-cli web3 solc truffle truffle-contract zeppelin-solidity
4.執行ganache-cli,埠預設是8545
5.配置myetherwallet設定自定義的網路:https://www.myetherwallet.com/
6.安裝vs code
7.安裝MetaMask外掛(Chrome),配置網路同myetherumwallet

二、開發
1.truffle unbox vue-box, 其他的box可以在http://truffleframework.com/boxes/中找到
2.新建自己的Contract,示例程式碼如下:

pragma solidity ^0.4.18;

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

contract FirstToken is StandardToken {
    string public name = "FirstToken"; 
    string public symbol = "FST";
    uint public decimals = 18;
    uint public INITIAL_SUPPLY = 10000 * (10 ** decimals);

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

3.編輯migrations\2_deploy_contracts.js增加自己編寫的Contract的部署程式碼:deployer.deploy(FirstToken);
4.配置專案的truffle.config中的網路,確保埠是8545
5.執行truffle comiple 編譯contract
6.執行truffle migrate --reset部署網路
7.新建自己的測試頁面,關鍵程式碼如下:

<script>
import Web3 from 'web3'
import contract from 'truffle-contract'
import artifacts from '../../build/contracts/FirstToken.json'
const FirstTokenContract = contract(artifacts)

export default {
   name: 'FirstToken',
    data() {
        return {
            web3: null,
            account: null,
            token: null,
            address: '0x554f40f004758c2043992379465a04371ffdd9e1',
            num: 10,
            result: null
        }
    },
    created() {
        if (typeof web3 !== 'undefined') {
            this.web3 = Object.freeze(new Web3(web3.currentProvider))
        } else {
            this.web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"))
        }
        FirstTokenContract.setProvider(this.web3.currentProvider)
        this.account = this.web3.eth.coinbase
        this.web3.eth.defaultAccount = this.web3.eth.coinbase
        FirstTokenContract.defaults({from: this.web3.eth.coinbase})
        FirstTokenContract.deployed().then((instance) => {
            instance.balanceOf(this.account).then((value) => this.token = value)
        });
    },
    methods: {
        send() {
            return FirstTokenContract.deployed()
                .then((instance) => {
                    console.log('from:' + this.account)
                    console.log('to:' + this.address);
                    instance.transfer(this.address, this.num)
                    return instance
                })
                .then((instance) => {
                    instance.balanceOf(this.address).then((value) => this.result = value)
                    instance.balanceOf(this.account).then((value) => this.token = value)
                })
                .catch((e) => {
                    console.error(e)
                })
        },
        query() {
            return FirstTokenContract.deployed()
                .then((instance) => {
                    instance.balanceOf(this.address).then((value) => this.result = value)
                })
                .catch((e) => {
                    console.error(e)
                })
        },
    }
}
</script>

  

8.執行send方法的時候注意要在MetaMask中點選提交才會真正執行

相關網站:
1.Truffle: http://truffleframework.com/
2.MyEtherWallet: https://www.myetherwallet.com
3.Solidity: http://solidity.readthedocs.io/en/v0.4.21/
4.Zeppelin: https://github.com/OpenZeppelin/zeppelin-solidity
5.MetaMask:https://metamask.io/

編外網站:
1.代幣的市值:www.coinmarketcap.com
2.Rinkeby測試網的地址:https://www.rinkeby.io

相關文章