Interacting With MetaMask
For instructions on how to install and setup Metamask to work with Harmony blockchain please click here.
You can connect and sign transactions with Metamask using the Web3 library - which is fully compatible with the Harmony RPC API.
Do not forget that an important difference from One Wallet is that Metamsk sends transactions itself (extension side). While One Wallet only sign transaction, and then sending happens on browser side.
To use next example, you need to include the following libraries:
First step - you need to detect provider and connect to Metamask:
Next step - you can use connected provider with Web3 to sign and send transactions:
After executing this function, an interactive MetaMask window will open in which you can sign the transaction and change the gasPrice / gasLimit parameters - if it necessary.
Full code example will be here: https://github.com/harmony-one/ethhmy-bridge.frontend/blob/web3_hmy/src/pages/Examples/TransactionExample.tsx
Also you can use all Provider API from official MetaMask and Web3 docs: https://docs.metamask.io/guide/ethereum-provider.html#table-of-contents
For instructions on how to install and setup Metamask to work with Harmony blockchain please click here.
The completed code can be found here.
For reference, the smart contract code will look as follows:
For setting up your own project just replace the code in userWallet.js
in One Wallet guide with the following.
This will connect your metamask.
Setup your contract object. Ininit.js
:
You just completed the tutorial to interact with smart contract using metamask and web3 on Harmony Network!
This tutorial help developers to interact with Metamask installed on a user's device, to add (or switch to) the Harmony chain's network.
Harmony is a multi-sharded chain, currently with shard 0, 1, 2 and 3 running on Mainnet. To execute the code snippet below successfull, you can test on a device that has Metamask extension / add-on installed and visible to your app.
Please note that window.web3
has been deprecated. Use window.ethereum
instead. To migrate, use this guide from Metamask.
When this code snippet is executed, users will see message to approve adding or switching to correct Harmony chain.
You can see more information about Metamask documentation related to the Ethereum Provider API at https://docs.metamask.io/guide/ethereum-provider.html
To see a full list of Chain IDs, please visit https://chainlist.org/ and search for Harmony