0
votes

I have a simple solidity contract with a set() function. 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.

If I use the UI in Remix, the new transactions (with the updated string values) are associated with the original contract. When I attempt the same thing with web3js, brand new contracts are being created.

I am wanting all new get() calls with web3js to be associated with the original contract.

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

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.Contractcreating a new 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'

Thanks!!!

1
It looks like you're missing a to address in your transaction. A transaction that's not to any address is a contract creation transaction.user94559
Could be wrong here, but i think you missed something when pasting your code here. It doesn't look correct. I think you missed the closing brackets.nikos fotiadis

1 Answers

0
votes

Adding 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!