This might be a double, but I have seen different answers so far, and it's very confusing. I have the following contract:

pragma solidity ^0.4.19;

contract Sum {
   event Summed(uint256 num1, uint256 num2, uint256 result);

   function getSum(uint256 num1, uint256 num2) public returns (uint256 result) {
       uint256 result;
       result = num1 + num2;
       return result;

       Summed(num1, num2, result);

How to call the getSum() from the Truffle Console starting from initiating the contract (I'm using Ganache), and what is the difference (in terms of syntax) in calling the getSum() from the Console and by using the Web3js?

I'm using the latest versions of Truffle and Ganache.

2 Answers 2


First, add the pure modifier to your getSum() function so it does what you want it to do. Then deploy it with $ truffle migrate.

Then $ truffle console

> var sum;

> Sum.deployed().then(function(instance) { sum = instance; });

> sum
A lot of truffle contract stuff ... 

> var a = 1;
> var b = 2;
> var response;
> sum.getSum(a,b).then(function(r) { response = r.toString(10); }); // because r is a bigNumber

> response

I'm just sort of winging it, so I hope I didn't flub the syntax.

Hope it helps.

  • This is retrieving the return result; The event emitter would never fire because it comes after the return. Listening to events on the client side is a different technique. Mar 4, 2018 at 1:10
  • Any source to read about event listening?
    – Ruham
    Mar 4, 2018 at 1:15
  • Have look over here. The essence of it is you would do something like sum.Summed.watch(function(error,result) { // what to do when event discovered }); ethereum.stackexchange.com/questions/32027/… Mar 4, 2018 at 2:03

If the contract has been deployed:

  Sum.at(-address-).getSum(param1, param2)

If it has not been deployed follow the steps in this answer to deploy it and get the address:

How to get deployed contract address using Truffle 3.x console?

  • Thanks. That's really weird, as this works for some functions, but for others it doesn't (invalid JSON rpc error).
    – Ruham
    Mar 4, 2018 at 1:16
  • This was the only helpful answer as this references the deployed contract using its address. The other answers rely on creating a local instance which is nice for pre-testing but not for talking to a contract which is already deployed to the ledger.
    – binaryguy
    Aug 7, 2018 at 19:17

Your Answer

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

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