[英]Calling the set() function (with web3js) of a solidity contract is creating a new contract address. Why?
I have a simple solidity contract with a set() function. 我有一个简单的带有set()函数的合同。 When I call the contract's set() function, the resulting transaction is at a newly created contract address instead of the contract address of where the solidity code lives. 当我调用合同的set()函数时,生成的事务位于新创建的合同地址,而不是实体代码所在的合同地址。
If I use the UI in Remix, the new transactions (with the updated string values) are associated with the original contract. 如果我在Remix中使用UI,则新事务(具有更新的字符串值)将与原始合同相关联。 When I attempt the same thing with web3js, brand new contracts are being created. 当我尝试使用web3js做同样的事情时,正在创建全新的合同。
I am wanting all new get() calls with web3js to be associated with the original contract. 我希望将所有新的带有web3js的get()调用与原始合同相关联。
Solidity Code 固体代码
pragma solidity ^0.4.0;
contract HashRecord {
string public hashValue;
function setHashValue(string newHashValue) public {
hashValue = newHashValue;
}
function getHashValue() public view returns (string) {
return hashValue;
}
}
web3js Code web3js代码
var Tx = require('ethereumjs-tx')
const Web3 = require('web3')
const web3 = new Web3('https://ropsten.infura.io/v3/d55489f8ea264a1484c293b05ed7eb85')
const abi = [ABI_CODE]
const contractAddress = '0x6c716feb775d5e7b34856edf75048a13fe0c16b0'
const myAccount = '0x59f568176e21EF86017EfED3660625F4397A2ecE'
const privateKey1 = new Buffer('PRIVATE_KEY', 'hex')
hashValue = 'newly updated value'
const contract = new web3.eth.Contract(abi, contractAddress,{
from: myAccount,
web3.eth.getTransactionCount(myAccount, (err, txCount) => {
//Smart contract data
const data = contract.methods.setHashValue(hashValue).encodeABI()
// Build the transaction
const txObject = {
nonce: web3.utils.toHex(txCount),
gasLimit: web3.utils.toHex(1000000),
gasPrice: '5000',
data: data,
from: myAccount,
}
// Sign the transaction
const tx = new Tx(txObject)
tx.sign(privateKey1)
const serializedTx = tx.serialize()
// Broadcast the transaction
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')).
on('receipt', console.log)
})
My guess is this has to do with const contract = new web3.eth.Contract
creating a new contract. 我的猜测是这与const contract = new web3.eth.Contract
创建新合同有关。 I can not figure out another way to do it though. 我无法找到另一种方法。
Again, I would like new values stored under the variable hashValue
to be associated with the original contract address of const contractAddress = '0x6c716feb775d5e7b34856edf75048a13fe0c16b0'
同样,我希望将存储在变量hashValue
下的新值与const contractAddress = '0x6c716feb775d5e7b34856edf75048a13fe0c16b0'
的原始合同地址相关联
Thanks!!! 谢谢!!!
Adding to: contractAddress,
添加to: contractAddress,
in the following code block 在以下代码块中
const txObject = {
nonce: web3.utils.toHex(txCount),
// value: web3.utils.toHex(web3.utils.toWei('0.1', 'ether')),
gasLimit: web3.utils.toHex(1000000),
gasPrice: '5000',
// gasPrice: '0x' + estimatedGas,
data: data,
from: myAccount,
to: contractAddress,
solves the problem. 解决了问题。
Thank you @smarx! 谢谢@smarx!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.