1

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.

2 Answers 2

3

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

2
  • Thanks a lot !! That helped. Aug 30, 2021 at 15:12
  • Still have an error message after using the code above,
    – wanghan02
    Apr 16, 2022 at 19:09
0

@maddie the send function has changed on web3 try this

$("#setMessageButton").click(function () {
myMessage.methods.setMessage($("#userInput").val())
.send({from: "<sender account>"})
.then(console.log);

});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.