Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
qiaopengjun5162 committed Jul 29, 2024
1 parent f3ac330 commit 71a95bf
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 187 deletions.
5 changes: 1 addition & 4 deletions script/DeployProxy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ contract DeployUUPSProxy is Script {
console.log("TokenFactoryV1 deployed to:", address(_implementation));

// Encode the initializer function call
bytes memory data = abi.encodeCall(
_implementation.initialize,
deployerAddress
);
bytes memory data = abi.encodeCall(_implementation.initialize, deployerAddress);

// Deploy the proxy contract with the implementation address and initializer
ERC1967Proxy proxy = new ERC1967Proxy(address(_implementation), data);
Expand Down
17 changes: 3 additions & 14 deletions script/TokenFactoryV2.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,11 @@ contract TokenFactoryV2Script is Script {
console.log("Deploying contracts with the account:", deployerAddress);
vm.startBroadcast(deployerPrivateKey);

Upgrades.upgradeProxy(
address(proxy),
"TokenFactoryV2.sol:TokenFactoryV2",
"",
deployerAddress
);
(bool successful, ) = address(proxy).call(
abi.encodeWithSelector(
TokenFactoryV2.setTokenAddress.selector,
address(erc20Token)
)
);
Upgrades.upgradeProxy(address(proxy), "TokenFactoryV2.sol:TokenFactoryV2", "", deployerAddress);
(bool successful,) =
address(proxy).call(abi.encodeWithSelector(TokenFactoryV2.setTokenAddress.selector, address(erc20Token)));
console.log("setTokenAddress success:", successful);

// console.log("TokenFactoryV1 deployed to:", address(factoryv2));

vm.stopBroadcast();
}
}
42 changes: 11 additions & 31 deletions src/ERC20Token.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ contract ERC20Token is
ERC20PermitUpgradeable,
ERC20VotesUpgradeable
{
uint public totalSupplyToken;
uint public perMint;
uint256 public totalSupplyToken;
uint256 public perMint;

/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
Expand All @@ -37,12 +37,10 @@ contract ERC20Token is
* @param _perMint perMint 用来控制每次发行的数量
*
*/
function initialize(
address initialOwner,
string memory _symbol,
uint _totalSupply,
uint _perMint
) public initializer {
function initialize(address initialOwner, string memory _symbol, uint256 _totalSupply, uint256 _perMint)
public
initializer
{
__ERC20_init("ERC20Token", _symbol);
__ERC20Burnable_init();
__ERC20Pausable_init();
Expand All @@ -62,40 +60,22 @@ contract ERC20Token is
}

function mint(address to) public {
uint currentSupply = totalSupply(); // 获取当前代币供应量
uint256 currentSupply = totalSupply(); // 获取当前代币供应量
// 确保铸造后总供应量不超过最大供应量
require(
currentSupply + perMint <= totalSupplyToken,
"Exceeds max total supply"
);
require(currentSupply + perMint <= totalSupplyToken, "Exceeds max total supply");
_mint(to, perMint);
}

// The following functions are overrides required by Solidity.

function _update(
address from,
address to,
uint256 value
)
function _update(address from, address to, uint256 value)
internal
override(
ERC20Upgradeable,
ERC20PausableUpgradeable,
ERC20VotesUpgradeable
)
override(ERC20Upgradeable, ERC20PausableUpgradeable, ERC20VotesUpgradeable)
{
super._update(from, to, value);
}

function nonces(
address owner
)
public
view
override(ERC20PermitUpgradeable, NoncesUpgradeable)
returns (uint256)
{
function nonces(address owner) public view override(ERC20PermitUpgradeable, NoncesUpgradeable) returns (uint256) {
return super.nonces(owner);
}
}
12 changes: 3 additions & 9 deletions src/TokenFactoryV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ contract TokenFactoryV1 is Initializable, OwnableUpgradeable, UUPSUpgradeable {
__UUPSUpgradeable_init();
}

function _authorizeUpgrade(
address newImplementation
) internal override onlyOwner {}
function _authorizeUpgrade(address newImplementation) internal override onlyOwner {}

/**
* 该方法用来创建 ERC20 token,(模拟铭文的 deploy)
Expand All @@ -37,11 +35,7 @@ contract TokenFactoryV1 is Initializable, OwnableUpgradeable, UUPSUpgradeable {
* deployInscription(string symbol, uint totalSupply, uint perMint)
*
*/
function deployInscription(
string memory symbol,
uint totalSupply,
uint perMint
) public {
function deployInscription(string memory symbol, uint256 totalSupply, uint256 perMint) public {
myToken = new ERC20Token();
myToken.initialize(msg.sender, symbol, totalSupply, perMint);
console.log("deployInscription newToken: ", address(myToken));
Expand All @@ -61,7 +55,7 @@ contract TokenFactoryV1 is Initializable, OwnableUpgradeable, UUPSUpgradeable {
token.mint(msg.sender); // Assuming ERC20Token has a mint function with (address, uint256) parameters
}

function size() public view returns (uint) {
function size() public view returns (uint256) {
return deployedTokens.length;
}
}
49 changes: 13 additions & 36 deletions src/TokenFactoryV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,13 @@ import "./ERC20Token.sol";
contract TokenFactoryV2 is Initializable, OwnableUpgradeable, UUPSUpgradeable {
ERC20Token myToken;
address[] public deployedTokens;
mapping(address => uint) public tokenPrices;
mapping(address => uint) public tokenperMint;
mapping(address => uint256) public tokenPrices;
mapping(address => uint256) public tokenperMint;
mapping(address => address) public tokenDeployUser;

event deployInscriptionEvent(
address indexed tokenAddress,
address indexed userAddress,
uint indexed price
);
event deployInscriptionEvent(address indexed tokenAddress, address indexed userAddress, uint256 indexed price);

event mintInscriptionEvent(
address indexed tokenAddress,
address indexed userAddress,
uint indexed amount
);
event mintInscriptionEvent(address indexed tokenAddress, address indexed userAddress, uint256 indexed amount);

/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
Expand All @@ -44,9 +36,7 @@ contract TokenFactoryV2 is Initializable, OwnableUpgradeable, UUPSUpgradeable {
myToken = ERC20Token(_tokenAddress);
}

function _authorizeUpgrade(
address newImplementation
) internal override onlyOwner {}
function _authorizeUpgrade(address newImplementation) internal override onlyOwner {}

/**
* 部署新的 ERC20 代币合约
Expand All @@ -55,32 +45,19 @@ contract TokenFactoryV2 is Initializable, OwnableUpgradeable, UUPSUpgradeable {
* @param perMint perMint 用来控制每次发行的数量,用于控制mintInscription函数每次发行的数量
* @param price 每个代币的价格 price 表示发行每个 token 需要支付的费用
*/
function deployInscription(
string memory symbol,
uint totalSupply,
uint perMint,
uint price
) public {
function deployInscription(string memory symbol, uint256 totalSupply, uint256 perMint, uint256 price) public {
require(bytes(symbol).length > 0, "Symbol cannot be empty");
require(totalSupply > 0, "Total supply must be greater than zero");
require(perMint > 0, "Per mint must be greater than zero");
require(price > 0, "Price must be greater than zero");

require(
address(myToken) != address(0),
"Implementation address is not set"
);
require(address(myToken) != address(0), "Implementation address is not set");

console.log("deployInscription msg.sender, address:", msg.sender);
// 使用 Clones 库创建最小代理合约实例
address newToken = Clones.clone(address(myToken));

ERC20Token(newToken).initialize(
msg.sender,
symbol,
totalSupply,
perMint
);
ERC20Token(newToken).initialize(msg.sender, symbol, totalSupply, perMint);

deployedTokens.push(newToken);
tokenPrices[newToken] = price;
Expand All @@ -95,13 +72,13 @@ contract TokenFactoryV2 is Initializable, OwnableUpgradeable, UUPSUpgradeable {
*/
function mintInscription(address tokenAddr) public payable {
ERC20Token token = ERC20Token(tokenAddr);
uint price = tokenPrices[tokenAddr];
uint perMint = tokenperMint[tokenAddr];
uint256 price = tokenPrices[tokenAddr];
uint256 perMint = tokenperMint[tokenAddr];
address userAddr = tokenDeployUser[tokenAddr];
require(msg.value >= (price * perMint), "Incorrect payment");
token.mint(msg.sender);
// 使用 call 方法转账,以避免 gas 限制问题 payable(userAddr).transfer(msg.value);
(bool success, ) = userAddr.call{value: msg.value}("");
(bool success,) = userAddr.call{value: msg.value}("");
require(success, "Transfer failed.");

emit mintInscriptionEvent(tokenAddr, userAddr, msg.value);
Expand All @@ -111,12 +88,12 @@ contract TokenFactoryV2 is Initializable, OwnableUpgradeable, UUPSUpgradeable {
* 提取合约余额
*/
function withdraw() external onlyOwner {
uint balance = address(this).balance;
uint256 balance = address(this).balance;
require(balance > 0, "No funds to withdraw");
payable(owner()).transfer(balance);
}

function size() public view returns (uint) {
function size() public view returns (uint256) {
return deployedTokens.length;
}
}
9 changes: 3 additions & 6 deletions test/ERC20TokenTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@ contract ERC20TokenTest is Test {
Account public newOwner = makeAccount("newOwner");
Account public user = makeAccount("user");
string public symbol = "ETK";
uint public totalSupply = 1_000_000 ether;
uint public perMint = 10 ether;
uint256 public totalSupply = 1_000_000 ether;
uint256 public perMint = 10 ether;

function setUp() public {
// 部署实现
ERC20Token implementation = new ERC20Token();
// Deploy the proxy and initialize the contract through the proxy
proxy = new ERC1967Proxy(
address(implementation),
abi.encodeCall(
implementation.initialize,
(owner.addr, symbol, totalSupply, perMint)
)
abi.encodeCall(implementation.initialize, (owner.addr, symbol, totalSupply, perMint))
);
// 用代理关联 MyToken 接口
myToken = ERC20Token(address(proxy));
Expand Down
Loading

0 comments on commit 71a95bf

Please sign in to comment.