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

山西省财政厅网站三基建设专栏饮食中心网站建设方案

山西省财政厅网站三基建设专栏,饮食中心网站建设方案,郑州seo管理,php网站开发路线题记 用Web3实现前端与智能合约的交互#xff0c;以下是操作流程和代码。 准备ganache环境 文章地址#xff1a;4.DApp-MetaMask怎么连接本地Ganache-CSDN博客 准备智能合约 文章地址#xff1a; 2.DApp-编写和运行solidity智能合约-CSDN博客 编写index.html文件 !…题记 用Web3实现前端与智能合约的交互以下是操作流程和代码。 准备ganache环境 文章地址4.DApp-MetaMask怎么连接本地Ganache-CSDN博客  准备智能合约  文章地址 2.DApp-编写和运行solidity智能合约-CSDN博客 编写index.html文件 !DOCTYPE html html head     titleName Contract Demo/title     !--导入web3库--     script srchttps://cdn.jsdelivr.net/npm/web31.5.2/dist/web3.min.js/script     script     // 检查Metamask是否已安装     if (typeof window.ethereum ! undefined) {     console.log(Metamask已安装);     }     // 设置Web3.js提供者为Metamask     const provider window.ethereum;     const web3 new Web3(provider);     // 请求Metamask连接到以太坊网络     provider.request({ method: eth_requestAccounts })     .then(() {       console.log(Metamask已连接到以太坊网络);     })     .catch((err) {       console.error(无法连接到以太坊网络, err);     });     function setName() {     // 合约地址     const contractAddress 0x32FDC4E86421143b1c27dE49542Bc8ECE2B162a0;     // 合约ABI     const contractABI [     {         inputs: [             {                 internalType: string,                 name: _name,                 type: string             }         ],         name: setName,         outputs: [],         stateMutability: nonpayable,         type: function     },     {         inputs: [],         name: getName,         outputs: [             {                 internalType: string,                 name: ,                 type: string             }         ],         stateMutability: view,         type: function     }     ];     const contract new web3.eth.Contract(contractABI, contractAddress);     const name document.getElementById(name).value;     // 替换为您的账户地址web3.eth.defaultAccount     const fromAddress 0x4e8eB4d1C203929074A3372F3703E556820fEA57;     //contract.methods.setName(name).send({from: fromAddress})     contract.methods.setName(name).send({from: fromAddress})     .on(transactionHash, function(hash){         console.log(Transaction Hash:, hash);     })     .on(receipt, function(receipt){         console.log(Transaction Receipt:, receipt);     })     .on(error, function(error){         console.error(Error:, error);     });     }     function getName() {     // 合约地址     const contractAddress 0x32FDC4E86421143b1c27dE49542Bc8ECE2B162a0;     // 合约ABI     const contractABI [     {         inputs: [             {                 internalType: string,                 name: _name,                 type: string             }         ],         name: setName,         outputs: [],         stateMutability: nonpayable,         type: function     },     {         inputs: [],         name: getName,         outputs: [             {                 internalType: string,                 name: ,                 type: string             }         ],         stateMutability: view,         type: function     }     ];     const contract new web3.eth.Contract(contractABI, contractAddress);     contract.methods.getName().call()     .then(function(result) {         console.log(Name:, result);         document.getElementById(nameValue).innerText result;     })     .catch(function(error) {         console.error(Error:, error);     });     }     /script /head body     h1设置姓名/h1     label forname姓名:/label     input typetext idname     button οnclicksetName()设置姓名/button     br     button οnclickgetName()得到姓名/button     br     span idnameValue/span /body /html !DOCTYPE html html headtitleName Contract Demo/title!--导入web3库--script srchttps://cdn.jsdelivr.net/npm/web31.5.2/dist/web3.min.js/scriptscript// 检查Metamask是否已安装if (typeof window.ethereum ! undefined) {console.log(Metamask已安装);}// 设置Web3.js提供者为Metamaskconst provider window.ethereum;const web3 new Web3(provider);// 请求Metamask连接到以太坊网络provider.request({ method: eth_requestAccounts }).then(() {console.log(Metamask已连接到以太坊网络);}).catch((err) {console.error(无法连接到以太坊网络, err);});function setName() {// 合约地址const contractAddress 0x32FDC4E86421143b1c27dE49542Bc8ECE2B162a0; // 合约ABIconst contractABI [{inputs: [{internalType: string,name: _name,type: string}],name: setName,outputs: [],stateMutability: nonpayable,type: function},{inputs: [],name: getName,outputs: [{internalType: string,name: ,type: string}],stateMutability: view,type: function}]; const contract new web3.eth.Contract(contractABI, contractAddress);const name document.getElementById(name).value;// 替换为您的账户地址web3.eth.defaultAccountconst fromAddress 0x4e8eB4d1C203929074A3372F3703E556820fEA57; //contract.methods.setName(name).send({from: fromAddress})contract.methods.setName(name).send({from: fromAddress}).on(transactionHash, function(hash){console.log(Transaction Hash:, hash);}).on(receipt, function(receipt){console.log(Transaction Receipt:, receipt);}).on(error, function(error){console.error(Error:, error);});}function getName() {// 合约地址const contractAddress 0x32FDC4E86421143b1c27dE49542Bc8ECE2B162a0; // 合约ABIconst contractABI [{inputs: [{internalType: string,name: _name,type: string}],name: setName,outputs: [],stateMutability: nonpayable,type: function},{inputs: [],name: getName,outputs: [{internalType: string,name: ,type: string}],stateMutability: view,type: function}]; const contract new web3.eth.Contract(contractABI, contractAddress);contract.methods.getName().call().then(function(result) {console.log(Name:, result);document.getElementById(nameValue).innerText result;}).catch(function(error) {console.error(Error:, error);});}/script /head bodyh1设置姓名/h1label forname姓名:/labelinput typetext idnamebutton onclicksetName()设置姓名/buttonbrbutton onclickgetName()得到姓名/buttonbrspan idnameValue/span /body /html执行程序  使用vscode的Live Server打开网页 参考这篇文章的执行方法1.Vue-在独立页面实现Vue的增删改查-CSDN博客  展示图  发起交易  完成交易  后记 觉得有用可以点赞或收藏
http://www.pierceye.com/news/390905/

相关文章:

  • 公司网站未备案公众号如何推广产品
  • 网站建设服务器环境配置郑州网站建设企业名录
  • e福州官方网站wordpress注册目录
  • 国际外贸网络交易平台网页seo搜索引擎优化
  • 做网做网站建设网站建设图片怎么切
  • 国外数码印花图案设计网站36kr wordpress
  • 上海网站建设设计公司zencart 网站入侵
  • 阜蒙县自治区建设学校网站汉中市住建局建设厅网站官网
  • windows 2008 iis怎么搭建网站手机网站模板建站
  • 优设网官网首页seo教程搜索引擎优化
  • 做问卷给钱的网站页面设计结课总结
  • 洛阳集团网站建设wordpress 深度优化
  • python做网站缺点湛江市建网站
  • 济南网站建设(选聚搜网络)在线购物网站建设
  • 珠海专业做网站公司昆明搜索引擎推广
  • 阿里云 建设网站怎么样百度推广一级代理商名单
  • 湛江网站制作网站吉林省四平市网站建设
  • 酒店网站建设公司推广方法有哪些
  • 网站建设免备案免费空间wordpress在线教育主题购买
  • 网站后台验证码不显示成都市建设工程施工安监站网站
  • 榆林网站优化做图的网站
  • 在微信中做网站淄博 网站seo优化
  • 黑龙江省建设协会网站首页怎么用word做网站
  • 企业网站网站建设价格wordpress阶梯插件
  • 企业品牌建设方案范文roseonly企业网站优化
  • 电影资源采集网站咋做在百度上怎么建立网站吗
  • 好的企业网站设计方案中小企业网站建设 论文
  • 毕设网站建设论文网站开发合同履约
  • 织梦贷款网站源码备案过的网站换域名
  • 南宁网站seo顾问广州正规的免费建站