如何解决使用 web3js 为 Quorum 上的合同交互发送签名交易
我目前正在运行 quorum-examples github repo 中给出的 7 个节点示例。我已经部署了一个非常简单的存储合约来获取和设置值。根据示例,我能够与 geth 节点 cmd 行内的智能合约进行交互。但是我想使用节点外的另一个地址与它进行交互,因此我编写了以下代码:
const Web3 = require('web3')
const web3 = new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:22000'))
const { Transaction } = require('@ethereumjs/tx')
const { default: Common } = require('@ethereumjs/common')
const run = async () => {
const contractInstance = await new web3.eth.Contract(
[
{
constant: true,inputs: [],name: 'storedData',outputs: [{ name: '',type: 'uint256' }],payable: false,type: 'function',},{
constant: false,inputs: [{ name: 'x',name: 'set',outputs: [],{
constant: true,name: 'get',outputs: [{ name: 'retVal',{
inputs: [{ name: 'initVal',type: 'constructor',],'0xd9d64b7dc034fafdba5dc2902875a67b5d586420'
)
const customCommon = Common.forCustomChain('mainnet',{
chainId: 10,})
const txCount = await web3.eth.getTransactionCount(
'ed9d02e382b34818e88b88a309c7fe71e65f419d'
)
const txData = {
nonce: web3.utils.toHex(txCount),gasLimit: '0x47b760',gasPrice: '0x00',value: '0x0',chainId: 10,to: '0xd9d64b7dc034fafdba5dc2902875a67b5d586420',data: contractInstance.methods.set(10).encodeABI(),}
const tx = Transaction.fromTxData(txData,{ common: customCommon })
const signedTx = tx.sign(
Buffer.from(
'e6181caaffff94a09d7e332fc8da9884d99902c7874eb74354bdcadf411929f1','hex'
)
)
const serializedTx = signedTx.serialize()
const result = await web3.eth.sendSignedTransaction(
`0x${serializedTx.toString('hex')}`
)
return result
}
run().then(console.log).catch(console.log)
但是,每当我尝试发送转换时,它总是出错
"Transaction has been reverted by the EVM:\n{\n \"blockHash\": \"0xd6b06321882912185f5e1d3401a012f58b6bbf7eee1e1d2c6c2cd80a0e13bbdc\",\n \"blockNumber\": 5,\n \"contractAddress\": null,\n \"cumulativeGasUsed\": 23751,\n \"from\": \"0x0fbdc686b912d7722dc86510934589e0aaf3b55a\",\n \"gasUsed\": 23751,\n \"logs\": [],\n \"logsBloom\": \"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\",\n \"status\": false,\n \"to\": \"0x9d13c6d3afe1721beef56b55d303b09e021e27ab\",\n \"transactionHash\": \"0x8c7fd175ab037e24e531804774e8b89bf5aea25de8d99aa9bc2c034229603299\",\n \"transactionIndex\": 0\n}",
如果需要更多信息,请告诉我,我会更新帖子。
编辑:1。智能合约代码如下:
pragma solidity ^0.5.0;
contract simplestorage {
uint public storedData;
constructor(uint initVal) public {
storedData = initVal;
}
function set(uint x) public {
storedData = x;
}
function get() view public returns (uint retVal) {
return storedData;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。