> For the complete documentation index, see [llms.txt](https://docs.polystream.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.polystream.xyz/project-architecture/cranker.md).

# Cranker

Cranker with chainlink automation upkeep

Chainlink Automation provides a decentralized network of automated agents (keepers) that trigger on-chain smart contract executions based on predefined conditions, eliminating manual intervention and operational risks.

#### Use Cases in Polystream

* **Epoch Management:**\
  Automated triggering of epoch changes to handle yield harvesting, yield optimization, and state updates at consistent intervals.
* **Reward Distributions:**\
  Automating distribution and accounting of accrued yield rewards at each epoch boundary to ensure timely and fair distribution.<br>

### How it Works (Technical Flow)

1. **Condition Definition:**\
   On-chain conditions (`checkUpkeep`) defined in the smart contract specify precisely when automation triggers should activate.

```solidity
solidityCopyEditfunction checkUpkeep(bytes calldata) external view returns (bool upkeepNeeded, bytes memory performData) {
    upkeepNeeded = (block.timestamp >= lastEpochTime + EPOCH_DURATION);
}
```

2. **Automated Execution:**\
   Once conditions are met, Chainlink Automation calls the `performUpkeep` function on your deployed contract:

```solidity
solidityCopyEditfunction performUpkeep(bytes calldata) external {
    checkAndHarvest(); // Harvest yield and distribute rewards
    optimizeYield();   // Automatically selects best yielding protocol
}
```

3. **$LINK Token Payments:**\
   Each automation execution consumes Chainlink's `$LINK` tokens from the project's Automation balance, compensating the keepers for their service.<br>

<figure><img src="https://511560137-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBRX14z25wLMxFcY12pGf%2Fuploads%2FMva0inEBcn8j5ANfgifO%2Fimage.png?alt=media&amp;token=c7b51428-2275-4989-a620-b1b627258ca1" alt=""><figcaption><p>Chainlink Automation dashboard showing periodic <code>performUpkeep</code> executions and $LINK payments</p></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.polystream.xyz/project-architecture/cranker.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
