Foundry project for a simple yield farming system written in Solidity, with extra utilities that demonstrate practical abi.encodePacked usage for DeFi-style payloads and identifiers.
The repository currently has three main contracts:
src/YieldFarmingPool.sol: the main yield farming contractsrc/MockToken.sol: ERC-20 token mock for local testingsrc/utils/ABIEncoder.sol: helper contract with compact encoding examples for DeFi data models
The project uses Foundry for compilation, testing, and formatting.
src/YieldFarmingPool.sol currently supports:
- Owner-only pool creation
- Pool tracking by deterministic
bytes32identifiers - Staking into active pools
- Withdrawals with reward settlement
- Reward claiming
- Owner-controlled reward rate updates
- Emergency token withdrawal for the owner
- Encoded pool snapshots through
getPoolEncodedData - Deterministic per-user hashes through
getUserHash - Active pool enumeration with
getActivePoolsandgetActivePoolsCount
Each pool stores:
- Staking token address
- Total staked balance
- Reward rate
- Last update timestamp
- Accumulated reward-per-token value
- Active status flag
src/utils/ABIEncoder.sol acts as a compact reference for abi.encodePacked patterns, including:
- Pool identifiers
- Trading positions
- Swap payloads
- Limit orders
- Yield positions
- Flash loan payloads
- Staking configuration snapshots
- Multi-pool user hashes
- Yield strategies
- Cross-chain bridge metadata
- Generic DeFi transaction IDs
- Stop-loss, take-profit, and trailing-stop order payloads
This contract is useful for learning how deterministic hashes and compact payloads can be built from structured DeFi inputs.
abi.encodePacked is compact, but it should be used carefully. Combining variable-length values without clear boundaries can create collisions. This repository shows safer patterns such as:
- Fixed field ordering
- Domain/type suffixes like
"LIMIT_ORDER_V1"and"YIELD_FARMING_USER" - Length validation for correlated arrays
Build the project:
forge buildRun all tests:
forge testRun only the ABI encoder tests:
forge test --match-contract ABIEncoderTestRun only the yield farming pool tests:
forge test --match-contract YieldFarmingPoolTestFormat the code:
forge fmtCheck formatting without modifying files:
forge fmt --checkGenerate a coverage summary:
forge coverage --report summaryThe repository now includes dedicated test suites for both the encoder helpers and the yield farming pool:
The automated tests currently validate:
- Deterministic pool IDs
- Timestamp-dependent trading position encoding
- Swap payload construction
- Custom error reverts on mismatched array lengths
- Hash consistency for order and position encodings
- Flash loan, bridge, and advanced order payloads
- Strategy encoding and multi-pool hashing
- Pool creation and duplicate protection
- Owner access control on administrative functions
- Staking, reward claiming, and withdrawals
- Full-position withdrawals
- Reward rate updates
- Emergency withdrawal behavior
- Encoded pool snapshots and active pool enumeration
Latest measured coverage summary:
╭--------------------------+------------------+------------------+-----------------+-----------------╮
| File | % Lines | % Statements | % Branches | % Funcs |
+====================================================================================================+
| src/MockToken.sol | 66.67% (4/6) | 66.67% (2/3) | 100.00% (0/0) | 66.67% (2/3) |
|--------------------------+------------------+------------------+-----------------+-----------------|
| src/YieldFarmingPool.sol | 100.00% (90/90) | 100.00% (84/84) | 100.00% (25/25) | 100.00% (14/14) |
|--------------------------+------------------+------------------+-----------------+-----------------|
| src/utils/ABIEncoder.sol | 100.00% (56/56) | 100.00% (54/54) | 100.00% (2/2) | 100.00% (17/17) |
|--------------------------+------------------+------------------+-----------------+-----------------|
| Total | 98.68% (150/152) | 99.29% (140/141) | 100.00% (27/27) | 97.06% (33/34) |
╰--------------------------+------------------+------------------+-----------------+-----------------╯
src/YieldFarmingPool.sol: 100.00% lines, 100.00% statements, 100.00% branches, 100.00% functionssrc/MockToken.sol: 66.67% lines, 66.67% statements, 100.00% branches, 66.67% functionssrc/utils/ABIEncoder.sol: 10.71% lines, 5.56% statements, 0.00% branches, 17.65% functions
These numbers were produced with:
forge coveragesrc/
MockToken.sol
YieldFarmingPool.sol
utils/
ABIEncoder.sol
test/
YieldFarmingPool.t.sol
utils/
ABIEncoder.t.sol
Foundry documentation: https://book.getfoundry.sh/