-1

Here is the Smart contract code. I want to get the result of function of UserRegister by using web3.

 function UserRegister(string memory userName,uint feiyong ) public payable returns(string memory isOK,string memory resourceID){
   //address conAddr;
   emit LogContractAddress(msg.sender,address(this));
   emit LogData(msg.data);
   //byte userID=sha3(bytes(userName));
   if (bytes(users[userName].name).length==0){

       emit UserRegisterEVENT("null",userName);
       return ("null",userName);
   }
   else
   {
       emit UserRegisterEVENT("UserName has been registered","null");
       return("UserName has been registered","null");
   }
}

The web3 client code is as followings:

  var userName="def";
     var userReg=contract1.methods.UserRegister("abc",100).send({from:'0x18244fbf3b47175cd6b4a1183a476c9e70cb7368'})  .on('transactionHash', function(hash){
    console.log("hash="+hash);
}) .on('confirmation', function(confirmationNumber, receipt){
    console.log("confirmationNumber="+confirmationNumber+"receipt="+receipt.toString());
})
.on('receipt', function(receipt){
    // receipt example
    //receipt.
    console.log("receipt="+receipt.toString);
    } )
.on('error',function(error){

    console.log("error="+error);
})
;

and the result is as followings:

   hash=0x39e027a89c27d03e71ab2a310adefceca3dde48f668d2692705466501dff563e
confirmationNumber=0receipt=[object Object]
receipt=function toString() { [native code] }
confirmationNumber=1receipt=[object Object]
confirmationNumber=2receipt=[object Object]
confirmationNumber=3receipt=[object Object]
confirmationNumber=4receipt=[object Object]
confirmationNumber=5receipt=[object Object]
confirmationNumber=6receipt=[object Object]
confirmationNumber=7receipt=[object Object]
confirmationNumber=8receipt=[object Object]
confirmationNumber=9receipt=[object Object]
confirmationNumber=10receipt=[object Object]
confirmationNumber=11receipt=[object Object]
confirmationNumber=12receipt=[object Object]
confirmationNumber=13receipt=[object Object]
confirmationNumber=14receipt=[object Object]
confirmationNumber=15receipt=[object Object]
confirmationNumber=16receipt=[object Object]
confirmationNumber=17receipt=[object Object]
confirmationNumber=18receipt=[object Object]
confirmationNumber=19receipt=[object Object]
confirmationNumber=20receipt=[object Object]
confirmationNumber=21receipt=[object Object]
confirmationNumber=22receipt=[object Object]
confirmationNumber=23receipt=[object Object]
confirmationNumber=24receipt=[object Object]

How can i get the register result from smart contract ? Thank you very much.

3

When sending the transaction like this:

const result = await smartContractObj.methods
        .methodToCall(parameter1, parameter2)
        .send( { gas: '5000000', from:masterAccount });

You get the event return value via:

result.events.UserRegisterEVENT.returnValues;

EDIT: Here is the complete Code

var myFunction = async () => {
    // Send the Transaction
    const result = await smartContractObj.methods
        .methodToCall(parameter1, parameter2)
        .send( { gas: '5000000', from:masterAccount });

    console.log("Transaction sent");

    // Get return value of the event
    return result.events.UserRegisterEVENT.returnValues;  
}

Just call the function. It will probably return an array. To get the value you need you should retrieve the output with a console.log once to get the value you need.

Edit2: With this code you can get from the Contract.

const result = await shoppingCart.methods.getCart(_cartID).call({from:masterAccount});

Works same as above put it in a async function.

You need to have a function like this in CONTRACT:

function getCart(uint _inputParam) public view returns(uint _obj){
        return obj;
    }
  • Comments are not for extended discussion; this conversation has been moved to chat. – eth Aug 21 at 23:31
0

HeiHei.

You can't get return values of this function in transaction receipt.

But the good news is you can get parameters of UserRegisterEVENT event instead (judging by the code they are identical to function's return values).

Just check the logs property of receipt object in your on('receipt') handler.

  • thank you, But, receipt.log is undefined. By the way, is there any way to get return values from the smart contract function ? and How ? is there any tutorials about this? – HeiHei Aug 19 at 0:52
  • you mentioned receipt.log. Is it a typo? You need receipt.logs. More here web3js.readthedocs.io/en/v1.2.1/… Transaction return values (unlike call return values) are accessible only from another contract, i.e. when a contract calls another contract's function – Oleg Kondrakhanov Aug 19 at 7:42
  • But how to get result from smart contract? – HeiHei Aug 19 at 9:29
  • it's not possible – Oleg Kondrakhanov Aug 19 at 10:12

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.