当前位置: 首页 > news >正文

anker 网站谁做的营销咨询公司排名前十

anker 网站谁做的,营销咨询公司排名前十,巴青网站制作,列举一个网络营销的案例6-5,web3浏览器链接区块链(react区块链实战) 6-5 web3浏览器链接区块链(调用读写合约与metamask联动) 6-5 web3浏览器链接区块链(调用读写合约与metamask联动) 这里就是浏览器端和智能合约的交…

6-5,web3浏览器链接区块链(react+区块链实战)

  • 6-5 web3浏览器链接区块链(调用读写合约与metamask联动)

6-5 web3浏览器链接区块链(调用读写合约与metamask联动)

这里就是浏览器端和智能合约的交互

两个库

Web3
Truffle contract //truffle在链接前端合约简单包了一层,比较好用

来到react项目的根目录下(在第一章进行了创建),
在这里插入图片描述

这里重新创建一个react项目
https://blog.csdn.net/u012118993/article/details/87288516
react创建新项目 使用creat-react-app快速新建一个react项目

(1)npm install -g create-react-app 全局安装(安装到整体)

(2)create-react-app reactproject 新建并对react项目进行命名(注:项目名称不能有大写)

(3)cd reactproject 通过命令进入文件夹内部,准备运行项目

(4)npm start 运行项目

E:\truffle\woniu-pet-shop

在truffle目录下创建
在这里插入图片描述

在react的项目下安装下面的文件
安装web3(安装到文件夹下面)

npm install web3 --save

在这里插入图片描述

再安装truffle-contract

npm install truffle-contract --save

Demo完成

1.链接合约
2.执行一下合约内部函数
3.添加ant.design ui库支持
4.完成项目

在这里插入图片描述

注意新的
在这里插入图片描述

App.js中的内容
如下

import React from 'react'
import Web3 from 'web3'
import TruffleContract from 'truffle-contract'
import AdoptionJson from './truffle/build/contracts/Adoption.json'	//引入前面智能合约编译好得到的json文件class App extends React.Component{constructor(props){super(props)this.web3 = nullthis.Adoption = nullthis.init()this.state = {//name:'woniu'}}init(){//如果网页中的web3不是undefinedif(typeof window.web3 != 'undefined'){}}render(){return <button></button>//hello,{this.state.name}}
}export default App

在这里插入图片描述

如果浏览器中装入了metamask插件

在浏览器中全局变量就有值
在这里插入图片描述

可以手动链接metamask,也可以自动连接
在这里插入图片描述

进行脚本启动,之前web3在浏览器控制台undefined是因为空网页,此时

在react项目自建的petshop中启动

Npm start

在浏览器打开
http://localhost:3000/

视频效果
在这里插入图片描述

视频安装新的包
在这里插入图片描述
在这里插入图片描述

自己的有报错,原因init函数没有保存

在这里插入图片描述

实际成功后如下效果
在这里插入图片描述

在这里插入图片描述

新加入

init(){//如果网页中的web3不是undefinedif(typeof window.web3 != 'undefined'){this.web3Provider = window.web3.currentProvider;	//metamask内置了web3的实例,实际可以手动链接}else{alert('请按照metamask')}}

更改代码保存后,再次访问网络,无需关闭重新npm start

此时刷新一下
当然这时无弹窗,将metamask钱包删掉之后就有弹窗

这时使用chrome没有钱包的浏览器访问
在这里插入图片描述

有弹窗出现,但是有些乱码了

又加入代码

在这里插入图片描述

所有代码如下

import React from 'react'
import Web3 from 'web3'
import TruffleContract from 'truffle-contract'
import AdoptionJson from './truffle/build/contracts/Adoption.json'	//引入前面智能合约编译好得到的json文件//1.链接合约
//2.执行一下合约内部函数
//3.添加ant.design ui库支持
//4.完成项目
class App extends React.Component{constructor(props){super(props)this.web3 = nullthis.Adoption = nullthis.init()this.state = {//name:'woniu'}}init(){//如果网页中的web3不是undefinedif(typeof window.web3 != 'undefined'){this.web3Provider = window.web3.currentProvider;	//metamask内置了web3的实例,实际可以手动链接}else{alert('please install metamask')}this.web3 = new Web3(this.web3Provider)		//将我们的this.web3Provider装载进来this.initAdoption()}initAdoption(){this.Adoption = TruffleContract(AdoptionJson)	//使用TruffleContract传入编译后的合约,然后创建实例,可以调用合约内部函数this.Adoption.setProvider(this.web3Provider)	//设置来源,链接合约return this.markAdopted()}//部署,这个是异步的,使用this.Adoption.deployed().then()也可以,这里用//this.markAdopted(){//部署链接一下//	const adoptionInstance = this.Adoption.deployed().then()	//}async markAdopted(){//部署链接一下//await同步方式获取异步数据const adoptionInstance = await this.Adoption.deployed()	//部署,这个是异步的,使用this.Adoption.deployed().then()也可以,这里用//调用合约内部函数getAdoptersconst adopters = await adoptionInstance.getAdopters.call()console.log(adopters)}render(){return <button></button>//hello,{this.state.name}}
}export default App

刷新界面如下
在这里插入图片描述

获取到合约的address了
在这里插入图片描述

使用上方的变量可以获取本地的地址及metamask的默认账号地址了
在这里插入图片描述

https://blog.csdn.net/weixin_41937552/article/details/106990561?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-1&spm=1001.2101.3001.4242

这里无法获得metamask的地址的原因如上
https://blog.csdn.net/weixin_39421014/article/details/103323245

可以先把metamask的隐私权限关闭
https://www.freesion.com/article/8518937500/
隐私模式的设置与兼容JS代码
在这里插入图片描述

https://blog.csdn.net/JackieDYH/article/details/115380677?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-0&spm=1001.2101.3001.4242
获取账户信息

经过代码的修改后在初始化的函数部分修改,

可以使用metamask链接网站了,并且能够打印出当前的metamask地址
在这里插入图片描述

接下来进行点击事件的代码更改
在这里插入图片描述

此处点击领养会弹出框用来支付的,因为要调用写入函数,写到链上的,此处的领养不用转钱但需要父手续费

本地成功调用需要写入区块链的函数(当点击按钮时如下)
在这里插入图片描述

最终成功运行的所有的代码如下:

import React from 'react'
import Web3 from 'web3'
import TruffleContract from 'truffle-contract'
import AdoptionJson from './truffle/build/contracts/Adoption.json'	//引入前面智能合约编译好得到的json文件//1.链接合约
//2.执行一下合约内部函数
//3.添加ant.design ui库支持
//4.完成项目
class App extends React.Component{constructor(props){super(props)this.web3 = nullthis.Adoption = nullthis.init()this.state = {//name:'woniu'}}async init(){//如果网页中的web3不是undefined//if(typeof window.web3 != 'undefined'){//	this.web3Provider = window.web3.currentProvider;	//metamask内置了web3的实例,实际可以手动链接//}else{//	alert('please install metamask')//}//this.web3 = new Web3(this.web3Provider)		//将我们的this.web3Provider装载进来//this.initAdoption()/* 新版的方式 *///var web3Provider;if (window.ethereum) {this.web3Provider = window.ethereum;try {// 请求用户授权await window.ethereum.enable();} catch (error) {// 用户不授权时console.error("User denied account access")}} else if (window.web3) {   // 老版 MetaMask Legacy dapp browsers...this.web3Provider = window.web3.currentProvider;} else {this.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');}this.web3 = new Web3(this.web3Provider);//web3js就是你需要的web3实例this.web3.eth.getAccounts(function (error, result) {if (!error)console.log(result)//授权成功后result能正常获取到账号了//this.account = result});//this.account =result//this.account =accountthis.initAdoption()}initAdoption(){this.Adoption = TruffleContract(AdoptionJson)	//使用TruffleContract传入编译后的合约,然后创建实例,可以调用合约内部函数this.Adoption.setProvider(this.web3Provider)	//设置来源,链接合约return this.markAdopted()}//部署,这个是异步的,使用this.Adoption.deployed().then()也可以,这里用//this.markAdopted(){//部署链接一下//	const adoptionInstance = this.Adoption.deployed().then()	//}async markAdopted(){//部署链接一下//await同步方式获取异步数据const adoptionInstance = await this.Adoption.deployed()	//部署,这个是异步的,使用this.Adoption.deployed().then()也可以,这里用//调用合约内部函数getAdoptersconst adopters = await adoptionInstance.getAdopters.call()console.log(adopters)}async adopt(petId){//const account = window.web3.eth.defaultAccount		//获取metamask中默认的账户// 授权获取账户const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });const myAccount = accounts[0];	//获取当前metamask的地址const adoptionInstance = await this.Adoption.deployed()		//再次进行部署await adoptionInstance.adopt(petId,{from:myAccount})	//调用adopt只传递唯一一个参数,以及来源之前获取的地址,进行写入函数this.markAdopted()}render(){//onclick点击事件,调用领养函数return <button onClick={()=>this.adopt(2)}>领养第二个</button>//hello,{this.state.name}}
}export default App

已经成功执行所有的函数,读取写入函数

此代码还有一些缺陷,若交易失败会报错,页面也会报错,
点击拒绝,或者直接退出时
在这里插入图片描述

在这里插入图片描述

接下来就是美化ui

http://www.hkea.cn/news/187548/

相关文章:

  • 做网站去哪个平台资源优化排名网站
  • 备案的网站名称可以改吗百度青岛代理公司
  • 专做进口批发的网站关键词优化多少钱
  • 做网站有了空间在备案吗百度权重高的网站有哪些
  • 做空间的网站著名的网络营销案例
  • 做网站客户尾款老不给怎么办百度推广年费多少钱
  • 想要将网站信息插到文本链接怎么做百度关键词搜索
  • 江苏网站备案要多久seo域名综合查询
  • 大型网站建设机构津seo快速排名
  • 建设证件查询官方网站宁波做网站的公司
  • 那些网站招聘在家里做的客服网店推广策略
  • 湘西 网站 建设 公司sem代运营托管公司
  • 用css为wordpress排版西安seo外包服务
  • vs2005做网站百度推广官方网站登录入口
  • 乐从网站建设公司北京seo优化推广
  • 如何在网上接做网站的小项目市场监督管理局电话
  • 淘宝购物站优化
  • 石家庄最新疫情轨迹河南网站优化公司哪家好
  • 网站色彩搭配服务器ip域名解析
  • 哪个网站专业做安防如何注册域名网站
  • 穆棱市住房和城乡建设局网站关键词词库
  • 成都网站建设市场什么是网络营销的核心
  • 深圳找人做网站廊坊优化外包
  • 衡阳市城市建设投资有限公司网站湖南企业seo优化报价
  • css做网站常用百度权重优化软件
  • 合合肥网站建设制作网站用什么软件
  • 杭州网站设计公司推荐网络推广与优化
  • 移动惠生活app下载网址荆门网站seo
  • 做网站很赚钱吗关键词自助优化
  • wordpress小工具里的用户中心南京谷歌优化