简单介绍

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

创建迁移文件:位于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/

Truffle 快速构建 DApp的更多相关文章

  1. 玩转Windows服务系列——使用Boost.Application快速构建Windows服务

    玩转Windows服务系列——创建Windows服务一文中,介绍了如何快速使用VS构建一个Windows服务.Debug.Release版本的注册和卸载,及其原理和服务运行.停止流程浅析分别介绍了Wi ...

  2. Apache Commons CLI官方文档翻译 —— 快速构建命令行启动模式

    昨天通过几个小程序以及Hangout源码学习了CLI的基本使用,今天就来尝试翻译一下CLI的官方使用手册. 下面将会通过几个部分简单的介绍CLI在应用中的使用场景. 昨天已经联系过几个基本的命令行参数 ...

  3. 使用Asp.net WebAPI 快速构建后台数据接口

    现在的互联网应用,无论是web应用,还是移动APP,基本都需要实现非常多的数据访问接口.其实对一些轻应用来说Asp.net WebAPI是一个很快捷简单并且易于维护的后台数据接口框架.下面我们来快速构 ...

  4. 利用Nodejs快速构建应用原型

    利用Nodejs快速构建应用原型 开发一个应用往往需要快速的构建原型,然后在此基础上设计和改进,前端可能立马能看到效果,但是后端业务逻辑不会那么快,这个时候其实我们需要额只是一些模拟数据,所以不需要真 ...

  5. C# Winform 通过FlowLayoutPanel及自定义的编辑控件,实现快速构建C/S版的编辑表单页面

    个人理解,开发应用程序的目的,不论是B/S或是C/S结构类型,无非就是实现可供用户进行查.增.改.删,其中查询用到最多,开发设计的场景也最为复杂,包括但不限于:表格记录查询.报表查询.导出文件查询等等 ...

  6. Java Swing快速构建窗体应用程序

    以前接触java感觉其在桌面开发上,总是不太方便,没有一个好的拖拽界面布局工具,可以快速构建窗体. 最近学习了一下NetBeans IDE 8.1,感觉其窗体设计工具还是很不错的 , 就尝试一下做了一 ...

  7. 【Android】如何快速构建Android Demo

    [Android]如何快速构建Android Demo 简介 在 Android 学习的过程中,经常需要针对某些项目来写一些测试的例子,或者在做一些 demo 的时候,都需要先写 Activity 然 ...

  8. 使用 Responsive Elements 快速构建响应式网站

    Responsive Elements 可以使任何元素来适应和应对他们所占据的区域.这是一个轻量的 JavaScript 库,你可以轻松嵌入到你的项目.元素会更具自己的宽度,自动响应和适应空间的增加或 ...

  9. Winform 通过FlowLayoutPanel及自定义的编辑控件,实现快速构建C/S版的编辑表单页面 z

    http://www.cnblogs.com/zuowj/p/4504130.html 不论是B/S或是C/S结构类型,无非就是实现可供用户进行查.增.改.删,其中查询用到最多,开发设计的场景 也最为 ...

随机推荐

  1. [AST Babel Plugin] Hanlde ArrowFunction && FunctionExpression

    Continue with previous post: https://www.cnblogs.com/Answer1215/p/12342540.html Now we need to think ...

  2. css之float(浮动)的特性

    详解CSS float属性  float本身不脱离文档流,但是和 position:absolute;搭配使用会脱离文档流 阅读目录 基础知识 float的详细细节 float特殊情况 clear属性 ...

  3. STM32F103之定时器学习记录

    /==============翻译STM32F103开发手册定时器部分========================/ 14 高级控制计时器(TIM1和TIM8) 14.1 TIM1和TIM8介绍 ...

  4. AcWing 802. 区间和 离散化

    https://www.acwing.com/problem/content/804/ #include <iostream> #include <vector> #inclu ...

  5. 矩阵快速幂 F[n]=F[n-2]*2+F[n-1]+i^4 hdu5950

    #include<cstdio> #include<algorithm> #include<math.h> #include<string.h> usi ...

  6. 【网页浏览】怀旧xp画图网页版

    非常古老的WindowsXP画图工具 传送链接

  7. calloc函数的使用和对内存free的认识

    #include<stdlib.h> void *calloc(size_t n, size_t size): free(); 目前的理解:  n是多少个这样的size,这样的使用类似有f ...

  8. C# RichTextBox实现背景透明

    这几天在做一个文本编辑器,要将RichTextBox的背景透明,但是发现C#的RichTextBox是不支持将背景设置为Transparent(透明). 网上找了好多方法,但都不行. 后来自己想了个办 ...

  9. python 字符串的一些函数

    split()函数 split()      以 空格 为分割符分割字符串,返回列表 split('_')   以'_'为分割符分割字符串,返回列表 strip() 函数  去掉前后的空格 下面是字符 ...

  10. Visual C++ 2013 and Visual C++ Redistributable Package 更新版官网下载地址

    Visual C++ 2013 and Visual C++ Redistributable Visual C++ 2013 and Visual C++ Redistributable Packag ...