How to get return value from solidity contract

0 votes

I am using web3 and I have the following code:

pragma solidity ^0.4.2;

contract Charity{


    function ping() public constant returns (uint) {

        return 200;

    }



}

And I am compiling/calling it in typescript with:

import * as fs       from 'fs'       ;

import * as solc     from 'solc'     ;

import * as Web3     from 'web3'     ;



var web3   = new Web3(new Web3.providers.WebsocketProvider('ws://localhost:8546'));


var contract_path : string = "path/to/Charity.sol"

const input       = fs.readFileSync(contract_path)

const output      = solc.compile(input.toString(), 1);

var contract_name = ":" + pr.last(contract_path.split("/")).split(".")[0]

const bytecode    = output.contracts[contract_name].bytecode

const abi_        = JSON.parse(output.contracts[contract_name].interface);


web3.eth.getAccounts().then(accounts => {


    var coinbase = accounts[0];

    var receiver = accounts[1];


    // create contract

    var myContract = new web3.eth.Contract([], {

          from     : coinbase

        , gasPrice : '20000000000'

    });


    // set address to coinbase, and jsonInterface to abi

    myContract.options.address = coinbase;

    myContract.options.jsonInterface = abi_;


    // deploy contract -> problem, how do I get the abi in here?

    var deployedContract = myContract.deploy({


        data: '0x' + bytecode,


    }).send({


        from: coinbase,

        gas : 1500000 ,

        gasPrice: '30000000000000'           


    }, (err, hash) => {


        if (err) { console.log("error on deployment: ", err) }

        console.log("Hash: ", hash)

    })


    // send contract fn to network to be executed

    // problem: this is not doing what it's suppose to

    myContract.methods.ping().send({ from : coinbase }, (err, val) => {

        console.log("ping(): ", err, val)

    })


    myContract.methods.ping().send({from: coinbase})

    .on('transactionHash', function(hash){

        console.log("hash: ", hash)

    })

    .on('receipt', function(receipt){

        console.log("recepit: ", receipt)

    })

    .on('confirmation', function(confirmationNumber, receipt){

        console.log("conffirmation: ", receipt)

    })

    .on('error', console.error);       




});

 myContract.methods.ping().send... is not returning the value 200 .

How to properly get the return value?

Aug 16, 2018 in Blockchain by digger
• 26,740 points
5,079 views

1 answer to this question.

0 votes

Once you call a constant function, you should use call() instead of send() method.

To know more, visit: http://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-call

answered Aug 16, 2018 by slayer
• 29,370 points

Related Questions In Blockchain

0 votes
1 answer

Ethereum solidity: Unable to get value from another contract

I have faced similar issues when compiling ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 69,220 points
1,681 views
0 votes
1 answer

Solidity: How to return id from hash value?

You can do it by storing the hash ...READ MORE

answered Oct 22, 2018 in Blockchain by Omkar
• 69,220 points
2,413 views
+1 vote
2 answers

How to return value from a chaincode in Hyperledger Fabric?

Hyperledger Fabric supports only 2 types of ...READ MORE

answered Jun 13, 2018 in Blockchain by Perry
• 17,100 points
3,004 views
0 votes
1 answer

How to get return value of external function?

You can do this. I have shared ...READ MORE

answered Aug 13, 2018 in Blockchain by digger
• 26,740 points
1,960 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,790 points
1,947 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,471 views
0 votes
1 answer
0 votes
1 answer

How to get value from solidity contract using java?

web3j caters for this very use case. It ...READ MORE

answered Sep 28, 2018 in Blockchain by slayer
• 29,370 points
2,056 views
0 votes
1 answer

How can I get the same return value as solidity `abi.encodePacked` in Golang?

This is what I used:  package main import ( ...READ MORE

answered Sep 26, 2018 in Blockchain by slayer
• 29,370 points
4,263 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP