0

When i call the setMessage() function this doesn't log any transaction in the blockchain. But the getMessage() function works by bringing the message which was previously set using the remix IDE straight away.

If i try to set a message with Remix IDE immediately my metamask opens for the confirmation. This does not happen with my frontend application.

This is what i try to run.

<script>

    // Connect a the web3 provider
    if (typeof web3 !== 'undefined') {
        web3 = new Web3(web3.currentProvider);
    } else {
        web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/v3/298df63315214b1688a3c2b29e03217d"));
    }

    // Set a default account
    web3.eth.defaultAccount = web3.eth.accounts[0];

    //console.log(web3.eth.defaultAccount);

    // Get the contract address
    var RemixContract = new web3.eth.Contract([
        {
            "constant": true,
            "inputs": [],
            "name": "getMessage",
            "outputs": [
                {
                    "internalType": "string",
                    "name": "",
                    "type": "string"
                }
            ],
            "payable": false,
            "stateMutability": "view",
            "type": "function"
        },
        {
            "constant": false,
            "inputs": [
                {
                    "internalType": "string",
                    "name": "x",
                    "type": "string"
                }
            ],
            "name": "setMessage",
            "outputs": [],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
        }
    ], '0xa212507364d7E73909Aad1ac114178dDfB48e476');

    console.log(RemixContract);

    $("#setMessageButton").click(function () {
        var val = $("#userInput").val();
        RemixContract.methods.setMessage(val)
        .call()
        .then(function(result){
            console.log(result)
        })
    });

</script>

And this is my smart contract in Remix IDE :

pragma solidity ^0.4.24;

contract Message {
string myMessage;

function setMessage(string x) public {
    myMessage = x;
}

function getMessage() public view returns (string) {
    return myMessage;
}
}

Screenshot of the Remix IDE

Getting the callback response like this:

enter image description here

Someone kindly let me know what i'm missing. Thanks in advance.

1

when you are calling setMessage function you should use send instead of call

$("#setMessageButton").click(function () {
        var val = $("#userInput").val();
        RemixContract.methods.setMessage(val)
        .send()
        .then(function(result){
            console.log(result)
        })
    });

Hope this helps

1
  • Thanks a lot !! That helped. Aug 30 '21 at 15: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.