This project demonstrates advanced data encoding techniques essential for DeFi protocol development using Solidity's abi.encode, abi.encodePacked and keccak256 functions. It provides a complete set of encoding and hashing functions to create unique identifiers across DeFi operations such as liquidity pools, trading positions, limit orders, yield farming strategies or cross-chain operations. All logic is covered by a complete testing suite with 100% test coverage.
ABI (Application Binary Interface) encoding is the standard method for encoding function calls and data in Ethereum.
| Feature | abi.encode | abi.encodePacked | 
|---|---|---|
| Encoding Type | Full padding (32-byte slots) | Compact, no padding | 
| Gas Cost | Higher due to padding overhead | Lower due to compression | 
| Security | No collision risk | Potential collision risk with dynamic types | 
| Output Size | Larger, predictable structure | Smaller, minimal footprint | 
| Example | uint16(0x1234), uint16(0x5678)→ 64 bytes | uint16(0x1234), uint16(0x5678)→ 4 bytes (0x12345678) | 
🔐 keccak256(encodedData) is a cryptographic hashing function widely used in the Ethereum ecosystem to generate unique identifiers from encoded data. The encoded data is passed through the hashing function, which produces a unique bytes32 value. Key cryptographic hash properties:
- Fixed-size output: Regardless of the input size, it always returns a 32-byte value.
- Deterministic: The same input will always produce the same output.
- Collision resistance: It is computationally infeasible to find two different inputs that result in the same hash.
- Preimage resistance: It is practically impossible to reverse the process and deduce the original input from the hash (one-way function).
- Avalanche effect: A change of even a single bit in the input results in a completely different hash output.
In DeFi protocols, hashing and encoding is crucial for:
- Unique Identifiers: Pool IDs, position tracking, order management
- Hash Generation: Creating deterministic, collision-resistant identifiers
- Gas Optimization: Minimizing transaction costs through compact encoding
- Cross-Protocol Compatibility: Standardized data formats across DeFi ecosystem
Each function encodes specific DeFi concepts into bytes and/or hashes them with keccak256 for uniqueness and integrity.
- createPoolIdentifier– Generates unique pool IDs based on token pair and fee, invariant to token order.
- encodeTradingPosition– Encodes user trading positions with timestamp and produces a unique position hash.
- encodeSwapData– Encodes swap paths, amounts, and deadline while validating input consistency.
- encodeLimitOrder– Encodes data of a limit order with a string identifier.
- encodeYieldPosition– Hashes data from a yield farming position.
- encodeFlashLoanData– Concatenates flash loan parameters plus callback data.
- encodeStakingPoolConfig– Encodes staking configuration including block timestamp for uniqueness.
- createUserMultiPoolHash– Builds a unique user identifier across multiple pools.
- encodeYieldStrategy– Encodes yield strategies data, pool allocation and weights.
- encodeCrossChainBridgeData– Encodes cross-chain transfer data.
- createDeFiTransactionId– Produces unique IDs for DeFi transactions.
- encodeStopLossOrder,- encodeTakeProfitOrder,- encodeTrailingStopOrder– Encodes risk management orders with discriminators.
The project implements exhaustive testing patterns essential for production DeFi protocols. Test includes functions like:
✅ Invariant testing: to verify token ordering consistency regardless of input order.
test_createPoolIdentifier_sameForBothTokenOrders()
✅ Happy path: to ensure the correct verification of encoded data and hash generation test_encodeTradingPosition_returnsExpectedDataAndHash()
✅ Negative cases: to prevent from malformed input data and revert cases validation. test_encodeSwapData_revertsOnLengthMismatch()
💯 100% Code Coverage is achieved across all metrics:
Ran 17 tests for test/ABIEncoderDemo.t.sol:ABIEncoderDemoTest
Suite result: ok. 17 passed; 0 failed; 0 skipped
╭------------------------+-----------------+-----------------+---------------+-----------------╮
| File                   | % Lines         | % Statements    | % Branches    | % Funcs         |
+==============================================================================================+
| src/ABIEncoderDemo.sol | 100.00% (52/52) | 100.00% (51/51) | 100.00% (4/4) | 100.00% (14/14) |
╰------------------------+-----------------+-----------------+---------------+-----------------╯- Solidity (^0.8.24) – Smart contract programming language
- Foundry – Advanced testing framework with fuzzing and invariant testing
This project is licensed under the MIT License.