21

I'm learning Solidity. I've set up Metamask, truffle and testrpc. I can send ether from one account to another via metamask. But I want to Metamask to access my deployed contract, and call my contract functions.

Question: Can I test and interact with my deployed contract using metamask?

5 Answers 5

7

Not using Metamask alone, but you can in combination with other tools. One such tool is MyEtherWallet; go to the "Contracts" tab, enter the contract address and ABI, click Access, and then choose "Metamask/Mist" as the means you want to access your wallet. Then MyEtherWallet will give you the UI to construct the transaction, and when it's time to broadcast it, it will give you a Metamask popup to sign and transmit it.

4
  • thanks for the answer. Can I use "MyEtherWallet" for testing contracts?
    – Aydin
    Commented Aug 29, 2017 at 17:53
  • 1
    In the top-right of the MyEtherWallet is a drop-down for selecting which Ethereum node to connect to. If you have a private testnet server running with your testing contract, you can pick "Custom" from that list and enter your local node's address to connect directly to it for testing. Commented Aug 29, 2017 at 17:56
  • @MidnightLightning I am not sure if I understand what is happening. Does this mean that you are using MEW to help you sign and authorize the contract to allow Metamask to run on your behalf?
    – Strawberry
    Commented Apr 12, 2018 at 7:56
  • 1
    @Strawberry, MEW is a tool that assembles the transaction; putting together a message that says "I am account A and I want to execute function B on contract C". Formatting the transaction correctly is what MEW is good at. But then to have that transaction be approved by the blockchain, proof that "Account A" really did approve it needs to be provided (signature). So MEW asks Metamask "would you please sign this transaction I created?", and Metamask pops up a dialog indicating "This site wants me to sign on your behalf; that cool?". That make sense? Commented Apr 13, 2018 at 10:59
10

Metamask is great for client side interactions with smart contracts and their functions. If installed, you can access the web3 that is injected into your browser:

// Get the contract instance using your contract's abi and address:
const contractInstance = web3.eth.contract(abi).at(contractAddress);

// Call a function of the contract:
contractInstance.someFunction({ from: sender, value: someValue, gas: limit },
  (err, res) => { /** do something with results **/ });

You can even access the currently selected account in metamask with web3.eth.accounts[0].

This will require some front end development, but it's pretty straight forward.

The other alternative is to use Ethereum Wallet > Contracts > Watch Contract. Again, enter the contract address and abi, and you can access all of the contract's functions through Ethereum Wallet's UI.

2
  • 3
    Thank for the answer. Your answer is good, could you elaborate it a little bit more and tell me how these components fit together. As I'm a beginner and know how to write some smart contracts and test it. But, it's not clear to me how different components fit together, in the real world (not just for testing). For instance, how MetaMask, web3, truffle, testrpc etc work together. I know testrpc is for only testing, and I'm using all the above components (except for web3) for the testing but I'd like to go a bit deeper, i.e. front end development. Thanks.
    – Aydin
    Commented Aug 31, 2017 at 9:17
  • web3 is not supported by Metamask since 2021 Jan, so your answer is not working , refer to: docs.metamask.io/guide/…
    – Siwei
    Commented Jun 30, 2022 at 3:05
3

Example:

web3 = new Web3(web3.currentProvider);
  let myContract = new web3.eth.Contract(abi, address);
  let amount = 1;
  amount = web3.utils.toWei(amount.toString(), 'ether');
  let response = await myContract.methods
    .createPayment(1, address)  //function in contract
    .send({
      from: window.web3.currentProvider.selectedAddress,
      to: address,
      value: amount,
      gasPrice: '20000000000' 
    });
  console.log("response: ", response);
2

MyEtherWallet and Mist wallet both are deprecated now.
One of the best alternatives to these is MyCrypto wallet. It is recommended by the MEW and mist developers also. It has simplistic web UI as well as desktop versions are also available.

1

This works in 2022, change the abi and address , and put it in your HTML file:

    async function loadWeb3() {
      if (window.ethereum) {
        window.web3 = new Web3(window.ethereum);
        window.ethereum.enable();
      }
    }

    async function loadContract() {
      let abi = [] // your abi here
      let address = "0xa1b2c3d4..."  // your contract address here
      return await new window.web3.eth.Contract(abi, address);
    }
    async function getCurrentAccount() {
      const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
      return accounts[0];
    }

    async function run() {
      await loadWeb3();
      window.contract = await loadContract();
      const account = await getCurrentAccount();
      let result = await window.contract.methods.mint().send({ from: account });
    }

    if (typeof window.ethereum !== 'undefined') {
      run()
    }

refer to: https://ethereum.stackexchange.com/a/131437/30431

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.