I wrote a factory contract for generating some tokens and it is able to successfully create tokens. However, when I import the generated token address to my wallet, the supply is 0 even though I have set it to a value:
Factory contract snippet:
contract TokenFactory {
event MyTokenCreated(address contractAddress);
function createNewMyToken() public returns (address) {
MyToken myToken = new MyToken(2000000);
emit MyTokenCreated(address(myToken));
return address(myToken);
}
MyToken.sol
pragma solidity ^0.8.7;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
constructor (uint256 initialSupply) ERC20("MyToken", "MY") {
_mint(msg.sender, initialSupply * 10 ** 18);
}
}
After generating MyToken from TokenFactory and importing the generated token's address to my wallet, I expected the supply to be 2000000, but it is 0.
Does anyone have any idea why this is happening?
Note that you are sending tokens to msg.sender, and in this case, the msg.sender will always be the factory contract. So if you want to mint those tokens to your own wallet, you can either give MyToken a new address param where your factory contract will pass your address, or just use tx.origin, so that it references the transaction original creator address.