ganache私链部署智能合约+本地网络Dapp
参考自(3条消息) 区块链投票应用:使用solidity+truffle+metamsk开发Dapp应用_一袋芋头的博客-CSDN博客下载了项目示例webpack之后
我们需要将里面的其他合约都删除,也可以直接删除这两个文件夹里的内容
然后就可以开始正片了(当然,你得先前就安装好环境)
开启ganache私链,
为了后续实验方便,这里我们通过指定数据存放目录来确保账号等数据保持不变,采用如下命令来启动ganache-cli私链:
#ganache-cli -db /root/MyGanacheData
然后我们需要去创建合约,编译部署至该私链上
首先在项目的合约目录,也就是/contract目录下创建sol文件
$ touch Voting.sol
$ gedit Voting.sol
// SPDX-License-Identifier: SimPL-2.0
pragma solidity >=0.4.18 <0.8.0;contract Voting{bytes32[] public candodateList;mapping(bytes32 => uint8) public votesReceived;constructor(bytes32[] memory _candidateListName) public {candodateList = _candidateListName;}function validateCan(bytes32 _candidate) internal view returns(bool) {for(uint8 i = 0; i< candodateList.length; i++) {if(candodateList[i] == _candidate)return true;}return false;}function voteForCan(bytes32 _candidate) public {votesReceived[_candidate] += 1;}function totalVotes(bytes32 _candidate)public view returns(uint8) {return votesReceived[_candidate];}
}
然后我们进行编译,truffle compile,编译成功说明我们的.sol合约文件没有啥语法错误,并无卵用
然后基于这个sol文件来生成迁移文件 truffle create migration deploy_Voting,命令执行后会在migrations下生成迁移文件
前面一串数字是truffle部署时所用的ID,这里没有用,不用管,继续,我们需要更改这个js,需要传入参数
// const ConvertLib = artifacts.require("ConvertLib");
// const MetaCoin = artifacts.require("MetaCoin");
const Voting = artifacts.require("Voting");module.exports = function(deployer) {// deployer.deploy(ConvertLib);// deployer.link(ConvertLib, MetaCoin);// deployer.deploy(MetaCoin);deployer.deploy(Voting, ["0x4100000000000000000000000000000000000000000000000000000000000000","0x4200000000000000000000000000000000000000000000000000000000000000","0x4300000000000000000000000000000000000000000000000000000000000000"]);//注意Voting部署时有参数
};
修改网络方式,修改truffle-conflig.js
module.exports = {networks: {development: {host: 'localhost',port: 8545,network_id: '*' // Match any network id}}
}
下面我们就可以部署合约了,truffle migrate
这是合约部署 的详细情况
这是ganache私链的部署情况,因为一次部署就相当于是一次交易,所以会生成区块,会有交易哈希值产生,到此合约已经部署完毕,但是我们不好使用这个合约,所以我们需要写前端来方便我们使用它
在app/src/目录下写index.html和index.js文件
index.html
<!DOCTYPE html>
<html><head><title>Voting</title>
</head>
<style>input {display: block;margin-bottom: 12px;}
</style><body><h1>Voting App</h1><p>Alice : <strong id="alice">loading...</strong> tickets</p><p>Bob : <strong id="bob">loading...</strong> tickets</p><label>VotoFor :</label><input type="text" id="candidate" /><button onclick="App.voteForCan()">vote</button><script src="index.js"></script>
</body></html>
index.js
import Web3 from "web3";
//导入Voting.json
import votingArtifact from "../../build/contracts/Voting.json";
//定义两个值,方便传参
const aInBytes32 = "0x4100000000000000000000000000000000000000000000000000000000000000";
const bInBytes32 = "0x4200000000000000000000000000000000000000000000000000000000000000";
const cInBytes32 = "0x4300000000000000000000000000000000000000000000000000000000000000";//定义了app
const App = {web3: null,account: null,voting: null, //定义一个voting的实例//启动时需要的start: async function() {const { web3 } = this;try {// get contract instanceconst networkId = await web3.eth.net.getId();//要改成自己的网络const deployedNetwork = votingArtifact.networks[networkId];this.voting = new web3.eth.Contract(votingArtifact.abi,deployedNetwork.address,);// get accountsconst accounts = await web3.eth.getAccounts();this.account = accounts[0];//先刷新一下界面this.ready();} catch (error) {console.error("Could not connect to contract or chain.");}},//start函数后还应该有哪些函数呢//1.refresh展示最新票数refresh: async function(id, nameInBytes32) {const { totalVotes } = this.voting.methods;//从区块链拿到票数const tickets = await totalVotes(nameInBytes32).call();//写到页面上去,并更新最新票数const element = document.getElementById(id);element.innerHTML = tickets.toString();},//2.ready:当网页好的时候要把票数读出来ready: async function() {try {this.refresh("alice", aInBytes32);this.refresh("bob", bInBytes32);this.refresh("clilly", cInBytes32);} catch (err) {console.log(err);}},//3.votingfor函数(中间需要展示票数的函数)voteForCan: async function() {try {//先获取到合约的方法const { voteForCan } = this.voting.methods;//再获取候选者名字:来自输入框const candidateName = document.getElementById("candidate").value;//拿到票数后对应加一if (candidateName == "Alice") {await voteForCan(aInBytes32).send({ from: this.account });this.refresh("alice", aInBytes32);} else {if (candidateName == "Bob") {await voteForCan(bInBytes32).send({ from: this.account });this.refresh("bob", bInBytes32);}else{if (candidateName == "Cilly") {await voteForCan(cInBytes32).send({ from: this.account });this.refresh("cilly", cInBytes32);}}}} catch (err) {//如果有err就打印一下console.log(err);}}};window.App = App;window.addEventListener("load", function() {if (window.ethereum) {// use MetaMask's providerApp.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();
});
然后运行
必须在App目录运行
npm run dev
额,报错了error:0308010C:digital envelope routines::unsupported,什么依托答辩,没办法,只好上网搜解答
export NODE_OPTIONS=--openssl-legacy-provider
临时解决问题,编译成功了,很牛逼,但是不知道为什么,用了再说
然后去自己浏览器打开http://localhost:8080/
可以开始使用,每次投票都会生成一个区块,保证了数据的不容篡改
ganache私链部署智能合约+本地网络Dapp相关推荐
- Linux环境下搭建区块链私有链+部署智能合约
文章目录 一.前期准备 二.安装goland环境 三.安装go版本的以太坊源码,并编译 四.私有链搭建 五.私有链节点加入 六.部署智能合约 七.参考链接 一.前期准备 安装更新相关组件 sudo y ...
- 如何在私有链部署智能合约
2017年12月25日 15:08:38 阅读数:5629 原文:How To Write, Deploy, and Interact with Ethereum Smart Contracts on ...
- 【区块链】DOCKER部署量子链私有网络环境以及部署智能合约实践
DOCKER部署量子链私有网络环境以及部署智能合约实践 安装环境 安装Docker https://store.docker.com/editions/community/docker-ce-desk ...
- 代币转账_手把手教你从源代码开始搭建多节点以太坊私链(五)部署智能合约及代币发行...
一.安装以太坊合约编译环境 安装solc 智能合约代码的编译可以通过第三方平台或者软件.不过,为了安全起见,还是搭建自己的编译器比较好.(But be aware that if the compil ...
- 区块链学习(3) 以太坊测试环境编译并部署智能合约(mac版)
选择编写智能合约的语言 Ethereum上的智能合约需要使用solidity语言来撰写.虽然还有其他能用来撰写智能合约的语言如Serpent(类Python).lll(类Fortran),但目前看到所 ...
- 区块链100讲:Truffle——一个更简单的部署智能合约的方法
本期<区块链100讲>我们将介绍一个更简单的部署智能合约的方法:Truffle. 1 什么是Truffle ? Truffle是针对基于以太坊的Solidity语言的一套开发框架.本身基于 ...
- 使用remix和matemask部署智能合约到以太坊测试网络
目录 1. 平台准备 1.1 网页版remix 1.2 Chrome浏览器插件metamask 2. 部署智能合约 1. 平台准备 1.1 网页版remix 进入网址 https://remix.et ...
- 区块链之java调用智能合约(二)部署智能合约
前言 上一节,已经说过,如何的创建一个合约,如何编译合约,然后在java中调用. 但是呢,这些还远远不够.那么还差哪些呢? 现在就是如何将创建的智能合约部署的对应的共链,私链,测试链中了. 需要部署后 ...
- 区块链开发入门:基于以太坊智能合约构建 ICO DApp
写给前端开发者的第一本区块链开发入门指南,通过从 0 到 1 实战开发一个 ICO DApp 项目(基于 V 神的 DAICO 设计思想),深入掌握区块链及以太坊技术. 为什么要选择区块链开发? 未来 ...
最新文章
- 超详细的Python matplotlib 绘制柱状图
- return 和 方法的递归调用
- git 修改分支名字_开发中必须要掌握的 Git 技巧
- linux phpunit 安装,PHPUnit安装教程
- 关于程序、进程和线程
- 【报告分享】2020海外留学趋势报告.pdf(附下载链接)
- 大神干货:腾讯广告算法大赛亚军女极客生存图鉴
- byte数组转double_java数组(不同类型的初始值)
- Windows下CMake安装教程
- 在电脑上如何剪辑音乐?
- 使用批处理文件删除所有子文件夹?
- Windows 7 64位 旗舰版 激活 心得 提示:系统保留分区设置驱动器号
- 应用代码(4)——基于ADS1110芯片的高精度温度(PT1000)采集
- 如何读书阅读--每天一本书计划
- AI:ModelScope(一站式开源的模型即服务共享平台)的简介、安装、使用方法之详细攻略
- 商汤科技创业领导_从技术专家到领导者的创业之旅
- C++ - 文件读写(fstream)
- Material Design的基础知识
- HanLP 部署及NLP+ML双生树思维导图
- 一个大四前端实习生的2018年总结