blockchainsolidityerc20decentralized-applications

Passing dynamically generated values to ERC20 Constructor


Issue

Hi everyone! I'm currently working on an website that autogenerates ERC20 token by the value supplied by user. The biggest concern i've been getting is passing user values to ERC20 constructor since it expects two arguments in the constructor which is _name and _symbol and i dont want this to be hardcoded into the smart contract since it depends largely on the value input by user.

Attempt I

I try to overwrite the two values from openzepellin ERC20 but to no avail, i got a token deployed with no name

Token.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract DamiKing is ERC20 {
address public admin;
string name;
string symbol;

event serverStatus(string serverMessage);
constructor() ERC20(name, symbol) 
payable {

    _name = name;
    _symbol = symbol;
    _mint(msg.sender, 10000 * 10 ** decimals());
    admin = msg.sender;
}

deploy.js

async function main() {
const [owner] = await hre.ethers.getSigners();
const DMKContractFactory = await hre.ethers.getContractFactory("DamiKing");
const DMKContract = await DMKContractFactory.deploy("damilare", "afo");
const x = await DMKContract.deployed();
console.log("Smart Contract Deployed with the address: ",DMKContract.address);
console.log("Total Suppy: ",await DMKContract.checkBalance(owner.address), await 
DMKContract.symbol());
console.log("owner address: ", owner.address);
console.log(x)
}

Attempt II

I tried to pass a variable name to the ERC20 constructor so i can supply that during deployment but i got an error of Expected ',' but got 'memory'solidity(2314)

Token.sol

    constructor() ERC20(string memory name, string memory symbol) payable {
    _mint(msg.sender, 10000 * 10 ** decimals());
    admin = msg.sender;
}

Attempt III

I tried to ignore the constructor of ERC20 and focus mainly on my contract constructor, that end up badly with an error of Contract "DamiKing" should be marked as abstract.

 constructor(string memory name, string memory symbol) payable {
    _mint(msg.sender, 10000 * 10 ** decimals());
    admin = msg.sender;
}

Any help will be highly appreciated, i've been stuck for weeks!


Solution

  • First of all, I want to make sure if I understood it right, You are making a website that takes inputs from user, i.e token Name and Token Symbol, and deploys an erc20 token based on that. If that the case.

    You can achieve this by using the Factory pattern. I found a random git by doing a simple google search, you can see the code here.