博客
关于我
[区块链笔记12] 第一个DApp--demo
阅读量:327 次
发布时间:2019-03-04

本文共 3396 字,大约阅读时间需要 11 分钟。

初始化项目

git clone https://github.com/truffle-box/bare-box

如果是linux环境需要truffle.json cp truffle-config.js truffle.js

编辑InfoContract.sol

pragma solidity ^0.4.23;contract Info {       string name;    uint age;    event doneEvent(string name, uint age);    function setInfo(string _name, uint _age) public {           name = _name;        age = _age;        emit doneEvent(name, age);    }    function getInfo() public view returns (string, uint) {           return (name, age);    }}

编译合约 truffle compile

打开Ganache,分配一个区块链网络

配置truffle.js文件, 127.0.0.1和7545端口

编辑一个2_info_migration.js文件

const Info = artifacts.require("Info");module.exports = function(deployer) {     deployer.deploy(Info);};

部署合约truffle migrate

npm init用npm初始化,因为后续要用npm 来install包

npm install truffle-contract安装truffle-contract,方便和智能合约交互

创建src和js和css文件夹,用于存放html和js和css文件

把web3.min.js和jquery.min.js和truffle-contract.min.js拷贝到js目录下

css目录下创建index.css,内容如下

body {   	background-color: #F0F0F0;}#info {   	padding: 20px;	background-color: #FFF;}#button {   	width: 100px;}

编辑src下的index.html

	
Dapp demo

Dapp demo

正在加载

编辑app.js文件

App = {   	web3Provider: null,	contracts: {   },	init: function(){   		ethereum.enable();		return App.initWeb3();	},	initWeb3: function(){   		 		if (typeof web3 !=='undefined') {   			App.web3Provider = web3.currentProvider;			web3 = new Web3(App.web3Provider);			//web3.eth.defaultAccount = web3.eth.accounts[0];		} else {   			App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');			web3 = new Web3(App.web3Provider);		}		// ethereum.enable(function(){     //           web3.eth.getAccounts((error, accounts) => {     //                web3.eth.defaultAccount = accounts[0];  //                 console.log(accounts)  //           })  //       }) 		App.initContract();	},	initContract: function() {   		$.getJSON('../build/contracts/InfoContract.json', function(data) {   			App.contracts.InfoContract = TruffleContract(data);			App.contracts.InfoContract.setProvider(App.web3Provider);			App.watchChanged();			return App.getInfo();		});		App.bindEvents();					},	getInfo: function() {   		App.contracts.InfoContract.deployed().then(function(instance) {   			return instance.getInfo.call();		}).then(function(result) {   			$('#loader').hide();			$('#info').html(result[0] + '(' + result[1] + ')years old');		}).catch(function(error) {   			alert(error);		});	},		bindEvents: function() {   		$('#button').click(function() {   			$('#loader').show();			App.contracts.InfoContract.deployed().then(function(instance) {   				var message = {   from: "0x549697a9ca1D3D5a2068b165f6B5AC70Da6ef813", gas:500000};				return instance.setInfo.sendTransaction($('#name').val(), $('#age').val(), message);			}).then(function(result) {   				return App.getInfo();			}).catch(function(error) {   				alert(error);			});		});	},	watchChanged: function() {   		App.contracts.InfoContract.deployed().then(function(instance) {   			var infoEvent = instance.doneEvent();			infoEvent.watch(function(error, result) {   				$('#loader').hide();				$('#info').html(result.args.name + '(' + result.args.age + ')years old');			});		});	}}$(window).on('load', function() {   	App.init();});

开启服务器,接着就可以进行操作了

但是会报错,不知道啥原因。明白的大佬麻烦回复一下。

不过我不报太大希望了,之前一篇博客问的问题二百多个人访问,没有一个人回复的,真的很心累了。
第1个bug
在这里插入图片描述

在这里插入图片描述

第2个bug
在这里插入图片描述

转载地址:http://bknh.baihongyu.com/

你可能感兴趣的文章
Mysql:mysql 5.X 报错 ERROR 1193 (HY000): Unknown system variable ‘validate_password_length‘
查看>>
MySQL:MySQL执行一条SQL查询语句的执行过程
查看>>
Mysql:SQL性能分析
查看>>
mysql:SQL按时间查询方法总结
查看>>
MySQL:什么样的字段适合加索引?什么样的字段不适合加索引
查看>>
MySQL:判断逗号分隔的字符串中是否包含某个字符串
查看>>
MySQL:某个ip连接mysql失败次数过多,导致ip锁定
查看>>
MySQL:索引失效场景总结
查看>>
Mysql:避免重复的插入数据方法汇总
查看>>
M_Map工具箱简介及地理图形绘制
查看>>
m_Orchestrate learning system---二十二、html代码如何变的容易
查看>>
n = 3 , while n , continue
查看>>
n 叉树后序遍历转换为链表问题的深入探讨
查看>>
N-Gram的基本原理
查看>>
n1 c语言程序,全国青少年软件编程等级考试C语言经典程序题10道七
查看>>
nacos config
查看>>
Nacos Derby 远程命令执行漏洞(QVD-2024-26473)
查看>>
Nacos 与 Eureka、Zookeeper 和 Consul 等其他注册中心的区别
查看>>
Nacos2.X 配置中心源码分析:客户端如何拉取配置、服务端配置发布客户端监听机制
查看>>
NacosClient客户端搭建,微服务注册进nacos
查看>>