最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting return value of Solidity contract function from web3-1.0.0-beta.27 - Stack Overflow

programmeradmin0浏览0评论

I am running web3 1.0.0-beta.27, pragma solidity ^0.4.2;

contract Charity{

    function ping() public constant returns (uint) {
        return 200;
    }


}

And I am piling/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      = solcpile(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);        



});

The issue is that myContract.methods.ping().send... is not returning the value 200 on the callbacks as I desired, is there a way to remedy this?

I am running web3 1.0.0-beta.27, pragma solidity ^0.4.2;

contract Charity{

    function ping() public constant returns (uint) {
        return 200;
    }


}

And I am piling/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.pile(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);        



});

The issue is that myContract.methods.ping().send... is not returning the value 200 on the callbacks as I desired, is there a way to remedy this?

Share Improve this question edited May 15, 2023 at 15:13 TylerH 21.1k79 gold badges79 silver badges114 bronze badges asked Jan 28, 2018 at 2:20 xiaolingxiaoxiaolingxiao 4,8956 gold badges44 silver badges91 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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

Check the documentation: http://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-call

When you don't modify state, you just use call(). If you modify state you need a transaction (send) and then you need to wait for the transaction to be mined. When way to do it is to emit an event and listen for it.

发布评论

评论列表(0)

  1. 暂无评论