基于以太坊的Token开发步骤
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