> 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

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="/files/QOBCQnmm32XPZzyiXmlB" 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
