Build your own custom transaction
Preparing a transaction for signing via MetaMask means creating a transaction object containing essential details, such as the sender's address, recipient's address, amount to be sent, and gas fees. This transaction object is then presented to MetaMask, which securely signs the transaction using the user's private key, ensuring the transaction's authenticity and integrity before it's broadcasted to the Ethereum network.
MetaMask is designed as a browser extension to provide a user-friendly interface and secure key management for interacting with dApps and web services. Connecting from Node.js is not supported because MetaMask focuses on end-user interactions within web browsers, while Node.js is a server-side JavaScript runtime typically used for backend development.
In summary, you can prepare any transaction, like deploying a new smart contract or interacting with any existing one.
Use the TatumSDK (
@tatumio/tatum
) to prepare, sign and broadcast the transaction using MetaMask.You will leverage the WalletProvider submodule, which includes multiple browser-based wallet extensions. MetaMask is just one of them.
TypeScript
JavaScript
1
// yarn add @tatumio/tatum
2
import {TatumSDK, Network, Ethereum} from '@tatumio/tatum'
3
4
const tatum = await TatumSDK.init<Ethereum>({network: Network.ETHEREUM})
5
6
// Prepare your payload for a signing
7
const payload = {
8
to: '0x4675C7e5BaAFBFFbca748158bEcBA61ef3b0a263',
9
amount: '0x012F',
10
data: '0x000......' // your custom data
11
}
12
13
const txId: string = await tatum.walletProvider.metaMask.customPayload(payload)
14
15
console.log(txId)
16
17
// We have prepared a custom transaction from your default connected MetaMask account
1
// Install with: npm install @tatumio/tatum
2
const { TatumSDK, Network } = require("@tatumio/tatum");
3
4
(async () => {
5
try {
6
const tatum = await TatumSDK.init({ network: Network.ETHEREUM });
7
// Prepare your payload for a signing
8
const payload = {
9
to: '0x4675C7e5BaAFBFFbca748158bEcBA61ef3b0a263',
10
amount: '0x012F',
11
data: '0x000......' // your custom data
12
};
13
14
const txId = await tatum.walletProvider.metaMask.customPayload(payload);
15
console.log(txId);
16
} catch (error) {
17
console.error("Error signing a transaction using MetaMask:", error);
18
}
19
})();
20
21
22
// We have prepared a custom transaction from your default connected MetaMask account
1
interface TxPayload {
2
// - The string of the address to the transaction is directed to
3
to?: string
4
5
// - (optional) The string of the address the transaction is sent from
6
from?: string
7
8
// - (optional) The integer of the gas provided for the transaction execution
9
gas?: string
10
11
// - (optional) The integer of the gasPrice used for each paid gas encoded as hexadecimal
12
gasPrice?: string
13
14
// - (optional) The integer of the value sent with this transaction encoded as hexadecimal
15
value?: string
16
17
// The string of the hash of the method signature and encoded parameters, see the Ethereum Contract ABI
18
data?: string
19
}
- txId - string, transaction hash of signed and broadcasted transaction
- Example:
"0xdb1e03f4cea29265f031bfc0514b07c15a5fc5e5cc2fd47f7d9a54c74f5c5637"