【问题标题】:Trying to deploy smart contract without truffle (a manual route)尝试在没有松露的情况下部署智能合约(手动路线)
【发布时间】:2023-03-14 16:20:02
【问题描述】:

我是初学者,想将前端(angular js)与区块链集成。

我已经使用 Truffle 成功编译和部署了智能合约。 我可以在不使用松露(即手动路由)的情况下部署智能合约吗? 我搜索了很多博客,但找不到更好的问题来源。如果你有更好的资源,请帮助我

【问题讨论】:

    标签: angular5 blockchain ethereum truffle


    【解决方案1】:

    我已经使用 nodeJS 创建了计划合约部署器。请在下面找到代码。

    第 1 步:在您的计算机中安装 nodeJS 和 NPM。

    第 2 步:创建“deployer”文件夹并将 package.json 添加到同一文件夹中

    第 3 步:在“deployer”文件夹下创建“contracts”和“compiled”文件夹

    第 4 步:在“deployer”文件夹中创建 deployer.js 文件。

    第 5 步:运行 $npm install 命令

    第 6 步:将您的智能合约“.sol”文件保存在“contracts”文件夹中。

    第 7 步:运行 $node deployer.js 命令在本地 Ganache 客户端(松露)上部署合约。如果您有其他客户端或区块链节点。请更新“deployer.js”文件中的 rpc url。或者安装 Ganache RPC 客户端,你会发现 truffle 站点。

    文件:

    package.json:

    {
      "name": "deployer",
      "version": "1.0.0",
      "description": "Test deployer",
      "main": "deployer.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "Mahesh Patil",
      "license": "ISC",
      "dependencies": {
        "async": "^2.6.0",
        "body-parser": "1.15.2",
        "express": "4.14.0",
        "request": "2.79.0",
        "solc": "0.4.8",
        "web3": "0.18.2"
      }
    }
    

    =========== deployer.js:

    var Web3 = require("web3");
    // Replace the blockchain node url, I am installed Ganache client very easy for testing 
    var web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:7545/"));
    var fs = require('fs');
    var solc = require('solc');
    var async = require('async');
    
    var cDir = fs.readdirSync("./contracts");
    console.log(cDir);
    var contracts = cDir.reduce(function (acc, file) {
        acc[file] = fs.readFileSync("./contracts/" + file, { encoding: "utf8" });
        return acc;
    }, {});
    
    var output = solc.compile({ sources: contracts }, 1);
    if (output.errors) {
        throw output.errors;
    };
    
    var owner = web3.eth.coinbase;
    web3.eth.defaultAccount = owner;
    var contracts = [];
    web3.personal.unlockAccount(owner, "", 120000, function (err, success) {
        var all = [];
        Object.keys(output.contracts).forEach(function (name) {
            var contract = output.contracts[name];
            contract.interface = JSON.parse(contract.interface);
            deployContract(contract, name).then(res => {
                console.log(name, " Address: ", res);
            })
    
        });
    });
    
    function deployContract(contract, fileName) {
        return new Promise((resolve, reject) => {
            web3.eth.contract(contract.interface).new({
                data: "0x" + contract.bytecode,
                gas: 900000, // If you get gas issue please change this value according error
                // privateFor: [],
                from: owner,
            }, function (err, myContract) {
                if (err) {
                    console.log(err);
                    reject(err);
                }
                if (!err) {
                    if (!myContract.address) {
                        console.log(fileName + " : " + myContract.transactionHash); // The hash of the transaction, which deploys the contract
                    } else {
                        contract.address = myContract.address;
                        fs.writeFileSync("./compiled/" + fileName + ".json", JSON.stringify(contract, null, 4));
                        //cb(null, myContract.address); // the contract address
                        resolve(myContract.address);
                    }
    
                }
            });
        });
    
    }
    

    【讨论】:

      【解决方案2】:

      虽然不理想,但如果您安装了 Metamask,您可以在技术上使用 Remix 在线 IDE (http://remix.ethereum.org/)。

      【讨论】:

      • 当然。谢谢,我会尝试你建议的方式
      猜你喜欢
      • 2019-09-09
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-05
      • 2017-10-11
      • 1970-01-01
      相关资源
      最近更新 更多