本教程是使用trufflesuite部署一个metacoin的教程总结
本地跑Eth网络 使用 http://trufflesuite.com/ganache 跑本地Eth的网络节点
创建文件夹 1 2 mkdir MetaCoin cd MetaCoin
1 2 3 4 # truffle 下载 truffle unbox metacoin # 或者直接github下载,上面可能被墙 git clone [email protected] :trufflesuite/metacoin-playground.git
查看下载的文件目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 . ├── LICENSE ├── contracts │ ├── ConvertLib.sol │ ├── MetaCoin.sol #定义了metacoin这个币,主文件 │ ├── MetaCoinLargeBalance.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ ├── 2_deploy_contracts.js │ └── 3_add_large_balance.js ├── test │ ├── TestMetaCoin.sol │ └── metacoin.js └── truffle-config.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 // metacoin.sol pragma solidity >=0.4.25 <0.6.0; import "./ConvertLib.sol"; // This is just a simple example of a coin-like contract. // It is not standards compatible and cannot be expected to talk to other // coin/token contracts. If you want to create a standards-compliant // token, see: https://github.com/ConsenSys/Tokens. Cheers! contract MetaCoin { mapping (address => uint) balances; event Transfer(address indexed _from, address indexed _to, uint256 _value); constructor() public { balances[tx.origin] = 10000; } function sendCoin(address receiver, uint amount) public returns(bool sufficient) { if (balances[msg.sender] < amount) return false; balances[msg.sender] -= amount; balances[receiver] += amount; emit Transfer(msg.sender, receiver, amount); return true; } function getBalanceInEth(address addr) public view returns(uint){ return ConvertLib.convert(getBalance(addr),2); } function getBalance(address addr) public view returns(uint) { return balances[addr]; } }
该文件定义了几个方法
sendCoin:
发送代币到指定账号
getBalanceInEth
获得token价值多少eth
getBalance
获得该账号下有多少代币
1 balances[tx.origin] = 10000;
声明了此次发行代币总共 10000 个
运行测试 1 truffle test ./test/TestMetaCoin.sol
编译文件
编译之后会多出来一个build文件夹,以及编译之后的文件,大多数都是ABI文件。
运行Ganache 直接双击运行 Ganache,运行一个eth网络
然后打开 truffle-config.js
文件,加入网络信息
1 2 3 4 5 6 7 8 9 module.exports = { networks: { development: { host: "127.0.0.1", port: 7545, network_id: "*" } } };
运行迁移
上述看到就部署成功了。
查看一下部署的信息,我们有三个部署文件
1 2 3 4 ├── migrations │ ├── 1_initial_migration.js │ ├── 2_deploy_contracts.js │ └── 3_add_large_balance.js
所以部署的时候,就产生了三个部署的区块信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 Compiling your contracts... =========================== > Everything is up to date, there is nothing to compile. Starting migrations... ====================== > Network name: 'development' > Network id: 5777 > Block gas limit: 6721975 (0x6691b7) 1_initial_migration.js ====================== Deploying 'Migrations' ---------------------- > transaction hash: 0xa999803d787364325d8a57ee2c0174d07aeb25d5d9d91cba08495832bce90b7e > Blocks: 0 Seconds: 0 > contract address: 0x1D75F9193DEf8708e7e3197A69274A8a043B1c44 > block number: 6 > block timestamp: 1640010017 > account: 0x0374AD83DfEB8cfD94889631255AE43B2Aa93bbe > balance: 11.98576634 > gas used: 225237 (0x36fd5) > gas price: 20 gwei > value sent: 0 ETH > total cost: 0.00450474 ETH > Saving migration to chain. > Saving artifacts ------------------------------------- > Total cost: 0.00450474 ETH 2_deploy_contracts.js ===================== Deploying 'ConvertLib' ---------------------- > transaction hash: 0xb7a016567233bf0070d22259c317c7af1d6c2cceee74b993ce78e87bfdf4d12f > Blocks: 0 Seconds: 0 > contract address: 0x26b09eCB5a7Ad418a028deae36E9e457db00e40d > block number: 8 > block timestamp: 1640010018 > account: 0x0374AD83DfEB8cfD94889631255AE43B2Aa93bbe > balance: 11.98300992 > gas used: 95458 (0x174e2) > gas price: 20 gwei > value sent: 0 ETH > total cost: 0.00190916 ETH Linking ------- * Contract: MetaCoin <--> Library: ConvertLib (at address: 0x26b09eCB5a7Ad418a028deae36E9e457db00e40d) Deploying 'MetaCoin' -------------------- > transaction hash: 0x84c9701c1d85d66d9fe60a55564d12c7ac30d1eb3098e7896b011f5db649caef > Blocks: 0 Seconds: 0 > contract address: 0x10BD02647FE442dA2E86D4b05a4aaEC24464314E > block number: 9 > block timestamp: 1640010018 > account: 0x0374AD83DfEB8cfD94889631255AE43B2Aa93bbe > balance: 11.97727886 > gas used: 286553 (0x45f59) > gas price: 20 gwei > value sent: 0 ETH > total cost: 0.00573106 ETH > Saving migration to chain. > Saving artifacts ------------------------------------- > Total cost: 0.00764022 ETH 3_add_large_balance.js ====================== Linking ------- * Contract: MetaCoinLargeBalance <--> Library: ConvertLib (at address: 0x26b09eCB5a7Ad418a028deae36E9e457db00e40d) Deploying 'MetaCoinLargeBalance' -------------------------------- > transaction hash: 0x17ed0571363b0765758cadfbdffcbc5792116e0b24e460ee94a55ef0e8cf119e > Blocks: 0 Seconds: 0 > contract address: 0x2dc17d0F8F5230cA62d3Aa5B2e30E6df8E551Cb5 > block number: 11 > block timestamp: 1640010018 > account: 0x0374AD83DfEB8cfD94889631255AE43B2Aa93bbe > balance: 11.97100022 > gas used: 286569 (0x45f69) > gas price: 20 gwei > value sent: 0 ETH > total cost: 0.00573138 ETH > Saving migration to chain. > Saving artifacts ------------------------------------- > Total cost: 0.00573138 ETH Summary ======= > Total deployments: 4
至此,我们就完成了一个新的币的发行
新合约的使用和交互 我们可以使用 truffle console
进行交互
1 2 let instance = await MetaCoin.deployed() # 定义一个 MetaCoin合约的实例 let accounts = await web3.eth.getAccounts() # 获取web3中eth的区块账号
查看部署合约账号中的token余额 即初始化,我们创建了多少代币
1 2 3 let balance = await instance.getBalance(accounts[0]) balance.toNumber() # 输出为 10000
查看token价值多少eth 1 2 3 let ether = await instance.getBalanceInEth(accounts[0]) ether.toNumber() # 20000
这里设置为1token = 2eth,所以输出20000
进行代币转账 1 2 #给address1,转账500token,我们最开始的instance是初始化账户,拥有10000token instance.sendCoin(accounts[1], 500)
查看address1账户余额 1 2 let received = await instance.getBalance(accounts[1]) received.toNumber()
查看address0 账户余额 1 2 let newBalance = await instance.getBalance(accounts[0]) newBalance.toNumber()