ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Truffle 快速构建 DApp

2020-01-14 18:07:42  阅读:574  来源: 互联网

标签:function const web3 gas DApp 构建 truffle Truffle ETH


简单介绍

官网传送门  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

 

项目目录结构:

创建合约:位于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;
    }
}
View Code

 

创建迁移文件:位于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});
}
3_deploy_voting.js

 

编译智能合约

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();
});
index.js

 

 

 

相关资料

官网文档:https://www.trufflesuite.com/docs/truffle/overview

参考博文:https://truffle.tryblockchain.org/

标签:function,const,web3,gas,DApp,构建,truffle,Truffle,ETH
来源: https://www.cnblogs.com/biaogejiushibiao/p/12193371.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有