Skip to content

Commit

Permalink
Fix bugs, add get collections and common base
Browse files Browse the repository at this point in the history
  • Loading branch information
0xaguspunk committed Dec 10, 2024
1 parent a078f9f commit 805d213
Show file tree
Hide file tree
Showing 13 changed files with 458 additions and 177 deletions.
17 changes: 9 additions & 8 deletions goat.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"name": "🌋 Core",
"path": "./typescript/packages/core"
},
// Adapters
{
"name": "[Adapter] 🎧 eleven-labs",
"path": "./typescript/packages/adapters/eleven-labs"
Expand All @@ -28,6 +29,7 @@
"name": "[Adapter] 🤖 vercel-ai",
"path": "./typescript/packages/adapters/vercel-ai"
},
// Plugins
{
"name": "[Plugin] 💰 erc20",
"path": "./typescript/packages/plugins/erc20"
Expand All @@ -40,6 +42,11 @@
"name": "[Plugin] 💰 coingecko",
"path": "./typescript/packages/plugins/coingecko"
},
{
"name": "[Plugin] 🌐 Solana NFTs",
"path": "./typescript/packages/plugins/solana-nfts"
},
// Wallets
{
"name": "[Wallet] 🍀 crossmint",
"path": "./typescript/packages/wallets/crossmint"
Expand All @@ -48,22 +55,16 @@
"name": "[Wallet] 🌞 solana",
"path": "./typescript/packages/wallets/solana"
},
{
"name": "[Plugin] 🌐 Solana NFTs",
"path": "./typescript/packages/plugins/solana-nfts"
},
{
"name": "[Plugin] 🌐 Minting API",
"path": "./typescript/packages/plugins/minting-api"
},
{
"name": "[Wallet] 💳 viem",
"path": "./typescript/packages/wallets/viem"
},
// Docs
{
"name": "📚 Docs",
"path": "./docs"
},
// Examples
{
"name": "🚀 Examples",
"path": "./typescript/examples"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const { custodial } = crossmint(apiKey);
wallet: await custodial({
chain: "solana",
email: email,
env: "staging",
connection: new Connection("https://api.devnet.solana.com", "confirmed"),
}),
});
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 @@ -32,7 +32,7 @@ const walletClient = createWalletClient({
model: openai("gpt-4o-mini"),
tools: tools,
maxSteps: 5,
prompt: "Get my balance in USDC",
prompt: "Get the balance of the USDC token",
});

console.log(result.text);
Expand Down
13 changes: 11 additions & 2 deletions typescript/packages/wallets/crossmint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"name": "@goat-sdk/crossmint",
"version": "0.1.7",
"sideEffects": false,
"files": ["dist/**/*", "README.md", "package.json"],
"files": [
"dist/**/*",
"README.md",
"package.json"
],
"scripts": {
"build": "tsup",
"clean": "rm -rf dist",
Expand All @@ -12,6 +16,7 @@
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"dependencies": {
"@crossmint/common-sdk-base": "0.3.1",
"@goat-sdk/core": "workspace:*",
"@solana/web3.js": "catalog:",
"abitype": "^1.0.6",
Expand All @@ -31,5 +36,9 @@
"bugs": {
"url": "https://github.com/goat-sdk/goat/issues"
},
"keywords": ["ai", "agents", "web3"]
"keywords": [
"ai",
"agents",
"web3"
]
}
10 changes: 4 additions & 6 deletions typescript/packages/wallets/crossmint/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { EVMTypedData } from "@goat-sdk/core";
import type { SupportedSmartWalletChains } from "./chains";
import type { CrossmintApiClient } from "@crossmint/common-sdk-base";

type CoreSignerType =
| "evm-keypair"
Expand Down Expand Up @@ -191,10 +192,8 @@ type APIResponse =
| SignTypedDataResponse
| ApproveSignatureResponse;

export function createCrossmintAPI(apiKey: string, env: "staging" | "production") {
const baseUrl =
env === "staging" ? "https://staging.crossmint.com/api/v1-alpha2" : "https://wwww.crossmint.com/api/v1-alpha2";

export function createCrossmintAPI(crossmintClient: CrossmintApiClient) {
const baseUrl = `${crossmintClient.baseUrl}/api/v1-alpha2`;
/**
* Makes an HTTP request to the Crossmint API.
*
Expand All @@ -208,8 +207,7 @@ export function createCrossmintAPI(apiKey: string, env: "staging" | "production"

// Set default headers and merge with any additional headers
const headers = new Headers({
"X-API-KEY": apiKey,
"Content-Type": "application/json",
...crossmintClient.authHeaders,
...(options.headers || {}),
});

Expand Down
125 changes: 114 additions & 11 deletions typescript/packages/wallets/crossmint/src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,21 @@ import {
sepolia,
skaleNebulaTestnet,
victionTestnet,
astarZkEVM,
bsc,
chiliz,
mainnet,
shape,
shapeSepolia,
skaleNebula,
xai,
xaiTestnet,
astarZkyoto,
zora,
zoraSepolia,
avalanche,
} from "viem/chains";
import type { Chain as GoatChain } from "@goat-sdk/core";

const faucetChains = [
"arbitrum-sepolia",
Expand Down Expand Up @@ -40,37 +54,126 @@ const smartWalletChains = [

export type SupportedSmartWalletChains = (typeof smartWalletChains)[number];

const chainMap: Record<SupportedFaucetChains | SupportedSmartWalletChains, Chain> = {
arbitrum: arbitrum,
export const mintingChains = [
"arbitrum",
"arbitrum-sepolia",
"astar-zkevm",
"avalanche",
"avalanche-fuji",
"base",
"base-sepolia",
"bsc",
"chiliz",
"chiliz-spicy-testnet",
"ethereum",
"ethereum-sepolia",
"optimism",
"optimism-sepolia",
"polygon",
"polygon-amoy",
"shape",
"shape-sepolia",
"skale-nebula",
"skale-nebula-testnet",
"soneium-minato-testnet",
"xai",
"xai-sepolia-testnet",
"zkyoto",
"zora",
"zora-sepolia",
] as const;

export type SupportedMintingChains = (typeof mintingChains)[number];

const chainMap: Record<
SupportedFaucetChains | SupportedSmartWalletChains | SupportedMintingChains,
Chain
> = {
arbitrum,
"arbitrum-sepolia": arbitrumSepolia,
base: base,
"astar-zkevm": astarZkEVM,
avalanche,
"avalanche-fuji": avalancheFuji,
base,
"base-sepolia": baseSepolia,
optimism: optimism,
bsc,
chiliz,
ethereum: mainnet,
optimism,
"optimism-sepolia": optimismSepolia,
polygon: polygon,
polygon,
"polygon-amoy": polygonAmoy,
"avalanche-fuji": avalancheFuji,
"ethereum-sepolia": sepolia,
shape,
"shape-sepolia": shapeSepolia,
"skale-nebula": skaleNebula,
"skale-nebula-testnet": skaleNebulaTestnet,
"viction-testnet": victionTestnet,
xai,
"xai-sepolia-testnet": xaiTestnet,
zkyoto: astarZkyoto,
zora,
"zora-sepolia": zoraSepolia,
"chiliz-spicy-testnet": {
id: 88882,
} as Chain,
"soneium-minato-testnet": {
id: 88882,
} as Chain,
};

export function getViemChain(chain: SupportedSmartWalletChains | SupportedFaucetChains): Chain {
export function getViemChain(
chain: SupportedSmartWalletChains | SupportedFaucetChains
): Chain {
const viemChain = chainMap[chain];
if (!viemChain) {
throw new Error(`Unsupported chain: ${chain}`);
}
return viemChain;
}

const testnetChains = ["arbitrum-sepolia", "base-sepolia", "optimism-sepolia", "polygon-amoy"] as const;
export function getCrossmintChainString(chain: GoatChain): string {
if (chain.type === "solana") {
return "solana";
}
if (chain.type === "aptos") {
return "aptos";
}

if (chain.type === "evm") {
// from chain.id figure out the chain name
const chainName = Object.keys(chainMap).find(
(key): key is keyof typeof chainMap =>
chainMap[key as keyof typeof chainMap].id === chain.id
);
if (!chainName) {
throw new Error(`Unsupported chain: ${chain.id}`);
}
return chainName;
}

export function getEnv(chain: SupportedSmartWalletChains): "staging" | "production" {
return (testnetChains as readonly string[]).includes(chain) ? "staging" : "production";
throw new Error(`Unsupported chain: ${chain.type}`);
}

const faucetChainIds = new Set(faucetChains.map((chainName) => chainMap[chainName].id));
const testnetChains = [
"arbitrum-sepolia",
"base-sepolia",
"optimism-sepolia",
"polygon-amoy",
] as const;

const faucetChainIds = new Set(
faucetChains.map((chainName) => chainMap[chainName].id)
);

export function isChainSupportedByFaucet(chainId: number): boolean {
return faucetChainIds.has(chainId);
}

const mintingChainIds = new Set(
mintingChains.map((chainName) => chainMap[chainName].id)
);

export function isChainSupportedByMinting(chainId: number): boolean {
return mintingChainIds.has(chainId);
}
9 changes: 4 additions & 5 deletions typescript/packages/wallets/crossmint/src/custodial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import type { SolanaReadRequest, SolanaTransaction, SolanaWalletClient } from "@
import { type Connection, PublicKey, TransactionMessage, VersionedTransaction } from "@solana/web3.js";
import bs58 from "bs58";
import { createCrossmintAPI } from "./api";
import type { CrossmintApiClient } from "@crossmint/common-sdk-base";

type CommonParameters = {
chain: "solana";
connection: Connection;
env?: "staging" | "production";
};

type EmailLocatorParameters = CommonParameters & {
Expand Down Expand Up @@ -38,12 +38,12 @@ function getLocator(params: CustodialOptions): string | number {
return `userId:${params.userId}:solana-custodial-wallet`;
}

export function custodialFactory(apiKey: string) {
export function custodialFactory(crossmintClient: CrossmintApiClient) {
return async function custodial(params: CustodialOptions): Promise<SolanaWalletClient> {
const { connection, env = "staging" } = params;
const { connection } = params;

const locator = `${getLocator(params)}`;
const client = createCrossmintAPI(apiKey, env);
const client = createCrossmintAPI(crossmintClient);
const { address } = await client.getWallet(locator);

return {
Expand Down Expand Up @@ -102,7 +102,6 @@ export function custodialFactory(apiKey: string) {
const latestTransaction = await client.checkTransactionStatus(locator, transactionId);

if (latestTransaction.status === "success") {
console.log(`Transaction ${latestTransaction.status}`);
return {
hash: latestTransaction.onChain?.txId ?? "",
};
Expand Down
10 changes: 4 additions & 6 deletions typescript/packages/wallets/crossmint/src/faucet.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import type { Chain, EVMWalletClient, Plugin } from "@goat-sdk/core";
import { z } from "zod";
import { isChainSupportedByFaucet } from "./chains";
import type { CrossmintApiClient } from "@crossmint/common-sdk-base";

export const topUpBalanceParametersSchema = z.object({
wallet: z.string().optional().describe("The address to top up the balance of"),
amount: z.number().min(1).max(100).describe("The amount of tokens to top up"),
});

export function faucetFactory(apiKey: string) {
export function faucetFactory(client: CrossmintApiClient) {
return function faucet(): Plugin<EVMWalletClient> {
return {
name: "Crossmint Faucet",
Expand Down Expand Up @@ -51,10 +52,7 @@ export function faucetFactory(apiKey: string) {

const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-API-KEY": apiKey,
},
headers: client.authHeaders,
body: JSON.stringify({
amount: parameters.amount,
currency: "usdc",
Expand All @@ -63,7 +61,7 @@ export function faucetFactory(apiKey: string) {
};

const response = await fetch(
`https://staging.crossmint.com/api/v1-alpha2/wallets/${resolvedWalletAddress}/balances`,
`${client.baseUrl}/api/v1-alpha2/wallets/${resolvedWalletAddress}/balances`,
options,
);

Expand Down
Loading

0 comments on commit 805d213

Please sign in to comment.