[英]Call constructor and deploy with web3 and ropsten
我創建了一個已成功編譯的測試合約,並希望使用 web3js 在 ropsten 測試網絡上廣播。 我有一個正在使用的metmask帳戶。 我想知道how can i call constructor of my contract and then broadcast on ropsten
。 我確實在沒有構造函數的情況下廣播了我的一份合約,並且確實有我的交易哈希鍵,即0xf7e5a8e93db9989b033b85323cdff713ba88b547ef64a544550e145961999aac
但我Error encountered during contract execution [Reverted]
以下錯誤Error encountered during contract execution [Reverted]
Transaction has been reverted by the EVM
控制台Transaction has been reverted by the EVM
, Transaction has been reverted by the EVM
Error encountered during contract execution [Reverted]
I would like to know also why i am getting this error after broadcast has been done
const fs = require('fs')
var Tx = require('ethereumjs-tx').Transaction
const Web3 = require('web3')
const web3 = new Web3('https://ropsten.infura.io/v3/KEY')
deploy()
function deploy () {
const privateKey = 'PRIVATE_KEY'
const account1 = 'ACCOUNT_NUMBER'
const privateKey1 = Buffer.from(privateKey, 'hex')
const contractData = fs.readFileSync('../../build/contracts/Testcontract.json')
const contract = JSON.parse(contractData)
const byteCode = contract['bytecode']
web3.eth.getTransactionCount(account1, (err, txCount) => {
const txObject = {
nonce: web3.utils.toHex(txCount),
gasLimit: web3.utils.toHex(100000),
gasPrice: web3.utils.toHex(web3.utils.toWei('0.004', 'gwei')),
data: byteCode
}
let tx = new Tx(txObject, {'chain':'ropsten'})
tx.sign(privateKey1)
const serializedTx = tx.serialize()
const raw = '0x' + serializedTx.toString('hex')
web3.eth.sendSignedTransaction(raw, (err, txHash) => {
console.log('err: ', err, 'txHash: ', txHash)
})
})
}
我與構造函數的合同看起來像這樣
pragma solidity ^0.5.11;
contract Testcontract {
string public senderName;
string public receiverName;
uint public transferAmount;
constructor (string memory _sender, string memory _receiver, uint _amount) public {
senderName = _sender;
receiverName = _receiver;
transferAmount = _amount;
}
}
更新
通過 Infura 節點部署合約的一種簡單方法是使用Truffle 。 在你的項目根目錄下
$ npm install truffle
$ truffle init
這是有關如何配置 Truffle 以使用 Infura 項目的教程。 總之,安裝Truffle的hd-wallet-provider
$ npm install @truffle/hdwallet-provider
將truffle-config.js
替換為
const HDWalletProvider = require("@truffle/hdwallet-provider");
const mnemonic = "orange apple banana...";
module.exports = {
networks: {
ropsten: {
provider: () => new HDWalletProvider(mnemonic, 'https://ropsten.infura.io/v3/<YOUR INFURA PROJECT ID>'),
network_id: 3, // Ropsten's id
},
},
compilers: {
solc: {
version: "0.5.11", // Fetch exact version from solc-bin (default: truffle's version)
}
}
}
前面的代碼將 Truffle 配置為連接到 Infura 並使用您的帳戶。 在./migrations
目錄(由./migrations
truffle init
創建)創建一個新文件2_deploy_testContrac.js
,您可以在其中定義如何部署合約並提供 TestContract 所需的參數。
var TestContract = artifacts.require("./TestContract");
module.exports = function(deployer) {
deployer.deploy(TestContract, "AliceSender", "BobSender", 120);
}
最后,通過執行部署你的合約
$ truffle migrate --network ropsten
您的交易被還原的原因是因為 TestContract 的構造函數需要三個參數,但您給出了零。 由於無法執行構造函數,您的部署事務也被還原。
您可以使用web3.eth.Contract.deploy代替手動創建部署事務。 使用此方法,您可以方便地提供合約構造函數所需的所有參數。
更新:下面的解決方案不適用於 Infura,因為 Infura API 不公開web3.eth.personal
函數,它只允許發送rawTranscations
。
您應該能夠使用以下代碼部署您的合約。 請注意,它主要是從官方 web3 文檔中復制粘貼的。
var contractData = fs.readFileSync('../../build/contracts/Testcontract.json');
var contract = JSON.parse(contractData);
var abi = contract['abi'];
var bytecode = contract['bytecode'];
var testContract = eth3.eth.Contract(abi);
var account = ...;
var password = ...;
web3.eth.personal.unlockAccount(account, password);
testContract.deploy({
data: bytecode,
arguments: ['SenderAlice', 'ReceiverBob', 120]
})
.send({
from: account,
gas: 1500000,
gasPrice: '30000000000000'
}, function(error, transactionHash){
console.log(error, transactionHash);
})
.on('error', function(error){
console.log(error);
})
.on('transactionHash', function(transactionHash){
console.log(transactionHash);
})
.on('receipt', function(receipt){
console.log(receipt.contractAddress) // contains the new contract address
})
.on('confirmation', function(confirmationNumber, receipt){
console.log(confirmationNumber, transactionHash);
})
.then(function(newContractInstance){
console.log(newContractInstance.options.address) // instance with the new contract address
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.