I am trying to make a token generator, I have made two contracts the following way:
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract Token is ERC20 {
constructor(string memory name, string memory symbol) ERC20(name, symbol) {
_mint(msg.sender, 1000);
}
}
TokenGenerator contract:
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "./ERC20.sol";
contract TokenGenerator{
function generateToken(string memory name, string memory symbol) public returns(address){
Token token = new Token(name, symbol);
address myAddress = address(token);
return myAddress;
}
}
However, I am struggling to understand how to use it in the frontend side. The way I generally do is that I deploy the contract and use that abi to generate more token, in which case the token holder is the deployed contract address and not the account that is generating the token. How do I generate a token whereupon every time the user clicks generate a new smart contract of the token is made on the go, and the token holder is the user account address?
I would recommend you:
emit
some event on generateToken
transaction execution. So you will be able to subscribe to this event on your UI and keep track of newly generated Token
contracts
keep track of the generated Token
contract addresses on the blockchain side for easy access outside of the blockchain
contract TokenGenerator { address[] public allTokens; event TokenGeneration(address indexed tokenAddress); function generateToken(string memory name, string memory symbol) public returns (address) { Token token = new Token(name, symbol); address myAddress = address(token); // Add the address of generated Token contract to the allTokens array allTokens.push(myAddress); // Emit event with the address of generated Token contract emit TokenGeneration(myAddress); return myAddress; } }
So now:
TokenGeneration
event to get notified when the new Token contract is generatedallTokens
arrayThe ABI for all Token contracts will be the same.
I was committing an error by deploying the contract and using the deployed contract address to call generateToken
function. With this method, the contract address becomes the msg.sender
and not the account address interacting with the smart contract. I did the following to have the account address as the token holder and generate a new address every time someone clicks the generate token button:
const generateToken = async () => { try { const { ethereum } = window; if (ethereum) { const provider = new ethers.providers.Web3Provider(ethereum); const signer = provider.getSigner(); const factory = new ethers.ContractFactory(Token.abi, bytecode, signer); const tokenGeneratorContract = await factory.deploy(name, symbol); await console.log("Token Address:", tokenGeneratorContract.address); } else { console.log("Ethereum object doesn't exist!"); } } catch (error) { console.log(error); } };
I didn't have to use the TokenGenerator contract and only used the Token contract, its ABI and bytecode to deploy the contract. So generally one would have the private key to deploy the contract. Signer from ethers.js allows one to create a function where the client can deploy the contract directly through their Metamask account, without knowing their private_key.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.