Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
qiaopengjun5162 committed Jul 28, 2024
1 parent 382984d commit 31577c8
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 226 deletions.
19 changes: 0 additions & 19 deletions script/ERC20TokenV2.s.sol

This file was deleted.

112 changes: 0 additions & 112 deletions src/ERC20TokenV2.sol

This file was deleted.

15 changes: 7 additions & 8 deletions src/TokenFactoryV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Utils.sol";
import "./ERC20Token.sol";
import "./ERC20TokenV2.sol";

/// @custom:oz-upgrades-from TokenFactoryV1
contract TokenFactoryV2 is Initializable, OwnableUpgradeable, UUPSUpgradeable {
ERC20Token myToken;
ERC1967Proxy proxy;
Expand Down Expand Up @@ -45,35 +45,34 @@ contract TokenFactoryV2 is Initializable, OwnableUpgradeable, UUPSUpgradeable {
* @param symbol symbol 表示 Token 的名称
* @param totalSupply totalSupply 表示可发行的数量
* @param perMint perMint 用来控制每次发行的数量,用于控制mintInscription函数每次发行的数量
* @param price 每个代币的价格 price 表示发行每个 token 需要支付的费用
* @param _price 每个代币的价格 price 表示发行每个 token 需要支付的费用
*/
function deployInscription(
string memory symbol,
uint totalSupply,
uint perMint,
uint price
uint _price
) public onlyOwner {
console.log("deployInscription msg.sender, address:", msg.sender);
// 使用 Clones 库创建最小代理合约实例
address proxyInstance = Clones.clone(implementation);
ERC20TokenV2(proxyInstance).initialize(
ERC20Token(proxyInstance).initialize(
msg.sender,
symbol,
totalSupply,
perMint,
price
perMint
);

deployedTokens.push(proxyInstance);
tokenPrices[proxyInstance] = price;
tokenPrices[proxyInstance] = _price;
}

/**
* 铸造 ERC20 代币
* @param tokenAddr 代币地址
*/
function mintInscription(address tokenAddr) public payable {
ERC20TokenV2 token = ERC20TokenV2(tokenAddr);
ERC20Token token = ERC20Token(tokenAddr);
uint price = tokenPrices[tokenAddr];
require(msg.value >= price, "Incorrect payment");
token.mint(msg.sender);
Expand Down
11 changes: 0 additions & 11 deletions test/ERC20TokenTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,4 @@ contract ERC20TokenTest is Test {
myToken.mint(user.addr);
assertEq(myToken.balanceOf(user.addr), 10 ether);
}

// 测试升级
function testUpgradeability() public {
// Upgrade the proxy to a new version; ERC20TokenV2
Upgrades.upgradeProxy(
address(proxy),
"ERC20TokenV2.sol:ERC20TokenV2",
"",
owner.addr
);
}
}
49 changes: 0 additions & 49 deletions test/ERC20TokenV2Test.sol

This file was deleted.

29 changes: 10 additions & 19 deletions test/TokenFactoryV1Test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Utils.sol";
import {Upgrades} from "openzeppelin-foundry-upgrades/Upgrades.sol";
import {ERC20Token} from "../src/ERC20Token.sol";
import {ERC20TokenV2} from "../src/ERC20TokenV2.sol";
import {TokenFactoryV1} from "../src/TokenFactoryV1.sol";
import {TokenFactoryV2} from "../src/TokenFactoryV2.sol";

contract TokenFactoryV1Test is Test {
TokenFactoryV1 public factoryv1;
TokenFactoryV2 public factoryv2;
ERC20Token public myToken;
ERC20TokenV2 public myToken2;

ERC1967Proxy proxy;
Account public owner = makeAccount("owner");
Account public newOwner = makeAccount("newOwner");
Expand Down Expand Up @@ -75,21 +74,13 @@ contract TokenFactoryV1Test is Test {
vm.stopPrank();
}

// // 测试升级
// function testUpgradeability() public {
// // Upgrade the proxy to a new version; TokenFactoryV2
// address proxyAddress = address(proxy);
// TokenFactoryV2 implementationV2 = new TokenFactoryV2();

// address newImplementation = address(implementationV2);

// // Upgrade the proxy to the new implementation with arguments

// Upgrades.upgradeProxy(
// address(proxy),
// "TokenFactoryV2.sol:TokenFactoryV2",
// "",
// owner.addr
// );
// }
// 测试升级
function testUpgradeability() public {
Upgrades.upgradeProxy(
address(proxy),
"TokenFactoryV2.sol:TokenFactoryV2",
"",
owner.addr
);
}
}
16 changes: 8 additions & 8 deletions test/TokenFactoryV2Test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {Test, console} from "forge-std/Test.sol";
import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Utils.sol";
import {TokenFactoryV2} from "../src/TokenFactoryV2.sol";
import {ERC20TokenV2} from "../src/ERC20TokenV2.sol";
import {ERC20Token} from "../src/ERC20Token.sol";

contract CounterTest is Test {
TokenFactoryV2 public factoryv2;
ERC20TokenV2 public myToken2;
ERC20Token public myToken;
ERC1967Proxy proxy;
ERC1967Proxy proxy2;
Account public owner = makeAccount("owner");
Expand All @@ -23,24 +23,24 @@ contract CounterTest is Test {
function setUp() public {
// vm.startPrank(owner.addr);
// 部署实现
ERC20TokenV2 implementation = new ERC20TokenV2();
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, price)
(owner.addr, symbol, totalSupply, perMint)
)
);
// 用代理关联 MyToken 接口
myToken2 = ERC20TokenV2(address(proxy));
myToken = ERC20Token(address(proxy));

TokenFactoryV2 implementationV2 = new TokenFactoryV2();
proxy2 = new ERC1967Proxy(
address(implementationV2),
abi.encodeCall(
implementationV2.initialize,
(owner.addr, address(myToken2))
(owner.addr, address(myToken))
)
);
// 用代理关联 TokenFactoryV2 接口
Expand All @@ -60,7 +60,7 @@ contract CounterTest is Test {
address deployedTokenAddress = factoryv2.deployedTokens(0);
assertEq(factoryv2.tokenPrices(deployedTokenAddress), price);
// Create an instance of the deployed token contract
ERC20TokenV2 deployedToken = ERC20TokenV2(deployedTokenAddress);
ERC20Token deployedToken = ERC20Token(deployedTokenAddress);
console.log("Deployed token address:", deployedTokenAddress);
console.log("Deployed token:", address(deployedToken));
assertEq(address(deployedToken), deployedTokenAddress);
Expand All @@ -82,7 +82,7 @@ contract CounterTest is Test {
assertEq(factoryv2.size(), 1);
// Fetch the deployed token address
address deployedTokenAddress = factoryv2.deployedTokens(0);
ERC20TokenV2 deployedToken = ERC20TokenV2(deployedTokenAddress);
// ERC20Token deployedToken = ERC20Token(deployedTokenAddress);
vm.startPrank(user.addr);
vm.deal(user.addr, 1 ether);
factoryv2.mintInscription{value: price}(deployedTokenAddress);
Expand Down

0 comments on commit 31577c8

Please sign in to comment.