最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error with old version of Web3 when trying to log Remix contract - Stack Overflow

programmeradmin6浏览0评论

I'm trying to get the details logged on the browser console from a simple Remix contract. However, when I try the following html, I get an error saying:

web3.eth.contract is not a function

Looking through the documentation, I see that changing:

var RemixContract = web3.eth.contract([

for this:

var RemixContract = new web3.eth.Contract([

Allows at least for the assignation of the contract variable, but then I cannot call the smartcontract data using the RemixContract.at function. I believe this is a conflict with the old Web3 version but I can't figure out how to bring the data into the browser console.

The desired output is: Be able to see my contract's details on the browser console.

<!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">
    <!--The following line contains the source of web3 in case it is not on the directory-->
    <script src=".js/dist/web3.min.js"></script>
</head>

<body>
    <div>
        <h1>Deploy a Remix Contract</h1>
    </div>

    <script>

        // Connect to the web3 provider
        if (typeof web3 !== 'undefined') {
            web3 = new Web3(web3.currentProvider);
        } else {
            web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
        }

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

        // Get the contract abi
        // To get the contract ABI from Remix, go to the Compile tab and grab the ABI
        var RemixContract = 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"
    }
]);

        // Get the contract address
        var myMessage = RemixContract.at('0xd9145CCE52D386f254917e481eB44e9943F39138')

        console.log(myMessage);

    </script>
</body>

</html>

I'm trying to get the details logged on the browser console from a simple Remix contract. However, when I try the following html, I get an error saying:

web3.eth.contract is not a function

Looking through the documentation, I see that changing:

var RemixContract = web3.eth.contract([

for this:

var RemixContract = new web3.eth.Contract([

Allows at least for the assignation of the contract variable, but then I cannot call the smartcontract data using the RemixContract.at function. I believe this is a conflict with the old Web3 version but I can't figure out how to bring the data into the browser console.

The desired output is: Be able to see my contract's details on the browser console.

<!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">
    <!--The following line contains the source of web3 in case it is not on the directory-->
    <script src="https://cdn.jsdelivr/gh/ethereum/web3.js/dist/web3.min.js"></script>
</head>

<body>
    <div>
        <h1>Deploy a Remix Contract</h1>
    </div>

    <script>

        // Connect to the web3 provider
        if (typeof web3 !== 'undefined') {
            web3 = new Web3(web3.currentProvider);
        } else {
            web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
        }

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

        // Get the contract abi
        // To get the contract ABI from Remix, go to the Compile tab and grab the ABI
        var RemixContract = 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"
    }
]);

        // Get the contract address
        var myMessage = RemixContract.at('0xd9145CCE52D386f254917e481eB44e9943F39138')

        console.log(myMessage);

    </script>
</body>

</html>
Share Improve this question edited Nov 30, 2022 at 20:38 TylerH 21.1k79 gold badges79 silver badges114 bronze badges asked May 1, 2021 at 23:35 johan855johan855 1,6265 gold badges29 silver badges54 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 13

Found the answer, I was using an old syntax for web3, I should have replaced this:

// Get the contract address
var RemixContract = web3.eth.contract(CONTRACT-ABI-HERE);
// Get the contract abi
var myMessage = RemixContract.at('CONTRACT ADDRESS HERE');
console.log(myMessage);

with this:

var RemixContract = new web3.eth.Contract(CONTRACT-ABI, CONTRACT ADDRESS);
console.log(RemixContract)

Following your path, next you will have hard times trying to call the setMessage() function with your newly created contract. If we are on the same page, please try the following code, it saved me ton of time:

web3.eth.getAccounts().then(function(accounts){            
    web3.eth.defaultAccount = accounts[0]                        
    var myContract = new web3.eth.Contract(contractABI, contractAddress);            
    myContract.methods.setMessage('your-new-message').send({from: accounts[0]});
    myContract.methods.getMessage().call().then(console.log);
});
发布评论

评论列表(0)

  1. 暂无评论