13

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?

6

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.

  • thanks for the answer. Can I use "MyEtherWallet" for testing contracts? – Ay. Aug 29 '17 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. – MidnightLightning Aug 29 '17 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 Apr 12 '18 at 7:56
  • @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? – MidnightLightning Apr 13 '18 at 10:59
6

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
    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. – Ay. Aug 31 '17 at 9:17
1

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);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.