使用.deploy()部署合约时出现问题



我的代码:

exports.deployContract = async function (account, contractData) {
var contract = new web3.eth.Contract(contractData.abi);
const randomID = getUUID();
toReturn = {
contractAddress: "",
};
const mainAccount = {
address: account.address,
privateKey: account.privateKey,
};
// Deploy contract
const executed = await contract
.deploy({ data: contractData.evm.bytecode.object, arguments: [randomID] })
.send({ gas: 900000, from: mainAccount.address });
console.log("Contract deployed to", executed.options.address);
var storedId = "";
await executed.methods
.id()
.call()
.then((result) => {
storedId = result;
console.log("ID: ", result);
});
return {
address: executed.options.address,
someid: storedId,
};
};

当我传递一个使用web3.eth.accounts.create((创建的帐户时,contract.deploy((.send((失败,返回错误-";未捕获错误错误:返回错误:无法识别发件人帐户;。

如何解决此错误?

我通过使用sendSignedTransaction重写它来实现它。

exports.deployContract = async function (account, contractData) {
var contract = new web3.eth.Contract(contractData.abi);
const randomID = getUUID();
toReturn = {
contractAddress: "",
};
const mainAccount = {
address: account.address,
privateKey: account.privateKey,
};
// console.log("Bytecode: ", contractData.evm.bytecode.object);
// console.log("ABI: ", contractData.abi);
const newIdentityContract = new web3.eth.Contract(contractData.abi);
const newIdentityContractTx = newIdentityContract.deploy({
data: contractData.evm.bytecode.object,
arguments: [randomID],
});
const encodedABI = newIdentityContractTx.encodeABI();
const createTransaction = await web3.eth.accounts.signTransaction(
{
data: encodedABI,
gas: 900000,
},
mainAccount.privateKey
);
const createReceipt = await web3.eth.sendSignedTransaction(
createTransaction.rawTransaction
);
console.log(`Contract deployed at address: ${createReceipt.contractAddress}`);
return {
contractAddress: createReceipt.contractAddress,
someId: randomID,
};
};

相关内容

  • 没有找到相关文章

最新更新