# Getting Started with Solidity - Automate

Ditto Automate provides a connection bridge to the The Ditto Entry Point (DEP). DEP is a contract designed to facilitate the routing of workflows and validating the desired intents based on the smart contract policies. The created and registered workflows are then monitored by the Ditto execution layer, a collection of operators that strive to execute the registered workflows as soon as feasible. As a developer, you can take advantage of our automation framework to create DEP-compatible automations.

As Ditto Execution Layer is undergoing huge migration to Meta Automations the documentation will undergo big changes. Please contact us if you desire to integrate the execution layer into your dApp.

Main automation playground repository:

{% embed url="<https://github.com/dittonetwork/automate/tree/templates>" %}

Example automated contract utilizing automation repository:

{% embed url="<https://github.com/dittonetwork/automate-example>" %}

### Automation Setup for Solidity Developers

Solidity developers can clone the [example](https://github.com/dittonetwork/automate-example) automation repository. After cloning, run `forge install` to install the Ditto automation library.

In the `src` folder, you will find two contracts:

* `Mock`: This contract stores a value that can be incremented on every even block.
* `AutomateCounter`: This contract inherits from `DittoAutomate` and enables easy, permissionless execution.

In the `test` folder, you will see an example of a full integration test in a forked environment on Polygon.&#x20;

To run the integration test, use the following command to set up rpc url:

```bash
export POL_RPC_URL=https://polygon.llamarpc.com
```

And start the tests:

```bash
forge test
```

### Overview

The DittoAutomateIOFramework V1 is a comprehensive automation solution for smart contracts. Below is an overview of its components:

<figure><img src="https://2531562015-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBdJzcIn8Wor30K36G8eP%2Fuploads%2FCnEqwWP5UWkOxdkxDQTg%2Fimage.png?alt=media&#x26;token=bf6b354d-b06d-417f-b9f1-9e0afa95b1d8" alt=""><figcaption></figcaption></figure>

#### Key Components:

1. **Automate.sol**: The parent contract to inherit from, providing core automation functionality.
2. **DEP (Ditto Entry Point)**: The central contract managing workflow execution and integration.
3. **Custom Contracts**: Contracts that inherit from Automate.sol to implement specific automation logic.
4. **CheckerBase**: The base contract for implementing custom checkers, including:
   * Time Checker
   * HF (Health Factor) Checker
5. **V1 Showcase: AutomateOracleMock**: A demonstration of Automate.sol combined with TimeChecker.
6. **AutomateBaseTest V1**: Allows for seamless, close-to-production fork tests of any inherited contracts.
7. **Templates:** Examples of utilization of DeFi strategies powered with Ditto Execution Layer via AA modules: DCA, LimitOrder.

### Terms and Definitions

DEP : The Ditto contract enables the execution of real-time automations based on user requests.

Automation : A contract tailored to your personal needs that executes actions on the blockchain using DEP control.

Checker : An additional verification contract that your automation references to ensure that the necessary conditions have been met.

### What is Automation?

Automation consists of checkers and action. Checkers allow or revert the workflow executions signaling to execution network that the conditions are not met. To perform a check, it must be initialized beforehand. Checkers are optional, so you can create automations without checks.

#### Automate.sol

A contract that can be inherited to create your own automations. [Automate.sol](https://github.com/dittonetwork/automate/blob/master/src/connect/Automate.sol) interacts with DittoEntryPoint by creating automation on DittoEntryPoint. Then the executors give the DittoEntryPoint command to execute one or another automation on Automate.sol.

The [AutomateOracleMock.sol](https://github.com/dittonetwork/automate/blob/master/src/workflows/AutomateOracleMock.sol) contract shows you how to properly inherit Automate.sol in order to create your own automation that will perform the tasks you need.

#### Automate7579.sol

The module of the [EIP-7579 standard](https://eips.ethereum.org/EIPS/eip-7579#modules). The contract works as a layer between DittoEntryPoint and account abstraction of the [EIP-4337 standard](https://eips.ethereum.org/EIPS/eip-4337). [Automate7579.sol](https://github.com/dittonetwork/automate/blob/master/src/connect/Automate7579.sol) offers a unified option for creating and executing automations on account abstraction.

#### AutomateOracleMock.sol

An example of how you can inherit an Automate.sol contract.

The contract will rewrite the test data through the function updateTimestamp. This function can only be called by the contract itself. This is indicated by the onlyItself modifier.

{% code title="AutomateOracleMock.sol" %}

```solidity
function updateTimestamp() external onlyItself {
      // Update the mapping with the current block number and timestamp
      blockTimestamps[block.number] = block.timestamp;
}
```

{% endcode %}

The main function in the contract creates automation with 60 seconds interval time checker, that will trigger the function on automated contract.

{% code title="AutomateOracleMock.sol" %}

```solidity
function startOracle() external onlyOwner {
        // getting the ID for the checker
        uint256 checkerId = CheckerTime(CHECKER).getNextCheckerId(address(this));

        // forming a calldata for initializing the checker
        CallInfo[] memory inits = new CallInfo[](1);
        bytes memory timeCheckerInitializeOnTarget =
            abi.encodeCall(CheckerTime.initialize, (uint128(block.timestamp), 60)); // Update interval in seconds
        inits[0] = CallInfo({ target: address(CHECKER), value: 0, callData: timeCheckerInitializeOnTarget });
        // Prepare the actions for the workflow
        CallInfo[] memory actions = new CallInfo[](2);
        // calldata to check if enough time has passed
        bytes memory checkTimeOnTarget = abi.encodeCall(CheckerTime.checkTime, (checkerId));
        actions[0] = CallInfo({ target: address(CHECKER), value: 0, callData: checkTimeOnTarget });
        // calldata for calling updateTimestamp
        bytes memory updateTimestampOnTarget = abi.encodeCall(this.updateTimestamp, ());
        actions[1] = CallInfo({ target: address(this), value: 0, callData: updateTimestampOnTarget });

        // adding automation to DittoEntryPoint
        _activateWorkflow(
            inits,
            actions,
            ORACLE_UPDATE_WORKFLOW_KEY,
            MAX_GAS_LIMIT_WORKFLOW, // Max gas limit
            MAX_GAS_PRICE_WORKFLOW, // Max gas price
            3 // 3 executions
        );
    } 
```

{% endcode %}

#### AutomateSendMock.sol

A simplified version of [AutomateOracleMock.sol](https://github.com/dittonetwork/automate/blob/master/src/workflows/AutomateOracleMock.sol) that does not include checkers.

#### DexModule7579.sol

The module makes swaps for account abstraction with built-in protection against MEV attacks. Before using the module, it must be installed on account abstraction according to the [EIP-7579 standard](https://eips.ethereum.org/EIPS/eip-7579#modules).

#### AaveModule7579.sol

With this module, account abstraction can easily interact with the Aave protocol. The module has a function that can make an emergency repayment of the debt, for this you need to specify only the supplyToken and borrowToken.

### How to use framework

There are contracts in the repository that are not recommended to be changed:

* src/base
* src/connect

They should be considered as basic for creating automation.

The src/modules/checkers folder contains examples of checkers that you can use as-is or modify to suit your needs. Additionally, you have the option to create your own checkers tailored specifically to your automation requirements.

The src/modules directory will include functionality for working with DeFi logic and is open to further enhancements.

You can create new types of automation by studying AutomateOracleMock.sol as an example.

In the tests, we have prepared AutomateBaseTest, which allows for integration testing with DEP on forks:

* testing of checkers;
* testing the payPrefund function;
* testing the runWorkflow function;

AutomateBaseTest identifies valid executors and simulates their execution on your contracts.
