Quickstart: Use MetaMask to connect and deploy a smart contract

In this quickstart you'll use MetaMask to connect to an Azure Blockchain Service network and use Remix to deploy a smart contract. Metamask is a browser extension to manage an Ether wallet and perform smart contract actions.

If you don't have an Azure subscription, create a free account before you begin.

Prerequisites

Get endpoint address

You need the Azure Blockchain Service endpoint address to connect to the blockchain network. The endpoint address and access keys are in the Azure portal.

  1. Sign in to the Azure portal.

  2. Navigate to your Azure Blockchain Service member.

  3. Select Transaction nodes and the default transaction node link.

    Select default transaction node

  4. Select Connection strings > Access keys.

  5. Copy the endpoint address from HTTPS (Access key 1).

    Connection string

Connect MetaMask

  1. Open MetaMask browser extension and sign in.

  2. In the network dropdown, select Custom RPC.

    Custom RPC

  3. In New Network > New RPC URL, paste the endpoint address you copied above.

  4. Select Save.

    If connection was successful, the private network displays in the network drop-down.

    New network

Deploy smart contract

Remix is a browser-based Solidity development environment. Using MetaMask and Remix together, you can deploy and take actions on smart contracts.

  1. In your browser, navigate to https://remix.ethereum.org.

  2. Select New file in the Home tab under File.

    Name the new file simple.sol.

    Create file

    Select OK.

  3. In the Remix editor, paste in the following simple smart contract code.

    pragma solidity ^0.5.0;
    
    contract simple {
        uint balance;
    
        constructor() public{
            balance = 0;
        }
    
        function add(uint _num) public {
            balance += _num;
        }
    
        function get() public view returns (uint){
            return balance;
        }
    }
    

    The simple contract declares a state variable named balance. There are two functions defined. The add function adds a number to balance. The get function returns the value of balance.

  4. To compile the contract, first select the Solidity compiler pane then select the Compile simple.sol.

    Screen capture shows a contract being compiled.

  5. Select the Deploy & Run pane then set the Environment to Injected Web3 to connect through MetaMask to your blockchain member.

    Run tab

  6. Select the simple contract, then Deploy.

    Screen capture shows deploy and run transactions with a contract selected and Deploy selected.

  7. A MetaMask notification alerts you of insufficient funds to perform the transaction.

    For a public blockchain network, you would need Ether to pay for the transaction cost. Since this is a private network in a consortium, you can set gas price to zero.

  8. Select Gas Fee > Edit > Advanced, set the Gas Price to 0.

    Gas price

    Select Save.

  9. Select Confirm to deploy the smart contract to the blockchain.

  10. In the Deployed Contracts section, expand the simple contract.

    Deployed contract

    Two actions, add and get, map to the functions defined in the contract.

  11. To perform an add transaction on the blockchain, enter a number to add, then select add. You may get a gas estimation failure message from Remix: "You are sending the transaction to a private blockchain that does not require gas." Select Send Transaction to force the transaction.

  12. Similar to when you deployed the contract, a MetaMask notification alerts you of insufficient funds to perform the transaction.

    Since this is a private network in a consortium, we can set gas price to zero.

  13. Select Gas Fee > Edit > Advanced, set the Gas Price to 0, and select Save.

  14. Select Confirm to send the transaction to the blockchain.

  15. Select get action. This is a call to query node data. A transaction isn't needed.

The debug pane of Remix shows details about the transactions on the blockchain:

Debug history

You can see the simple contract creation, transaction for simple.add, and call to simple.get.

To see transaction history in MetaMask, open the MetaMask browser extension and look in the History section for a log of the deployed contract and transactions.

Next steps

In this quickstart, you used the MetaMask browser extension to connect to an Azure Blockchain Service transaction node, deploy a smart contract, and send a transaction to the blockchain. Try the next tutorial to use Azure Blockchain Development Kit for Ethereum and Truffle to create, build, deploy, and execute a smart contract function via a transaction.