设置工厂合同产生的令牌的初始供应
我写了一份工厂合同来生成一些令牌,它能够成功创建令牌。但是,当我将生成的令牌地址导入钱包时,即使我将其设置为一个值:
工厂合同摘要:
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);
}
}
,从tokenfactory生成mytoken并将生成的令牌地址导入我的钱包,我预计供应将为2000000,但它是0。
有人知道为什么会发生这种情况吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,您将令牌发送给msg.sender,在这种情况下,MSG.Sender将永远是工厂合同。因此,如果您想将这些令牌铸造到自己的钱包中,则可以给MyToken提供一个新的地址参数,其中工厂合同将通过您的地址,或者只使用tx.origin,以便引用交易原始创建者地址。
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.