0

I am new in Blockchain ethereum, I am trying to call a function in contract, using Ganache without Metamask it works. I am trying to test the contract in pre-production in Rinkeby through infura.

I know this can be old API but I am not sure what I am doing wrong, I googled but I dindnt find correct response for it.

the error is : method eth_sendTransaction does not exist/is not available.

here is the contract:

pragma solidity ^0.4.24;

contract Message {
    string myMessage;

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

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

and frontend code all in HTML:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Deploy a Remix Contract</title>

    <link rel="stylesheet" type="text/css" href="main.css">

    <script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>

</head>

<body>

    <div>
        <h1>Ethereum Secret Messenger</h1>
        <hr>

        <label for="message">This site writes a secret message to the Ethereum
            blockchain!</label>

        <input id="userInput" type="text">
        <button id="setMessageButton">Set secret message</button>

    

    </div>

    <script>
    
        // Connect a the web3 provider
        if (typeof web3 !== 'undefined') {

            web3 = new Web3(web3.currentProvider);
            console.log("Not defined");
        } else {

            web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/v3/.....My_Connection"));
        
            console.log("Defined")
        }
                

        web3.eth.defaultAccount = web3.eth.accounts[0];
        console.log("eth.account " + web3.eth.accounts[0]);

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


    console.log(RemixContract);

    $("#setMessageButton").click(function () {
        let userMessage = $("#userInput").val()
    console.log("user message " + userMessage)
        RemixContract.methods.setMessage(userMessage)
.send({from:"0x....."})
.then(console.log);
});

    </script>
</body>

</html>

I have another question which it confused me, I use infura to connect to Rinkeby, since infura connect to node in this case probably I dont need Metamask, I can create transaction and sign it with private-key then send it. so using Metamask with infura does it make sense?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Steven
  • 762
  • 1
  • 10
  • 27

1 Answers1

0

either use infura+private-key or metamask which will be web3 provider, for that you need to create a button and connect your metamask to your dApp.