简体   繁体   English

如何使用 React web 应用程序部署智能合约?

[英]How to deploy a smart contract using react web app?

To deploy a smart contract I have so far used remix ide.到目前为止,为了部署智能合约,我使用了 remix ide。 But now I am in a need to create a website that allows to deploy smart contract just hitting a button?但是现在我需要创建一个网站,只需点击一个按钮就可以部署智能合约? Can I do that?我可以这样做吗?

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface AnotherContract {
    function walletOfOwner(address owner) external view returns (uint256[] memory);
}

contract Demo{
    uint public similarity;
    uint256 public hasRedPill;
    constructor(
    string memory _name,
    string memory _symbol,
    string memory _initBaseURI
  )  {
     
    // some code
  }
    function test() public view returns(uint256  ){
       AnotherContract anotherContract = AnotherContract(address(0x116486FD64Ba04F7B789278B239E2e5A1e2f7b39));
      return anotherContract.walletOfOwner(msg.sender).length;
    }
}

Let's say I want to deploy this demo contract using my react js web app.假设我想使用我的 react js web 应用程序部署这个演示合约。 Here I have to first send the constructor parameters then deploy the contract.在这里,我必须先发送构造函数参数,然后部署合约。 In return I need the byte code, abi and address of the contract.作为回报,我需要合约的字节码、abi 和地址。

Is there any way to do that?有没有办法做到这一点? I have a sense that it is possible since remix ide also provides a ui to deploy the contract.我觉得这是可能的,因为 remix ide 还提供了一个用于部署合约的 ui。 I am new to this.我是新来的。 Please help.请帮忙。

  1. Set up Truffle设置松露
npm install -g truffle
  1. Make an empty repository, cd into it, then创建一个空的存储库, cd进入它,然后
truffle init
  1. Configure the Local network and the provider配置本地网络和提供程序

In truffle.config.js , add the following snippet inside the module.exports:truffle.config.js中,在 module.exports 中添加以下代码段:

module.exports = {
  networks: {
    development: {
     host: "127.0.0.1",
     port: 7545,
     network_id: "*",
    },
  },
  compilers: {
    solc: {
      version: "0.8.0",
      settings: {
       optimizer: {
         enabled: false,
         runs: 200
       }
      }
    }
  }
};

  1. Now Compile smart Contracts现在编译智能合约
truffle compile
  1. Migrate Smart Contracts迁移智能合约
truffle migrate

That's it, it should create an ABI array and contract address in the .json file就是这样,它应该在.json文件中创建一个ABI arraycontract address

The answer to your question is going to be a VERY long one.你的问题的答案将是一个很长的答案。 Sagar's suggestion is a good lead although there is a lot that needs to be covered here. Sagar 的建议是一个很好的线索,尽管这里需要涵盖很多内容。

I would recommend you watch the crash course linked below to get you situated.我建议您观看下面链接的速成课程,以帮助您定位。 This course will help you step by step and go into quite a bit of detail (which greatly helped me).本课程将帮助您逐步了解 go 的一些细节(这对我有很大帮助)。 I see you already have some backend knowledge so to cover the front end I would suggest you skip to lesson 10 where NextJS/React are covered.我看到你已经掌握了一些后端知识,所以为了涵盖前端,我建议你跳到第 10 课,其中涵盖了 NextJS/React。

Link to guide: Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript指南链接:使用 JavaScript 学习区块链、Solidity 和全栈 Web3 开发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM