metamask不。我打电话给合同时打电话

发布于 2025-02-09 13:25:09 字数 3109 浏览 2 评论 0原文

我是区块链以太坊的新手,我正在尝试使用合同中的功能,使用无元掩模的Ganache起作用。我正在尝试通过Infura在Rinkeby进行预生产的合同。

我知道这可能是旧的API,但我不确定我在做什么错,我搜索了谷歌搜索,但我没有找到正确的响应。

错误是:方法eth_sendtransaction不存在/不可用。

这是合同:

pragma solidity ^0.4.24;

contract Message {
    string myMessage;

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

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

html中的frontend代码:

<!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>

我还有另一个问题使我感到困惑,我使用infura连接到rinkeby,由于在这种情况下,Infura连接到节点可能不需要MetAmask,因此我可以创建事务并用私钥签名,然后发送。因此,将Metamask与Infura一起使用是否有意义?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚你发端 2025-02-16 13:25:09

使用INFURA+私钥或MetAmask(将是Web3提供商),因为您需要创建一个按钮并将MetAmask连接到DAPP。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文