I'm a bit confused about creating a token transaction (from account of User1 -> account of User2) as part of a blockchain-based crowdfunding platform. I have most of the other aspects figured out regarding application components and stuff, but the overall integration of MetaMask, accounts and transactions continues to stump me.
My initial goal is to have the NextJS Backend (/api
routes) handle contract and transaction calls. From my observations, most tutorials online manage such calls from the react frontend itself.
From my past interactions with similar projects, the MetaMask extension manages a lot of the web3
setup, including configuring providers (!) and account addresses. Is this same metamask provider also responsible for user approval and signing prompts?
Considering that a MetaMask-less backend (NextJS API) will be handling contract and transaction calls. How would one proceed with integrating a MetaMask wallet to the backend? My inital thought train goes as follows:
Current configuration plan:
window.ethereum
provider?)Logical flow:
This, is where I have hit a roadblock. My current observation is -
Thanking all in advance, arnitdo
I've searched a lot for documentation regarding metamask and pure backend-based transactions, however I have not encountered any sufficient documentation. Perhaps my direction is wrong, or I'm trying something which isn't quite feasible
Metamask is a chrome extension, you cannot use it in the backend. chrome extensions inject a property into the window
global object, in this case metamask add ethereum
property. so we reach the window.ethereum
to set up a provider. Provider is a wrapper object, it wraps the window.ethereum
and you create provider via a third party library. depending on the third party api, you will have slighly different provider objects.
Metamask is just an easy way to use a wallet to sign the transaction. Behind the scene it uses infura node to connect the blockchain.
If you are on the back end, you can create your provider with signing up for infura. for example
const { ethers } = require("ethers");
const provider = new ethers.providers.JsonRpcProvider(INFURA_URL_MAINNET);
If you are in the backend since you do not have metamask, you have to create your own walled using secret key. you can go to metamask, choose an account and extract its secret key and create a wallet on the backend
const wallet = new ethers.Wallet(WALLET_SECRET);
const signer = wallet.connect(provider);
then you construct a transaction, and execute it via the signer
const txArgs = {
to: toAddress,
from: YOUR_WALLET_ADDRESS,
data: yourEncodedData,
gasLimit: ethers.utils.hexlify(3000000),
};
const tx = await signer.sendTransaction(txArgs);