0

I have in a contract:

    function createCourt() external returns (uint256) {
        uint256 _id = ++nonce;
        courtOwners[_id] = msg.sender;
        return _id;
    }

Now, when I try to call this function from web3.js I stumble an impossibility to return values.

I was recommened to use events instead to return a value. But if I and another user calls createCourt():

    function createCourt() external {
        uint256 _id = ++nonce;
        courtOwners[_id] = msg.sender;
        emit CourtCreated(_id);
    }

how can I determine which of several events returned my court, not another one?

Or any other way to get the court number after I create it?

2

You use the transaction's hash to obtain the transaction's receipt, if you made a single call to createCourt you have a single event in the receipt.

Another option is to accept an random input parameter and use that as id.

  • I see no courtId value in the events in the transaction: ``` [ { logIndex: 0, transactionIndex: 0, transactionHash: '0xfeab887b0f738ae14077a38d010849493f58083794cde292558a3c8a3cdb71d9', blockHash: '0x3871cc151e60ab4841301586a1a5e4b2b6a965ad5414234f7633d4c3e725f3ee', blockNumber: 5, address: '0x39A831bD503b921f210c7a88499631f1AEFc8dBC', type: 'mined', id: 'log_6c056ece', event: 'CourtCreated', args: [Result] } ] ``` – porton Jan 10 at 18:23
  • @porton It is in the args object. – Ismael Jan 10 at 18:34
0

You can add second parameter to your event:

event CourtCreated(address indexed _address, uint256 _id);

And later on execute the event in your methods like this:

emit CourtCreated(msg.sender, _id);

Now with the keyword indexed before _address parameter with web3.js you can filter the events by the address. You can check which events are executed by you and which by other users.

If you are using web3 0.2.x you have to use method ContractInstance.myEvent() - read more here or if you are using web3 1.x.x you have to use ContractInstance.getPastEvents() - read more here.

  • It is not enough: If several parallel processes of the same msg.sender create courts, I still don't know how each process would know which is its court and which is of another process. I consider to use random numbers instead. – porton Jan 10 at 18:03
  • Your question was about events separation. If there is more to be cleared please upload the full of code of contract. – Miroslav Nedelchev Jan 10 at 18:06
  • My contract: github.com/vporton/courts/blob/… – porton Jan 10 at 18:13

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.