Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Allora Network machine learning #149

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 38 additions & 0 deletions typescript/packages/plugins/allora/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Goat Allora Plugin 🐐 (TypeScript)

(Allora Network)[https://allora.network] plugin for Goat. Provides developers with direct access to advanced, self-improving AI inferences, enabling high-performance insights without introducing additional complexity. Developers can create smarter and more efficient AI agents while maintaining the streamlined experience GOAT is known for.

## Installation

```
npm install @goat-sdk/plugin-allora
```

## Setup

```typescript
import { allora } from '@goat-sdk/plugin-allora'

const plugin = allora({
apiKey: process.env.ALLORA_API_KEY,
})
```

## Available Actions

### Fetch Price Prediction

Fetches the current trending cryptocurrencies.

## Goat

<div align="center">
Go out and eat some grass.

[Docs](https://ohmygoat.dev) | [Examples](https://github.com/goat-sdk/goat/tree/main/typescript/examples) | [Discord](https://discord.gg/goat-sdk)</div>

## Goat 🐐

Goat 🐐 (Great On-chain Agent Toolkit) is an open-source library enabling AI agents to interact with blockchain protocols and smart contracts via their own wallets.


40 changes: 40 additions & 0 deletions typescript/packages/plugins/allora/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@goat-sdk/plugin-allora-network",
"version": "0.1.0",
"files": [
"dist/**/*",
"README.md",
"package.json"
],
"scripts": {
"build": "tsup",
"clean": "rm -rf dist",
"test": "vitest run --passWithNoTests"
},
"sideEffects": false,
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"dependencies": {
"@goat-sdk/core": "workspace:*",
"axios": "1.7.9"
},
"peerDependencies": {
"@goat-sdk/core": "workspace:*"
},
"homepage": "https://ohmygoat.dev",
"repository": {
"type": "git",
"url": "git+https://github.com/goat-sdk/goat.git"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/goat-sdk/goat/issues"
},
"keywords": [
"ai",
"agents",
"web3",
"allora"
]
}
22 changes: 22 additions & 0 deletions typescript/packages/plugins/allora/src/allora.plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { PluginBase } from '@goat-sdk/core'
import { AlloraService } from './allora.service'
import { PricePredictionTimeframes } from './types'

interface AlloraPluginOptions {
apiKey: string
network: 'mainnet' | 'testnet'
}

export class AlloraPlugin extends PluginBase {
constructor({ apiKey }: AlloraPluginOptions) {
super('allora', [ new AlloraService(apiKey) ])
}

supportsChain = () => true
}

export function allora(options: AlloraPluginOptions) {
options.network = options.network || 'testnet'
return new AlloraPlugin(options)
}

31 changes: 31 additions & 0 deletions typescript/packages/plugins/allora/src/allora.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Tool } from '@goat-sdk/core'
import { createToolParameters } from '@goat-sdk/core'
import { z } from 'zod'
import { AlloraAPIClient } from './api'

export class AlloraService {
private readonly client: AlloraAPIClient

constructor(apiKey: string) {
this.client = new AlloraAPIClient(apiKey)
}

@Tool({
description: 'Fetch a future price prediction for a crypto asset from Allora Network. Specify 5 minutes from now `5m`, or 8 hours from now `8h`.',
})
async getPricePrediction(params: GetAlloraPricePredictionParams) {
const { ticker, timeframe } = params
const response = await this.client.fetchAlloraPricePrediction(ticker, timeframe)
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
return await response.json()
}
}

const alloraPricePredictionSchema = z.object({
ticker: z.enum(['BTC', 'ETH']).describe('The ticker of the currency for which to fetch a price prediction.'),
timeframe: z.enum(['5m', '8h']).describe('The timeframe for the prediction (currently, either "5m" or "8h").'),
})

type GetAlloraPricePredictionParams = z.infer<typeof alloraPricePredictionSchema>
74 changes: 74 additions & 0 deletions typescript/packages/plugins/allora/src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import path from 'path'
import axios from 'axios'
import { Token, SolanaTokenData } from './tokens'

export interface AlloraInferenceData {
network_inference: string
network_inference_normalized: string
confidence_interval_percentiles: string[]
confidence_interval_percentiles_normalized: string[]
confidence_interval_values: string[]
confidence_interval_values_normalized: string[]
topic_id: string
timestamp: number
extra_data: string
}

export interface AlloraAPIResponse {
request_id: string
status: boolean
data: {
signature: string
inference_data: AlloraInferenceData
}
}

export enum AlloraPricePredictionToken {
BTC = 'BTC',
ETH = 'ETH',
}

export enum AlloraPricePredictionTimeframe {
'5m' = '5m',
'8h' = '8h',
}

export class AlloraAPIClient {
private apiKey: string
private apiRoot: string

constructor(apiKey: string, apiRoot: string = 'https://api.upshot.xyz/v2/allora') {
this.apiKey = apiKey
this.apiRoot = apiRoot[apiRoot.length - 1] === '/' ? apiRoot.substr(0, apiRoot.length - 1) : apiRoot
}

public async fetchAlloraPricePrediction(
asset: AlloraPricePredictionToken,
timeframe: AlloraPricePredictionTimeframe,
): Promise<Partial<AlloraInferenceData>> {
const url = `consumer/price/ethereum-11155111/${asset}/${timeframe}`
const resp = await this.fetchAlloraAPIData(url)
if (!resp?.data?.inference_data) {
throw new Error(`API response missing data: ${JSON.stringify(resp)}`)
}
return resp.data.inference_data
}

private async fetchAlloraAPIData(endpoint: string): Promise<Partial<AlloraAPIResponse>> {
endpoint = endpoint[0] === '/' ? endpoint.substr(1) : endpoint

const url = `${this.apiRoot}/${endpoint}`
const response = await axios.get(url, {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'x-api-key': this.apiKey,
},
})
if (response.status >= 400) {
throw new Error(`Allora plugin: error requesting price prediction: url=${url} status=${response.status} body=${JSON.stringify(response.data, null, 4)}`)
}

return response.data
}
}
6 changes: 6 additions & 0 deletions typescript/packages/plugins/allora/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "../../../tsconfig.base.json",
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
Loading