A yield farming smart contract infrastructure for ERC20 tokens.
This protocol implements specific features to handle edge cases in decentralized finance, including gas minimization, transaction reduction, and non-standard token compatibility.
- Context: Standard staking requires an
approvetransaction followed by astaketransaction. - Implementation: Integrated
IERC20Permit. Users sign an off-chain message, and the contract executes the approval and the deposit in a single transaction (stakeWithPermit).
- Context: Tokens with transfer taxes result in the contract receiving fewer tokens than the requested amount.
- Implementation: The
_stakefunction calculates the balance delta before and after the transfer. Only the actual amount received is credited to the user's position, maintaining internal accounting solvency.
- Context: EVM storage operations (
SSTORE,SLOAD) are the most expensive opcodes. - Implementation: The
Poolstruct is packed. Thetokenaddress (20 bytes),isActiveboolean (1 byte), andlastUpdateTime(8 bytes) share a single 32-byte storage slot. This reduces the total slot requirement from 6 to 4.
- Context: Centralized control over user funds introduces security risks.
- Implementation: Access control is divided into
DEFAULT_ADMIN_ROLEandPOOL_MANAGER_ROLE. TherescueTokensfunction explicitly reverts if an administrator attempts to withdraw assets that belong to an active staking pool.
The test suite utilizes Foundry, featuring:
- Fuzzing: Property-based tests with bounded inputs to validate reward math.
- Mocking: Integration of
MockRevertingTokenandMockTokenPermitto test failure states and signature recovery.
- Language: Solidity
0.8.24 - Standards: ERC20, EIP-2612
- Framework: Foundry
// Measure exact balance changes to support fee-on-transfer tokens
uint256 balBefore = IERC20(pool.token).balanceOf(address(this));
IERC20(pool.token).safeTransferFrom(account, address(this), amount);
uint256 balAfter = IERC20(pool.token).balanceOf(address(this));
uint256 actualAmountReceived = balAfter - balBefore;
user.amount += actualAmountReceived;The protocol is rigorously tested using Foundry, featuring property-based fuzzing and exact balance delta verifications.
To execute the test suite:
forge testTo view the coverage report:
forge coverageCoverage Output (Core Contracts):
| File | % Lines | % Statements | % Branches | % Funcs |
|---|---|---|---|---|
| src/YieldFarmingPool.sol | 100.00% | 98.46% | 90.48% | 100.00% |
| src/ABIEncoderDemo.sol | 100.00% | 100.00% | 100.00% | 100.00% |