$npx -y skills add wshobson/agents --skill defi-protocol-templatesImplement DeFi protocols with production-ready templates for staking, AMMs, governance, and flash loans. Use when building decentralized finance applications or smart contract protocols.
| 1 | # DeFi Protocol Templates |
| 2 | |
| 3 | Production-ready templates for common DeFi protocols including staking, AMMs, governance, and flash loans. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | - Building staking platforms with reward distribution |
| 8 | - Implementing AMM (Automated Market Maker) protocols |
| 9 | - Creating governance token systems |
| 10 | - Integrating flash loan functionality |
| 11 | |
| 12 | ## Staking Contract |
| 13 | |
| 14 | ```solidity |
| 15 | // SPDX-License-Identifier: MIT |
| 16 | pragma solidity ^0.8.0; |
| 17 | |
| 18 | import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 19 | import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; |
| 20 | import "@openzeppelin/contracts/access/Ownable.sol"; |
| 21 | |
| 22 | contract StakingRewards is ReentrancyGuard, Ownable { |
| 23 | IERC20 public stakingToken; |
| 24 | IERC20 public rewardsToken; |
| 25 | |
| 26 | uint256 public rewardRate = 100; // Rewards per second |
| 27 | uint256 public lastUpdateTime; |
| 28 | uint256 public rewardPerTokenStored; |
| 29 | |
| 30 | mapping(address => uint256) public userRewardPerTokenPaid; |
| 31 | mapping(address => uint256) public rewards; |
| 32 | mapping(address => uint256) public balances; |
| 33 | |
| 34 | uint256 private _totalSupply; |
| 35 | |
| 36 | event Staked(address indexed user, uint256 amount); |
| 37 | event Withdrawn(address indexed user, uint256 amount); |
| 38 | event RewardPaid(address indexed user, uint256 reward); |
| 39 | |
| 40 | constructor(address _stakingToken, address _rewardsToken) { |
| 41 | stakingToken = IERC20(_stakingToken); |
| 42 | rewardsToken = IERC20(_rewardsToken); |
| 43 | } |
| 44 | |
| 45 | modifier updateReward(address account) { |
| 46 | rewardPerTokenStored = rewardPerToken(); |
| 47 | lastUpdateTime = block.timestamp; |
| 48 | |
| 49 | if (account != address(0)) { |
| 50 | rewards[account] = earned(account); |
| 51 | userRewardPerTokenPaid[account] = rewardPerTokenStored; |
| 52 | } |
| 53 | _; |
| 54 | } |
| 55 | |
| 56 | function rewardPerToken() public view returns (uint256) { |
| 57 | if (_totalSupply == 0) { |
| 58 | return rewardPerTokenStored; |
| 59 | } |
| 60 | return rewardPerTokenStored + |
| 61 | ((block.timestamp - lastUpdateTime) * rewardRate * 1e18) / _totalSupply; |
| 62 | } |
| 63 | |
| 64 | function earned(address account) public view returns (uint256) { |
| 65 | return (balances[account] * |
| 66 | (rewardPerToken() - userRewardPerTokenPaid[account])) / 1e18 + |
| 67 | rewards[account]; |
| 68 | } |
| 69 | |
| 70 | function stake(uint256 amount) external nonReentrant updateReward(msg.sender) { |
| 71 | require(amount > 0, "Cannot stake 0"); |
| 72 | _totalSupply += amount; |
| 73 | balances[msg.sender] += amount; |
| 74 | stakingToken.transferFrom(msg.sender, address(this), amount); |
| 75 | emit Staked(msg.sender, amount); |
| 76 | } |
| 77 | |
| 78 | function withdraw(uint256 amount) public nonReentrant updateReward(msg.sender) { |
| 79 | require(amount > 0, "Cannot withdraw 0"); |
| 80 | _totalSupply -= amount; |
| 81 | balances[msg.sender] -= amount; |
| 82 | stakingToken.transfer(msg.sender, amount); |
| 83 | emit Withdrawn(msg.sender, amount); |
| 84 | } |
| 85 | |
| 86 | function getReward() public nonReentrant updateReward(msg.sender) { |
| 87 | uint256 reward = rewards[msg.sender]; |
| 88 | if (reward > 0) { |
| 89 | rewards[msg.sender] = 0; |
| 90 | rewardsToken.transfer(msg.sender, reward); |
| 91 | emit RewardPaid(msg.sender, reward); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | function exit() external { |
| 96 | withdraw(balances[msg.sender]); |
| 97 | getReward(); |
| 98 | } |
| 99 | } |
| 100 | ``` |
| 101 | |
| 102 | ## AMM (Automated Market Maker) |
| 103 | |
| 104 | ```solidity |
| 105 | // SPDX-License-Identifier: MIT |
| 106 | pragma solidity ^0.8.0; |
| 107 | |
| 108 | import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 109 | |
| 110 | contract SimpleAMM { |
| 111 | IERC20 public token0; |
| 112 | IERC20 public token1; |
| 113 | |
| 114 | uint256 public reserve0; |
| 115 | uint256 public reserve1; |
| 116 | |
| 117 | uint256 public totalSupply; |
| 118 | mapping(address => uint256) public balanceOf; |
| 119 | |
| 120 | event Mint(address indexed to, uint256 amount); |
| 121 | event Burn(address indexed from, uint256 amount); |
| 122 | event Swap(address indexed trader, uint256 amount0In, uint256 amount1In, uint256 amount0Out, uint256 amount1Out); |
| 123 | |
| 124 | constructor(address _token0, address _token1) { |
| 125 | token0 = IERC20(_token0); |
| 126 | token1 = IERC20(_token1); |
| 127 | } |
| 128 | |
| 129 | function addLiquidity(uint256 amount0, uint256 amount1) external returns (uint256 shares) { |
| 130 | token0.transferFrom(msg.sender, address(this), amount0); |
| 131 | token1.transferFrom(msg.sender, address(this), amount1); |
| 132 | |
| 133 | if (totalSupply == 0) { |
| 134 | shares = sqrt(amount0 * amount1); |
| 135 | } else { |
| 136 | shares = min( |
| 137 | (amount0 * totalSupply) / reserve0, |
| 138 | (amount1 * totalSupply) / reserve1 |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | require(shares > 0, "Shares = 0"); |
| 143 | _mint(msg.sender, shares); |
| 144 | _update( |
| 145 | token0.balanceOf(address(this)), |
| 146 | token1.balanceOf(address(this)) |
| 147 | ); |
| 148 | |
| 149 | emit Mint(msg.sender, shares); |
| 150 | } |
| 151 | |
| 152 | function removeLiquidity(uint256 shares) external returns (uin |