Skip to content

Commit

Permalink
Allora plugin PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
spooktheducks committed Dec 30, 2024
1 parent 9887d4b commit 7305646
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
4 changes: 2 additions & 2 deletions typescript/packages/plugins/allora/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 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.
[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

Expand All @@ -22,7 +22,7 @@ const plugin = allora({

### Fetch Price Prediction

Fetches the current trending cryptocurrencies.
Fetches a price prediction for the given asset and timeframe.

## Goat

Expand Down
6 changes: 3 additions & 3 deletions typescript/packages/plugins/allora/src/allora.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { AlloraService } from './allora.service'
import { PricePredictionTimeframes } from './types'

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

export class AlloraPlugin extends PluginBase {
Expand All @@ -16,7 +16,7 @@ export class AlloraPlugin extends PluginBase {
}

export function allora(options: AlloraPluginOptions) {
options.network = options.network || 'testnet'
options.rpcEndpoint = options.rpcEndpoint || 'https://api.upshot.xyz/v2/allora'
return new AlloraPlugin(options)
}

24 changes: 10 additions & 14 deletions typescript/packages/plugins/allora/src/allora.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,22 @@ import { AlloraAPIClient } from './api'
export class AlloraService {
private readonly client: AlloraAPIClient

constructor(apiKey: string) {
constructor(apiKey: string | undefined) {
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()
async getPricePrediction(parameters: GetAlloraPricePredictionParameters) {
const { ticker, timeframe } = parameters
return this.client.fetchAlloraPricePrediction(ticker, timeframe)
}
}

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>
export class GetAlloraPricePredictionParameters extends createToolParameters(
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").'),
})
) {}
31 changes: 19 additions & 12 deletions typescript/packages/plugins/allora/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,25 @@ export enum AlloraPricePredictionTimeframe {
'8h' = '8h',
}

export enum AlloraPricePredictionSignatureFormat {
EthereumSepolia = 'ethereum-11155111'
}

export class AlloraAPIClient {
private apiKey: string
private apiKey: string | null | undefined
private apiRoot: string

constructor(apiKey: string, apiRoot: string = 'https://api.upshot.xyz/v2/allora') {
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,
asset: AlloraPricePredictionToken,
timeframe: AlloraPricePredictionTimeframe,
signatureFormat: AlloraPricePredictionSignatureFormat = AlloraPricePredictionSignatureFormat.EthereumSepolia,
): Promise<Partial<AlloraInferenceData>> {
const url = `consumer/price/ethereum-11155111/${asset}/${timeframe}`
const url = `consumer/price/${signatureFormat}/${asset}/${timeframe}`
const resp = await this.fetchAlloraAPIData(url)
if (!resp?.data?.inference_data) {
throw new Error(`API response missing data: ${JSON.stringify(resp)}`)
Expand All @@ -58,13 +63,15 @@ export class AlloraAPIClient {
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,
},
})
const headers: Record<string, string> = {
'Content-Type': 'application/json',
'Accept': 'application/json',
}
if (!!this.apiKey) {
headers['x-api-key'] = this.apiKey
}

const response = await axios.get(url, { headers })
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)}`)
}
Expand Down

0 comments on commit 7305646

Please sign in to comment.