Skip to content

Commit 770ce0b

Browse files
feat: implement latest requirements - BigBank inherits Bank with min deposit limit and Admin contract with adminWithdraw functionality
- Add BigBank contract inheriting from Bank with 0.001 ether minimum deposit requirement - Add Admin contract with own owner and adminWithdraw(IBank bank) method - Admin contract calls IBank interface withdraw method to transfer funds - BigBank supports ownership transfer to Admin contract - Add comprehensive workflow tests in NewWorkflow.t.sol - Update Bank contract with virtual functions for inheritance - Add deployment script for latest requirements (DeployLatestRequirements.s.sol) - Update README with complete project structure and workflow documentation - Fix fuzz test for ownership transfer edge cases - Enable via_ir compiler option in foundry.toml for complex inheritance - Add Makefile commands for workflow testing Architecture: IBank (interface) -> Bank (implements IBank) -> BigBank (inherits Bank + min deposit) -> Admin (manages BigBank) All tests passing: 41/41 ✅
1 parent 82841a6 commit 770ce0b

9 files changed

Lines changed: 836 additions & 21 deletions

File tree

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ test-unit: ## Run unit tests only
3030
test-fuzz: ## Run fuzz tests only
3131
forge test --match-contract BankFuzzTest
3232

33+
test-workflow: ## Run complete workflow test
34+
forge test --match-contract CompleteWorkflowTest -vv
35+
3336
test-coverage: ## Run tests with coverage report
3437
forge coverage
3538

@@ -48,6 +51,10 @@ deploy-sepolia: ## Deploy to Sepolia testnet
4851
@echo "Deploying to Sepolia..."
4952
forge script script/Deploy.s.sol --rpc-url $(SEPOLIA_RPC_URL) --broadcast --verify
5053

54+
deploy-complete: ## Deploy complete workflow (BigBank + Admin)
55+
@echo "Deploying complete workflow..."
56+
forge script script/DeployCompleteWorkflow.s.sol --rpc-url http://localhost:8545 --broadcast --private-key $(PRIVATE_KEY)
57+
5158
# Interactions
5259
deposit: ## Make a deposit (requires BANK_CONTRACT_ADDRESS and DEPOSIT_AMOUNT env vars)
5360
forge script script/interactions/Deposit.s.sol --rpc-url $(RPC_URL) --broadcast

README.md

Lines changed: 183 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,26 @@
44

55
## 功能特性
66

7+
### Bank合约 (基础版)
78
-**存款功能**: 用户可以通过Metamask等钱包直接向合约地址发送ETH进行存款
89
-**余额记录**: 合约记录每个地址的累计存款金额
910
-**管理员提取**: 仅合约所有者可以提取合约中的资金
1011
-**排行榜**: 实时维护存款金额前3名的用户列表
1112
-**安全保护**: 包含重入攻击防护和访问控制
1213

14+
### BigBank合约 (增强版)
15+
-**最小存款限制**: 仅支持 ≥0.001 ether 的存款,使用modifier权限控制
16+
-**继承Bank功能**: 完全兼容Bank合约的所有功能
17+
-**Admin合约管理**: 支持Admin合约调用withdraw()方法
18+
-**双重管理权限**: 合约owner和Admin合约都可以提取资金
19+
20+
### Admin合约 (管理系统)
21+
-**独立所有权**: Admin合约有自己的Owner
22+
-**BigBank管理**: 可以接收BigBank合约的管理权并操作
23+
-**adminWithdraw功能**: 实现adminWithdraw(IBank bank)方法
24+
-**IBank接口调用**: 通过IBank接口调用withdraw方法
25+
-**资金转移**: 将Bank合约资金转移到Admin合约地址
26+
1327
## 技术栈
1428

1529
- **Solidity**: ^0.8.19
@@ -22,20 +36,26 @@
2236
```
2337
bank-foundry/
2438
├── src/
25-
│ ├── Bank.sol # 主合约
39+
│ ├── Bank.sol # 基础Bank合约(实现IBank接口)
40+
│ ├── BigBank.sol # 增强版BigBank合约(继承Bank + 最小存款限制)
41+
│ ├── Admin.sol # Admin管理合约(adminWithdraw功能)
2642
│ └── interfaces/
27-
│ └── IBank.sol # 合约接口
43+
│ └── IBank.sol # Bank合约接口定义
2844
├── test/
29-
│ ├── Bank.t.sol # 单元测试
30-
│ ├── BankFuzz.t.sol # 模糊测试
45+
│ ├── Bank.t.sol # Bank合约单元测试
46+
│ ├── BankFuzz.t.sol # Bank合约模糊测试
47+
│ ├── NewWorkflow.t.sol # 完整工作流程测试
3148
│ └── utils/
3249
│ └── TestHelper.sol # 测试辅助工具
3350
├── script/
34-
│ ├── Deploy.s.sol # 部署脚本
51+
│ ├── Deploy.s.sol # Bank合约部署脚本
52+
│ ├── DeployLatestRequirements.s.sol # 最新需求部署脚本
3553
│ └── interactions/
3654
│ ├── Deposit.s.sol # 存款交互脚本
3755
│ └── Withdraw.s.sol # 提取交互脚本
3856
├── foundry.toml # Foundry配置
57+
├── Makefile # 构建和部署工具
58+
├── GITHUB_SETUP.md # GitHub集成指南
3959
└── README.md # 项目文档
4060
```
4161

@@ -74,11 +94,20 @@ forge test --match-contract BankTest
7494
# 运行模糊测试
7595
forge test --match-contract BankFuzzTest
7696

97+
# 运行完整工作流程测试
98+
forge test --match-contract NewWorkflowTest -vv
99+
77100
# 查看测试覆盖率
78101
forge coverage
79102

80103
# 查看Gas报告
81104
forge test --gas-report
105+
106+
# 使用Makefile命令 (推荐)
107+
make test # 运行所有测试
108+
make test-workflow # 运行工作流程测试
109+
make build # 编译合约
110+
make deploy-local # 本地部署
82111
```
83112

84113
### 4. 本地部署测试
@@ -87,8 +116,11 @@ forge test --gas-report
87116
# 启动本地测试网络
88117
anvil
89118

90-
# 在新终端中部署合约
119+
# 在新终端中部署Bank合约
91120
forge script script/Deploy.s.sol --rpc-url http://localhost:8545 --broadcast --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
121+
122+
# 或部署最新需求的完整合约组合(BigBank + Admin)
123+
forge script script/DeployLatestRequirements.s.sol --rpc-url http://localhost:8545 --broadcast --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
92124
```
93125

94126
### 5. 环境配置
@@ -125,6 +157,55 @@ forge script script/Deploy.s.sol --rpc-url $SEPOLIA_RPC_URL --broadcast --verify
125157
forge verify-contract <CONTRACT_ADDRESS> src/Bank.sol:Bank --etherscan-api-key $ETHERSCAN_API_KEY --chain sepolia
126158
```
127159

160+
## 最新需求工作流程
161+
162+
### 完整的BigBank + Admin工作流程
163+
164+
按照最新需求,项目实现了以下架构:
165+
166+
1. **Bank合约** - 实现IBank接口的基础合约
167+
2. **BigBank合约** - 继承Bank,添加≥0.001 ether最小存款限制
168+
3. **Admin合约** - 有自己的Owner,实现adminWithdraw(IBank bank)功能
169+
170+
#### 工作流程步骤:
171+
172+
```bash
173+
# 1. 部署所有合约
174+
forge script script/DeployLatestRequirements.s.sol --rpc-url http://localhost:8545 --broadcast
175+
176+
# 2. BigBank合约管理权自动转移给Admin合约(部署脚本中完成)
177+
178+
# 3. 用户向BigBank存款(必须≥0.001 ether)
179+
# 可以通过直接转账或调用deposit()函数
180+
181+
# 4. Admin合约Owner调用adminWithdraw()提取BigBank资金
182+
# Admin合约通过IBank接口调用BigBank的withdraw()方法
183+
# 资金被转移到Admin合约地址
184+
185+
# 5. Admin Owner可以进一步将资金提取到个人地址
186+
```
187+
188+
#### 架构关系图:
189+
190+
```
191+
IBank (interface)
192+
193+
Bank (implements IBank)
194+
195+
BigBank (inherits Bank + MIN_DEPOSIT = 0.001 ether)
196+
↓ (transferOwnership)
197+
Admin (owner + adminWithdraw(IBank bank))
198+
```
199+
200+
#### 核心特性验证:
201+
202+
- ✅ Bank实现IBank接口
203+
- ✅ BigBank继承Bank并添加最小存款限制
204+
- ✅ BigBank支持管理权转移
205+
- ✅ Admin有独立的Owner
206+
- ✅ Admin通过IBank接口调用withdraw方法
207+
- ✅ 完整的工作流程测试通过
208+
128209
## 合约交互
129210

130211
### 存款操作
@@ -150,30 +231,60 @@ forge script script/interactions/Withdraw.s.sol --rpc-url $SEPOLIA_RPC_URL --bro
150231

151232
## 合约API
152233

153-
### 主要函数
234+
### Bank合约 (IBank接口)
154235

155236
- `deposit()`: 存款函数
156237
- `withdraw(uint256 amount)`: 管理员提取函数
157238
- `getBalance(address user)`: 查询用户余额
158239
- `getTopDepositors()`: 获取前3名存款用户
159240
- `getTotalDeposits()`: 获取总存款额
160241
- `getContractBalance()`: 获取合约余额
242+
- `transferOwnership(address newOwner)`: 转移所有权
243+
244+
### BigBank合约 (继承Bank)
245+
246+
- `MIN_DEPOSIT`: 最小存款常量(0.001 ether)
247+
- `deposit()`: 存款函数(增强版,带最小金额限制)
248+
- `getMinDeposit()`: 获取最小存款要求
249+
- `meetsMinDeposit(uint256 amount)`: 检查金额是否满足最小存款要求
250+
- `transferOwnership(address newOwner)`: 转移所有权(可转给Admin合约)
251+
252+
### Admin合约
253+
254+
- `adminWithdraw(IBank bank)`: 从指定Bank合约提取所有资金
255+
- `adminWithdrawAmount(IBank bank, uint256 amount)`: 从Bank合约提取指定金额
256+
- `withdrawToOwner(uint256 amount)`: 将Admin合约资金提取给Owner
257+
- `withdrawAllToOwner()`: 提取Admin合约所有资金给Owner
258+
- `transferOwnership(address newOwner)`: 转移Admin合约所有权
259+
- `getAdminInfo()`: 获取Admin合约信息
260+
- `getBankInfo(IBank bank)`: 获取指定Bank合约信息
161261

162262
### 事件
163263

164264
- `Deposit(address indexed user, uint256 amount, uint256 newBalance)`
165265
- `Withdraw(address indexed admin, uint256 amount)`
166266
- `TopDepositorsUpdated(address[3] topUsers, uint256[3] topAmounts)`
267+
- `OwnershipTransferred(address indexed previousOwner, address indexed newOwner)`
268+
- `AdminWithdrawal(address indexed bankContract, uint256 amount, address indexed owner)`
269+
- `FundsReceived(address indexed from, uint256 amount)`
167270

168271
## 安全特性
169272

170-
1. **访问控制**: 只有合约所有者可以提取资金
171-
2. **重入保护**: 防止重入攻击
172-
3. **输入验证**: 验证存款和提取金额
273+
1. **访问控制**:
274+
- Bank/BigBank: 只有合约所有者可以提取资金
275+
- Admin: 只有Admin Owner可以操作
276+
2. **重入保护**: 所有资金操作都使用nonReentrant修饰符
277+
3. **输入验证**:
278+
- 验证存款和提取金额
279+
- BigBank强制最小存款限制
280+
- 地址零值检查
173281
4. **溢出保护**: Solidity 0.8+内置溢出保护
282+
5. **接口隔离**: Admin通过IBank接口操作,降低耦合
283+
6. **权限分离**: BigBank管理权可转移给Admin合约
174284

175285
## 测试覆盖
176286

287+
### Bank合约测试
177288
- ✅ 基本存款和提取功能
178289
- ✅ 排行榜更新逻辑
179290
- ✅ 权限控制测试
@@ -182,6 +293,25 @@ forge script script/interactions/Withdraw.s.sol --rpc-url $SEPOLIA_RPC_URL --bro
182293
- ✅ Gas优化测试
183294
- ✅ 安全性测试
184295

296+
### BigBank合约测试
297+
- ✅ 继承Bank功能验证
298+
- ✅ 最小存款限制测试
299+
- ✅ modifier权限控制测试
300+
- ✅ 管理权转移测试
301+
302+
### Admin合约测试
303+
- ✅ adminWithdraw功能测试
304+
- ✅ IBank接口调用测试
305+
- ✅ 权限控制测试
306+
- ✅ 资金转移测试
307+
308+
### 完整工作流程测试
309+
- ✅ Bank实现IBank接口验证
310+
- ✅ BigBank继承Bank验证
311+
- ✅ 端到端工作流程测试
312+
- ✅ 错误场景测试
313+
- ✅ 架构关系验证
314+
185315
## Gas优化
186316

187317
合约已经过Gas优化:
@@ -193,17 +323,37 @@ forge script script/interactions/Withdraw.s.sol --rpc-url $SEPOLIA_RPC_URL --bro
193323

194324
### 添加新功能
195325

326+
#### 扩展Bank功能
196327
1.`src/Bank.sol` 中实现新功能
197-
2.`src/interfaces/IBank.sol` 中添加接口
328+
2.`src/interfaces/IBank.sol` 中添加接口(如需要)
198329
3.`test/Bank.t.sol` 中添加测试
199-
4. 运行测试确保功能正常
330+
4. 如果BigBank需要继承新功能,更新 `src/BigBank.sol`
331+
332+
#### 扩展BigBank功能
333+
1.`src/BigBank.sol` 中实现BigBank特有功能
334+
2.`test/NewWorkflow.t.sol` 中添加相关测试
335+
3. 确保最小存款限制得到正确应用
336+
337+
#### 扩展Admin功能
338+
1.`src/Admin.sol` 中实现新的管理功能
339+
2. 确保通过IBank接口进行Bank合约操作
340+
3.`test/NewWorkflow.t.sol` 中添加相关测试
341+
342+
### 测试策略
343+
344+
1. **单元测试**: 测试单个合约的功能
345+
2. **集成测试**: 测试合约间的交互
346+
3. **工作流程测试**: 测试完整的业务流程
347+
4. **模糊测试**: 测试边界条件和异常情况
200348

201349
### 提交流程
202350

203351
1. 运行所有测试: `forge test`
204-
2. 检查代码覆盖率: `forge coverage`
205-
3. 检查Gas使用: `forge test --gas-report`
206-
4. 提交代码
352+
2. 运行工作流程测试: `forge test --match-contract NewWorkflowTest -vv`
353+
3. 检查代码覆盖率: `forge coverage`
354+
4. 检查Gas使用: `forge test --gas-report`
355+
5. 确保编译无警告: `forge build`
356+
6. 提交代码
207357

208358
## 许可证
209359

@@ -219,4 +369,21 @@ MIT License
219369
- 这是一个演示项目,不建议在主网上使用真实资金
220370
- 部署前请进行充分的安全审计
221371
- 私钥管理请遵循最佳实践
222-
- 测试网部署建议使用测试ETH
372+
- 测试网部署建议使用测试ETH
373+
374+
### 最新需求相关注意事项
375+
376+
1. **BigBank最小存款限制**: 所有存款必须≥0.001 ether,否则交易会失败
377+
2. **管理权转移**: BigBank部署后应将管理权转移给Admin合约
378+
3. **Admin操作**: Admin合约通过IBank接口操作,确保了合约间的松耦合
379+
4. **测试环境**: 建议使用NewWorkflow测试验证完整工作流程
380+
5. **合约升级**: 如需修改需求,建议重新部署而非尝试升级合约
381+
382+
### 部署顺序
383+
384+
推荐的部署顺序:
385+
1. 部署Bank合约(可选,用于对比)
386+
2. 部署BigBank合约
387+
3. 部署Admin合约
388+
4. 将BigBank管理权转移给Admin合约
389+
5. 测试完整工作流程

foundry.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ solc = "0.8.19"
66
optimizer = true
77
optimizer_runs = 200
88
gas_reports = ["*"]
9+
via_ir = true
910

1011
[profile.ci]
1112
fuzz = { runs = 10000 }
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.19;
3+
4+
import "forge-std/Script.sol";
5+
import "../src/Bank.sol";
6+
import "../src/BigBank.sol";
7+
import "../src/Admin.sol";
8+
import "../src/interfaces/IBank.sol";
9+
10+
/// @title Deploy Latest Requirements Script
11+
/// @notice Deploys contracts according to latest requirements
12+
contract DeployLatestRequirementsScript is Script {
13+
function run() external {
14+
// Get deployment parameters
15+
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
16+
address deployer = vm.addr(deployerPrivateKey);
17+
18+
// Get admin owner address (can be same as deployer for demo)
19+
address adminOwner;
20+
try vm.envAddress("ADMIN_OWNER_ADDRESS") returns (address addr) {
21+
adminOwner = addr;
22+
} catch {
23+
adminOwner = deployer;
24+
}
25+
26+
vm.startBroadcast(deployerPrivateKey);
27+
28+
// Step 1: Deploy contracts
29+
Bank bank = new Bank();
30+
BigBank bigBank = new BigBank();
31+
Admin admin = new Admin(adminOwner);
32+
33+
// Step 2: Transfer BigBank ownership to Admin contract
34+
bigBank.transferOwnership(address(admin));
35+
36+
vm.stopBroadcast();
37+
38+
// Verify deployments
39+
require(bank.owner() == deployer, "Bank owner incorrect");
40+
require(bigBank.owner() == address(admin), "BigBank owner incorrect");
41+
require(admin.owner() == adminOwner, "Admin owner incorrect");
42+
43+
// Log deployment information
44+
// Removed console.log to avoid compilation issues
45+
}
46+
}

0 commit comments

Comments
 (0)