Last active
May 27, 2022 14:20
-
-
Save mingderwang/3787fd9bae5671e49c2a09cabdcd3c00 to your computer and use it in GitHub Desktop.
These codes are for a token monster DAPP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.4; | |
import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol"; | |
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; | |
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol"; | |
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; | |
contract BasicToken is Initializable, ERC20Upgradeable, UUPSUpgradeable, OwnableUpgradeable { | |
constructor() initializer {} | |
function initialize(string memory name, string memory symbol) initializer public { | |
__ERC20_init(name, symbol); | |
__UUPSUpgradeable_init(); | |
} | |
function mint(address to, uint256 amount) public onlyOwner{ | |
_mint(to, amount); // 100000 * (10 ** 18) | |
} | |
function _authorizeUpgrade(address newImplementation) internal override onlyOwner { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.8.4; | |
//SPDX-License-Identifier: MIT | |
import "./BasicToken.sol"; | |
import "@openzeppelin/contracts/access/Ownable.sol"; //https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol | |
contract TokenFactory is Ownable { | |
event TokenCreated(address indexed token, uint); | |
address[] newContracts; | |
function createTokenSalted(bytes32 salt, address initialOwner, string memory name, string memory symbol, uint256 initAmount) public { | |
address predictedAddress = address(uint160(uint(keccak256(abi.encodePacked( | |
int8(-1), | |
address(this), | |
salt, | |
keccak256(abi.encodePacked( | |
type(BasicToken).creationCode | |
)) | |
))))); | |
BasicToken newToken = new BasicToken{salt: salt}(initialOwner, name, symbol, initAmount); | |
require(address(newToken) == predictedAddress,"predict address is not correct"); | |
newContracts.push(predictedAddress); | |
emit TokenCreated(predictedAddress, newContracts.length); | |
} | |
function howManyTokens() external view returns (uint) { | |
return newContracts.length; | |
} | |
function newTokenAddress() external view returns (string memory) { | |
return toAsciiString(newContracts[newContracts.length - 1]); | |
} | |
function toAsciiString(address x) internal pure returns (string memory) { | |
bytes memory s = new bytes(40); | |
for (uint i = 0; i < 20; i++) { | |
bytes1 b = bytes1(uint8(uint(uint160(x)) / (2**(8*(19 - i))))); | |
bytes1 hi = bytes1(uint8(b) / 16); | |
bytes1 lo = bytes1(uint8(b) - 16 * uint8(hi)); | |
s[2*i] = char(hi); | |
s[2*i+1] = char(lo); | |
} | |
return string(s); | |
} | |
function char(bytes1 b) internal pure returns (bytes1 c) { | |
if (uint8(b) < 10) return bytes1(uint8(b) + 0x30); | |
else return bytes1(uint8(b) + 0x57); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment