@zimezone/z-command 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (182) hide show
  1. package/package.json +4 -1
  2. package/templates/agents/api-documenter.agent.md +161 -0
  3. package/templates/agents/architect-review.agent.md +146 -0
  4. package/templates/agents/arm-cortex-expert.agent.md +288 -0
  5. package/templates/agents/backend-architect.agent.md +309 -0
  6. package/templates/agents/backend-security-coder.agent.md +152 -0
  7. package/templates/agents/bash-pro.agent.md +285 -0
  8. package/templates/agents/c-pro.agent.md +35 -0
  9. package/templates/agents/c4-code.agent.md +320 -0
  10. package/templates/agents/c4-component.agent.md +227 -0
  11. package/templates/agents/c4-container.agent.md +248 -0
  12. package/templates/agents/c4-context.agent.md +235 -0
  13. package/templates/agents/conductor-validator.agent.md +245 -0
  14. package/templates/agents/csharp-pro.agent.md +38 -0
  15. package/templates/agents/customer-support.agent.md +148 -0
  16. package/templates/agents/database-admin.agent.md +142 -0
  17. package/templates/agents/database-architect.agent.md +238 -0
  18. package/templates/agents/database-optimizer.agent.md +144 -0
  19. package/templates/agents/debugger.agent.md +30 -0
  20. package/templates/agents/deployment-engineer.agent.md +0 -0
  21. package/templates/agents/devops-troubleshooter.agent.md +138 -0
  22. package/templates/agents/django-pro.agent.md +159 -0
  23. package/templates/agents/docs-architect.agent.md +77 -0
  24. package/templates/agents/dotnet-architect.agent.md +175 -0
  25. package/templates/agents/dx-optimizer.agent.md +63 -0
  26. package/templates/agents/elixir-pro.agent.md +38 -0
  27. package/templates/agents/error-detective.agent.md +32 -0
  28. package/templates/agents/event-sourcing-architect.agent.md +42 -0
  29. package/templates/agents/fastapi-pro.agent.md +171 -0
  30. package/templates/agents/firmware-analyst.agent.md +330 -0
  31. package/templates/agents/frontend-security-coder.agent.md +149 -0
  32. package/templates/agents/haskell-pro.agent.md +37 -0
  33. package/templates/agents/hr-pro.agent.md +105 -0
  34. package/templates/agents/incident-responder.agent.md +190 -0
  35. package/templates/agents/ios-developer.agent.md +198 -0
  36. package/templates/agents/java-pro.agent.md +156 -0
  37. package/templates/agents/javascript-pro.agent.md +35 -0
  38. package/templates/agents/julia-pro.agent.md +187 -0
  39. package/templates/agents/legal-advisor.agent.md +49 -0
  40. package/templates/agents/malware-analyst.agent.md +272 -0
  41. package/templates/agents/mermaid-expert.agent.md +39 -0
  42. package/templates/agents/minecraft-bukkit-pro.agent.md +104 -0
  43. package/templates/agents/mobile-security-coder.agent.md +163 -0
  44. package/templates/agents/monorepo-architect.agent.md +44 -0
  45. package/templates/agents/observability-engineer.agent.md +228 -0
  46. package/templates/agents/performance-engineer.agent.md +167 -0
  47. package/templates/agents/php-pro.agent.md +43 -0
  48. package/templates/agents/posix-shell-pro.agent.md +284 -0
  49. package/templates/agents/quant-analyst.agent.md +32 -0
  50. package/templates/agents/reference-builder.agent.md +167 -0
  51. package/templates/agents/reverse-engineer.agent.md +202 -0
  52. package/templates/agents/risk-manager.agent.md +41 -0
  53. package/templates/agents/ruby-pro.agent.md +35 -0
  54. package/templates/agents/rust-pro.agent.md +156 -0
  55. package/templates/agents/sales-automator.agent.md +35 -0
  56. package/templates/agents/scala-pro.agent.md +60 -0
  57. package/templates/agents/search-specialist.agent.md +59 -0
  58. package/templates/agents/security-auditor.agent.md +138 -0
  59. package/templates/agents/seo-authority-builder.agent.md +116 -0
  60. package/templates/agents/seo-cannibalization-detector.agent.md +103 -0
  61. package/templates/agents/seo-content-auditor.agent.md +63 -0
  62. package/templates/agents/seo-content-planner.agent.md +88 -0
  63. package/templates/agents/seo-content-refresher.agent.md +98 -0
  64. package/templates/agents/seo-content-writer.agent.md +76 -0
  65. package/templates/agents/seo-keyword-strategist.agent.md +75 -0
  66. package/templates/agents/seo-meta-optimizer.agent.md +72 -0
  67. package/templates/agents/seo-snippet-hunter.agent.md +94 -0
  68. package/templates/agents/seo-structure-architect.agent.md +88 -0
  69. package/templates/agents/service-mesh-expert.agent.md +41 -0
  70. package/templates/agents/sql-pro.agent.md +146 -0
  71. package/templates/agents/tdd-orchestrator.agent.md +183 -0
  72. package/templates/agents/temporal-python-pro.agent.md +349 -0
  73. package/templates/agents/terraform-specialist.agent.md +137 -0
  74. package/templates/agents/test-automator.agent.md +203 -0
  75. package/templates/agents/threat-modeling-expert.agent.md +44 -0
  76. package/templates/agents/tutorial-engineer.agent.md +118 -0
  77. package/templates/agents/ui-ux-designer.agent.md +188 -0
  78. package/templates/agents/ui-visual-validator.agent.md +192 -0
  79. package/templates/agents/vector-database-engineer.agent.md +43 -0
  80. package/templates/skills/angular-migration/SKILL.md +410 -0
  81. package/templates/skills/api-design-principles/SKILL.md +528 -0
  82. package/templates/skills/api-design-principles/assets/api-design-checklist.md +155 -0
  83. package/templates/skills/api-design-principles/assets/rest-api-template.py +182 -0
  84. package/templates/skills/api-design-principles/references/graphql-schema-design.md +583 -0
  85. package/templates/skills/api-design-principles/references/rest-best-practices.md +408 -0
  86. package/templates/skills/architecture-decision-records/SKILL.md +428 -0
  87. package/templates/skills/architecture-patterns/SKILL.md +494 -0
  88. package/templates/skills/async-python-patterns/SKILL.md +694 -0
  89. package/templates/skills/auth-implementation-patterns/SKILL.md +634 -0
  90. package/templates/skills/changelog-automation/SKILL.md +552 -0
  91. package/templates/skills/code-review-excellence/SKILL.md +520 -0
  92. package/templates/skills/competitive-landscape/SKILL.md +479 -0
  93. package/templates/skills/context-driven-development/SKILL.md +385 -0
  94. package/templates/skills/cost-optimization/SKILL.md +274 -0
  95. package/templates/skills/cqrs-implementation/SKILL.md +554 -0
  96. package/templates/skills/data-quality-frameworks/SKILL.md +587 -0
  97. package/templates/skills/data-storytelling/SKILL.md +453 -0
  98. package/templates/skills/database-migration/SKILL.md +424 -0
  99. package/templates/skills/dbt-transformation-patterns/SKILL.md +561 -0
  100. package/templates/skills/debugging-strategies/SKILL.md +527 -0
  101. package/templates/skills/defi-protocol-templates/SKILL.md +454 -0
  102. package/templates/skills/dependency-upgrade/SKILL.md +409 -0
  103. package/templates/skills/deployment-pipeline-design/SKILL.md +359 -0
  104. package/templates/skills/distributed-tracing/SKILL.md +438 -0
  105. package/templates/skills/dotnet-backend-patterns/SKILL.md +815 -0
  106. package/templates/skills/dotnet-backend-patterns/assets/repository-template.cs +523 -0
  107. package/templates/skills/dotnet-backend-patterns/assets/service-template.cs +336 -0
  108. package/templates/skills/dotnet-backend-patterns/references/dapper-patterns.md +544 -0
  109. package/templates/skills/dotnet-backend-patterns/references/ef-core-best-practices.md +355 -0
  110. package/templates/skills/e2e-testing-patterns/SKILL.md +547 -0
  111. package/templates/skills/employment-contract-templates/SKILL.md +507 -0
  112. package/templates/skills/error-handling-patterns/SKILL.md +636 -0
  113. package/templates/skills/event-store-design/SKILL.md +437 -0
  114. package/templates/skills/fastapi-templates/SKILL.md +567 -0
  115. package/templates/skills/git-advanced-workflows/SKILL.md +400 -0
  116. package/templates/skills/github-actions-templates/SKILL.md +333 -0
  117. package/templates/skills/go-concurrency-patterns/SKILL.md +655 -0
  118. package/templates/skills/grafana-dashboards/SKILL.md +369 -0
  119. package/templates/skills/helm-chart-scaffolding/SKILL.md +544 -0
  120. package/templates/skills/helm-chart-scaffolding/assets/Chart.yaml.template +42 -0
  121. package/templates/skills/helm-chart-scaffolding/assets/values.yaml.template +185 -0
  122. package/templates/skills/helm-chart-scaffolding/references/chart-structure.md +500 -0
  123. package/templates/skills/helm-chart-scaffolding/scripts/validate-chart.sh +244 -0
  124. package/templates/skills/javascript-testing-patterns/SKILL.md +1025 -0
  125. package/templates/skills/langchain-architecture/SKILL.md +338 -0
  126. package/templates/skills/llm-evaluation/SKILL.md +471 -0
  127. package/templates/skills/microservices-patterns/SKILL.md +595 -0
  128. package/templates/skills/modern-javascript-patterns/SKILL.md +911 -0
  129. package/templates/skills/monorepo-management/SKILL.md +622 -0
  130. package/templates/skills/nextjs-app-router-patterns/SKILL.md +544 -0
  131. package/templates/skills/nodejs-backend-patterns/SKILL.md +1020 -0
  132. package/templates/skills/nx-workspace-patterns/SKILL.md +452 -0
  133. package/templates/skills/openapi-spec-generation/SKILL.md +1028 -0
  134. package/templates/skills/paypal-integration/SKILL.md +467 -0
  135. package/templates/skills/pci-compliance/SKILL.md +466 -0
  136. package/templates/skills/postgresql/SKILL.md +204 -0
  137. package/templates/skills/projection-patterns/SKILL.md +490 -0
  138. package/templates/skills/prometheus-configuration/SKILL.md +392 -0
  139. package/templates/skills/prompt-engineering-patterns/SKILL.md +201 -0
  140. package/templates/skills/prompt-engineering-patterns/assets/few-shot-examples.json +106 -0
  141. package/templates/skills/prompt-engineering-patterns/assets/prompt-template-library.md +246 -0
  142. package/templates/skills/prompt-engineering-patterns/references/chain-of-thought.md +399 -0
  143. package/templates/skills/prompt-engineering-patterns/references/few-shot-learning.md +369 -0
  144. package/templates/skills/prompt-engineering-patterns/references/prompt-optimization.md +414 -0
  145. package/templates/skills/prompt-engineering-patterns/references/prompt-templates.md +470 -0
  146. package/templates/skills/prompt-engineering-patterns/references/system-prompts.md +189 -0
  147. package/templates/skills/prompt-engineering-patterns/scripts/optimize-prompt.py +279 -0
  148. package/templates/skills/python-packaging/SKILL.md +870 -0
  149. package/templates/skills/python-performance-optimization/SKILL.md +869 -0
  150. package/templates/skills/python-testing-patterns/SKILL.md +907 -0
  151. package/templates/skills/rag-implementation/SKILL.md +403 -0
  152. package/templates/skills/react-modernization/SKILL.md +513 -0
  153. package/templates/skills/react-native-architecture/SKILL.md +671 -0
  154. package/templates/skills/react-state-management/SKILL.md +429 -0
  155. package/templates/skills/risk-metrics-calculation/SKILL.md +555 -0
  156. package/templates/skills/rust-async-patterns/SKILL.md +517 -0
  157. package/templates/skills/secrets-management/SKILL.md +346 -0
  158. package/templates/skills/security-requirement-extraction/SKILL.md +677 -0
  159. package/templates/skills/shellcheck-configuration/SKILL.md +454 -0
  160. package/templates/skills/similarity-search-patterns/SKILL.md +558 -0
  161. package/templates/skills/slo-implementation/SKILL.md +329 -0
  162. package/templates/skills/sql-optimization-patterns/SKILL.md +493 -0
  163. package/templates/skills/stripe-integration/SKILL.md +442 -0
  164. package/templates/skills/tailwind-design-system/SKILL.md +666 -0
  165. package/templates/skills/temporal-python-testing/SKILL.md +158 -0
  166. package/templates/skills/temporal-python-testing/resources/integration-testing.md +455 -0
  167. package/templates/skills/temporal-python-testing/resources/local-setup.md +553 -0
  168. package/templates/skills/temporal-python-testing/resources/replay-testing.md +462 -0
  169. package/templates/skills/temporal-python-testing/resources/unit-testing.md +328 -0
  170. package/templates/skills/terraform-module-library/SKILL.md +249 -0
  171. package/templates/skills/terraform-module-library/references/aws-modules.md +63 -0
  172. package/templates/skills/threat-mitigation-mapping/SKILL.md +745 -0
  173. package/templates/skills/track-management/SKILL.md +593 -0
  174. package/templates/skills/typescript-advanced-types/SKILL.md +717 -0
  175. package/templates/skills/uv-package-manager/SKILL.md +831 -0
  176. package/templates/skills/vector-index-tuning/SKILL.md +521 -0
  177. package/templates/skills/wcag-audit-patterns/SKILL.md +555 -0
  178. package/templates/skills/workflow-orchestration-patterns/SKILL.md +316 -0
  179. package/templates/skills/workflow-patterns/SKILL.md +623 -0
  180. package/templates/agents/game-developer.agent.md +0 -57
  181. package/templates/agents/kubernetes-specialist.agent.md +0 -56
  182. package/templates/agents/market-researcher.agent.md +0 -47
@@ -0,0 +1,454 @@
1
+ ---
2
+ name: defi-protocol-templates
3
+ description: Implement DeFi protocols with production-ready templates for staking, AMMs, governance, and lending systems. Use when building decentralized finance applications or smart contract protocols.
4
+ ---
5
+
6
+ # DeFi Protocol Templates
7
+
8
+ Production-ready templates for common DeFi protocols including staking, AMMs, governance, lending, and flash loans.
9
+
10
+ ## When to Use This Skill
11
+
12
+ - Building staking platforms with reward distribution
13
+ - Implementing AMM (Automated Market Maker) protocols
14
+ - Creating governance token systems
15
+ - Developing lending/borrowing protocols
16
+ - Integrating flash loan functionality
17
+ - Launching yield farming platforms
18
+
19
+ ## Staking Contract
20
+
21
+ ```solidity
22
+ // SPDX-License-Identifier: MIT
23
+ pragma solidity ^0.8.0;
24
+
25
+ import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
26
+ import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
27
+ import "@openzeppelin/contracts/access/Ownable.sol";
28
+
29
+ contract StakingRewards is ReentrancyGuard, Ownable {
30
+ IERC20 public stakingToken;
31
+ IERC20 public rewardsToken;
32
+
33
+ uint256 public rewardRate = 100; // Rewards per second
34
+ uint256 public lastUpdateTime;
35
+ uint256 public rewardPerTokenStored;
36
+
37
+ mapping(address => uint256) public userRewardPerTokenPaid;
38
+ mapping(address => uint256) public rewards;
39
+ mapping(address => uint256) public balances;
40
+
41
+ uint256 private _totalSupply;
42
+
43
+ event Staked(address indexed user, uint256 amount);
44
+ event Withdrawn(address indexed user, uint256 amount);
45
+ event RewardPaid(address indexed user, uint256 reward);
46
+
47
+ constructor(address _stakingToken, address _rewardsToken) {
48
+ stakingToken = IERC20(_stakingToken);
49
+ rewardsToken = IERC20(_rewardsToken);
50
+ }
51
+
52
+ modifier updateReward(address account) {
53
+ rewardPerTokenStored = rewardPerToken();
54
+ lastUpdateTime = block.timestamp;
55
+
56
+ if (account != address(0)) {
57
+ rewards[account] = earned(account);
58
+ userRewardPerTokenPaid[account] = rewardPerTokenStored;
59
+ }
60
+ _;
61
+ }
62
+
63
+ function rewardPerToken() public view returns (uint256) {
64
+ if (_totalSupply == 0) {
65
+ return rewardPerTokenStored;
66
+ }
67
+ return rewardPerTokenStored +
68
+ ((block.timestamp - lastUpdateTime) * rewardRate * 1e18) / _totalSupply;
69
+ }
70
+
71
+ function earned(address account) public view returns (uint256) {
72
+ return (balances[account] *
73
+ (rewardPerToken() - userRewardPerTokenPaid[account])) / 1e18 +
74
+ rewards[account];
75
+ }
76
+
77
+ function stake(uint256 amount) external nonReentrant updateReward(msg.sender) {
78
+ require(amount > 0, "Cannot stake 0");
79
+ _totalSupply += amount;
80
+ balances[msg.sender] += amount;
81
+ stakingToken.transferFrom(msg.sender, address(this), amount);
82
+ emit Staked(msg.sender, amount);
83
+ }
84
+
85
+ function withdraw(uint256 amount) public nonReentrant updateReward(msg.sender) {
86
+ require(amount > 0, "Cannot withdraw 0");
87
+ _totalSupply -= amount;
88
+ balances[msg.sender] -= amount;
89
+ stakingToken.transfer(msg.sender, amount);
90
+ emit Withdrawn(msg.sender, amount);
91
+ }
92
+
93
+ function getReward() public nonReentrant updateReward(msg.sender) {
94
+ uint256 reward = rewards[msg.sender];
95
+ if (reward > 0) {
96
+ rewards[msg.sender] = 0;
97
+ rewardsToken.transfer(msg.sender, reward);
98
+ emit RewardPaid(msg.sender, reward);
99
+ }
100
+ }
101
+
102
+ function exit() external {
103
+ withdraw(balances[msg.sender]);
104
+ getReward();
105
+ }
106
+ }
107
+ ```
108
+
109
+ ## AMM (Automated Market Maker)
110
+
111
+ ```solidity
112
+ // SPDX-License-Identifier: MIT
113
+ pragma solidity ^0.8.0;
114
+
115
+ import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
116
+
117
+ contract SimpleAMM {
118
+ IERC20 public token0;
119
+ IERC20 public token1;
120
+
121
+ uint256 public reserve0;
122
+ uint256 public reserve1;
123
+
124
+ uint256 public totalSupply;
125
+ mapping(address => uint256) public balanceOf;
126
+
127
+ event Mint(address indexed to, uint256 amount);
128
+ event Burn(address indexed from, uint256 amount);
129
+ event Swap(address indexed trader, uint256 amount0In, uint256 amount1In, uint256 amount0Out, uint256 amount1Out);
130
+
131
+ constructor(address _token0, address _token1) {
132
+ token0 = IERC20(_token0);
133
+ token1 = IERC20(_token1);
134
+ }
135
+
136
+ function addLiquidity(uint256 amount0, uint256 amount1) external returns (uint256 shares) {
137
+ token0.transferFrom(msg.sender, address(this), amount0);
138
+ token1.transferFrom(msg.sender, address(this), amount1);
139
+
140
+ if (totalSupply == 0) {
141
+ shares = sqrt(amount0 * amount1);
142
+ } else {
143
+ shares = min(
144
+ (amount0 * totalSupply) / reserve0,
145
+ (amount1 * totalSupply) / reserve1
146
+ );
147
+ }
148
+
149
+ require(shares > 0, "Shares = 0");
150
+ _mint(msg.sender, shares);
151
+ _update(
152
+ token0.balanceOf(address(this)),
153
+ token1.balanceOf(address(this))
154
+ );
155
+
156
+ emit Mint(msg.sender, shares);
157
+ }
158
+
159
+ function removeLiquidity(uint256 shares) external returns (uint256 amount0, uint256 amount1) {
160
+ uint256 bal0 = token0.balanceOf(address(this));
161
+ uint256 bal1 = token1.balanceOf(address(this));
162
+
163
+ amount0 = (shares * bal0) / totalSupply;
164
+ amount1 = (shares * bal1) / totalSupply;
165
+
166
+ require(amount0 > 0 && amount1 > 0, "Amount0 or amount1 = 0");
167
+
168
+ _burn(msg.sender, shares);
169
+ _update(bal0 - amount0, bal1 - amount1);
170
+
171
+ token0.transfer(msg.sender, amount0);
172
+ token1.transfer(msg.sender, amount1);
173
+
174
+ emit Burn(msg.sender, shares);
175
+ }
176
+
177
+ function swap(address tokenIn, uint256 amountIn) external returns (uint256 amountOut) {
178
+ require(tokenIn == address(token0) || tokenIn == address(token1), "Invalid token");
179
+
180
+ bool isToken0 = tokenIn == address(token0);
181
+ (IERC20 tokenIn_, IERC20 tokenOut, uint256 resIn, uint256 resOut) = isToken0
182
+ ? (token0, token1, reserve0, reserve1)
183
+ : (token1, token0, reserve1, reserve0);
184
+
185
+ tokenIn_.transferFrom(msg.sender, address(this), amountIn);
186
+
187
+ // 0.3% fee
188
+ uint256 amountInWithFee = (amountIn * 997) / 1000;
189
+ amountOut = (resOut * amountInWithFee) / (resIn + amountInWithFee);
190
+
191
+ tokenOut.transfer(msg.sender, amountOut);
192
+
193
+ _update(
194
+ token0.balanceOf(address(this)),
195
+ token1.balanceOf(address(this))
196
+ );
197
+
198
+ emit Swap(msg.sender, isToken0 ? amountIn : 0, isToken0 ? 0 : amountIn, isToken0 ? 0 : amountOut, isToken0 ? amountOut : 0);
199
+ }
200
+
201
+ function _mint(address to, uint256 amount) private {
202
+ balanceOf[to] += amount;
203
+ totalSupply += amount;
204
+ }
205
+
206
+ function _burn(address from, uint256 amount) private {
207
+ balanceOf[from] -= amount;
208
+ totalSupply -= amount;
209
+ }
210
+
211
+ function _update(uint256 res0, uint256 res1) private {
212
+ reserve0 = res0;
213
+ reserve1 = res1;
214
+ }
215
+
216
+ function sqrt(uint256 y) private pure returns (uint256 z) {
217
+ if (y > 3) {
218
+ z = y;
219
+ uint256 x = y / 2 + 1;
220
+ while (x < z) {
221
+ z = x;
222
+ x = (y / x + x) / 2;
223
+ }
224
+ } else if (y != 0) {
225
+ z = 1;
226
+ }
227
+ }
228
+
229
+ function min(uint256 x, uint256 y) private pure returns (uint256) {
230
+ return x <= y ? x : y;
231
+ }
232
+ }
233
+ ```
234
+
235
+ ## Governance Token
236
+
237
+ ```solidity
238
+ // SPDX-License-Identifier: MIT
239
+ pragma solidity ^0.8.0;
240
+
241
+ import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Votes.sol";
242
+ import "@openzeppelin/contracts/access/Ownable.sol";
243
+
244
+ contract GovernanceToken is ERC20Votes, Ownable {
245
+ constructor() ERC20("Governance Token", "GOV") ERC20Permit("Governance Token") {
246
+ _mint(msg.sender, 1000000 * 10**decimals());
247
+ }
248
+
249
+ function _afterTokenTransfer(
250
+ address from,
251
+ address to,
252
+ uint256 amount
253
+ ) internal override(ERC20Votes) {
254
+ super._afterTokenTransfer(from, to, amount);
255
+ }
256
+
257
+ function _mint(address to, uint256 amount) internal override(ERC20Votes) {
258
+ super._mint(to, amount);
259
+ }
260
+
261
+ function _burn(address account, uint256 amount) internal override(ERC20Votes) {
262
+ super._burn(account, amount);
263
+ }
264
+ }
265
+
266
+ contract Governor is Ownable {
267
+ GovernanceToken public governanceToken;
268
+
269
+ struct Proposal {
270
+ uint256 id;
271
+ address proposer;
272
+ string description;
273
+ uint256 forVotes;
274
+ uint256 againstVotes;
275
+ uint256 startBlock;
276
+ uint256 endBlock;
277
+ bool executed;
278
+ mapping(address => bool) hasVoted;
279
+ }
280
+
281
+ uint256 public proposalCount;
282
+ mapping(uint256 => Proposal) public proposals;
283
+
284
+ uint256 public votingPeriod = 17280; // ~3 days in blocks
285
+ uint256 public proposalThreshold = 100000 * 10**18;
286
+
287
+ event ProposalCreated(uint256 indexed proposalId, address proposer, string description);
288
+ event VoteCast(address indexed voter, uint256 indexed proposalId, bool support, uint256 weight);
289
+ event ProposalExecuted(uint256 indexed proposalId);
290
+
291
+ constructor(address _governanceToken) {
292
+ governanceToken = GovernanceToken(_governanceToken);
293
+ }
294
+
295
+ function propose(string memory description) external returns (uint256) {
296
+ require(
297
+ governanceToken.getPastVotes(msg.sender, block.number - 1) >= proposalThreshold,
298
+ "Proposer votes below threshold"
299
+ );
300
+
301
+ proposalCount++;
302
+ Proposal storage newProposal = proposals[proposalCount];
303
+ newProposal.id = proposalCount;
304
+ newProposal.proposer = msg.sender;
305
+ newProposal.description = description;
306
+ newProposal.startBlock = block.number;
307
+ newProposal.endBlock = block.number + votingPeriod;
308
+
309
+ emit ProposalCreated(proposalCount, msg.sender, description);
310
+ return proposalCount;
311
+ }
312
+
313
+ function vote(uint256 proposalId, bool support) external {
314
+ Proposal storage proposal = proposals[proposalId];
315
+ require(block.number >= proposal.startBlock, "Voting not started");
316
+ require(block.number <= proposal.endBlock, "Voting ended");
317
+ require(!proposal.hasVoted[msg.sender], "Already voted");
318
+
319
+ uint256 weight = governanceToken.getPastVotes(msg.sender, proposal.startBlock);
320
+ require(weight > 0, "No voting power");
321
+
322
+ proposal.hasVoted[msg.sender] = true;
323
+
324
+ if (support) {
325
+ proposal.forVotes += weight;
326
+ } else {
327
+ proposal.againstVotes += weight;
328
+ }
329
+
330
+ emit VoteCast(msg.sender, proposalId, support, weight);
331
+ }
332
+
333
+ function execute(uint256 proposalId) external {
334
+ Proposal storage proposal = proposals[proposalId];
335
+ require(block.number > proposal.endBlock, "Voting not ended");
336
+ require(!proposal.executed, "Already executed");
337
+ require(proposal.forVotes > proposal.againstVotes, "Proposal failed");
338
+
339
+ proposal.executed = true;
340
+
341
+ // Execute proposal logic here
342
+
343
+ emit ProposalExecuted(proposalId);
344
+ }
345
+ }
346
+ ```
347
+
348
+ ## Flash Loan
349
+
350
+ ```solidity
351
+ // SPDX-License-Identifier: MIT
352
+ pragma solidity ^0.8.0;
353
+
354
+ import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
355
+
356
+ interface IFlashLoanReceiver {
357
+ function executeOperation(
358
+ address asset,
359
+ uint256 amount,
360
+ uint256 fee,
361
+ bytes calldata params
362
+ ) external returns (bool);
363
+ }
364
+
365
+ contract FlashLoanProvider {
366
+ IERC20 public token;
367
+ uint256 public feePercentage = 9; // 0.09% fee
368
+
369
+ event FlashLoan(address indexed borrower, uint256 amount, uint256 fee);
370
+
371
+ constructor(address _token) {
372
+ token = IERC20(_token);
373
+ }
374
+
375
+ function flashLoan(
376
+ address receiver,
377
+ uint256 amount,
378
+ bytes calldata params
379
+ ) external {
380
+ uint256 balanceBefore = token.balanceOf(address(this));
381
+ require(balanceBefore >= amount, "Insufficient liquidity");
382
+
383
+ uint256 fee = (amount * feePercentage) / 10000;
384
+
385
+ // Send tokens to receiver
386
+ token.transfer(receiver, amount);
387
+
388
+ // Execute callback
389
+ require(
390
+ IFlashLoanReceiver(receiver).executeOperation(
391
+ address(token),
392
+ amount,
393
+ fee,
394
+ params
395
+ ),
396
+ "Flash loan failed"
397
+ );
398
+
399
+ // Verify repayment
400
+ uint256 balanceAfter = token.balanceOf(address(this));
401
+ require(balanceAfter >= balanceBefore + fee, "Flash loan not repaid");
402
+
403
+ emit FlashLoan(receiver, amount, fee);
404
+ }
405
+ }
406
+
407
+ // Example flash loan receiver
408
+ contract FlashLoanReceiver is IFlashLoanReceiver {
409
+ function executeOperation(
410
+ address asset,
411
+ uint256 amount,
412
+ uint256 fee,
413
+ bytes calldata params
414
+ ) external override returns (bool) {
415
+ // Decode params and execute arbitrage, liquidation, etc.
416
+ // ...
417
+
418
+ // Approve repayment
419
+ IERC20(asset).approve(msg.sender, amount + fee);
420
+
421
+ return true;
422
+ }
423
+ }
424
+ ```
425
+
426
+ ## Resources
427
+
428
+ - **references/staking.md**: Staking mechanics and reward distribution
429
+ - **references/liquidity-pools.md**: AMM mathematics and pricing
430
+ - **references/governance-tokens.md**: Governance and voting systems
431
+ - **references/lending-protocols.md**: Lending/borrowing implementation
432
+ - **references/flash-loans.md**: Flash loan security and use cases
433
+ - **assets/staking-contract.sol**: Production staking template
434
+ - **assets/amm-contract.sol**: Full AMM implementation
435
+ - **assets/governance-token.sol**: Governance system
436
+ - **assets/lending-protocol.sol**: Lending platform template
437
+
438
+ ## Best Practices
439
+
440
+ 1. **Use Established Libraries**: OpenZeppelin, Solmate
441
+ 2. **Test Thoroughly**: Unit tests, integration tests, fuzzing
442
+ 3. **Audit Before Launch**: Professional security audits
443
+ 4. **Start Simple**: MVP first, add features incrementally
444
+ 5. **Monitor**: Track contract health and user activity
445
+ 6. **Upgradability**: Consider proxy patterns for upgrades
446
+ 7. **Emergency Controls**: Pause mechanisms for critical issues
447
+
448
+ ## Common DeFi Patterns
449
+
450
+ - **Time-Weighted Average Price (TWAP)**: Price oracle resistance
451
+ - **Liquidity Mining**: Incentivize liquidity provision
452
+ - **Vesting**: Lock tokens with gradual release
453
+ - **Multisig**: Require multiple signatures for critical operations
454
+ - **Timelocks**: Delay execution of governance decisions