0

I have two different smart contracts interaction_1 and interaction_2

I input the data(team) from remix ide.

What I want to accomplish is when user input team in user_input function at interaction_1, teamAcceptedEvent.watch in interaction_test.js will start working, and then to put interaction_1 contract address into interaction_2 getValue function, and interaction_2 contract will receive the user input from interaction_1.

It works fine when I tested it in remix, but when I use web3.js to do it, everything goes smoothly, but interaction_2 didn't get the data from interaction_1.

I'm not sure is there any problem with sendTransaction or the gas price? I'm just trying to test two smart contract interact using web3.js. thanks

interaction_1

pragma solidity ^0.5.1;
contract interaction_1 {

    string public description;
    string public user_prediction;
    uint public amount;
    event teamAccepted(string team_1);


    function user_input (string memory team) public payable {
           user_prediction = team;
           emit teamAccepted(user_prediction);

    }
    function get_user_input()view public returns(string memory) {
        return user_prediction;
    }

}

contract interaction_2 {
    function getValue (address addr)public returns (string memory);
}

interaction_2

pragma solidity ^0.5.1;

contract interaction_2{
    string public get_user_prediction;
    function getValue (address addr)public returns (string memory){ 
        interaction_1 b = interaction_1(addr);
        return get_user_prediction = b.get_user_input();
    }
}
contract interaction_1 {
    function user_input (string memory team_1)public payable;
    function get_user_input()public returns(string memory);
}

interaction_test.js

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

var abi_1=[...];

var abi_2=[...];

var contractAddress_1 = "0xa4349e4081b28acb426a3332a17a7333bcf3e640";
var contractAddress_2 = "0x0d2c978af4fdeec4070a6147a352235d0c01ca26";
var interaction_1 = web3.eth.contract(abi_1).at(contractAddress_1);
var interaction_2 = web3.eth.contract(abi_2).at(contractAddress_2);

var teamAcceptedEvent = interaction_1.teamAccepted();

teamAcceptedEvent.watch(function(error, result){
            if (!error)
                { 
                    if(result.args.team_1 != null && result.args.team_1 != '')
                    {
                        console.log("get user input");
                        interaction_2.getValue.sendTransaction("0xa4349e4081b28acb426a3332a17a7333bcf3e640",{
                            from:web3.eth.accounts[0],
                            gas:2000000},function (error, result){ 
                            if(!error){
                                console.log(result);
                            } else{
                                console.log(error);
                            }
                        });                 
                }  
                } else {      
                    console.log(error);
                }
        });
2
  • What is your Web3 version? Commented Sep 4, 2019 at 13:20
  • You have here two different contracts named interaction_1 and two different contracts named interaction_2. Please clear that up! Commented Sep 4, 2019 at 13:29

1 Answer 1

0

If you want to get return value from a solidity function, you need to define it as view function, though you can still retrieve value by making change in your javascript part using

interaction_2.getValue.call()

instead of

interaction_2.getValue.sendTransaction()

In this way, you can call the function and retrieve the return value without making any change in the state.

In solidity, view function does not change any state and accesses state and returns value if defined to do so.

So you can define the function getValue in interaction_2 like below

function getValue (address addr)public view returns (string memory)

instead of

function getValue (address addr)public  returns (string memory)

Besides, in javascript part, when calling method getValue, you need to use

interaction_2.getValue.call()

instead of

interaction_2.getValue.sendTransaction()

Hope it will help you.

2
  • Thanks for your answer, I have tried to define it as view function, but it got an error, it shows this expression modifies the state. because what I want is interaction_2 to receive the data from interaction_1, then interaction_2 will modifies the states(get_user_prediction inside get_user_prediction state's will make the change).
    – leojail
    Commented Sep 5, 2019 at 5:53
  • Then, do no change to getValue() function. To get the address you want, you need to call interaction_2.getValue.call(). After make the transaction using interaction_2.getValue.sendTransaction() Commented Sep 9, 2019 at 9:11

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.