Web3js

使用“truffle-contract”進行合約部署

  • March 13, 2020

我的目的是使用 Node-js 將智能合約部署到專用網路。我在 Truffle 和 Ganache 工作。我用“松露契約”試過了。但是在部署過程中會發生錯誤。我的契約名稱是“部署”。

我用來部署的命令:myContract.deploy().send(...);.

錯誤:TypeError: myContract.deploy is not a function

我不知道是什麼問題。請提供適當的程式碼。

應用程序.js:

var fs=require("fs");
var solc=require("solc");
var contract=require("truffle-contract");
var Web3=require('web3');
var web3=new Web3('ws://127.0.0.1:8545');
var address="0xfE72f8B082CF099715785d23FBeaed9272F27559";
var DeployJson=fs.readFileSync("E:/Deploy/build/contracts/Deploy.json");
var DeployJs=JSON.parse(DeployJson);
var DeployAbi=DeployJs.abi;
var contract=require("truffle-contract");
var myContract=contract({abi: DeployAbi, unlinked_binary:"0x608060405234801561001057600080fd5b5061011d806100206000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806318620ef014602d575b600080fd5b603360ab565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101560715780820151818401526020810190506058565b50505050905090810190601f168015609d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60606040518060400160405280601581526020017f436f6e7472616374206973206465706c6f79656421000000000000000000000081525090509056fea265627a7a723158206cddf11cab61deaceb98041f67bc88c98dfa84738b1c39323262ccd385732c7864736f6c634300050c0032"});
    myContract.setProvider(web3);
    myContract.deploy().send({from:address, gas:1000000, gasPrice:200});

提供的 API 中沒有deploy方法require('truffle-contract')

鑑於truffleContract = require('truffle-contract'),你需要做這樣的事情:

const myContract = truffleContract({abi: ..., unlinked_binary: ...});
const block = await web3.eth.getBlock('latest');
myContract.setProvider(web3.currentProvider);
myContract.defaults({from: address, gas: block.gasLimit});
const myInstance = await myContract.new({from: address, gas: 1000000, gasPrice: 200});
return myInstance;

當然,您必須在async函式中執行此操作。

順便說一句,gasPrice200 的 a 非常低。

您可能想將其更改為"2e9"(2 gwei) …

引用自:https://ethereum.stackexchange.com/questions/80496