2017-07-13 27 views
1

美好的一天,調用合同方法並手動簽名時出錯。 SendTransaction的作品SendRawTransaction沒有

我正在寫一個節點API來暴露我的區塊鏈上的方法(部署和測試松露)。我使用web3.js,ethereumjs-tx,ethereum,松露和堅實作爲我的技術堆棧。

var txMethodData = masterKeyContract.myMethod.getData(myParams); 

交易PARAMS是:

const txParams = { 
    nonce: web3.toHex(web3.eth.getTransactionCount(web3.eth.coinbase)), 
    gasPrice: web3.toHex(web3.eth.gasPrice), 
    gasLimit: web3.toHex(2000000), 
    from: mainAccount, 
    value: '0x00', 
    to: targetContract.address, 
    data: txMethodData, 
    chainId: 3 
}; 

IM使用ethereumjs-TX

const EthereumTx = require('ethereumjs-tx'); 

與鏈接到我的mainAccount的私有密鑰簽名的交易

const tx = new EthereumTx(txParams); 
tx.sign(privateKey); 
const serializedTx = tx.serialize(); 
web3.eth.sendRawTransaction("0x" + serializedTx.toString('hex'), function (err1, resp1) { 
    if (err1) { 
     console.log(err1); 
    } else { 
     console.log(resp1); 
    } 
}); 

而且我得到了錯誤天然氣*價格+價值的資金不足。我從mainAccount(txParams的from:字段)發送此事務。所以我登錄我的mainAccount餘額

web3.eth.getBalance(mainAccount, function (error, result) { 
    if (!error) { 
     console.log(web3.fromWei(result.toNumber(), "ether")); 
    } else { 
     console.error(error); 
    } 
}); 

而結果是252.12609391539726。所以它不能沒有資金。我甚至估計了web3.eth.estimateGas(txParams)交易,它給了我97899.目前ropstein區塊的天然氣限制是4,707,806。所以我應該有足夠的。所以問題在於爲什麼我的資金不足。

我懷疑的唯一原因是from:field,這是我的mainAccount實際上並不是交易的付款人。

UPDATE: 問題可能是與簽約,因爲我只是

web3.eth.sendTransaction(txParams, function (err1, resp1) { 
    if (err1) { 
     console.log(err1); 
    } else { 
     console.log(resp1); 
    } 
}); 

測試,它的工作使這個問題實際上是爲什麼sendRawTransaction不起作用。可能與我簽署交易的方式有關?

我檢查了

const privateKey = Buffer.from('[private_key_inserted_here]', 'hex'); 

實際上與我mainAccount。 private_key_inserted_here從「密文」字段中從與我的主帳戶相關的密鑰庫中獲取。我通過匹配密鑰庫的「地址」字段來檢查與我的mainAccount相關的信息。

在此先感謝。

回答

1

有些事情,我可以看到

  • 這是gas,而不是不需要gasLimit
  • chainId
  • 你真的應該管理你自己的nonce而是依靠節點送你正確的隨機數。換句話說,不從節點查詢,但保持周圍

一個變量,如果你願意,你可以看看簡約的錢包簽名類我寫 https://gist.github.com/gaiazov/e65a3e36c67eaf46fe81982cd193316d

+0

我會去看一下的,而不是氣體氣體限制的確是一個錯誤。感謝您將它指向我。 – andu

相關問題