0

I need to update value on deployed contract ( I dont have sol neither it has been deployed on my platform ) I want to update param with setter in contract and call getter to see changed value .. always default value comes back!

here is the code Im using :

new web3.eth.Contract(HelloWorld.abi,'0x085Ab4C596535FFCE5B520D277f1C01236a656CB').methods.setMessage('Hi').call()

=> Result {}

then call :

new web3.eth.Contract(HelloWorld.abi,'0x085Ab4C596535FFCE5B520D277f1C01236a656CB').methods.getMessage().call()

=> 'hello world10'

Im using Truffle console:

truffle console --network ropsten

one comment if I use :

HelloWorld.deployed().then(instance => instance.setMessage('new Hi').call())

then call:

HelloWorld.deployed().then(instance => instance.getMessage.call()).then(result => message = result)

then I got expected value .. but I don't have deployed contract. neither sol. all what I have part ABI

Is it wrong with my calls that should be async .. or I need to redeploy contract ?

Im still new in this crypto-world

Maher Abuthraa
  • 17,493
  • 11
  • 81
  • 103

1 Answers1

2

welcome to the "crypto-world" as you named it.

I've got a bit lost on your question but what you want is to deploy a contract with an initial message and then change that message using setMessage method, all through the console.

First thing you are doing wrong is calling the "setMessage" method using .call(). When you want to change something in the blockchain, you will send a transaction, and to send a transaction you need to specify an account. The correct method should be .send({ from: "you-account-address" }).

In order to simplify a bit, let me just show a few lines that you can use and then explain

$ > truffle console --network ropsten
$ (truffle) > helloInstance = await HelloWorld.deployed()
$ (truffle) > await helloInstance.getMessage()
$ (truffle) > await helloInstance.setMessage("new Hi", { from: "you-account-address" })

So, basically, all calls are async. First, you call HelloWorld.deployed() because when you deployed the contracts with truffle, truffle created a folder named "build" where it saved the contracts with all the necessary data, that's why you don't need to specify the address neither the abi. This is totally different from using web3.js in which you need to specify both.

Then, you can just call the methods.

You can find more detailed information here https://www.trufflesuite.com/docs/truffle/getting-started/interacting-with-your-contracts

  • Thank you .. It helps a lot .. still I need to do that with ABI (as JSON) I cannot deploy that contract as you mentioned because I dont have the sol – Maher Abuthraa Jul 12 '19 at 07:43
  • I dont think using ';' is correct here .. console shows error – Maher Abuthraa Jul 12 '19 at 07:47
  • 1
    you right, I removed the ; thank you. what do you mean by "I dont have the sol"? Using the command line you can do what I've shown above, in the frontend, if you want to simplify, there's a truffle-contract package that you can use, which simplifies a lot. Also, I recommend you to have a look at the react box here https://truffle-box.github.io/ – obernardovieira Jul 13 '19 at 10:34