1

I want to read the result of these code in truffle console . I used truffle v.3.2.5 and testrpc v.3.0.5 If such question return with two parameter exist then provide me a appropriate link .

pragma solidity ^0.4.0;
contract shapeCalculator {
    function rectangle(uint w,uint h)
    returns (uint s, uint p )
    {
    s=w*h;
    p=2*(w+h);
    }
}
1

In order to compile and run your contract in truffle, you can follow below commands -

$ truffle console
truffle(development)> compile
truffle(development)> migrate
truffle(development)> contract.new('Argument if Any')

truffle(development)> contract.at("contract address").functionName()   

Or

truffle(development)> contract.at('contract address').functionName("parameter", function (e, result) { if (!e) {console.log(result)}})

Note: Don't forget to add any new contracts to the migration file.

More elaborated answer -

You can see return values only from Constant function, so make your function constant -

pragma solidity ^0.4.0;
contract shapeCalculator {
    function rectangle(uint w,uint h) constant
    returns (uint s, uint p ){
    s=w*h;
    p=2*(w+h);
    }
}

then do the following -

$ truffle console
truffle(development)> compile
truffle(development)> migrate
truffle(development)> shapeCalculator.new().then(function(res) { sc = shapeCalculator.at(res.address) })
truffle(development)> sc.rectangle(10,10)
  • contract.new('Argument if Any') ----> what does this do ? – Gopal ojha Jun 21 '17 at 9:05
  • if you have a constructor defined in the contract and that takes some argument, so you can pass them here while instantiating. – Sanchit Jun 21 '17 at 9:08
  • I expect output 100 from the calculation but i get this information as below :- { tx: '0x652034bc965252bedb9ebf7c2d1c7f8eab5b4d51d59a1954725d64d2d9af4726', receipt: { transactionHash: '0x652034bc965252bedb9ebf7c2d1c7f8eab5b4d51d59a1954725d64d2d9af4726', transactionIndex: 0, blockHash: '0x153882ab5da296a93616b1504f7179fd5e47274a0c53a198e8dc0c71ab2dde6b', blockNumber: 36, gasUsed: 21662, cumulativeGasUsed: 21662, contractAddress: null, logs: [] }, logs: [] } – Gopal ojha Jun 21 '17 at 9:10
  • In your case, there is no constructor. I gave you a generic solution, you can skip passing argument. – Sanchit Jun 21 '17 at 9:11
  • Also, just to mention. You can get return values only from constant functions. So make your function constant. – Sanchit Jun 21 '17 at 9:20
0

If your contract is mentioned in the migrations file, then use the following:

$ truffle console
truffle(development)> compile
truffle(development)> myContract = contract.deployed()

truffle(development)> myContract.then(function(instance){return instance.yourFunctionhere()})
  • truffle(development)> migrate --reset Using network 'development'. Running migration: 1_initial_migration.js Replacing Migrations... Migrations: 0x83348f6920e0140837a41f3d433a1786604ed17d Saving successful migration to network... Saving artifacts... Running migration: 2_deploy_contracts.js Replacing shapeCalculator... shapeCalculator: 0xc082ac94bf3f240558726bc48ad7c82de521b40e Saving successful migration to network... Saving artifacts... now the thing is , please write the syntex for this to read output . when i compiled and done as u said it throws the errors . – Gopal ojha Jun 21 '17 at 7:12
  • @Gopalojha Those are not errors. – Jossie Calderon Jun 21 '17 at 8:11
  • truffle(development)> mycontract.then(function(shapeCalculator){returns shapeCalculator.rectangle()}) SyntaxError: Unexpected identifier – Gopal ojha Jun 21 '17 at 9:01
  • truffle(development)> myContract.then(function(shapeCalculator){shapeCalculator.rectangle()}) undefined truffle(development)> (node:5536) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): BigNumber Error: new BigNumber() not a number: [object Object] – Gopal ojha Jun 21 '17 at 9:36
  • Oh finally i realized your answer @Jossie Calderon . You suggested that not using 'return' . In your syntex there is 'instance' . how do i create the instance of the contract ? Give me simple contract example with compile syntax . – Gopal ojha Jun 23 '17 at 7:42

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.