首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Web3客户端的可靠智能合同中获得响应?

如何从Web3客户端的可靠智能合同中获得响应?
EN

Ethereum用户
提问于 2019-08-18 12:06:29
回答 2查看 1.8K关注 0票数 -1

这是智能合同代码。我想通过使用UserRegister来获得web3函数的结果。

代码语言:javascript
复制
 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");
   }
}

web3客户端代码如下:

代码语言:javascript
复制
  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);
})
;

其结果如下:

代码语言:javascript
复制
   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]

如何获得智能合同的注册结果?非常感谢。

EN

回答 2

Ethereum用户

发布于 2019-08-19 11:55:44

在像这样发送事务时:

代码语言:javascript
复制
const result = await smartContractObj.methods
        .methodToCall(parameter1, parameter2)
        .send( { gas: '5000000', from:masterAccount });

通过以下方法获得事件返回值:

代码语言:javascript
复制
result.events.UserRegisterEVENT.returnValues;

编辑:这是完整的代码

代码语言:javascript
复制
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;  
}

打给函数就行了。它可能会返回一个数组。要获得所需的值,您应该使用一个console.log检索输出一次,以获得所需的值。

Edit2:有了这些代码,您可以从合同中获得信息。

代码语言:javascript
复制
const result = await shoppingCart.methods.getCart(_cartID).call({from:masterAccount});

工作原理与上面一样,把它放在一个异步函数中。

您需要在合同中有这样一个函数:

代码语言:javascript
复制
function getCart(uint _inputParam) public view returns(uint _obj){
        return obj;
    }
票数 3
EN

Ethereum用户

发布于 2019-08-18 13:01:15

HeiHei。

无法在事务收据中获取此函数的返回值。

但好消息是,您可以获得UserRegisterEVENT事件的参数(从代码判断它们与函数的返回值相同)。

只需在您的logs处理程序中检查receipt对象的on('receipt')属性。

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/74012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档