1

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.

2 Answers 2

1

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;
}
0

@Mehran Ra's code doesn't seem to work in my case. Specifically you the provider line didn't work for me. Adjusted the code would look like this:

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.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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.