Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
qiaopengjun5162 committed Jul 29, 2024
1 parent 221bb9d commit bfa166c
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/TokenFactoryV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import "./ERC20Token.sol";

/// @custom:oz-upgrades-from TokenFactoryV1
contract TokenFactoryV2 is Initializable, OwnableUpgradeable, UUPSUpgradeable {
address myToken;
ERC20Token myToken;
ERC1967Proxy proxy;
address[] public deployedTokens;
mapping(address => uint) public tokenPrices;
Expand All @@ -27,8 +27,8 @@ contract TokenFactoryV2 is Initializable, OwnableUpgradeable, UUPSUpgradeable {
__UUPSUpgradeable_init();
}

function setLibraryAddress(address _libraryAddress) public onlyOwner {
myToken = _libraryAddress;
function setTokenAddress(address _tokenAddress) public onlyOwner {
myToken = ERC20Token(_tokenAddress);
}

function _authorizeUpgrade(
Expand Down Expand Up @@ -59,7 +59,7 @@ contract TokenFactoryV2 is Initializable, OwnableUpgradeable, UUPSUpgradeable {
console.log("deployInscription msg.sender, address:", msg.sender);
// 使用 Clones 库创建最小代理合约实例
// address proxyInstance = Clones.clone(address(implementation));
address newToken = Clones.clone(myToken);
address newToken = Clones.clone(address(myToken));
// address proxyInstance = createClone(implementation);

ERC20Token(newToken).initialize(
Expand Down
109 changes: 104 additions & 5 deletions test/TokenFactoryV1Test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ contract TokenFactoryV1Test is Test {
TokenFactoryV1 public factoryv1;
TokenFactoryV2 public factoryv2;
ERC20Token public myToken;
ERC20Token deployedToken;

ERC1967Proxy proxy;
Account public owner = makeAccount("owner");
Expand All @@ -22,8 +23,11 @@ contract TokenFactoryV1Test is Test {
string public symbol = "ETK";
uint public totalSupply = 1_000_000 ether;
uint public perMint = 10 ether;
uint public price = 10 ** 16; // 0.01 ETH in wei

function setUp() public {
myToken = new ERC20Token();
myToken.initialize(msg.sender, symbol, totalSupply, perMint);
// 部署实现
TokenFactoryV1 implementation = new TokenFactoryV1();
// Deploy the proxy and initialize the contract through the proxy
Expand All @@ -46,7 +50,7 @@ contract TokenFactoryV1Test is Test {
address deployedTokenAddress = factoryv1.deployedTokens(0);

// Create an instance of the deployed token contract
ERC20Token deployedToken = ERC20Token(deployedTokenAddress);
deployedToken = ERC20Token(deployedTokenAddress);

// Verify token initialization
assertEq(deployedToken.symbol(), symbol);
Expand All @@ -69,7 +73,7 @@ contract TokenFactoryV1Test is Test {
address deployedTokenAddress = factoryv1.deployedTokens(0);

// Create an instance of the deployed token contract
ERC20Token deployedToken = ERC20Token(deployedTokenAddress);
deployedToken = ERC20Token(deployedTokenAddress);

// Verify token initialization
assertEq(deployedToken.symbol(), symbol);
Expand All @@ -89,7 +93,7 @@ contract TokenFactoryV1Test is Test {
assertEq(factoryv1.size(), 1);
// Fetch the deployed token address
address deployedTokenAddress = factoryv1.deployedTokens(0);
ERC20Token deployedToken = ERC20Token(deployedTokenAddress);
deployedToken = ERC20Token(deployedTokenAddress);
vm.startPrank(user.addr);
factoryv1.mintInscription(deployedTokenAddress);
assertEq(deployedToken.balanceOf(user.addr), 10 ether);
Expand All @@ -107,7 +111,7 @@ contract TokenFactoryV1Test is Test {
assertEq(factoryv1.size(), 1);
// Fetch the deployed token address
address deployedTokenAddress = factoryv1.deployedTokens(0);
ERC20Token deployedToken = ERC20Token(deployedTokenAddress);
deployedToken = ERC20Token(deployedTokenAddress);

factoryv1.mintInscription(deployedTokenAddress);
assertEq(
Expand All @@ -130,5 +134,100 @@ contract TokenFactoryV1Test is Test {
);
}

function testV1Upgradeability() public {}
function testERC20Functionality() public {
vm.startPrank(user.addr);
factoryv1.deployInscription(symbol, totalSupply, perMint);
address deployedTokenAddress = factoryv1.deployedTokens(0);
deployedToken = ERC20Token(deployedTokenAddress);

factoryv1.mintInscription(deployedTokenAddress);
vm.stopPrank();
assertEq(deployedToken.balanceOf(user.addr), perMint);
}

function testVerifyUpgradeability() public {
testERC20Functionality();
vm.prank(owner.addr);
// TokenFactoryV2 factoryV2 = new TokenFactoryV2();
assertEq(deployedToken.balanceOf(user.addr), perMint); ///
// 升级代理合约
Upgrades.upgradeProxy(
address(proxy),
"TokenFactoryV2.sol:TokenFactoryV2",
"",
owner.addr
);
// TokenFactoryV2 factoryV2 = TokenFactoryV2(address(proxy));
factoryv2 = TokenFactoryV2(address(proxy));
console.log("Verify upgradeability");
vm.prank(owner.addr);
(bool s, ) = address(proxy).call(
abi.encodeWithSignature(
"setTokenAddress(address)",
address(myToken)
)
);
require(s);

// 验证新的功能
vm.startPrank(user.addr);
deal(user.addr, price);
(bool success, ) = address(proxy).call(
abi.encodeWithSelector(
factoryv2.deployInscription.selector,
symbol,
totalSupply,
perMint,
price
)
);
assertEq(success, true);

(bool su, bytes memory deployedTokenAddressBytes) = address(proxy).call(
abi.encodeWithSelector(factoryv2.deployedTokens.selector, 0)
);
assertEq(su, true);
address deployedTokenAddress = abi.decode(
deployedTokenAddressBytes,
(address)
);

console.log("deployedTokenAddress", deployedTokenAddress);
(bool sus, bytes memory deployedTokensLengthBytes) = address(proxy)
.call(abi.encodeWithSelector(factoryv2.size.selector));
assertEq(sus, true);
uint256 deployedTokensLength = abi.decode(
deployedTokensLengthBytes,
(uint256)
);
console.log("deployedTokensLength", deployedTokensLength);
assertEq(deployedTokensLength, 2);

(bool su2, bytes memory deployedTokenAddressBytes2) = address(proxy)
.call(abi.encodeWithSelector(factoryv2.deployedTokens.selector, 1));
assertEq(su2, true);
address deployedTokenAddress2 = abi.decode(
deployedTokenAddressBytes2,
(address)
);

assertNotEq(deployedTokenAddress, deployedTokenAddress2);

deployedToken = ERC20Token(deployedTokenAddress2);
(bool mintSuccess, ) = address(proxy).call{value: price}(
abi.encodeWithSignature(
"mintInscription(address)",
deployedTokenAddress2
)
);
require(mintSuccess, "Minting of token failed");

assertEq(factoryv2.tokenPrices(deployedTokenAddress), 0);
assertEq(factoryv2.tokenPrices(deployedTokenAddress2), price);

assertEq(deployedToken.balanceOf(user.addr), 10 ether);
assertEq(deployedToken.totalSupply(), perMint);
assertEq(deployedToken.totalSupplyToken(), totalSupply);
vm.stopPrank();
}
}
2 changes: 1 addition & 1 deletion test/TokenFactoryV2Test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ contract CounterTest is Test {
vm.prank(owner.addr);
(bool success, ) = address(proxy).call(
abi.encodeWithSelector(
factoryv2.setLibraryAddress.selector,
factoryv2.setTokenAddress.selector,
address(myToken)
)
);
Expand Down

0 comments on commit bfa166c

Please sign in to comment.