0

Here is the function isDude in a contract. If the address is recorded, then isDude returns true. Otherwise it returns false. Here is the contract function isDude:

function isDude(address _dude_address) external returns (bool) {
        if (_dude_address == address(0)) return false;
        if (!dudes[_dude_address]) emit dudeNotConfirmed(msg.sender, _dude_address);
        return dudes[_dude_address];  //<<==return boolean
    }

The dapp is using ethers 5.0.x to call the function as below:

    let dudeOnChain1 = await contractDude.isDude(wallet.address); //<<==wallet.address is on chain and shall return true
    let dudeOnChain = await dudeOnChain1.wait();
    console.log("Dude on china : ", dudeOnChain);
    let d2 = await contractDude.isDude("0x617F2E2fD72FD9D5503197092aC168c91465E7f2"); //<<==this address is not on chain and shall return false
    let d21 = await d2.wait();
    console.log("Is on chain d21: ", d21);

Here is the console output of return for wallet.address which shall be true:

[Thu Dec 24 2020 23:56:14.725]  LOG      Dude on china :  {"blockHash": "0x2658158ba56d6ae53fd042ad442330c3fe9e76a84b13ff70a068fbe79175cf68", "blockNumber": 106269, "byzantium": true, "confirmations": 2, "contractAddress": null, "cumulativeGasUsed": {"hex": "0x5c90", "type": "BigNumber"}, "events": [], "from": "0xF89b29Fb7D2a2Ecf56A4BA57f1680aa9d47BE538", "gasUsed": {"hex": "0x5c90", "type": "BigNumber"}, "logs": [], "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "status": 1, "to": "0x19B9F726278BCca713ef999E4810cbB1A2977A41", "transactionHash": "0x2c2ad792447450e50f0e52b457f8e92e10389e74a40bf2b1ef326ae6480fadc0", "transactionIndex": 0}

Here is the return false for address which is not on chain (event dudeNotConfirmed has been fired):

[Thu Dec 24 2020 23:56:16.586]  LOG      Is on chain d21:  {"blockHash": "0x7081b4fec59c5c8d6cc97a881adf10b6c1c21ab762033f9039c815e2a4dde62e", "blockNumber": 106271, "byzantium": true, "confirmations": 1, "contractAddress": null, "cumulativeGasUsed": {"hex": "0x61d7", "type": "BigNumber"}, "events": [{"address": "0x19B9F726278BCca713ef999E4810cbB1A2977A41", "args": [Array], "blockHash": "0x7081b4fec59c5c8d6cc97a881adf10b6c1c21ab762033f9039c815e2a4dde62e", "blockNumber": 106271, "data": "0x000000000000000000000000f89b29fb7d2a2ecf56a4ba57f1680aa9d47be538000000000000000000000000617f2e2fd72fd9d5503197092ac168c91465e7f2", "decode": [Function anonymous], "event": "dudeNotConfirmed", "eventSignature": "dudeNotConfirmed(address,address)", "getBlock": [Function anonymous], "getTransaction": [Function anonymous], "getTransactionReceipt": [Function anonymous], "logIndex": 0, "removeListener": [Function anonymous], "topics": [Array], "transactionHash": "0x8f289cb0f4b77789d8c24adc81ad5d7243af3882e52cdb52d08e3e7ca9ed3fda", "transactionIndex": 0}], "from": "0xF89b29Fb7D2a2Ecf56A4BA57f1680aa9d47BE538", "gasUsed": {"hex": "0x61d7", "type": "BigNumber"}, "logs": [{"address": "0x19B9F726278BCca713ef999E4810cbB1A2977A41", "blockHash": "0x7081b4fec59c5c8d6cc97a881adf10b6c1c21ab762033f9039c815e2a4dde62e", "blockNumber": 106271, "data": "0x000000000000000000000000f89b29fb7d2a2ecf56a4ba57f1680aa9d47be538000000000000000000000000617f2e2fd72fd9d5503197092ac168c91465e7f2", "logIndex": 0, "topics": [Array], "transactionHash": "0x8f289cb0f4b77789d8c24adc81ad5d7243af3882e52cdb52d08e3e7ca9ed3fda", "transactionIndex": 0}], "logsBloom": "0x00000000000000000000000000000000000002000000000000000000000000000000000000000000040000000000000000000000000000000010000000000000000000000000008000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000", "status": 1, "to": "0x19B9F726278BCca713ef999E4810cbB1A2977A41", "transactionHash": "0x8f289cb0f4b77789d8c24adc81ad5d7243af3882e52cdb52d08e3e7ca9ed3fda", "transactionIndex": 0}

The 2 returns above are much more than just a boolean. How to extract the boolean return of the isDude from the return object above?

2
  • This function is wrong ( u trigger a transaction but don't change the state in the Blockchain).. U should change external to view and remove the emit event
    – Majd TL
    Dec 25 '20 at 8:33
  • Need to emit event when there is no hit. That's why the function was changed from view to external.
    – user938363
    Dec 26 '20 at 0:28
1

There are different types of functions in solidity, a normal function

external (public) functions

those are used to change the state of a contract and could emit events. those functions require to send a transaction to the blockchain. those function could return values (like you function), however this returned value is only avaliable for other contracts and will not returned for you (so if another contract called your function, it will get true or false.. but you from the outside you will get a transaction object back, because it is a transaction)

there is another type of function

view returns (...)

those functions are used to get the state (the value saved inside a variable in the contract). you cant change anything inside the contract and you cant emit events. those functions return the raw value to you so you will get true or false back.

So summarized you need to change your contract and a tip for you better to use mapping and not arrays

mapping (address => bool) dudes;

event RegisterDude(address dude, ...)
..
..
..

function setDude(address _dudeAddress) external {
    dudes[_dudeAddress] = true;
    emit RegisterDude(_dudeAddress, ....);
}

function isDude(address _dude_address) view returns (bool) {
        return dudes[_dude_address]; 
    }

that code is only an example to give you an idea of how it should work. About etherjs.. I work more with web3js but I assume they are similar.. so there is a way to trigger a transaction as you did, and another way to get values without trigger a transaction "probably read-only" (what you should look for)

2
  • In dapp, what is a better way to receive return when calling a contract function? The contract does have add and remove dude functions which are not shown in the post. isDude was view function and was changed after adding event when there is no hit.
    – user938363
    Dec 26 '20 at 0:27
  • events are usually used to trigger something (like in your backend, which can listen to events) when a change is happening in the smart contract. In your case, there is no change, and that why you should not use an event. if you need an event (which is 100% wrong) you need to send a transaction as you did, and then get the transaction object and look in the logs section(in the logs of the transaction object you can see the events, probably encoded)
    – Majd TL
    Dec 26 '20 at 14:15

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.