-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add chromia examples and chromia wallet * add CHR * add docs * Finalize Chromia Account * update docs * update recipient address * resolve * add version bump --------- Co-authored-by: Agus <[email protected]>
- Loading branch information
Showing
26 changed files
with
921 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"goat-examples-vercel-ai-chromia": minor | ||
"@goat-sdk/wallet-chromia": minor | ||
"@goat-sdk/core": minor | ||
--- | ||
|
||
Added Chromia and send-chr tools |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
OPENAI_API_KEY= | ||
EVM_PRIVATE_KEY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Vercel AI with Chromia Example | ||
|
||
## Setup | ||
|
||
Copy the `.env.template` and populate with your values. | ||
|
||
``` | ||
cp .env.template .env | ||
``` | ||
|
||
## Usage | ||
|
||
``` | ||
npx ts-node index.ts | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { openai } from "@ai-sdk/openai"; | ||
import { generateText } from "ai"; | ||
import { getOnChainTools } from "@goat-sdk/adapter-vercel-ai"; | ||
import { createClient } from "postchain-client"; | ||
import { CHROMIA_MAINNET_BRID, chromia } from "@goat-sdk/wallet-chromia"; | ||
import { createConnection, createInMemoryEvmKeyStore, createKeyStoreInteractor } from "@chromia/ft4"; | ||
import { sendCHR } from "@goat-sdk/core"; | ||
|
||
require("dotenv").config(); | ||
|
||
const privateKey = process.env.EVM_PRIVATE_KEY; | ||
|
||
if (!privateKey) { | ||
throw new Error("EVM_PRIVATE_KEY is not set in the environment"); | ||
} | ||
|
||
(async () => { | ||
const chromiaClient = await createClient({ | ||
nodeUrlPool: ["https://system.chromaway.com:7740"], | ||
blockchainRid: CHROMIA_MAINNET_BRID.ECONOMY_CHAIN | ||
}); | ||
const connection = createConnection(chromiaClient); | ||
const evmKeyStore = createInMemoryEvmKeyStore({ | ||
privKey: privateKey, | ||
} as any); | ||
const keystoreInteractor = createKeyStoreInteractor(chromiaClient, evmKeyStore) | ||
const accounts = await keystoreInteractor.getAccounts(); | ||
const accountAddress = accounts[0].id.toString("hex"); | ||
console.log("ACCOUNT ADDRESS: ", accountAddress); | ||
|
||
const tools = await getOnChainTools({ | ||
wallet: chromia({ | ||
client: chromiaClient, | ||
accountAddress, | ||
keystoreInteractor, | ||
connection | ||
}), | ||
plugins: [ | ||
sendCHR() | ||
], | ||
}); | ||
|
||
const result = await generateText({ | ||
model: openai("gpt-4o-mini"), | ||
tools: tools, | ||
maxSteps: 5, | ||
prompt: "send 0.0001 CHR to <recipient address>", | ||
}); | ||
|
||
console.log(result.text); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "goat-examples-vercel-ai-chromia", | ||
"version": "0.1.1", | ||
"description": "", | ||
"private": true, | ||
"scripts": { | ||
"test": "vitest run --passWithNoTests" | ||
}, | ||
"author": "", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@ai-sdk/openai": "^1.0.4", | ||
"@chromia/ft4": "catalog:", | ||
"@goat-sdk/adapter-vercel-ai": "workspace:*", | ||
"@goat-sdk/core": "workspace:*", | ||
"@goat-sdk/wallet-chromia": "workspace:*", | ||
"ai": "catalog:", | ||
"dotenv": "^16.4.5", | ||
"postchain-client": "catalog:" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"outDir": "dist" | ||
}, | ||
"include": ["index.ts"], | ||
"exclude": ["node_modules", "dist"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { formatUnits } from "viem"; | ||
import type { z } from "zod"; | ||
import type { ChromiaWalletClient } from "../wallets"; | ||
import type { getAddressParametersSchema, getCHRBalanceParametersSchema } from "./parameters"; | ||
|
||
export function getAddress( | ||
walletClient: ChromiaWalletClient, | ||
parameters: z.infer<typeof getAddressParametersSchema>, | ||
): string { | ||
return walletClient.getAddress(); | ||
} | ||
|
||
export async function getBalance( | ||
walletClient: ChromiaWalletClient, | ||
parameters: z.infer<typeof getCHRBalanceParametersSchema>, | ||
): Promise<string> { | ||
try { | ||
const balance = await walletClient.balanceOf(parameters.address ?? getAddress(walletClient, {})); | ||
|
||
return formatUnits(balance.value, balance.decimals); | ||
} catch (error) { | ||
throw new Error(`Failed to fetch balance: ${error}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { z } from "zod"; | ||
|
||
export const getAddressParametersSchema = z.object({}); | ||
|
||
export const getCHRBalanceParametersSchema = z.object({ | ||
address: z | ||
.optional(z.string()) | ||
.describe("The address to get the balance of, defaults to the address of the wallet"), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { DeferredTool } from "../tools"; | ||
import type { ChromiaWalletClient } from "../wallets"; | ||
import { getAddress, getBalance } from "./methods"; | ||
import { getAddressParametersSchema, getCHRBalanceParametersSchema } from "./parameters"; | ||
|
||
export const deferredChromiaTools: DeferredTool<ChromiaWalletClient>[] = [ | ||
{ | ||
name: "get_address", | ||
description: "This {{tool}} returns the address of the Chromia wallet.", | ||
parameters: getAddressParametersSchema, | ||
method: getAddress, | ||
}, | ||
{ | ||
name: "get_chr_balance", | ||
description: "This {{tool}} returns the CHR balance of a Chromia wallet.", | ||
parameters: getCHRBalanceParametersSchema, | ||
method: getBalance, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { z } from "zod"; | ||
import type { Plugin } from "./plugins"; | ||
import { ChromiaWalletClient } from "../wallets"; | ||
|
||
export const CHR_ASSET_ID = | ||
"5f16d1545a0881f971b164f1601cbbf51c29efd0633b2730da18c403c3b428b5"; | ||
|
||
export function sendCHR(): Plugin<ChromiaWalletClient> { | ||
return { | ||
name: "send_chr", | ||
supportsSmartWallets: () => false, | ||
supportsChain: (chain) => chain.type === "chromia", | ||
getTools: async () => { | ||
return [ | ||
{ | ||
name: "send_chr", | ||
description: "This {{tool}} sends CHR to an address on a Chromia chain.", | ||
parameters: sendCHRParametersSchema, | ||
method: sendCHRMethod, | ||
} | ||
] | ||
} | ||
} | ||
} | ||
|
||
const sendCHRParametersSchema = z.object({ | ||
to: z.string().describe("The address to send CHR to"), | ||
amount: z.string().describe("The amount of CHR to send"), | ||
}); | ||
|
||
async function sendCHRMethod( | ||
walletClient: ChromiaWalletClient, | ||
parameters: z.infer<typeof sendCHRParametersSchema>, | ||
): Promise<string> { | ||
try { | ||
const { to, amount } = parameters; | ||
await walletClient.sendTransaction({to, assetId: CHR_ASSET_ID, amount}); | ||
return `CHR sent to ${to} with amount ${amount}`; | ||
} catch (error) { | ||
return `Error sending CHR: ${error}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
import { DictPair, QueryObject, RawGtv } from "postchain-client"; | ||
import { TransactionWithReceipt } from "@chromia/ft4"; | ||
import type { WalletClient } from "./core"; | ||
|
||
export function isChromiaWalletClient(wallet: WalletClient): wallet is ChromiaWalletClient { | ||
return wallet.getChain().type === "chromia"; | ||
} | ||
|
||
export type ChromiaTransaction = { | ||
to: string; | ||
assetId: string; | ||
amount: string; | ||
}; | ||
|
||
export type ChromiaReadRequest = string | QueryObject<RawGtv | DictPair>; | ||
|
||
export type ChromiaReadResult = RawGtv; | ||
|
||
export type ChromiaTransactionResult = TransactionWithReceipt; | ||
|
||
export interface ChromiaWalletClient extends WalletClient { | ||
sendTransaction: (transaction: ChromiaTransaction) => Promise<ChromiaTransactionResult>; | ||
read: (request: ChromiaReadRequest) => Promise<ChromiaReadResult>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.