1

I created a very simple contract. Deployment is finished but it does not work as my expectation. Can you help me to fix it?

My expectation is changing "num" value after "add(int)" transaction.

The contract is followings.

Tx

https://ropsten.etherscan.io/tx/0x74426948905cd6e70e8b9d64a660b3c179b7c8a224ca5cd0234842768eb501db

Raw Code

pragma solidity ^0.4.0;
contract Addition {
  int num = 0;
  function add(int a){
    num += a;
  }
  function get() returns(int){
    return num;
  }
}

Although I did sendTransaction with "add(1)" data, the get value does not change.

The sendTransaction is below

Tx

https://ropsten.etherscan.io/tx/0x26db79c7c3c65992f17f4f62d5d191705933e785dff54db6d27e6f3a92d7117e

What I did:

(1) sendRawTransaction (create a contract)

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://xxxx:xxxx"));

var Tx = require('ethereumjs-tx');
var privateKey = new Buffer('xxxx', 'hex')

var rawTx = {
  nonce: '0x00',
  gasPrice: '0x10D1F',
  gasLimit: '0x10D1F',
  to: '0x692a70d2e424a56d2c6c27aa97d1a86395877b3a',
  data: '606060405260008055341561001357600080fd5b5b60d6806100226000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680636d4ce63c14604757806387db03b714606d575b600080fd5b3415605157600080fd5b6057608d565b6040518082815260200191505060405180910390f35b3415607757600080fd5b608b60048080359060200190919050506097565b005b6000805490505b90565b8060008082825401925050819055505b505600a165627a7a723058207e968ad53b91047ab71334a49530112b3725aaa27d9c6d3860da0bcf09c5c16d0029'
}

var tx = new Tx(rawTx);
tx.sign(privateKey);

var serializedTx = tx.serialize();

console.log('0x' + serializedTx.toString('hex'));

web3.eth.sendRawTransaction(serializedTx.toString('hex'), function(err, hash) {
  if (!err) {
    console.log(hash);
  } else {
    console.log(err);
  }
});

(2) sendRawTransaction (execute add() method)

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://xxxx:xxxx"));

var Tx = require('ethereumjs-tx');
var privateKey = new Buffer('xxxx', 'hex')

var rawTx = {
  nonce: '0x03',
  gasPrice: '0x53d9',
  gasLimit: '0x53d9',
  to: '0x692a70d2e424a56d2c6c27aa97d1a86395877b3a',
  data: '0x87db03b70000000000000000000000000000000000000000000000000000000000000001' // add(int256) with param 1
}

var tx = new Tx(rawTx);
tx.sign(privateKey);

var serializedTx = tx.serialize();

console.log('0x' + serializedTx.toString('hex')); // 0xf885038253d98253d994692a70d2e424a56d2c6c27aa97d1a86395877b3a80a487db03b700000000000000000000000000000000000000000000000000000000000000011ba0ec6de7c9c1f31dc95014156479925b2ebd933b715f2bb4450f5d74992d7b3423a016f6a7c6e1885412400a622f82ffa033936c06755896096c7638936c065571c1

web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
  if (!err) {
    console.log(hash); // 0x26db79c7c3c65992f17f4f62d5d191705933e785dff54db6d27e6f3a92d7117e
  } else {
    console.log(err);
  }
});

(3) call (execute get() method)

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://xxxx:xxxx"));

var result = web3.eth.call({
    to: "0x692a70d2e424a56d2c6c27aa97d1a86395877b3a",
    data: "0x6d4ce63c" // get()
});

console.log(result); // 0x => I expected 1
7
  • What does the contract return? I mean, the output is 0 right? Jul 17, 2017 at 14:06
  • 2
    It seems you are not deploying correctly your contract. To deploy a contract you have to send your message to the null address, ie {to: null}, but your are sending your message to 0x692a70.. which is an external owned account.
    – Ismael
    Jul 17, 2017 at 14:06
  • @PaoloGuerra yes (3) call returned 0x
    – zono
    Jul 17, 2017 at 14:08
  • 2
    Anyway I agree with Ismal, the contract seems not be deployed correctly; also consider to add "constant" to your get function: function get() constant returns(int) { return num; } Jul 17, 2017 at 14:12
  • 1
    Don't forget when you are done to answer this question so it can be mark as resolved later. Thanks!
    – Ismael
    Jul 17, 2017 at 14:14

1 Answer 1

1

The contract was never deployed. You're sending function calls to a non-contract address.

See this answer to your similar question: Is it possible to see contract raw code from TxHash?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.