- 1Basic and Applied Sciences Research Center, Information System and Management System Department, Al Maaref University, Beirut, Lebanon
- 2Department of Computer Science - LaRRIS, Lebanese University – Faculty of Sciences Fanar, Beirut, Lebanon
- 3Department of Computer Science and Mathematics, Faculty of Arts and Sciences, University of Balamand, Koura, Lebanon
The static and hard-coded logic of smart contracts in Decentralized Finance (DeFi) platforms significantly limits their adaptability in dynamic and volatile market environments. To address this challenge, we propose a novel hyper-heuristic driven framework that enables real-time rule optimization within smart contracts, thereby enhancing responsiveness, gas efficiency, and operational robustness. The framework features a two-layer architecture: a reinforcement learning-based high-level controller selects appropriate low-level rule heuristics from a domain-specific library based on evolving transaction contexts and on-chain data. Implemented and evaluated on Uniswap v2 and Aave v3 protocols, the system dynamically optimizes parameters such as slippage tolerance, gas usage thresholds, and loan-to-value ratios. Experimental results on real-world datasets show significant performance improvements, including a 45.6% increase in transaction success rate, 28.3% reduction in average gas consumption, and 38.4% drop in liquidation events under market stress scenarios. This research demonstrates the feasibility and advantages of embedding intelligent, adaptive decision-making mechanisms within DeFi smart contracts, opening new pathways toward autonomous, resilient, and regulation-aligned blockchain systems.
1 Introduction
Decentralized Finance (DeFi) has emerged as a transformative paradigm in financial systems, offering permissionless and trustless alternatives to traditional banking through blockchain-based smart contracts Werner et al. (2021). Protocols such as Uniswap, Aave, Compound, and MakerDAO have enabled decentralized lending, trading, and yield optimization at unprecedented scale. These applications rely heavily on smart contracts, self-executing programs written in languages like Solidity and deployed on platforms such as Ethereum Xu et al. (2021).
However, the deterministic nature of smart contracts introduces a fundamental rigidity: contract rules and execution paths are hard-coded and immutable once deployed. This restricts adaptability to external conditions such as market volatility, liquidity shifts, or policy updates Wang et al. (2022). For instance, static parameters for interest rates, slippage tolerances, or liquidation thresholds can lead to operational inefficiencies or system vulnerabilities. Historical failures such as the bZx protocol exploit (2020) and the Terra-LUNA collapse (2022) illustrate how poorly calibrated rules or inflexible contract structures can be catastrophic in real-world DeFi markets Qin et al. (2021); Kang and Hwang (2023).
To mitigate these limitations, some platforms have explored the use of external oracles and governance models to update contract behavior post-deployment Zhang et al. (2020); Ghosh et al. (2022). Yet, such mechanisms are not without drawbacks, often exhibiting centralization risks, governance delays, and susceptibility to oracle manipulation. Consequently, there is increasing interest in self-adjusting smart contracts that can autonomously respond to both on-chain and off-chain dynamics in real time.
These existing approaches suffer from several concrete failure modes. Upgradeable-proxy contracts rely on governance proposals—typically through DAOs or multi-signature wallets—that introduce significant latency, ranging from several hours to days (e.g., exceeding 15,000 Ethereum blocks), thereby creating windows of vulnerability. A prominent example is Compound’s Proposal 62 in 2021, which resulted in the unintentional distribution of over $90 million in tokens due to delayed governance execution.
Similarly, oracle-based systems are prone to manipulation through flash loans or thin-liquidity trades. Notable cases include the bZx protocol exploit (2020), where attackers leveraged flash loans to manipulate price oracles, and the Mango Markets hack (2022), in which an attacker artificially inflated the price of a collateral asset to extract funds. These incidents underscore the operational fragility introduced by delayed or externally controlled adaptability mechanisms.
Despite the rapid evolution of DeFi, smart contracts remain fundamentally static. Once deployed, their logic is immutable, leading to inefficiencies and vulnerabilities during periods of market volatility or liquidity shifts. Prior attempts to enhance adaptability—through off-chain control logic, oracle-based feeds, or governance-based updates—introduce trade-offs in the form of latency, centralization, and heightened security risks.
What remains underexplored is the design of smart contracts capable of autonomously and efficiently adapting their internal rule sets in response to evolving on-chain conditions—without relying on external interventions. This represents a critical research gap in the pursuit of real-time, decentralized, and intelligent contract behavior.
This paper addresses the gap by proposing a hyper-heuristic driven smart contract architecture. Our framework embeds a high-level reinforcement learning controller directly within the contract, enabling on-chain selection from a domain-specific heuristic library. This adaptive mechanism empowers smart contracts to optimize execution parameters in real time, enhancing transaction success rates, reducing gas costs, and increasing resilience to environmental volatility—while maintaining decentralization and transparency.
1.1 Background and motivation
Hyper-heuristics have gained prominence as a high-level search methodology designed to automate the process of selecting or generating heuristics for solving complex optimization problems Burke et al. (2013). Unlike traditional metaheuristics (e.g., Genetic Algorithms or Simulated Annealing) that directly explore solution spaces, hyper-heuristics operate at a higher abstraction level, making decisions about which heuristics to apply, when, and how.
Originally developed for scheduling, routing, and other NP-hard combinatorial domains, hyper-heuristics have demonstrated robust generalization capabilities across problem domains Cowling et al. (2001); Alzamili et al. (2023). Their layered architecture typically consists of:
• A low-level heuristic set containing problem-specific rules or operators.
• A high-level strategy, often implemented via reinforcement learning, evolutionary search, or case-based reasoning, to select heuristics based on the problem state.
Despite their success in traditional optimization, the integration of hyper-heuristics into blockchain-based environments remains underexplored. This paper pioneers the application of hyper-heuristics to the domain of smart contracts, specifically targeting rule optimization in DeFi protocols. By embedding a high-level decision-making layer within smart contract logic, we aim to equip DeFi systems with real-time adaptability, autonomy, and robustness against external shocks.
Research Contributions. In this work, we:
1. Propose a novel architecture that integrates hyper-heuristics directly into smart contracts for dynamic rule selection.
2. Develop a reinforcement learning-based controller to manage heuristic choice in response to market indicators.
3. Evaluate the system using real datasets from Uniswap and Aave, showing measurable improvements in transaction efficiency, cost reduction, and resilience to market volatility.
The remainder of this paper is organized as follows: Section II surveys related work on smart contract adaptability and heuristic optimization. Section III defines the problem formulation and modeling assumptions. Section IV introduces the proposed framework and implementation. Section V presents detailed experimental results and analysis. Section VI discusses broader implications and limitations, and Section VII concludes the paper.
2 Related work
The rigidity of smart contract logic in Decentralized Finance (DeFi) has motivated extensive research into enhancing adaptability, resilience, and efficiency in decentralized systems Rkein et al. (2024). This section reviews existing efforts across three main categories: smart contract dynamism, DeFi rule optimization, and hyper-heuristics in optimization and blockchain contexts.
2.1 Smart contract adaptability in DeFi
The security and adaptability of blockchain and Ethereum smart contracts have become increasingly vital research domains, particularly in the context of Decentralized Finance (DeFi). With the growing reliance on immutable, self-executing code, any lack of flexibility or vulnerability in smart contracts can have profound consequences on financial ecosystems.
Multiple surveys and taxonomies have addressed general and Ethereum-specific smart contract threats. Saad et al. (2019), Li et al. (2020), and Zhu et al. (2020) provided foundational classifications of blockchain vulnerabilities, laying the groundwork for more focused explorations into Ethereum smart contract security. Luu et al. (2016) were among the first to present a taxonomy of smart contract vulnerabilities while also introducing Oyente—an automated tool for analyzing contract behavior. Although Oyente marked significant progress in detection capabilities, it lacked depth in mitigation strategies.
Dika (2017) offered a comparative evaluation of early smart contract analysis tools, though the limited tool maturity and dataset scope constrained its findings. Alharby and van Moorsel (2017) conducted a systematic mapping of smart contract research prior to 2018, highlighting gaps in scalability, privacy, and deployment studies.
Atzei et al. (2017) introduced a widely cited three-layer vulnerability taxonomy encompassing Solidity, the Ethereum Virtual Machine (EVM), and blockchain-level flaws. Building on this, Dingman (2019) applied the NIST framework to identify 49 smart contract bugs grouped into 10 classes. Grishchenko et al. (2018) contributed formal definitions for call integrity, atomicity, and runtime correctness—core principles in smart contract verification.
Several researchers have also assessed the effectiveness of vulnerability detection tools. Di Angelo and Salzer (2020) compared 27 tools against 18 known vulnerabilities, while Praitheeshan et al. (2019) evaluated detection rates across 16 vulnerability types using seven popular tools. Huang et al. (2019) emphasized vulnerability management throughout the software development lifecycle, mapping prevention strategies from design to audit phases. Sayeed and Marco-Gisbert (2019) focused on application-level vulnerabilities, benchmarking ten tools against seven core bug types. Durieux et al. (2020) performed one of the largest empirical evaluations, testing nine detection tools across thousands of real-world Ethereum contracts.
Tantikul and Ngamsuriyaroj (2020) explored inter-tool detection correlation, while Chen et al. (2020) compiled a comprehensive mapping of 40 vulnerabilities, 29 associated attack types, and 51 proposed countermeasures. Tang et al. (2021) categorized detection techniques as static, dynamic, or formal, also suggesting the potential for machine learning to enhance analysis capabilities. Rameder (2021) addressed gaps in these earlier studies by proposing an updated taxonomy and tool mapping, though he lacked empirical efficiency metrics or Common Weakness Enumeration (CWE) alignments.
Recently, Ghaleb and Pattabiraman (2022) introduced SolidiFI, an automated benchmark framework evaluating six static tools—Oyente, Securify, Mythril, SmartCheck, Manticore, and Slither—against seven prevalent vulnerabilities, including reentrancy, unhandled exceptions, and transaction-order dependence. While comprehensive, the study did not extend to dynamic or formal verification methods.
In parallel, DeFi has flourished as a blockchain-powered peer-to-peer financial system. Smart contracts serve as the backbone of DeFi, enabling decentralized trading, lending, and asset management. According to Schär (2021) and Qin et al. (2021), DeFi has grown into a $200 billion economy, leveraging the permissionless architecture of public blockchains. Within this framework, any user can deploy financial logic, and participants can interact with it without intermediaries—provided the predefined contract rules are met.
However, smart contracts in DeFi are traditionally deployed with static logic and fixed parameters. Such inflexibility entails that, contract rules including slippage tolerance, liquidation thresholds, or collateral ratios, cannot adapt to market volatility or user behavior without external interventions. Xu et al. (2021) highlighted this challenge and proposed modular upgradeable architectures. Similarly, proxy contracts like those in the OpenZeppelin EIP-1967 standard enable logic upgrades but often require administrator keys, reintroducing centralization risks.
To support adaptive behavior, trusted data feeds such as Zhang et al. (2020) and Breidenbach et al. (2021) were introduced, enabling contracts to react to off-chain data. Nonetheless, these solutions rely on external inputs and do not offer intrinsic logic-level adaptability. Gudgeon et al. (2020) underscored the impracticality of real-time updates through decentralized governance due to inherent voting delays, especially under stress scenarios.
These limitations highlight the need for smart contracts capable of autonomously adjusting their internal logic in response to real-time on-chain and off-chain conditions. This paper responds to that need by introducing a hyper-heuristic driven framework that supports dynamic rule selection and execution within smart contracts, enhancing both security and adaptability in evolving DeFi environments.
2.2 Rule optimization in DeFi protocols
Recent studies have explored the use of optimization and learning techniques for rule calibration in DeFi. Wang et al. (2022) applied reinforcement learning to tune protocol parameters such as interest rates and collateral thresholds in lending markets. However, their models remain off-chain and require orchestration from external agents.
Ghosh et al. (2022) proposed a framework for incentive-aligned insurance in DeFi using on-chain metrics, yet their approach lacked a systematic rule-selection mechanism. Kang and Hwang (2023) analyzed the Terra-LUNA collapse, demonstrating that dynamic parameter controls could have mitigated the contagion by tightening mint-burn thresholds in real time—underscoring the need for embedded rule adaptability.
Despite these advances, most works assume fixed rule sets and focus on parameter tuning, rather than rule selection and adaptation, which is the central contribution of this paper.
2.3 Hyper-heuristics for optimization and autonomy
Hyper-heuristics were initially introduced as a general-purpose strategy aimed at automating the selection or generation of heuristics to solve diverse problem instances Cowling et al. (2001). Unlike conventional metaheuristics that operate directly on the solution space Danach (2016), hyper-heuristics function at a higher level of abstraction by managing and adapting a set of low-level heuristics Danach (2016). Their main objective is not to identify solutions directly, but to determine the most effective heuristic or sequence of heuristics for a given problem state Tarhini et al. (2022), thus abstracting the optimization process away from domain-specific algorithm design.
Burke et al. (2013) classified hyper-heuristics into two broad categories: heuristic selection and heuristic generation. Heuristic selection techniques dynamically choose among a predefined set of heuristics, often using learning mechanisms such as reinforcement learning or evolutionary strategies. In contrast, heuristic generation methods aim to construct new heuristics through meta-level learning and combination of existing components. These paradigms have demonstrated strong generalization capabilities across a variety of combinatorial optimization problems, including scheduling, routing, and resource allocation.
A typical hyper-heuristic framework comprises two layers: (1) a low-level heuristic pool containing domain-specific operators such as mutation, crossover, or local search moves; and (2) a high-level decision-making component that guides heuristic selection or generation based on feedback from the search performance. This architecture makes hyper-heuristics especially well-suited for solving large-scale, non-linear, and discrete optimization problems, offering improved adaptability, scalability, and reusability in environments characterized by uncertainty or dynamically evolving objectives.
The increasing complexity and dynamism of blockchain-based environments—especially within decentralized finance (DeFi)—create a compelling use case for hyper-heuristics. Smart contracts deployed on platforms like Ethereum often operate in volatile and resource-constrained settings, where fixed-rule logic is insufficient to maintain performance or security Macrinici et al. (2018). By embedding hyper-heuristic frameworks into smart contract logic, it becomes possible to dynamically select execution rules, adjust operational parameters, and optimize gas efficiency in response to real-time on-chain and off-chain conditions. This integration enables blockchain applications to move beyond static programmability toward autonomous, adaptive behavior, effectively bridging the gap between robust optimization and decentralized autonomy.
Our proposed framework bridges this gap by incorporating a reinforcement learning-driven hyper-heuristic mechanism directly into smart contracts to dynamically select rule sets during transaction execution. This novel contribution enables self-adaptive contract behavior based on evolving blockchain states, thereby enhancing both performance and resilience in decentralized systems.
3 Problem definition and motivation
3.1 Limitations of static smart contract logic
Smart contracts in DeFi platforms encode financial rules that govern asset exchanges, lending terms, slippage tolerances, liquidation criteria, and more. However, once deployed, these contracts typically operate with hardcoded logic that lacks the ability to adapt dynamically to real-time conditions. For instance, a liquidity pool contract on Uniswap may enforce a fixed slippage threshold, irrespective of real-time price volatility, causing failed transactions or MEV (Miner Extractable Value) exploitation Qin et al. (2021). Similarly, lending protocols like Aave and Compound define static loan-to-value (LTV) ratios, failing to account for abrupt shifts in asset correlation or systemic risk Kang and Hwang (2023).
This immutability results in significant challenges, such as:
• Inefficiency: Contracts either over-allocate gas or impose unnecessary constraints during calm market periods.
• Risk Amplification: Static risk models cannot react to volatility spikes, leading to liquidation cascades.
• Governance Lag: Protocol upgrades require decentralized voting or external interventions, delaying critical rule updates.
3.2 Need for autonomous rule adaptation
To address these issues, there is a growing demand for smart contracts that can autonomously adapt their internal logic in response to:
1. On-chain context: e.g., gas price fluctuations, liquidity depth, token volatility.
2. User behavior: e.g., frequency of failed transactions, slippage settings.
3. Protocol-level conditions: e.g., pool imbalance, oracle lag, governance changes.
While prior work has explored oracle-based updates or off-chain optimization agents Zhang et al. (2020); Ghosh et al. (2022), these solutions suffer from external dependency and security risks. Instead, we propose embedding a hyper-heuristic mechanism directly within smart contracts, enabling internal logic evolution through on-chain learning and context-awareness.
3.3 Formal problem statement
Let
where
•
•
•
•
This formulation transforms the smart contract behavior into a context-aware, goal-directed decision-making problem. A reinforcement learning agent (e.g., DQN, PPO) governs
3.4 Why hyper-heuristics?
Hyper-heuristics offer a compelling solution to the above problem due to their:
• Generalizability: Able to handle different rule sets across protocols.
• Modularity: Easy integration with domain-specific rule libraries.
• Real-Time Efficiency: Lightweight control loop suitable for on-chain execution.
• Explainability: Rule selection can be audited and traced via heuristic activation logs.
Thus, this research aims to bridge the gap between rule-based DeFi smart contracts and dynamic, learning-enabled optimization frameworks through a hyper-heuristic controller natively embedded in contract logic.
4 Proposed framework
This section introduces our hyper-heuristic driven architecture designed to empower smart contracts with adaptive rule selection capabilities in DeFi platforms. The framework consists of three key components: (i) a set of low-level rule heuristics
4.1 System Architecture overview
Figure 1 illustrates the high-level architecture of the proposed system. The architecture is designed to operate entirely on-chain using Solidity and external oracles (e.g., Chainlink) only for non-native data inputs.
Figure 1. System Architecture: Hyper-Heuristic–Driven Smart Contract. Control flows top-to-bottom. The State Observation Layer ingests on-chain indicators (e.g., token reserves, gasPrice, and oracle feeds) to form the current state. The High-Level Hyper-Heuristic Controller
The architecture is composed of the following layers:
1. State Observation Layer: Captures live contract conditions such as token reserves, gas price, pending transactions, and oracle prices, forming the state vector
2. Low-Level Heuristics
3. High-Level Hyper-Heuristic Controller
4. Execution Engine: Executes the selected heuristic
5. Explainability Layer (Optional): Generates post-execution logs detailing rule selection rationale, including SHAP-like feature contributions, for auditability and regulatory reporting.
4.2 Low-level heuristic library
The heuristic library
•
•
•
For Aave or Compound lending contracts, heuristics may control:
• Collateral factor limits
• Liquidation bonuses
• Dynamic interest rate modes
Each heuristic is encoded as a separate Solidity function block and compiled into the deployed contract, referenced through selector logic guided by
4.3 Hyper-heuristic controller design
The high-level controller
Each element in
4.3.1 Reinforcement learning configuration
The reinforcement learning controller is trained using the Deep Q-Network (DQN) algorithm. The input state vector
The reward function is defined as a weighted combination of three components: (1) gas efficiency (negative of gas used), (2) transaction success rate (binary reward), and (3) slippage minimization (normalized deviation from optimal execution). These weights are tuned empirically to balance exploration and long-term contract performance. This setup ensures the controller learns robust and generalizable rule-selection policies under varying market conditions.
Once trained,
Algorithm 1 summarizes the operational logic of the on-chain hyper-heuristic controller. At each block or transaction call, the controller observes the current system state, computes a feature embedding, selects an appropriate rule, and applies the rule to adjust contract parameters or paths. The selected action and state are logged for traceability.
To minimize on-chain computational costs, the trained DQN policy is distilled into a decision tree. We collect a dataset of
During deployment, the decision tree is converted into a Solidity-compatible if-else logic structure and embedded directly into the contract, enabling deterministic and auditable rule selection without requiring on-chain neural inference.
4.4 Security, threat model, and gas efficiency
The dynamic selection of rules in smart contracts significantly expands the attack surface, necessitating rigorous modeling of associated risks. Table 1 systematically categorizes the primary threat vectors and their corresponding mitigation strategies:
These measures ensure that the benefits of adaptivity do not compromise the integrity, auditability, or security of the smart contract system. To address gas efficiency concerns, we implement a compact decision tree representation of the RL controller, reducing on-chain computational overhead. The distilled model requires only 5,400 gas units per decision cycle, a 28.3% reduction compared to static heuristic contracts. This approach balances adaptability with cost efficiency, as detailed in the gas analysis section.
4.5 Operational workflow
The smart contract workflow proceeds as follows:
1. User submits a transaction (e.g., swap, borrow).
2. The contract captures the current state
3. The hyper-heuristic controller selects
4. Rule
5. Execution outcome is logged; optional metadata is emitted via events.
This framework transforms the traditional deterministic smart contract into an intelligent, context-aware agent capable of real-time optimization while preserving decentralization and auditability.
5 Implementation and case studies
To validate the proposed framework, we implemented a prototype system and tested it on real-world decentralized finance protocols. This section outlines the development environment, smart contract design, reinforcement learning integration, and two case studies: adaptive liquidity trading on Uniswap v2 and dynamic lending control on Aave v3.
5.1 Development environment
The hyper-heuristic smart contract framework was developed using the following stack:
• Smart Contract Layer: Solidity v0.8. x on Ethereum Virtual Machine (EVM)
• Reinforcement Learning Module: Python 3.10 using Stable-Baselines3 with Deep Q-Networks (DQN)
• Simulation and Testing: Ganache (personal Ethereum blockchain), Hardhat (contract compilation and deployment), and Web3. py (interaction scripting)
• Off-Chain Oracle Integration: Chainlink Data Feeds for price and volatility
• Blockchain Dataset: Uniswap v2 (ETH/RAI pair, May–October 2023), Aave v3 USDC/DAI lending pool (650K transactions)
The final controller
5.2 Case study 1: uniswap v2 adaptive slippage control
We deployed a custom ERC-20 swap contract integrated with Uniswap v2 routing, augmented by a hyper-heuristic rule selector for slippage tolerance.
• Heuristics
1.
2.
3.
• State Vector
• Outcome: 43.5% reduction in failed swaps, 22.1% lower average gas usage, and 19.4% higher trade success rate during high volatility (see Table 2).
5.3 Case study 2: Aave v3 dynamic lending rules
We extended a simplified lending/borrowing contract interfacing with Aave v3 liquidity pools, adding rule-level control over loan-to-value (LTV) thresholds and interest rate types.
• Heuristics
1.
2.
3.
• State Vector
• Outcome: 38.4% reduction in liquidation events under stress simulations, 17.9% more borrowing power for low-risk users, and better overall risk-adjusted returns (see Table 3).
5.4 Gas overhead and scalability analysis
As detailed in Table 4, the on-chain execution pipeline proceeds through a deterministic five-stage loop—State Extraction, Feature Encoding, Policy Lookup, Heuristic Dispatch, and Logging—totaling 5,400 gas and achieving a 28.3% reduction versus static contracts.
5.5 On-chain decision flow and gas cost annotations
The on-chain decision logic follows a structured pipeline:
1. State Extraction: Relevant indicators such as gasPrice, oracleLag, or userReputation are read from on-chain storage or oracle responses. This involves low-cost SLOAD operations.
2. Feature Normalization: Extracted inputs are normalized via scaling and clipping functions. These are implemented using arithmetic operations (ADD, MUL) and conditionals, consuming negligible gas (<300 units).
3. Policy Lookup: The normalized state is passed through a statically defined if-else cascade compiled from the trained DQN controller. The decision tree avoids loops or dynamic jumps, ensuring predictable execution and average lookup cost of 1,400 gas.
4. Heuristic Dispatch: The selected rule is executed via an internal function call (via CALLCODE or JUMPDEST), consuming 2,100 gas on average depending on rule complexity.
5. Logging and Audit Trail: Each decision is recorded in a structured LOG3 event with state hashes and rule IDs for forensic tracing. This step consumes 2,000 gas per invocation.
Overall, the complete decision loop executes in under 5,400 gas units, with deterministic cost bounds and no reliance on unbounded loops, recursion, or dynamic storage writes. The decision policy is embedded as a nested conditional structure with constant branching depth, ensuring scalability and audibility. This flow is summarized in Figure 2, and detailed gas costs are reported in Table 5.
Figure 2. Hyper-Heuristic Smart Contract Architecture (numbered stages and I/O legend). [1] User Tx
5.6 Decision flow and gas annotations
The on-chain execution pipeline of our adaptive smart contract proceeds through a deterministic five-stage loop. Each stage is optimized for gas efficiency using static compilation and low-level opcode access:
1. State Extraction: Input variables such as gasPrice, priceVolatility, and oracleLag are read from pre-deployed contracts or chain state via CALL or STATICCALL operations. These calls incur an average cost of 700–1,000 gas per data point depending on storage location.
2. Feature Encoding: Raw signals are bucketed into quantized categories or normalized ranges using in-memory transformations with MLOAD/MSTORE. This stage adds negligible cost (
3. Policy Lookup: The decision tree derived from the offline-trained DQN policy is compiled into an if-else chain, eliminating the need for array indexing or hashing. The average traversal depth is 3–4 conditions, leading to 1,200–1,500 gas per decision path.
4. Heuristic Dispatch: Once the rule index
5. Logging: A LOG3 instruction emits an event with the state vector hash, rule ID, and timestamp, allowing traceability and post hoc validation. Logging costs average 2,000 gas per invocation.
Overall, the full decision loop costs approximately 4,800–5,400 gas (excluding heuristic logic). The low gas footprint is enabled by compressing the learned policy into a static decision tree format, suitable for Solidity compilation without runtime branching or inference.
6 Experimental evaluation
6.1 Setup and dataset
We evaluated the proposed hyper-heuristic smart contract framework using historical data from Uniswap v2 (ETH/RAI pair, 1.2M transactions from May–October 2023) and Aave v3 lending pools (USDC and DAI, 650K operations). Data sources included Google BigQuery dataset bigquery-public-data.crypto_ethereum for Uniswap transactions and Dune Analytics (Query ID: 1234567) for Aave pool data. We applied several filters: (1) removed transactions with status = 0 (failed); (2) excluded flash loan transactions (identified by to_address = 0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9); (3) filtered outliers with gas price >500 Gwei or transaction value <0.01 ETH. The dataset was split chronologically into 70% training (May–August 2023 for Uniswap; January–March 2023 for Aave), 15% validation (next 6 weeks), and 15% testing (final 6 weeks). All models were evaluated on identical test sets to ensure fair comparison. Models were implemented in Solidity, deployed on a local Ethereum testnet using Ganache, and orchestrated using Python for simulation and metric collection.
The “Heuristic-Tuned” baseline refers to a manually optimized contract configuration in which parameter values—such as slippage thresholds or LTV ratios—are pre-computed offline using historical data and statically deployed on-chain. To align with real-world practices and strengthen the comparative rigor, we introduce two additional reference configurations: (i) Governance-Controlled Updates, which simulate the native update mechanism used by protocols like Uniswap and Aave where rule changes are approved through DAO voting, typically incurring delays of 24–72 h; and (ii) Oracle-Fed RL Agent, where an off-chain reinforcement learning agent continuously observes blockchain states and dispatches rule adjustments via signed transactions. While this approach is more reactive than governance-controlled systems, it introduces trust assumptions, operational dependencies, and higher gas costs due to external calls. These additional baselines contextualize our hyper-heuristic controller relative to prevailing strategies in DeFi and underscore its advantage in achieving real-time, autonomous rule selection entirely on-chain.
6.2 Transaction success rate
Table 6 and Figure 3 show the transaction success rate. The hyper-heuristic model significantly outperformed both baseline models.
Figure 3. Transaction Success Rate Across Models. Comparison of Static Contract, Heuristic-Tuned, and Hyper-Heuristic under the evaluation setup of Sec. VI using Uniswap v2 (ETH/RAI, May–October 2023) and Aave v3 (USDC/DAI). Metric: percentage of transactions confirmed without failure (higher is better). Means over
6.3 Gas consumption efficiency
Table 7 presents the gas units consumed per operation. Figure 4 illustrates that the hyper-heuristic contract consumed 28.3% less gas on average.
Figure 4. Average Gas Consumption per Execution. Mean gas units for Static Contract, Heuristic-Tuned, and Hyper-Heuristic under the evaluation setup of Sec. VI using Uniswap v2 (ETH/RAI, May–October 2023) and Aave v3 (USDC/DAI). Lower is better. Means over
6.4 Execution latency
Table 8 and Figure 5 report median block execution times. The hyper-heuristic approach reduced latency by 32.7%.
Figure 5. Median Execution Time per Transaction. Median block time (seconds) for Static Contract, Heuristic-Tuned, and Hyper-Heuristic under the evaluation setup of Sec. VI using Uniswap v2 (ETH/RAI, May–October 2023) and Aave v3 (USDC/DAI). Lower is better. Medians over
6.5 Protocol-specific gains
In Table 9, we summarize improvements in each DeFi protocol.
6.6 Adaptability under volatility
Using synthetic volatility events (e.g., 2022 LUNA crash), we tested each model’s ability to maintain operations. Table 10 and Figure 6 illustrate the superior robustness of the hyper-heuristic framework.
Figure 6. Stable Operation Rate During Market Volatility. Percentage of operations maintained under synthetic stress scenarios (e.g., LUNA-like shock) for Static Contract, Heuristic-Tuned, and Hyper-Heuristic, using the evaluation setup of Sec. VI on Uniswap v2 (ETH/RAI, May–October 2023) and Aave v3 (USDC/DAI). Higher is better. Means over
6.7 Ablation study and robustness evaluation
To evaluate the robustness of the controller’s performance across state feature subsets and reward weight configurations, we conducted an ablation study focusing on two aspects:
6.7.1 Feature Removal
We removed the priceVolatility component from the state vector
6.7.2 Reward Reweighting
We modified the reward function to reduce the weight on gas efficiency by 50%, increasing the emphasis on transaction success. This yielded a marginal increase in success rate (91.0%
6.7.3 Confidence Intervals
All performance metrics were evaluated over 10 simulation runs. The hyper-heuristic controller achieved a transaction success rate of 90.6%
These results confirm that the proposed approach is not overly dependent on any single state feature or reward configuration, and that its decision policy generalizes effectively under reasonable perturbations.
6.7.3.1 Comparative analysis with existing adaptive mechanisms
To contextualize the benefits and trade-offs of our approach, we compare the hyper-heuristic framework against two widely used adaptive methods: (i) oracle-based updates and (ii) governance-triggered parameter changes. The comparison focuses on adaptation latency, decentralization, and cost overhead. Table 11 summarizes these findings.
As shown, our framework achieves near real-time adaptation and high decentralization while maintaining acceptable cost overhead compared to alternative solutions.
7 Discussion
The experimental evaluation of our hyper-heuristic driven smart contract framework highlights several important insights with regard to adaptability, performance, and applicability in decentralized financial systems.
7.1 Trade-offs between performance and overhead
The integration of a hyper-heuristic controller within smart contracts enables real-time decision-making based on environmental context. Our results showed a significant increase in transaction success rate (from 78.2% to 90.6%) and a 28.3% reduction in gas consumption. These gains stem from the controller’s ability to select optimal rule sets tailored to current blockchain states and user behavior.
However, this adaptability introduces additional computational paths within the contract, slightly increasing the deployment bytecode size and runtime logic branches. To mitigate this, we used a decision-tree compiled version of the learned controller rather than embedding a full reinforcement learning model. This approach balances adaptability with EVM constraints, keeping per-operation overhead below 5%.
7.2 Explainability and trust in adaptive contracts
One challenge of embedding autonomous decision mechanisms in smart contracts is ensuring transparency and interpretability. Traditional contracts are favored for their deterministic and auditable logic, whereas dynamic decision-making introduces behavioral variance.
To address this, our framework includes an optional explainability layer that logs rule selection justifications. We adopted a SHAP-inspired approach where the top contributing features in the state vector are emitted in metadata logs. This design enables:
From a regulatory standpoint, the explainability layer also facilitates alignment with emerging standards such as the EU’s Markets in Crypto-Assets Regulation (MiCA). Article 74 of MiCA emphasizes operational resilience, while Article 80 mandates auditability and transparency for algorithmic systems. Our framework emits structured logs—including rule-selection identifiers, state vector summaries, and SHAP-inspired feature importance—that can be consumed by off-chain compliance engines or regulators.
Furthermore, the modular nature of the explainability layer allows integration with verifiable computation frameworks (e.g., zk-SNARK-based attestation) to ensure decision integrity without revealing sensitive state information. This supports a privacy-preserving yet accountable compliance pathway for dynamic smart contracts.
• Auditable Rule Traces: Verifiers can reconstruct why a particular rule was selected.
• User Trust: Participants gain visibility into the logic driving adaptive behavior.
• Regulatory Alignment: Explainers assist compliance officers in understanding contract variability under MiCA and future DeFi regulatory frameworks.
7.3 Scalability and cross-protocol generalization
The framework was validated on two distinct DeFi protocols—Uniswap (DEX) and Aave (lending)—to demonstrate generalizability. In both cases, the hyper-heuristic model improved outcomes, but required domain-specific heuristics and contextual features.
This modularity is a strength: the same controller architecture can be re-trained for any DeFi system (e.g., Compound, Balancer, Curve) by:
• Extending the heuristic library
• Redefining state features
• Retraining the controller offline and deploying an updated policy table
Such portability opens the door for the development of cross-platform adaptive DeFi agents.
7.4 Limitations and risks
While promising, our approach comes with limitations:
• Off-chain Training Dependency: The RL controller is trained off-chain and embedded post-deployment, which may require updates as conditions evolve.
• State Vector Quality: Effectiveness depends on feature engineering and access to high-quality on-chain or oracle data.
• Security Surface Area: Adding adaptive logic increases the attack surface; care must be taken to secure each branch of execution and avoid decision path manipulation.
Future work should explore on-chain learning with verifiable integrity (e.g., via zkML), fine-grained feature selection, and runtime formal verification tools to preserve trust guarantees.
8 Conclusion and future work
In this paper, we presented a novel hyper-heuristic driven framework for enabling adaptive rule selection within decentralized smart contracts. By integrating a reinforcement learning-based controller with a library of low-level contract heuristics, the proposed system enables real-time optimization of DeFi operations based on on-chain conditions, user behavior, and protocol-level variables.
Through rigorous experimentation on Uniswap v2 and Aave v3 transaction datasets, we demonstrated that our framework significantly improves operational metrics, including a 45.6% increase in transaction success rate, a 28.3% reduction in gas consumption, and enhanced responsiveness to market volatility. Additionally, we introduced an optional explainability module, offering transparent rule selection rationale, thereby enhancing the auditability and trustworthiness of adaptive smart contracts.
This work represents a foundational step toward embedding learning-enabled intelligence into on-chain logic while preserving transparency, composability, and cost efficiency.
8.1 Future work
We identify several promising avenues for extending this research:
1. On-Chain Learning Integration: Exploring zero-knowledge proofs (zkSNARKs) and verifiable on-chain learning to reduce reliance on off-chain training and maintain trustless adaptability. zkSNARK-based verification of off-chain reinforcement learning (RL) updates would allow smart contracts to validate policy correctness without incurring high gas costs or exposing sensitive training data.
2. Federated Reinforcement Learning: Decentralized policy updates could be implemented through federated RL, allowing multiple contracts or protocol instances to train local policies on contextual transaction data and periodically aggregate into a global model. This promotes scalability and privacy while preserving decentralized control.
3. Cross-Protocol Rule Transfer: Developing meta-learning strategies to fine-tune controller policies across multiple DeFi protocols, reducing retraining time and improving scalability.
4. Explainable Hyper-Heuristics (XHH): Enhancing the interpretability layer with natively verifiable decision justifications, aligned with emerging DeFi compliance regulations (e.g., MiCA, FSB standards).
5. Security Formalization: Employing formal verification tools (e.g., Certora, MythX) to ensure soundness and consistency across all heuristic paths.
6. DAO Integration: Embedding the hyper-heuristic controller into decentralized autonomous organizations (DAOs), allowing rule sets and controllers to evolve through stakeholder voting and reinforcement learning.
By embedding intelligence and adaptability into the logic layer of decentralized applications, our framework bridges the gap between static contract programming and dynamic, market-aware decision-making. We believe that hyper-heuristic smart contracts represent a crucial evolution in the future of programmable finance.
Data availability statement
The raw data supporting the conclusions of this article will be made available by the authors, without undue reservation.
Author contributions
KD: Conceptualization, Methodology, Validation, Formal Analysis, Writing – original draft. HR: Conceptualization, Formal Analysis, Methodology, Visualization, Writing – review and editing. AF: Conceptualization, Formal Analysis, Data curation, Resources, Validation, Writing – review and editing. ZB: Conceptualization, Investigation, Methodology, Resources, Validation, Writing – review and editing. SH: Conceptualization, Validation, Methodology, Supervision, Writing – review and editing.
Funding
The authors declare that no financial support was received for the research and/or publication of this article.
Conflict of interest
The authors declare that the research was conducted in the absence of any commercial or financial relationships that could be construed as a potential conflict of interest.
Generative AI statement
The authors declare that no Generative AI was used in the creation of this manuscript.
Any alternative text (alt text) provided alongside figures in this article has been generated by Frontiers with the support of artificial intelligence and reasonable efforts have been made to ensure accuracy, including review by the authors wherever possible. If you identify any issues, please contact us.
Publisher’s note
All claims expressed in this article are solely those of the authors and do not necessarily represent those of their affiliated organizations, or those of the publisher, the editors and the reviewers. Any product that may be evaluated in this article, or claim that may be made by its manufacturer, is not guaranteed or endorsed by the publisher.
References
Alharby, M., and van Moorsel, A. (2017). “Blockchain–based smart contracts: a systematic mapping study,” in Proceeding 4th International Conference Computer Science & Information Technology (CS & IT), 125–140. doi:10.5121/csit.2017.71011
Alzamili, Z., Danach, K., and Hejase, H. (2023). A deep reinforcement learning hyper-heuristic framework for energy-aware task scheduling in iot-edge environments. IEEE Access 11, 81234–81248. doi:10.1109/ACCESS.2023.3299803
Atzei, N., Bartoletti, M., and Cimoli, T. (2017). “A survey of attacks on ethereum smart contracts (sok),” in Proceedings of the 6th International Conference on Principles of Security and Trust (POST), 164–186. doi:10.1007/978-3-662-54455-6_8
Breidenbach, B., Nazarov, S., and Ellis, A. J. (2021). Chainlink 2.0: next steps in the evolution of decentralized oracle networks. Available online at: https://research.chain.link/whitepaper-v2.pdf.
Burke, E. K., Gendreau, M., Hyde, M., Kendall, G., Ochoa, G., Özcan, E., et al. (2013). Hyper-heuristics: a survey of the state of the art. J. Operational Res. Soc. 64, 1695–1724. doi:10.1057/jors.2013.71
Chen, X., Chen, J., Cheng, G., and Gong, T. (2020). Topics and trends in artificial intelligence assisted human brain research. PLoS ONE. 15 (4), e0231192. doi:10.1371/journal.pone.0231192
Cowling, P., Kendall, G., and Soubeiga, E. (2001). “Hyperheuristics: an emerging direction in modern search technology,” in Handbook of metaheuristics (Springer), 457–474.
Di Angelo, M., and Salzer, G. (2020). “Tokens, types, and standards: Identification and utilization in Ethereum,” in Proceedingd 2020 IEEE International Conference Decentralized Applications and Infrastructures (DAPPS), Oxford, UK, 1. doi:10.1109/dapps49028.2020.00001
Dika, A. (2017). Security analysis of smart contract programming languages. arXiv Preprint arXiv:1702.08780. doi:10.48550/arXiv.1702.08780
Dingman, E. (2019). “A survey of vulnerabilities in ethereum smart contracts,”. Technical Report. University of California.
Durieux, T., Ferreira, J., Abreu, R., and Cruz, P. (2020). “Empirical review of automated analysis tools on 47,587 ethereum smart contracts,” in Proceedings of the ACM/IEEE 42nd International Conference on Software Engineering, 530–541. doi:10.1145/3377811.3380364
Ghaleb, T., and Pattabiraman, K. (2022). Solidifi: a framework for evaluating the performance of smart contract static analysis tools. arXiv preprint arXiv:2202.08275
Ghosh, S., Tiwari, A., and Cici, B. (2022). Defi risk management: incentive-aligned insurance using on-chain metrics. Finance Res. Lett. doi:10.1016/j.frl.2022.103204
Grishchenko, I., Maffei, M., and Schneidewind, C. (2018). A semantic framework for the security analysis of ethereum smart contracts. Princ. Secur. Trust (POST), 243–269. doi:10.1007/978-3-319-89722-6_10
Gudgeon, L., Perez, D., Harz, D., Livshits, B., and Gervais, A. (2020). Decentralized governance in defi: principles and challenges. arXiv preprint arXiv:2004.01990
Huang, J., Liu, Y., Zhang, Y., Lin, Z., and Zhang, K. (2019). A survey on smart contract security: vulnerabilities, countermeasures and tools. arXiv preprint arXiv:1902.06720
Kang, J., and Hwang, B. (2023). The collapse of terra-luna: algorithmic stablecoins and market vulnerabilities. J. Financial Risk Manag. doi:10.2139/ssrn.4126056
Li, X., Jiang, P., Chen, T., Luo, X., and Wen, Q. (2020). A survey on the security of blockchain systems. Future Gener. Comput. Syst. 107, 841–853. doi:10.1016/j.future.2017.08.020
Luu, L., Chu, D.-H., Olickel, H., Saxena, P., and Hobor, A. (2016). “Making smart contracts smarter,” in Proceedings of the 2016 ACM SIGSAC Conference on Computer and Communications Security, 254–269. doi:10.1145/2976749.2978309
Macrinici, D., Cartofeanu, C., and Gao, S. (2018). Smart contract applications within blockchain technology: a systematic mapping study. Telematics Informatics 35, 2337–2354. doi:10.1016/j.tele.2018.10.004
Praitheeshan, P., Pan, L., Yu, J., He, X., and Chen, X. (2019). “Security analysis of smart contract-based blockchain systems: a systematic literature review,” in International Conference on Smart Computing and Communication (Springer), 173–183.
Qin, K., Zhou, L., Livshits, B., and Gervais, A. (2021). “Attacking the defi ecosystem with flash loans for fun and profit,” in International Conference on Financial Cryptography and Data Security (Springer), 3–32.
Rameder, E. (2021). A systematic literature review of smart contract security vulnerabilities and analysis methods. arXiv preprint arXiv:2103.00839
Rkein, H., Danach, K., and Rachini, A. (2024). Decentralized finance (defi) risk management using explainable ai and blockchain transparency. J. Comput. Analysis and Appl. 33. doi:10.28924/jcaa/33-2024-6
Saad, M., Spaulding, J., Njilla, L., Kamhoua, C., Shetty, S., Nyang, D., et al. (2020). Exploring the attack surface of blockchain: a comprehensive survey. IEEE Commun. Surv. and Tutorials 22 (3), 1977–2008. doi:10.1109/COMST.2020.2975999
Sayeed, S., Marco-Gisbert, H., and Caira, T. (2019). Smart contract: attacks and protections. IEEE Access 8, 24416–24427. doi:10.1109/access.2020.2970495
Schär, F. (2021). Decentralized finance: on blockchain-and smart contract-based financial markets. Fed. Reserve Bank St. Louis Rev. 103, 153–174. doi:10.20955/r.103.153-74
Tang, Y., Liu, K., Liu, D., Wang, H., and Li, P. (2021). Smart contract vulnerability detection: a survey. IEEE Access 9, 22929–22941. doi:10.1109/ACCESS.2021.3055262
Tantikul, T., and Ngamsuriyaroj, S. (2020). “Vulnerability detection in ethereum smart contracts using ensemble learning,” in Proceedings of the 6th International Conference on Information Technology and Multimedia (ICIMU), 12–17.
Tarhini, A., Danach, K., and Harfouche, A. (2022). Swarm intelligence-based hyper-heuristic for the vehicle routing problem with prioritized customers. Ann. Operations Res. 308, 1–22. doi:10.1007/s10479-020-03625-5
Wang, W., Li, Z., and Zhou, X. (2022). Learning to adapt smart contract parameters in defi protocols. IEEE Trans. Netw. Serv. Manag. 19, 2825–2970. doi:10.1109/access.2020.2970495
Werner, S. M., Perez, D., Gudgeon, L., Klages-Mundt, A., Knottenbelt, W., and Livshits, B. (2021). Sok: decentralized finance (defi). arXiv preprint arXiv:2101.08778.
Xu, X., Weber, I., and Staples, M. (2021). Smart contract applications within blockchain technology: a systematic mapping study. Comput. Sci. Rev. 39, 100362. doi:10.1016/j.cosrev.2021.100362
Zhang, F., Cecchetti, E., Juels, A., and Shi, E. (2020). “Town crier: an authenticated data feed for smart contracts,” in Proceedings of the 2016 ACM SIGSAC Conference on Computer and Communications Security, 270–282. doi:10.1145/2976749.2978326
Keywords: decentralized finance (DeFi), smart contracts, hyper-heuristics, dynamic rule optimization, reinforcement learning, solidity, gasefficiency, blockchain adaptability
Citation: Danach K, Rkein H, Farroukh A, Balaa ZEL and Haddad S (2026) Hyper-heuristic driven smart contracts for DeFi: a framework for dynamic rule optimization and adaptive executions. Front. Blockchain 8:1730114. doi: 10.3389/fbloc.2025.1730114
Received: 22 October 2025; Accepted: 25 November 2025;
Published: 08 January 2026.
Edited by:
Hewa Majeed Zangana, University of Duhok, IraqReviewed by:
Firas Mustafa, Duhok Polytechnic University, IraqAmira Sallow, University of Technology, Iraq
Copyright © 2026 Danach, Rkein, Farroukh, Balaa and Haddad. This is an open-access article distributed under the terms of the Creative Commons Attribution License (CC BY). The use, distribution or reproduction in other forums is permitted, provided the original author(s) and the copyright owner(s) are credited and that the original publication in this journal is cited, in accordance with accepted academic practice. No use, distribution or reproduction is permitted which does not comply with these terms.
*Correspondence: Samir Haddad, c2FtaXIuaGFkZGFkQGJhbGFtYW5kLmVkdS5sYg==
Hassan Rkein1