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

傻瓜自助建站软件网络搜索工具

傻瓜自助建站软件,网络搜索工具,不锈钢餐具做外贸哪个网站好,table做网站的好处题记 用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>

    <title>Name Contract Demo</title>

    <!--导入web3库-->

    <script src="https://cdn.jsdelivr.net/npm/web3@1.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 for="name">姓名:</label>

    <input type="text" id="name">

    <button οnclick="setName()">设置姓名</button>

    <br>

    <button οnclick="getName()">得到姓名</button>

    <br>

    <span id="nameValue"></span>

</body>

</html>

<!DOCTYPE html>
<html>
<head><title>Name Contract Demo</title><!--导入web3库--><script src="https://cdn.jsdelivr.net/npm/web3@1.5.2/dist/web3.min.js"></script><script>// 检查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>
<body><h1>设置姓名</h1><label for="name">姓名:</label><input type="text" id="name"><button onclick="setName()">设置姓名</button><br><button onclick="getName()">得到姓名</button><br><span id="nameValue"></span>
</body>
</html>

执行程序 

       使用vscode的Live Server打开网页

       参考这篇文章的执行方法:1.Vue-在独立页面实现Vue的增删改查-CSDN博客 

展示图 

发起交易 

完成交易 

 后记

        觉得有用可以点赞或收藏!

http://www.rdtb.cn/news/14639.html

相关文章:

  • 做毕业设计实物的网站搜索关键词推荐
  • 传奇做网站中国万网
  • 江西网站建设价位网站推广策划书模板
  • 和狗做的网站好个人免费网站创建入口
  • 哈尔滨网站建设招聘百度反馈中心
  • 常平镇仿做网站网站seo如何优化
  • 一个网站没有备案附近电脑培训速成班一个月
  • 做网站搜爬闪必应搜索网站
  • 网站不备案做电影网站广告优化师适合女生吗
  • pageadmin 制作网站怎么绑定域名处理事件seo软件
  • 济南建设集团招聘信息网站2024年4月新冠疫情结束了吗
  • 哈尔滨服务好的建站方案微信朋友圈广告代理
  • 做博彩网站代理犯法吗seo优化一般包括哪些内容
  • 邢台网络运营中心处理中心百度优化师
  • 西湖区高端网站建设网站域名查询网
  • 做网站手机端需要pc端的源代码吗企业门户网站
  • 山西长治做网站公司有哪些百度收录什么意思
  • 设计网站源码seo网络推广专员招聘
  • 2023年中国企业500强seo教程
  • 西安做网站随机关键词生成器
  • 国外vps做网站测速白帽seo公司
  • 做网站需要投入多少钱廊坊seo外包公司费用
  • 使用dw设计个人简历网页模板百度网站优化
  • 做网站首页的表格的代码一份完整的活动策划方案
  • 六安建设机械网站千网推软文推广平台
  • 网站首页title怎么修改重庆seo标准
  • 网站无icp备案长沙推广引流
  • 海南州公司网站建设百度客户端在哪里打开
  • 哪个做企业网站什么叫口碑营销
  • 河北建设厅网站刷身份证电商平台怎么做