简单介绍
官网传送门 Truffle是针对基于以太坊的Solidity语言的一套开发框架。本身基于Javascript,使用以太坊虚拟机(EVM)的世界一流的开发环境,用于区块链的测试框架和资产管道,旨在简化开发人员的生活。
安装truffle
安装前提:
预装nodejs
1.全局安装
npm install -g truffle
建立软链接
ln -s /opt/node-v10.16.0-linux-x64/bin/truffle /usr/local/bin/truffle
2.单个项目安装
npm install truffle
3.查看版本号
truffle version
4.安装成功
构建应用
快速开始 https://www.trufflesuite.com/docs/truffle/quickstart
创建项目
mkdir simple-dapp
初始化项目
truffle init
Note: You can use the truffle unbox <box-name> command to download any of the other Truffle Boxes.
Note: To create a bare Truffle project with no smart contracts included, use truffle init.
本次采用基于webpack的box来构建
truffle unbox webpack
项目目录结构:
- app/:Diretory for app
contracts/
: Directory for Solidity contractsmigrations/
: Directory for scriptable deployment filestest/
: Directory for test files for testing your application and contractstruffle-config.js
: Truffle configuration file
创建合约:位于contracts文件夹下
pragma solidity >=0.4.21 <0.7.0; contract Voting { //候选人得票数目 mapping (bytes32=>uint8) public votesReceived; //候选人 bytes32[] public candidateList; //构造器 constructor(bytes32[] memory candidateNames) public { candidateList = candidateNames; } //查询投票数目 function totalVotesFor(bytes32 candidate) view public returns (uint8){ //非空校验 require(validCandidate(candidate)); return votesReceived[candidate]; } //投票 function VotingTo(bytes32 candidate) public { //非空校验 require(validCandidate(candidate)); //add votesReceived[candidate]+=1; } //校验 function validCandidate(bytes32 candidate) view public returns (bool){ for(uint i = 0;i<candidateList.length;i++){ if(candidateList[i]==candidate){ return true; } } return false; } }
创建迁移文件:位于migrations文件夹下
const voting = artifacts.require('../contracts/Voting.sol') module.exports = function (deployer) { deployer.deploy(voting,['Mikey','Leo','Don'].map(x=>web3.utils.asciiToHex(x)),{gas:2900000}); }
编译智能合约
truffle compile
生成json文件
启动truffle的开发者模式
truffle develop
部署智能合约
migrate
Starting migrations... ====================== > Network name: 'develop' > Network id: 5777 > Block gas limit: 0x6691b7 1_initial_migration.js ====================== Deploying 'Migrations' ---------------------- > transaction hash: 0x5d23f5f2452d41a20ee56b238efb21c9c81cb04e0105c874e5a26528538476e5 > Blocks: 0 Seconds: 0 > contract address: 0xD97313Cb13D2d39Bd8CD2CB6f3606aaCB89fa1Ad > block number: 13 > block timestamp: 1578995366 > account: 0x9c29d178Ba516574E372e3cf5Eda833359F4114F > balance: 99.96269208 > gas used: 263741 > gas price: 20 gwei > value sent: 0 ETH > total cost: 0.00527482 ETH > Saving migration to chain. > Saving artifacts ------------------------------------- > Total cost: 0.00527482 ETH 2_deploy_contracts.js ===================== Deploying 'ConvertLib' ---------------------- > transaction hash: 0x71bc728418b983c24aa541ffebffc8b2eafa53f1f5754e8a800a8f5688947069 > Blocks: 0 Seconds: 0 > contract address: 0x7caC31cFfE12a8A4D7F7bD83a97e3AF0E99206CE > block number: 15 > block timestamp: 1578995366 > account: 0x9c29d178Ba516574E372e3cf5Eda833359F4114F > balance: 99.95973214 > gas used: 105974 > gas price: 20 gwei > value sent: 0 ETH > total cost: 0.00211948 ETH Linking ------- * Contract: MetaCoin <--> Library: ConvertLib (at address: 0x7caC31cFfE12a8A4D7F7bD83a97e3AF0E99206CE) Deploying 'MetaCoin' -------------------- > transaction hash: 0x36bef9d0355bfcf5fd0fad5fd69574b9be37117b92883f03d1e76877cc07e045 > Blocks: 0 Seconds: 0 > contract address: 0xaAd2C9659fec947A764b244E37A281d219fb0a21 > block number: 16 > block timestamp: 1578995366 > account: 0x9c29d178Ba516574E372e3cf5Eda833359F4114F > balance: 99.9529182 > gas used: 340697 > gas price: 20 gwei > value sent: 0 ETH > total cost: 0.00681394 ETH > Saving migration to chain. > Saving artifacts ------------------------------------- > Total cost: 0.00893342 ETH 3_initial_voting.js =================== Deploying 'Voting' ------------------ > transaction hash: 0x0a5c69ce8595788ef70513f2533af7f7a2c230525d50d68abaf30e49fed2c2ff > Blocks: 0 Seconds: 0 > contract address: 0xdafBED4742Ff4b2bC17f0908D2E97DfA644B5417 > block number: 18 > block timestamp: 1578995366 > account: 0x9c29d178Ba516574E372e3cf5Eda833359F4114F > balance: 99.94517058 > gas used: 360358 > gas price: 20 gwei > value sent: 0 ETH > total cost: 0.00720716 ETH > Saving migration to chain. > Saving artifacts ------------------------------------- > Total cost: 0.00720716 ETH Summary ======= > Total deployments: 4 > Final cost: 0.0214154 ETH truffle(develop)>
部署成功
构建DApp
import Web3 from "web3"; import metaCoinArtifact from "../../build/contracts/MetaCoin.json"; const App = { web3: null, account: null, meta: null, start: async function() { const { web3 } = this; try { // get contract instance const networkId = await web3.eth.net.getId(); const deployedNetwork = metaCoinArtifact.networks[networkId]; this.meta = new web3.eth.Contract( metaCoinArtifact.abi, deployedNetwork.address, ); // get accounts const accounts = await web3.eth.getAccounts(); this.account = accounts[0]; this.refreshBalance(); } catch (error) { console.error("Could not connect to contract or chain."); } }, refreshBalance: async function() { const { getBalance } = this.meta.methods; const balance = await getBalance(this.account).call(); const balanceElement = document.getElementsByClassName("balance")[0]; balanceElement.innerHTML = balance; }, sendCoin: async function() { const amount = parseInt(document.getElementById("amount").value); const receiver = document.getElementById("receiver").value; this.setStatus("Initiating transaction... (please wait)"); const { sendCoin } = this.meta.methods; await sendCoin(receiver, amount).send({ from: this.account }); this.setStatus("Transaction complete!"); this.refreshBalance(); }, setStatus: function(message) { const status = document.getElementById("status"); status.innerHTML = message; }, }; window.App = App; window.addEventListener("load", function() { if (window.ethereum) { // use MetaMask's provider App.web3 = new Web3(window.ethereum); window.ethereum.enable(); // get permission to access accounts } else { console.warn( "No web3 detected. Falling back to http://127.0.0.1:8545. You should remove this fallback when you deploy live", ); // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail) App.web3 = new Web3( new Web3.providers.HttpProvider("http://127.0.0.1:8545"), ); } App.start(); });