Skip to content

Commit

Permalink
Add feature plugin-Jsonrpc (#159)
Browse files Browse the repository at this point in the history
* Add feature jsonrpc pugin

* updates package

* update-package.json

* Fix PR

---------

Co-authored-by: Agustin Armellini Fischer <[email protected]>
  • Loading branch information
lukrycyfa and 0xaguspunk authored Jan 2, 2025
1 parent e6ae292 commit 34596ce
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 2 deletions.
4 changes: 4 additions & 0 deletions goat.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
"name": "[Plugin] 🪐 jupiter",
"path": "./typescript/packages/plugins/jupiter"
},
{
"name": "[Plugin] ♏︎ jsonrpc",
"path": "./typescript/packages/plugins/jsonrpc"
},
{
"name": "[Plugin] ⛽️ pumpfun",
"path": "./typescript/packages/plugins/pumpfun"
Expand Down
36 changes: 36 additions & 0 deletions typescript/packages/plugins/jsonrpc/README .md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Goat JsonRpc Plugin 🐐 - TypeScript

JsonRpc plugin for [Goat 🐐](https://ohmygoat.dev). Use this plugin to make Rpc calls to endpoints.

## Installation
```
npm install @goat-sdk/plugin-jsonrpc
```

## Usage

```typescript
import { jsonrpc } from "@goat-sdk/plugin-jsonrpc";


const plugin = jsonrpc({
endpoint: process.env.ENDPOINT_URL as string,
});
```

## Available Actions

### Call A Method
- Make rpc call to an on chain method using an agent and a chat model e.g input
-`make a JSONRpc call with this method {{ eth_blockNumber }}`


## 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/2F8zTVnnFz)</div>

## Goat 🐐
Goat 🐐 (Great Onchain Agent Toolkit) is an open-source library enabling AI agents to interact with blockchain protocols and smart contracts via their own wallets.
33 changes: 33 additions & 0 deletions typescript/packages/plugins/jsonrpc/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@goat-sdk/plugin-jsonrpc",
"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:*",
"zod": "catalog:"
},
"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"],
"packageManager": "[email protected]"
}
1 change: 1 addition & 0 deletions typescript/packages/plugins/jsonrpc/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./jsonrpc.plugin";
18 changes: 18 additions & 0 deletions typescript/packages/plugins/jsonrpc/src/jsonrpc.plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Chain, PluginBase, WalletClientBase } from "@goat-sdk/core";
import { JSONRpcService } from "./jsonrpc.service";

export type JSONRpcOptions = {
endpoint: string;
};

export class JSONRpcPlugin extends PluginBase<WalletClientBase> {
constructor({ endpoint }: JSONRpcOptions) {
super("jsonrpc", [new JSONRpcService({ endpoint })]);
}

supportsChain = (chain: Chain) => true;
}

export function jsonrpc({ endpoint }: JSONRpcOptions) {
return new JSONRpcPlugin({ endpoint });
}
33 changes: 33 additions & 0 deletions typescript/packages/plugins/jsonrpc/src/jsonrpc.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Tool, WalletClientBase } from "@goat-sdk/core";
import { JSONRpcBodySchema } from "./parameters";

export class JSONRpcService {
private endpoint: string;

constructor({ endpoint }: { endpoint?: string } = {}) {
this.endpoint = endpoint ?? "";
}

@Tool({
description: "Make a remote procedure call to a JSON RPC endpoint",
})
async JSONRpcFunc(walletClient: WalletClientBase, parameters: JSONRpcBodySchema) {
try {
const url = new URL(`${this.endpoint}`);

const response = await fetch(url.toString(), {
method: "POST",
body: JSON.stringify(parameters),
headers: {},
});
console.log(response, "string");
if (!response.ok) {
throw new Error(`Failed to fetch: ${response.statusText}`);
}

return await response.json();
} catch (error) {
throw Error(`Failed to call endpoint: ${error}`);
}
}
}
17 changes: 17 additions & 0 deletions typescript/packages/plugins/jsonrpc/src/parameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createToolParameters } from "@goat-sdk/core";
import { z } from "zod";

export class JSONRpcBodySchema extends createToolParameters(
z.object({
method: z.string().describe("A string containing the name of the method to be invoked"),
params: z
.array(z.string())
.describe(
"A structured value that holds the parameter value to be used during the invokation of the method",
),
id: z.number().describe("An identifier established by the client that must contain a string number or null"),
jsonrpc: z
.string()
.describe("A string that specifies the version of the JSON-RPC protocol must be exactly {{'2.0'}}"),
}),
) {}
6 changes: 6 additions & 0 deletions typescript/packages/plugins/jsonrpc/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"]
}
6 changes: 6 additions & 0 deletions typescript/packages/plugins/jsonrpc/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from "tsup";
import { treeShakableConfig } from "../../../tsup.config.base";

export default defineConfig({
...treeShakableConfig,
});
11 changes: 11 additions & 0 deletions typescript/packages/plugins/jsonrpc/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"],
"tasks": {
"build": {
"inputs": ["src/**", "tsup.config.ts", "!./**/*.test.{ts,tsx}", "tsconfig.json"],
"dependsOn": ["^build"],
"outputs": ["dist/**"]
}
}
}
13 changes: 11 additions & 2 deletions typescript/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 34596ce

Please sign in to comment.