当前位置: 首页 > 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.zqtcl.cn/news/390905/

相关文章:

  • 地方门户网站盈利模式这样做微信网站
  • 企业网站要怎么做wordpress w3
  • 网站备案帐号找回密码seo优化工作有哪些
  • 美橙网站建设教程网站建站系统
  • 湖北网站建设公司哪家好重庆建站模板平台
  • 青岛企业建站最新上线的手游
  • 织梦网站wap精品下载
  • 专业做包装的电商网站搜索推广图片
  • 淘客网站佣金建设怎么制作网站页面
  • 网站数据库 mysql如何构建wordpress
  • 牙克石网站建设宁波pc营销型网站制作
  • 具有营销价值好的网站深圳工程建设服务网
  • 全flash网站源码app软件开发公司员工守则
  • 曹鹏wordpress建站seo视频本溪做网站的公司
  • 提示网站有风险老电脑做网站服务器
  • 怎么做网站导航外链出入青岛最新通知今天
  • 济宁房产网站建设海外电商怎么做如何从零开始
  • 网站优化插件中国建设银采购发文网站
  • 重庆企业网站的推广电力建设集团网站
  • 长沙制作网站词条有哪些网站可以做
  • 网站 网页区别简单的网页设计作品
  • 济南做网站推广有哪些公司天津建设工程信息网官方
  • 番禺市桥网站建设有关网站建设的知识
  • 信用中国 网站 支持建设怎么做网站美工
  • 做网站怎么样引流郑州最好的妇科医院排行
  • 云软件网站建设做仓单的网站
  • 邯郸做移动网站报价注册公司流程流程图
  • linux部署wordpress福州短视频seo推荐
  • 做地推的网站做网站感觉挣不到钱啊
  • 网站建设公司哪家好 搜搜磐石网络营销网站建设免费