Skip to content

Commit b2657cf

Browse files
committed
test: integration tests for TokenFactory deploy flow
1 parent 81ac690 commit b2657cf

4 files changed

Lines changed: 86 additions & 5 deletions

File tree

src/Token.sol

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ error ZeroAddress();
77
contract Token {
88
mapping(address => uint256) public balanceOf;
99
uint256 public totalSupply;
10+
string public name;
11+
string public symbol;
12+
uint8 public constant decimals = 18;
1013

1114
event Transfer(address indexed from, address indexed to, uint256 amount);
1215

13-
constructor(uint256 initialSupply) {
14-
balanceOf[msg.sender] = initialSupply;
16+
constructor(address owner, string memory _name, string memory _symbol, uint256 initialSupply) {
17+
if (owner == address(0)) revert ZeroAddress();
18+
name = _name;
19+
symbol = _symbol;
20+
balanceOf[owner] = initialSupply;
1521
totalSupply = initialSupply;
16-
emit Transfer(address(0), msg.sender, initialSupply);
22+
emit Transfer(address(0), owner, initialSupply);
1723
}
1824

1925
function transfer(address to, uint256 amount) external returns (bool) {

src/TokenFactory.sol

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.20;
33

4-
contract TokenFactory {}
4+
import {Token} from "./Token.sol";
5+
6+
contract TokenFactory {
7+
address[] public tokens;
8+
9+
event TokenDeployed(address indexed token, address indexed owner, string name, string symbol);
10+
11+
function deployToken(string memory _name, string memory _symbol, uint256 initialSupply) external returns (address) {
12+
Token token = new Token(msg.sender, _name, _symbol, initialSupply);
13+
tokens.push(address(token));
14+
emit TokenDeployed(address(token), msg.sender, _name, _symbol);
15+
return address(token);
16+
}
17+
18+
function tokensLength() external view returns (uint256) {
19+
return tokens.length;
20+
}
21+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.20;
3+
4+
import "forge-std/Test.sol";
5+
import {TokenFactory} from "../../src/TokenFactory.sol";
6+
import {Token} from "../../src/Token.sol";
7+
8+
contract TokenFactoryIntegrationTest is Test {
9+
TokenFactory factory;
10+
address alice = makeAddr("alice");
11+
address bob = makeAddr("bob");
12+
address carol = makeAddr("carol");
13+
14+
function setUp() public {
15+
factory = new TokenFactory();
16+
}
17+
18+
function testFactoryDeployToken() public {
19+
vm.prank(alice);
20+
address tokenA = factory.deployToken("TokenA", "TKA", 1000);
21+
Token token = Token(tokenA);
22+
assertEq(factory.tokensLength(), 1);
23+
assertEq(factory.tokens(0), tokenA);
24+
assertTrue(tokenA.code.length > 0);
25+
assertEq(token.name(), "TokenA");
26+
assertEq(token.symbol(), "TKA");
27+
assertEq(token.balanceOf(alice), 1000);
28+
assertEq(token.totalSupply(), 1000);
29+
}
30+
31+
function testFactoryDeploysUniqueTokenAddresses() public {
32+
vm.prank(alice);
33+
address tokenA = factory.deployToken("TokenA", "TKA", 1000);
34+
vm.prank(bob);
35+
address tokenB = factory.deployToken("TokenB", "TKB", 1000);
36+
assertEq(factory.tokensLength(), 2);
37+
assertEq(factory.tokens(0), tokenA);
38+
assertEq(factory.tokens(1), tokenB);
39+
assertTrue(tokenA != tokenB);
40+
}
41+
42+
function testDeployedTokensKeepIndependentState() public {
43+
vm.prank(alice);
44+
address tokenA = factory.deployToken("TokenA", "TKA", 1000);
45+
vm.prank(bob);
46+
address tokenB = factory.deployToken("TokenB", "TKB", 2000);
47+
Token token1 = Token(tokenA);
48+
Token token2 = Token(tokenB);
49+
assertEq(token1.name(), "TokenA");
50+
assertEq(token2.name(), "TokenB");
51+
assertEq(token1.symbol(), "TKA");
52+
assertEq(token2.symbol(), "TKB");
53+
assertEq(token1.balanceOf(alice), 1000);
54+
assertEq(token2.balanceOf(bob), 2000);
55+
assertEq(token1.totalSupply(), 1000);
56+
assertEq(token2.totalSupply(), 2000);
57+
}
58+
}

test/Unit/Token.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ contract TokenTest is Test {
88
Token token;
99

1010
function setUp() public {
11-
token = new Token(1000);
11+
token = new Token(address(this), "TestToken", "TT", 1000);
1212
}
1313

1414
function testTransferSuccess() public {

0 commit comments

Comments
 (0)