How to use ethereum contract's state changing method in javascript?

0 votes

I'm studying Ethereum smart contract. I deployed my greeter contract through Mist browser, and it worked well on Mist.

So I want to make simple Dapp using my deployed greeter contract.

My contract code in Solidity:

contract mortal {
  address owner;
  function mortal() { owner = msg.sender; }
  function kill() { if (msg.sender == owner) suicide(owner); }
}

contract greeter is mortal {
  string greeting;

  function greeter(string _greeting) public {
    greeting = _greeting;
  }

  function greet() constant returns (string) {
    return greeting;
  }

  function changeMsg(string msg) {
    greeting = msg;
  }
}

My Dapp code in Javascript:

_connect() {

  /* ... */

  contract = web3.eth.contract(CONTRACT_ABI);
  instance = contract.at(CONTRACT_ADDRESS);

}

_greet() {
  console.log(instance.greet());
}

_changeMsg(msg) {
  console.log(instance.changeMsg(msg));
}

_greet() function works well, it returns my greeting message.

But _changeMsg() function returns some hexa string only. How can I change greet message through _changeMsg() function?

Sep 11, 2018 in Blockchain by sabby
• 4,370 points
46 views

1 answer to this question.

0 votes
You'll find that the message is indeed updated: try calling _greet()again.

The hex that _changeMsg() is returning is the transaction hash, which is what's always returned by state changing methods (transactions).
answered Sep 11, 2018 by Christine
• 15,810 points

Related Questions In Blockchain

+1 vote
1 answer

How to store state data in Ethereum blockchain?

You won't have to overwrite the whole ...READ MORE

answered Apr 24, 2018 in Blockchain by Shashank
• 10,350 points
76 views
0 votes
1 answer

How to use Proc/lamba returned from a method in Ruby?

You need to remove the colon: list.select(&valid_transaction) The & ...READ MORE

answered Aug 24, 2018 in Blockchain by digger
• 26,600 points
47 views
–1 vote
1 answer
+1 vote
1 answer

How to deploy ethereum smart contracts on a website?

There are many ways to do this: 1 ...READ MORE

answered Mar 26, 2018 in Blockchain by Johnathon
• 9,090 points
112 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,810 points
188 views
0 votes
1 answer
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 68,480 points
119 views
0 votes
1 answer

How to make sure transactions take no fee in a private Ethereum blockchain?

In a private ethereum network you have ...READ MORE

answered Mar 26, 2018 in Blockchain by Christine
• 15,810 points

edited Mar 26, 2018 by Christine 146 views
+15 votes
5 answers