Tatum
Search…
How to use Tatum and MetaMask
Connect MetaMask and leverage Tatum's abstraction in no time
In a custodial app, you manage your users' private keys and transfer assets on their behalf. In a non-custodial app, users have their wallets that hold their private keys.
If you build any non-custodial application, most of your users will probably use MetaMask as their primary wallet. You can easily use Tatum to connect your users' MetaMask wallets to your app and leverage our abstraction, ready-to-go contracts, and simple transfers in a few easy steps.

Connect your MetaMask to a web page

The first step is to connect your web app with MetaMask. To do so, we'll create an Enable button on your web page that will request the accounts handled by MetaMask.
HTML
JS
1
<button class="enableMetamask">Enable MetaMAsk</button>
Copied!
​
1
let accounts = [];
2
document.querySelector('.enableMetamask')
3
.addEventListener('click', async () => {
4
accounts = await ethereum.request({
5
method: 'eth_requestAccounts'
6
});
7
console.log(accounts);
8
});
Copied!
​
When a user clicks on the Enable button, the MetaMask UI will prompt you for a connection. It will then expose the managed accounts to the web app.
In this example, we are assuming that MetaMask is installed as a Chrome extension. For more complex scenarios, take a look at these official MetaMask guides.

Prepare a transaction using Tatum

Next, we will leverage a functionality Tatum uses for its Key Management System (KMS) tool to prepare a transaction for MetaMask. We will prepare it in KMS using a signatureId, but we will prepare the transaction configuration instead of signing it and broadcasting it to the blockchain.
In this example, we'll deploy an NFT smart contract on the Polygon Mumbai testnet.
JS
1
// 1. Let's call the Deploy NFT with the signatureId present
2
// You can use any arbitrary UUID v4 string as a signatureId
3
const response = await axios.post('https://api-eu1.tatum.io/v3/nft/deploy/', {
4
"name": "Test Token",
5
"chain": "MATIC",
6
"symbol": "TTC",
7
"signatureId": "b7ad58f7-d826-4db5-8a52-4f492935a7b4"
8
}, {
9
headers: {
10
'x-api-key': '951cabe04de143b98b75c4d4ed4d2d99'
11
}
12
});
13
​
14
// 2. Response is the ID of the prepared transaction
15
const {
16
signatureId
17
} = response.data;
18
​
19
// 3. Let's grab the transaction and get the configuration
20
const {
21
data
22
} = await axios.get('https://api-eu1.tatum.io/v3/kms/' + signatureId, {
23
headers: {
24
'x-api-key': '951cabe04de143b98b75c4d4ed4d2d99'
25
}
26
});
Copied!
​
  1. 1.
    First, we'll invoke the Deploy NFT operation. We will use a signatureId in the call, which can be any arbitrary UUID v4 string. Again, we will not sign the transaction and broadcast it to the blockchain. We will only prepare the transaction config.
1
const response = await axios.post('https://api-eu1.tatum.io/v3/nft/deploy/', {
2
"name": "Test Token",
3
"chain": "MATIC",
4
"symbol": "TTC",
5
"signatureId": "b7ad58f7-d826-4db5-8a52-4f492935a7b4"
6
}, {
7
headers: {
8
'x-api-key': '951cabe04de143b98b75c4d4ed4d2d99'
9
}
10
});
Copied!
2. The response is the signatureId - the identifier of the prepared transaction that is waiting to be signed.
1
const {
2
signatureId
3
} = response.data;
Copied!
1
const {
2
data
3
} = await axios.get('https://api-eu1.tatum.io/v3/kms/' + signatureId, {
4
headers: {
5
'x-api-key': '951cabe04de143b98b75c4d4ed4d2d99'
6
}
7
});
Copied!

Send the transaction to MetaMask for signature

Now that we have prepared the transaction using KMS, we need to perform a few operations to get it ready for MetaMask.
We need to get the transaction configuration, set the correct sender account, and convert the gas price to be compatible with Metamask.
  1. 1.
    First, let's get the transaction configuration.
1
const txConfig = JSON.parse(data.serializedTransaction);
Copied!
2. Next, we'll set the correct sender account.
1
txConfig.from = accounts[0];
Copied!
3. Now, we'll convert the gasPrice from the decimal gwei format Tatum uses to the HEX gwei format that MetaMask uses.
1
txConfig.gasPrice = txConfig.gasPrice ? parseInt(txConfig.gasPrice).toString(16) : undefined;
Copied!
4. And finally, let's send the transaction to MetaMask.
1
console.log(await ethereum.request({
2
method: 'eth_sendTransaction',
3
params: [txConfig],
4
}));
Copied!
JS
1
// 1. Let's call the Deploy NFT with the signatureId present
2
// You can use any arbitrary UUID v4 string as a signatureId
3
const response = await axios.post('https://api-eu1.tatum.io/v3/nft/deploy/', {
4
"name": "Test Token",
5
"chain": "MATIC",
6
"symbol": "TTC",
7
"signatureId": "b7ad58f7-d826-4db5-8a52-4f492935a7b4"
8
}, {
9
headers: {
10
'x-api-key': '951cabe04de143b98b75c4d4ed4d2d99'
11
}
12
});
13
​
14
// 2. Response is the ID of the prepared transaction
15
const {
16
signatureId
17
} = response.data;
18
​
19
// 3. Let's grab the transaction and get the configuration
20
const {
21
data
22
} = await axios.get('https://api-eu1.tatum.io/v3/kms/' + signatureId, {
23
headers: {
24
'x-api-key': '951cabe04de143b98b75c4d4ed4d2d99'
25
}
26
});
27
​
28
// 4. Get the transaction config
29
const txConfig = JSON.parse(data.serializedTransaction);
30
​
31
// 5. Set the correct sender account
32
txConfig.from = accounts[0];
33
​
34
// 6. Put gas price in HEX
35
txConfig.gasPrice = txConfig.gasPrice ? parseInt(txConfig.gasPrice).toString(16) : undefined;
36
​
37
// 7. Send tx to MetaMask
38
console.log(await ethereum.request({
39
method: 'eth_sendTransaction',
40
params: [txConfig],
41
}));
Copied!
​

Recap

To sum it up, we have connected MetaMask to our web app, chosen the operation to perform (deploy an NFT smart contract), used Tatum in KMS mode to prepare the transaction, set the correct sender account and gas price parameters, and let MetaMask sign and broadcast the operation.
This same method can be used for many other blockchain operations, allowing you to connect MetaMask to your non-custodial app for a wide range of use cases.
Everything we have done here is available in this JSFiddle.
Last modified 29d ago