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 {
- account: null,
- token: null,
- address: 0x554f40f004758c2043992379465a04371ffdd9e1,
- num: 10,
- result: null
- }
- },
- created() {
- if (typeof web3 !== undefined) {
- web3 = new Web3(web3.currentProvider)
- } else {
- web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"))
- }
- FirstTokenContract.setProvider(web3.currentProvider)
- this.account = web3.eth.coinbase
- web3.eth.defaultAccount = web3.eth.coinbase
- FirstTokenContract.defaults({from: 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)
- })
- .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 中点击提交才会真正执行
相关网站:
- Truffle: http://truffleframework.com/
- MyEtherWallet: https://www.myetherwallet.com
- Solidity: http://solidity.readthedocs.io/en/v0.4.21/
- Zeppelin: https://github.com/OpenZeppelin/zeppelin-solidity
- MetaMask:https://metamask.io/
编外网站:
1. 代币的市值: www.coinmarketcap.com
2.Rinkeby 测试网的地址: https://www.rinkeby.io
来源: http://www.bubuko.com/infodetail-2543609.html