Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ keywords:

Randomness is a critical component in blockchain applications. It allows fair and unpredictable outcomes for use cases like gaming, lotteries, and cryptographic protocols.

The most basic approach that you can use to generate a random number on EVM chains is to use block hashes, which combines the block hash with a user-provided seed and hashes them together. You can use the has that results as a pseudo-random generator seed. However, this approach has limitations. A validator that influences the random source used to compute transactions can manipulate the block hash. The block proposer can decide what to include into a block and can iterate through different combinations until they find a favorable random source.
The most basic approach that you can use to generate a random number on EVM chains is to use block hashes, which combines the block hash with a user-provided seed and hashes them together. You can use the hash that results as a pseudo-random generator seed. However, this approach has limitations. A validator that influences the random source used to compute transactions can manipulate the block hash. The block proposer can decide what to include into a block and can iterate through different combinations until they find a favorable random source.

[Chainlink VRF][chainlink-vrf] is a popular tool that improves on this. It provides another approach you can use to generate provably random values on Ethereum and other blockchains. It relies on a decentralized oracle network to deliver cryptographically secure randomness from off-chain sources. However, this dependence on external oracles introduces several weaknesses, such as cost, latency, and scalability concerns.

In contrast, Flow offers a simpler and more integrated approach with its native onchain Randomness Beacon at the protocol level, which eliminates reliance on external oracles and sidestepping their associated risks.
In contrast, Flow offers a simpler and more integrated approach with its native onchain Randomness Beacon at the protocol level, which eliminates reliance on external oracles and the need to sidestep their associated risks.

In addition to instant randomness that is available to any transaction (via `revertibleRandom` function), Flow provides a solution to the problem of a user reverting a transaction with an unfavorable outcome. Commit-Reveal schemes on Flow also rely on protocol-native secure randomness and they fix the issue of post-selection by trustless users. Commit-Reveal tools on Flow can be used within both Cadence and Consumer Decentralized Finance (DeFi) contracts. This tutorial focuses on Cadence.
In addition to instant randomness that is available to any transaction (via `revertibleRandom` function), Flow provides a solution to the problem of a user who reverts a transaction with an unfavorable outcome. Commit-Reveal schemes on Flow also rely on protocol-native secure randomness and they fix the issue of post-selection by trustless users. Commit-Reveal tools on Flow can be used within both Cadence and Consumer Decentralized Finance (DeFi) contracts. This tutorial focuses on Cadence.

## Objectives

Expand Down Expand Up @@ -55,7 +55,7 @@ To illustrate this concept, we will build a Coin Toss game on Flow, which demons

### What is the Coin Toss Game?

The Coin Toss Game is a decentralized betting game that showcases the commit-reveal pattern. Players place bets without knowing the random outcome, which ensures fairness and resistance to manipulation.
The Coin Toss Game is a decentralized betting game that showcases the commit-reveal pattern. Players place bets and don't know the random outcome, which ensures fairness and resistance to manipulation.

The game consists of two distinct phases:

Expand All @@ -80,13 +80,13 @@ In addition, commit-reveal patterns solve the issue of revertible randoms:

:::info

One of the powers of Cadence transactions is that a developer can set post-conditions that must be true, or the transaction will revert. This is very useful for scenarios such as guaranteeing a user receives their purchase in a complex and multi-step transaction, but it also means that they can set conditions to reject the transaction. In an instant-win lottery, this would allow users to test large numbers of tickets for a win without paying the purchase price.
One of the powers of Cadence transactions is that a developer can set post-conditions that must be true, or the transaction will revert. This is very useful for scenarios such as to guarantee a user receives their purchase in a complex and multi-step transaction, but it also means that they can set conditions to reject the transaction. In an instant-win lottery, this would allow users to test large numbers of tickets for a win without purchase price payment.

:::

## Build the Coin Toss contract

In this section, we'll walk through how to construct the `CoinToss.cdc` contract, which contains the core logic for the Coin Toss game. To function properly, the contract relies on supporting contracts and a proper deployment setup.
In this section, we'll walk through how to construct the `CoinToss.cdc` contract, which contains the core logic for the Coin Toss game. To function properly, the contract relies on support contracts and a proper deployment setup.

This tutorial will focus specifically on how to write and understand the `CoinToss.cdc` contract, while you can find additional setup details in the [original GitHub repo][github-repo].

Expand Down Expand Up @@ -136,7 +136,7 @@ access(all) fun flipCoin(bet: @{FungibleToken.Vault}): @Receipt {

### Step 3: Implement the reveal phase With `revealCoin`

Now we implement the reveal phase with the `revealCoin` function. Here, the caller provides the Receipt they recieve at commitment. The contract then "flips a coin" with `_randomCoin()` providing the Receipt's contained Request. The reveal step is possible only when the protocol random source at the committed block height becomes available.
Now we implement the reveal phase with the `revealCoin` function. Here, the caller provides the Receipt they recieve at commitment. The contract then "flips a coin" and `_randomCoin()` provides the Receipt's contained Request. The reveal step is possible only when the protocol random source at the committed block height becomes available.

If result is 1, the user loses, but if it's 0, the user doubles their bet. Note that the caller could condition the revealed transaction, but they've already provided their bet amount, so there's no loss for the contract if they do.

Expand Down Expand Up @@ -213,14 +213,14 @@ transaction(betAmount: UFix64) {

![remix5-sc](./imgs/remix5.png)

5. Track it: You can take the transaction id to [FlowDiver][flow-diver][.io](https://testnet.flowdiver.io/tx/9c4f5436535d36a82d4ae35467b37fea8971fa0ab2409dd0d5f861f61e463d98) to have a full view of everything that's going on with this `FlipCoin` transaction.
5. Track it: You can take the transaction id to [FlowDiver][flow-diver][.io] to have a full view of everything about this `FlipCoin` transaction.

### Reveal the coin toss result

Let's reveal the outcome of your coin toss to see if you've won. This step uses the receipt from your bet, so ensure you use the same account that placed the bet. Here's how to do it:

1. Return to your Dev Environment: Open [run.dnz][run-dnz] again.
2. Enter the Reveal Code: Paste the following Cadence transaction into the editor:
2. Enter the Reveal Code. Paste the following Cadence transaction into the editor:

```cadence
import FlowToken from 0x7e60df042a9c0868
Expand Down Expand Up @@ -249,7 +249,7 @@ transaction {
}
```

After we run this transaction, we reveal the result of the coin flip and it's 1! It means we haven't won anything this time, but keep trying!
After we run this transaction, we reveal the result of the coin flip and it's 1! It means we haven't won anything this time, but we'll try again!

You can find the full transaction used for this example, with its result and events, at [FlowDiver.io/tx/][flow-diver-tx].

Expand All @@ -276,6 +276,7 @@ When you harness the built-in randomness capabilities on Flow, you can create en
[flow-docs]: https://developers.flow.com
[flow-diver]: https://testnet.flowdiver.io/
[github-repo]: https://github.com/onflow/random-coin-toss
[.io]: https://testnet.flowdiver.io/tx/9c4f5436535d36a82d4ae35467b37fea8971fa0ab2409dd0d5f861f61e463d98
[run-dnz]: https://run.dnz.dev/
[coin-toss-contract]: https://contractbrowser.com/A.b6c99d7ff216a684.CoinToss
[coin-toss-contract-code]: https://github.com/onflow/random-coin-toss/blob/main/contracts/CoinToss.cdc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ sidebar_label: Using AgentKit on Flow
sidebar_position: 2
---

# Getting Started with AgentKit on Flow
# Build Custom AI Agents on Flow with AgentKit

AgentKit is an ecosystem-agnostic modular developer toolkit that lets you rapidly build, deploy, and iterate on AI agents using pre-configured environments and ready-to-use templates.

In this guide, you'll set up your own custom agent that runs on **Flow's EVM-compatible testnet**, powered by **Langchain** and **Anthropic's Claude** LLM.

---

## Quickstart - Starting From Scratch
## Quickstart - start from scratch

Open your terminal and run:

Expand All @@ -36,7 +36,7 @@ Follow the interactive setup:

---

## Project Setup
## Project setup

When your scaffold is ready:

Expand All @@ -45,7 +45,7 @@ cd onchain-agent
npm install
```

Now open the project in your preferred integrated development environment (IDE) (for example, [Cursor](../cursor/index.md).
Now open the project in your preferred integrated development environment (IDE) (for example, [Cursor].

### Environment Configuration

Expand All @@ -56,7 +56,7 @@ Now open the project in your preferred integrated development environment (IDE)

### Get Your Anthropic API Key

- Head to [Anthropic Console](https://console.anthropic.com/dashboard).
- Head to [Anthropic Console].
- Create an account and **purchase credits**.
- Click **Create Key**, name it, and copy the API key.
- Add this to your `.env.local`:
Expand All @@ -65,10 +65,10 @@ Now open the project in your preferred integrated development environment (IDE)
ANTHROPIC_API_KEY=your_api_key_here
```

### Wallet Setup with MetaMask
### Wallet setup with MetaMask

1. Add [Flow Testnet](https://developers.flow.com/evm/using) to MetaMask.
2. Use the [Faucet](https://faucet.flow.com/fund-account) to fund your wallet.
1. Add [Flow Testnet] to MetaMask.
2. Use the [Faucet] to fund your wallet.
3. Get your private key:
- Click the `...` menu in MetaMask > **Account Details**.
- Enter your password, copy the private key.
Expand Down Expand Up @@ -104,11 +104,11 @@ http://localhost:3000

If your agent doesn't respond yet, no worries! You still need to configure your **LLM and client libraries**.

### Choose a Model
### Choose a model

Langchain supports many LLMs ([full list here](https://python.langchain.com/docs/integrations/llms/)).
Langchain supports many LLMs ([full list here]).

For this example, we'll use **Anthropic's `claude-3-5-haiku-20241022`**, a lightweight and affordable model. Alternatively, [DeepSeek](https://deepseek.com/) is highly recommended for budget-friendly usage.
For this example, we'll use **Anthropic's `claude-3-5-haiku-20241022`**, a lightweight and affordable model. Alternatively, [DeepSeek] is highly recommended for budget-friendly usage.

### Update `create-agent.ts`

Expand All @@ -134,9 +134,9 @@ npm install @langchain/anthropic

---

## Configure Flow and Viem Wallet
## Configure Flow and Viem wallet

### Update the Faucet Provider Logic
### Update the Faucet provider logic

Change this:

Expand All @@ -150,7 +150,7 @@ To:
const canUseFaucet = walletProvider.getNetwork().networkId == 'flow-testnet';
```

### Add Flow Context Message to Agent
### Add Flow context message to Agent

This gives your agent context about the Flow testnet:

Expand Down Expand Up @@ -196,11 +196,11 @@ agent = createReactAgent({

---

## You're Done!
## You're done!

You now have a working AI agent connected to Flow testnet using AgentKit!
You now have a working AI agent connected to Flow testnet with AgentKit!

You can send faucet tokens to your wallet and start testing smart contract interactions or onchain workflows.
You can send faucet tokens to your wallet and start to test smart contract interactions or onchain workflows.

---

Expand All @@ -214,11 +214,11 @@ This starter includes all of the necessary configurations to start building imme

---

## Adding AgentKit to an Existing Project
## Add AgentKit to a current project

Already have a project and want to add AgentKit? Follow these steps to integrate it into your codebase:

### Install the Package
### Install the package

Run this command in your project's root directory:

Expand All @@ -228,11 +228,11 @@ npm install onchain-agent@latest

This will:

- Download and install the latest version of the `onchain-agent` package
- Add it to the dependencies section of your `package.json`
- Update your `node_modules` folder accordingly
- Download and install the latest version of the `onchain-agent` package.
- Add it to the dependencies section of your `package.json`.
- Update your `node_modules` folder accordingly.

### Configure Environment
### Configure environment

1. Create or update your `.env` file with the necessary API keys:

Expand All @@ -249,7 +249,7 @@ FLOW_TESTNET_RPC_URL=https://testnet.evm.nodes.onflow.org
FLOW_MAINNET_RPC_URL=https://mainnet.evm.nodes.onflow.org
```

### Integrate AgentKit in Your Code
### Integrate AgentKit in your code

Import and configure AgentKit in your application:

Expand Down Expand Up @@ -284,7 +284,7 @@ const agent = createReactAgent({
// ...
```

### Add Specialized Tools (Optional)
### Add Specialized tools (optional)

To add specialized blockchain tools to your agent:

Expand Down Expand Up @@ -314,11 +314,25 @@ const agent = createReactAgent({

## Resources

- [AgentKit Docs](https://docs.cdp.coinbase.com/agent-kit/welcome)
- [Flow EVM Guide](https://developers.flow.com/evm/using)
- [Langchain LLM Integrations](https://python.langchain.com/docs/integrations/llms/)
- [Anthropic Model Comparison](https://docs.anthropic.com/en/docs/about-claude/models/all-models#model-comparison-table)
- [AgentKit Docs]
- [Flow EVM Guide]
- [Langchain LLM Integrations]
- [Anthropic Model Comparison]

---

Happy hacking on Flow!

<!-- Relative links, will not render on page -->

[Cursor]: ../cursor/index.md
[Anthropic Console]: https://console.anthropic.com/dashboard
[Flow Testnet]: https://developers.flow.com/evm/using
[Faucet]: https://faucet.flow.com/fund-account
[full list here]: https://python.langchain.com/docs/integrations/llms/
[DeepSeek]: https://deepseek.com/
[Fork the Flow AgentKit Starter]: https://github.com/Aliserag/flow-agentkit-starter
[AgentKit Docs]: https://docs.cdp.coinbase.com/agent-kit/welcome
[Flow EVM Guide]: https://developers.flow.com/evm/using
[Langchain LLM Integrations]: https://python.langchain.com/docs/integrations/llms/
[Anthropic Model Comparison]: https://docs.anthropic.com/en/docs/about-claude/models/all-models#model-comparison-table
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ keywords:

Plugins are a powerful way to extend the functionality of your Eliza AI agents. This guide will walk you through the process of how to create custom plugins that can enhance your agent's capabilities, from simple utilities to complex integrations with external services. You'll learn how to leverage the plugin system to create modular and reusable components for your AI agents.

## Learning Objectives
## Learning objectives

After you complete this tutorial, you will be able to:

Expand All @@ -33,21 +33,21 @@ After you complete this tutorial, you will be able to:

Before you get started with Eliza, make sure you have:

- [Node.js 23+] (using [nvm] is recommended)
- [Node.js 23+] (we recommend that you use [nvm])
- [pnpm 9+]
- Git for version control
- A code editor ([VS Code], [Cursor] or [VSCodium] recommended)
- A code editor (we recommend [VS Code], [Cursor] or [VSCodium])
- [Flow-cli] for Flow blockchain interaction.

> **Note for Windows Users:** [WSL 2] is required.
> **Note for Windows users:** [WSL 2] is required.

## Quickstart

Follow the [Quickstart Guide] to set up your development environment.

## Plugin Development
## Plugin development

### Create a Plugin repository from Template
### Create a plugin repository from Template

Visit [Eliza Plugin Template] and click "Use this template" to create a new repository.

Expand All @@ -59,7 +59,7 @@ Flow's Eliza plugin template uses Dependency Injection(`@elizaos-plugins/plugin-

:::

### Add the Plugin repository to your Eliza project
### Add the plugin repository to your Eliza project

Let's say you created a repository named `username/plugin-foo`.

Expand Down Expand Up @@ -93,7 +93,7 @@ Check the `agent/package.json` to make sure the plugin is added. You'll see some
}
```

### Build the Plugin
### Build the plugin

Build the plugin with the following command:

Expand All @@ -104,9 +104,9 @@ pnpm build --filter ./packages/plugin-foo
pnpm build
```

### Add Plugin to the `character.json` you want to use
### Add the plugin to the `character.json` you want to use

Let's say you want to add the plugin to the `sample` character which is `characters/sample.character.json`.
Let's say you want to add the plugin to the `sample` character, which is `characters/sample.character.json`.

```json
{
Expand All @@ -119,7 +119,7 @@ Let's say you want to add the plugin to the `sample` character which is `charact

:::warning

If you use Dependency Injection(`@elizaos-plugins/plugin-di`) in your plugin, remember to add it to the `postProcessors` field. And **`clients` field is deprecated** in the latest version of Eliza, so if you want to add clients, you also need to use `plugins` field.
If you use Dependency Injection(`@elizaos-plugins/plugin-di`) in your plugin, remember to add it to the `postProcessors` field. The **`clients` field is deprecated** in the latest version of Eliza, so if you want to add clients, you also need to use `plugins` field.

:::

Expand All @@ -136,7 +136,7 @@ If you use Dependency Injection(`@elizaos-plugins/plugin-di`) in your plugin, re
}
```

### Run the Eliza Agent with your Plugin
### Run the Eliza agent with your plugin

Run the Eliza agent to test the plugin.

Expand All @@ -147,7 +147,7 @@ pnpm start --character="characters/sample.character.json"
pnpm start:debug --character="characters/sample.character.json"
```

### Interact with the Agent
### Interact with the agent

Now, you're ready to start a conversation with your agent.

Expand All @@ -157,7 +157,7 @@ Open a new terminal window and run the client's http server.
pnpm start:client
```

## Plugin Registration
## Plugin registration

You need to register your plugin in the [Eliza Plugin Registry] to make it available for other users.

Expand Down
Loading