我的目标是发送已签名的交易以创建智能合约,但是,我遇到了几天无法解决的问题:
当我发送交易(在私有链上)时,有两个不同的结局:
- 1 Web3js 告诉我它有效,我可以看到区块上的交易。但是,当尝试联系合同时,我有以下输出:
错误:无法从 ABI 解码 uint256:0x
当我尝试eth.getCode(contractAddress)
在 Geth 中运行时,它会返回0x
- 2 Web3js 告诉我该交易还没有挖到 50 个区块(节点没有时间挖到 50 个区块)。但我可以看到区块中的交易(在 geth 中)。
这是我使用的代码:
web3g = result;
getContractInstance(function (error, instance) {
if (error) {
console.log(error);
} else {
let newContract = instance;
let deploy = newContract.deploy({
data: bytecode,
arguments: [MY ARGS]
}).encodeABI();
let gas = web3g.utils.toHex(3000000);
let gasPrice = web3g.utils.toHex(21000000000);
let gasLimit = web3g.utils.toHex(4000000);
let nonce;
web3g.eth.getTransactionCount(req.body.sender_address)
.then(function (result) {
nonce = result;
nonce = web3g.utils.toHex(nonce);
let transactionObject = {
gas: gas,
gasPrice: gasPrice,
gasLimit: gasLimit,
data: deploy,
from: req.body.sender_address,
nonce: nonce
};
web3g.eth.accounts.signTransaction(transactionObject, req.body.private_key, function (error, signedTx) {
if (error) {
console.log(error);
} else {
console.log(signedTx);
web3g.eth.sendSignedTransaction(signedTx.rawTransaction)
.on('confirmation', function (number, receipt) {
if (number == 1) {
// do stuff
有人可以指出我做错了什么吗?
注意:我可以使用上述方法发送签名交易以在地址之间转移以太币。
编辑:估计气体返回以下内容:错误:
返回错误:所需气体超过限额或交易总是失败