Interacting With Metamask
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:
1
npm i '@metamask/detect-provider' --save
2
npm i web3 --save
3
npm i 'bn.js' --save
Copied!
First step - you need to detect provider and connect to Metamask:
1
import detectEthereumProvider from '@metamask/detect-provider';
2
โ€‹
3
let ethAddress;
4
let isAuthorised = false;
5
โ€‹
6
const handleAccountsChanged = (accounts) => {
7
if (accounts.length === 0) {
8
console.error('Not found accounts');
9
} else {
10
ethAddress = accounts[0];
11
12
console.log('Your address: ', ethAddress);
13
}
14
}
15
โ€‹
16
export const signInMetamask = async () => {
17
const provider = await detectEthereumProvider();
18
โ€‹
19
// @ts-ignore
20
if (provider !== window.ethereum) {
21
console.error('Do you have multiple wallets installed?');
22
}
23
โ€‹
24
if (!provider) {
25
console.error('Metamask not found');
26
return;
27
}
28
โ€‹
29
// MetaMask events
30
provider.on('accountsChanged', handleAccountsChanged);
31
โ€‹
32
provider.on('disconnect', () => {
33
console.log('disconnect');
34
isAuthorised = false;
35
});
36
37
provider.on('chainIdChanged', chainId => console.log('chainIdChanged', chainId));
38
โ€‹
39
provider
40
.request({ method: 'eth_requestAccounts' })
41
.then(async params => {
42
handleAccountsChanged(params);
43
isAuthorised = true;
44
})
45
.catch(err => {
46
isAuthorised = false;
47
48
if (err.code === 4001) {
49
console.error('Please connect to MetaMask.');
50
} else {
51
console.error(err);
52
}
53
});
54
}
Copied!
Next step - you can use connected provider with Web3 to sign and send transactions:
1
new Web3(window.web3.currentProvider)
2
โ€‹
3
/* provider will use network RPC, wich was selected in MetaMask */
Copied!
1
const accounts = await ethereum.enable();
2
3
/* Now any request to sign a transaction will be redirected to MetaMask */
Copied!
1
import Web3 from 'web3';
2
const BN = require('bn.js');
3
โ€‹
4
const sendTransaction = async () => {
5
const web3 = new Web3(window.ethereum);
6
7
const receiverAddress = '0x430506383F1Ac31F5FdF5b49ADb77faC604657B2';
8
9
const gas = 6721900;
10
const gasPrice = new BN(await web3.eth.getGasPrice()).mul(new BN(1));
11
12
const result = await web3.eth
13
.sendTransaction({
14
from: account,
15
to: receiverAddress,
16
value: 1 * 1e18, // 1ONE
17
gasPrice,
18
gas,
19
})
20
.on('error', console.error);
21
โ€‹
22
console.log(`Send tx: ${result.transactionHash} result: `, result.status);
23
}
Copied!
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.
Also you can use all Provider API from official MetaMask and Web3 docs: https://docs.metamask.io/guide/ethereum-provider.html#table-of-contentsโ€‹
Last modified 9mo ago
Export as PDF
Copy link