0

what is the correct way to call a method of a contract that has recently been created.

following the example on the officially documentation (https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#new-contract), i have successfully created and deployed a new contract instances with the following code:

import commentContract from '../build/contracts/Comment.json';

const comment = new web3.eth.Contract(Comment.abi, { from: coinbase });

comment.deploy({ data: commentContract.bytecode })
    .send({ from: coinbase, gas: 1500000 })
    .then(deployedContract => {
        // return deployedContract.methods.setText('some text here', {from: coinbase})
        //     .then(result => console.log(result))

        return deployedContract.methods.setText('some text here')
            .then(result => console.log(result))
    });

this code does not work because of an error that setText expects only 1 parameter, instead 2 supplied ( because of {from: coinbase}). so i removed the {from: coinbase} part from the code and now it gives an error that then is not defined.

so i cant figure out what the correct way would be to call methods on this newly created contract.

1

In both case the syntax is wrong. If setText is going to change the status you need to send a transaction:

const tx = await deployedContract.methods.setText('some text').send({
  from: coinbase
});
console.log(tx)
// or
deployedContract.methods.setText('some text').send({
  from: coinbase
}).then(console.log);

if not you can just provide a call:

console.log(await deployedContract.methods.setText('some text').call())
// or
deployedContract.methods.setText('some text').call().then(console.log)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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