I've a functionality in my nextJs application to create an ERC20 token on a fly. Like user can input a contract name and symbol, and behind the scene through the Ether.js a new contract gets created and deployed. I've searched all around but couldn't find any such example. How can i achieve this.
1 Answer
Use this code :
const { ethers } = require('ethers');
const contractAbi = [...]; // ABI of your ERC-20 contract
const contractBytecode = '0x...'; // Compiled bytecode of your ERC-20 contract
const providerUrl = 'https://your-infura-url-here.com'; // Ethereum provider URL
async function deployTokenContract(name, symbol, initialSupply, privateKey) {
const provider = new ethers.providers.JsonRpcProvider(providerUrl);
const wallet = new ethers.Wallet(privateKey, provider);
const factory = new ethers.ContractFactory(contractAbi, contractBytecode, wallet);
const contract = await factory.deploy(name, symbol, initialSupply);
await contract.deployed();
return contract;
}