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

Crossmint Balance API Plugin #194

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -10,11 +10,11 @@
"author": "",
"license": "MIT",
"dependencies": {
"@ai-sdk/openai": "^1.0.4",
"@ai-sdk/openai": "^1.0.13",
"@goat-sdk/adapter-vercel-ai": "workspace:*",
"@goat-sdk/core": "workspace:*",
"@goat-sdk/plugin-erc20": "workspace:*",
"@goat-sdk/crossmint": "workspace:*",
"@goat-sdk/plugin-erc20": "workspace:*",
"@goat-sdk/wallet-evm": "workspace:*",
"ai": "catalog:",
"dotenv": "^16.4.5",
Expand Down
2 changes: 1 addition & 1 deletion typescript/examples/vercel-ai/viem/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ const walletClient = createWalletClient({
});

console.log(result.text);
})();
})();
2 changes: 1 addition & 1 deletion typescript/examples/vercel-ai/viem/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
"dotenv": "^16.4.5",
"viem": "2.21.49"
}
}
}
36 changes: 36 additions & 0 deletions typescript/packages/wallets/crossmint/src/currencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export const SOLANA_SUPPORTED_CURRENCIES = ["eurc", "bonk", "wif", "mother", "sol", "usdc"] as const;

export const EVM_SUPPORTED_CURRENCIES = [
"eth",
"matic",
"pol",
"chz",
"avax",
"xai",
"fuel",
"vic",
"usdc",
"usdce",
"busd",
"usdxm",
"weth",
"degen",
"brett",
"toshi",
"eurc",
"bnb",
"sfuel",
] as const;

export const OTHER_CHAIN_SUPPORTED_CURRENCIES = ["sui", "sei", "ada", "apt"] as const;

export const SUPPORTED_CURRENCIES = [
...EVM_SUPPORTED_CURRENCIES,
...SOLANA_SUPPORTED_CURRENCIES,
...OTHER_CHAIN_SUPPORTED_CURRENCIES,
] as const;

export type SupportedEVMCurrency = (typeof EVM_SUPPORTED_CURRENCIES)[number];
export type SupportedSolanaCurrency = (typeof SOLANA_SUPPORTED_CURRENCIES)[number];
export type SupportedOtherCurrency = (typeof OTHER_CHAIN_SUPPORTED_CURRENCIES)[number];
export type SupportedCurrency = (typeof SUPPORTED_CURRENCIES)[number];
4 changes: 3 additions & 1 deletion typescript/packages/wallets/crossmint/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { CrossmintApiClient } from "@crossmint/common-sdk-base";
import { faucetPlugin, mintPlugin } from "./plugins";
import { balancePlugin, faucetPlugin, mintPlugin } from "./plugins";
import { walletsPlugin } from "./plugins/wallets.plugin";
import { custodialFactory, smartWalletFactory } from "./wallets";

function crossmint(apiKey: string) {
const apiClient = new CrossmintApiClient(
{
Expand All @@ -21,6 +22,7 @@ function crossmint(apiKey: string) {
custodial: custodialFactory(apiClient),
smartwallet: smartWalletFactory(apiClient),
faucet: faucetPlugin(apiClient),
balance: balancePlugin(apiClient),
mint: mintPlugin(apiClient),
wallets: walletsPlugin(apiClient),
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { CrossmintApiClient } from "@crossmint/common-sdk-base";
import { Chain, PluginBase, WalletClientBase } from "@goat-sdk/core";
import { TokenBalanceService } from "./balance.service";

export class BalancePlugin extends PluginBase<WalletClientBase> {
constructor(client: CrossmintApiClient) {
super("balance", [new TokenBalanceService(client)]);
}

supportsChain(chain: Chain) {
return chain.type === "evm" || chain.type === "solana" || chain.type === "sui" || chain.type === "aptos";
}
}

export function balancePlugin(client: CrossmintApiClient) {
return () => {
return new BalancePlugin(client);
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { CrossmintApiClient } from "@crossmint/common-sdk-base";
import { Tool, WalletClientBase, createToolParameters } from "@goat-sdk/core";
import { EVMWalletClient } from "@goat-sdk/wallet-evm";
import { z } from "zod";
import { SUPPORTED_CURRENCIES } from "../currencies";

export class TokenBalanceParameters extends createToolParameters(
z.object({
wallet: z.string().describe("The address to get balances for"),
currencies: z.array(z.enum(SUPPORTED_CURRENCIES)).min(1).describe("The currencies to get balances for"),
chains: z.array(z.string()).optional().describe("The specific chains to query"),
}),
) {}

export interface TokenBalance {
currency: string;
decimals: number;
balances: {
[chain: string]: string;
total: string;
};
}

export interface GetWalletBalanceResponse {
balances: TokenBalance[];
}

export class TokenBalanceService {
constructor(private readonly client: CrossmintApiClient) {}

@Tool({
description: "Get the token balances of the current wallet",
})
async getBalance(walletClient: WalletClientBase, parameters: TokenBalanceParameters) {
const wallet = parameters.wallet ?? walletClient.getAddress();

let resolvedWalletAddress: string;
if (walletClient instanceof EVMWalletClient) {
resolvedWalletAddress = await walletClient.resolveAddress(wallet);
} else {
resolvedWalletAddress = wallet;
}

const queryParams = new URLSearchParams();
queryParams.append("currencies", parameters.currencies.join(","));

if (parameters.chains && parameters.chains.length > 0) {
queryParams.append("chains", parameters.chains.join(","));
}

const endpoint = `/api/v1-alpha2/wallets/${encodeURIComponent(resolvedWalletAddress)}/balances`;
const url = `${this.client.baseUrl}${endpoint}${queryParams.toString() ? `?${queryParams}` : ""}`;

const response = await fetch(url, {
method: "GET",
headers: this.client.authHeaders,
});

if (!response.ok) {
throw new Error(`Failed to get balances: ${await response.text()}`);
}

return (await response.json()) as GetWalletBalanceResponse;
}
}
1 change: 1 addition & 0 deletions typescript/packages/wallets/crossmint/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./faucet.plugin";
export * from "./balance.plugin";
export * from "./mint.plugin";
Loading
Loading