0

How can I call a method on a smart contract with the web3 provided by MetaMask? Building smart contracts with solidity was the easy part for me. I had relatively few problems thanks to https://remix.ethereum.org. I found out, the easiest way to deploy a smart contract is on remix using MetaMask. You can deploy to any network from there. Simple things like that I found very difficult to ascertain from existing documentation, which I find sparse and underdeveloped for all related technologies. The last remaining challenge for me is to actually connect users from my front end page (I’m using Node.js to serve html through localhost) to the smart contract, securely, and with web3 (or anything better). I’ve had a great deal of trouble with this last step. I decided to simplify the problem by using an example smart contract provided by remix.ethereum.org. Everything I have tried doesn’t seem to work. This example from a user on codepen seemed helpful: https://codepen.io/TheDBomb/pen/gXJZmJ, I was able to recreate it, but when I try to replace all the variables with my own, the transaction pulls up MetaMask, and it works at first, but then sends invalid data to the contract. The transaction, when submitted fails and says there is an ‘RPC Error’.

       const contract = web3.eth.contract(ABI).at(contractAddress);
       //const getCountFunc = contract.methods.store.getData(123);
       const getCountFunc = contract.store.getData(123);//MetaMask uses web3 version <1.0 (3/31/2020)
       incrementButtonEl.addEventListener('click', function() {
            if (disableTransactions) {
                return;
            }

            web3.eth.sendTransaction({
                    to: contractAddress,
                    gas: 800000,
                    gasPrice: 1000000000,
                    data: getCountFunc
                },
                (error, result) => {
                    console.log(result);
                }
            );
        });

I know MetaMask said they inject web3 as a convienience to developers, but as far as I have seen, it is the only way to call a method on a smart contract without having the user's private key. I would love for anyone to show me how to use my own copy of web3, call a function, and then have MetaMask prompt the user to sign the transaction. It is isn’t clear to me from the documentation on the website. They also said they were going to stop injecting web3 for developers very soon. The problem leads me to consider temporarily storing the user's wallet address and private key on their own local storage, which is submitted to the server for processing, but never stored. Is it safe to do so? I was reluctant because users might feel compromised, and I wouldn’t blame them.

TL; DR: Using a deployed copy of store.sol, as listed on remix.ethereum.org as a sample project, what is the exact javascript code necessary to invoke both functions using the web3 injected by MetaMask. If there is a better way to do this, what is it? I already deployed it to the Ropsten Network: 0xF55239a5729e7d7db2a3E0B3F9AA8B6a27705668

The ABI: var ABI = [{ "constant": false, "inputs": [{ "name": "num", "type": "uint256" }], "name": "store", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "retreive", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" } ];

Thanks, I hope you can help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.