Skip to content

Commit

Permalink
progress before rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNoushad committed Jan 2, 2025
1 parent 622ac3f commit 00fddff
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 52 deletions.
62 changes: 45 additions & 17 deletions src/agent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
import {
CollectionDeployment,
CollectionOptions,
Config,
GibworkCreateTaskReponse,
JupiterTokenData,
MintCollectionNFTResponse,
Expand All @@ -75,28 +76,55 @@ export class SolanaAgentKit {
public connection: Connection;
public wallet: WalletAdapter;
public wallet_address: PublicKey;
public openai_api_key: string | null;

/**
* @deprecated Using openai_api_key directly in constructor is deprecated.
* Please use the new constructor with Config object instead:
* @example
* const agent = new SolanaAgentKit(privateKey, rpcUrl, {
* OPENAI_API_KEY: 'your-key'
* });
*/
constructor(private_key: string, rpc_url: string, openai_api_key: string | null);
constructor(private_key: string, rpc_url: string, config: Config);
public config: Config;
private keypair: Keypair | null = null;
private walletId: string;

constructor(
private_key: string,
rpc_url = "https://api.mainnet-beta.solana.com",
config: Config,
walletIdOrPrivateKey: string,
rpc_url: string,
configOrOpenAIKey?: Config | string | null
) {
this.connection = new Connection(rpc_url);
this.wallet = Keypair.fromSecretKey(bs58.decode(private_key));

if (walletIdOrPrivateKey.length === 88 || walletIdOrPrivateKey.length === 64) {
// Assume it's a private key
this.keypair = Keypair.fromSecretKey(bs58.decode(walletIdOrPrivateKey));
this.walletId = this.keypair.publicKey.toString();
this.wallet = walletManager.connectWallet(this.walletId, this.keypair);
} else {
// Assume it's a wallet ID
this.walletId = walletIdOrPrivateKey;
this.wallet = walletManager.getWallet(this.walletId);
}

this.wallet_address = this.wallet.publicKey;
this.config = config;

if (typeof configOrOpenAIKey === 'string' || configOrOpenAIKey === null) {
console.warn('Using openai_api_key directly in constructor is deprecated. Please use the Config object instead.');
this.config = { OPENAI_API_KEY: configOrOpenAIKey || undefined };
} else {
this.config = configOrOpenAIKey || {};
}
}

get openai_api_key(): string | null {
return this.config.OPENAI_API_KEY || null;
}

getKeypair(): Keypair | null {
return this.keypair;
}

logKeypairDetails(): void {
if (this.keypair) {
console.log(this.keypair.publicKey.toString());
console.log(bs58.encode(this.keypair.secretKey));
} else {
console.log("No keypair available. Using an external wallet.");
}
}

// Tool methods
async requestFaucetFunds() {
return request_faucet_funds(this);
Expand Down
68 changes: 39 additions & 29 deletions src/tools/walletmanager.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
// walletManager.ts
import { Keypair, PublicKey, Transaction, VersionedTransaction } from "@solana/web3.js";
import { Wallet } from "@coral-xyz/anchor";
import bs58 from "bs58";

export interface WalletAdapter {
publicKey: PublicKey;
signTransaction<T extends Transaction | VersionedTransaction>(transaction: T): Promise<T>;
signAllTransactions<T extends Transaction | VersionedTransaction>(transactions: T[]): Promise<T[]>;
}
import { WalletAdapter, WalletConfig, WalletState, WalletError, AnchorWallet } from "../types/walletTypes";

class KeypairWallet implements WalletAdapter {
constructor(private keypair: Keypair) {}
Expand Down Expand Up @@ -34,7 +29,7 @@ class KeypairWallet implements WalletAdapter {

export class WalletManager {
private static instance: WalletManager;
private currentWallet: WalletAdapter | null = null;
private wallets: Map<string, WalletState> = new Map();

private constructor() {}

Expand All @@ -45,42 +40,57 @@ export class WalletManager {
return WalletManager.instance;
}

connectWallet(walletOrPrivateKeyOrKeypair: WalletAdapter | string | Keypair): void {
connectWallet(id: string, walletOrPrivateKeyOrKeypair: WalletAdapter | string | Keypair, config: WalletConfig = {}): WalletAdapter {
let wallet: WalletAdapter;
let keypair: Keypair | undefined;

if (typeof walletOrPrivateKeyOrKeypair === 'string') {
const keypair = Keypair.fromSecretKey(bs58.decode(walletOrPrivateKeyOrKeypair));
this.currentWallet = new KeypairWallet(keypair);
keypair = Keypair.fromSecretKey(bs58.decode(walletOrPrivateKeyOrKeypair));
wallet = new KeypairWallet(keypair);
} else if (walletOrPrivateKeyOrKeypair instanceof Keypair) {
this.currentWallet = new KeypairWallet(walletOrPrivateKeyOrKeypair);
keypair = walletOrPrivateKeyOrKeypair;
wallet = new KeypairWallet(keypair);
} else {
this.currentWallet = walletOrPrivateKeyOrKeypair;
wallet = walletOrPrivateKeyOrKeypair;
}

this.wallets.set(id, {
wallet,
keypair,
config,
lastUsed: Date.now(),
} as WalletState);

return wallet;
}

getWallet(): WalletAdapter {
if (!this.currentWallet) {
throw new Error("No wallet connected. Please connect a wallet first.");
getWallet(id: string): WalletAdapter {
const walletState = this.wallets.get(id);
if (!walletState) {
throw new WalletError(`No wallet found with id: ${id}`, 'WALLET_NOT_FOUND');
}
return this.currentWallet;
walletState.lastUsed = Date.now();
return walletState.wallet;
}

disconnectWallet(id: string): void {
this.wallets.delete(id);
}

disconnectWallet(): void {
this.currentWallet = null;
isConnected(id: string): boolean {
return this.wallets.has(id);
}

isConnected(): boolean {
return this.currentWallet !== null;
getAnchorWallet(id: string): AnchorWallet {
const adapter = this.getWallet(id);
return adapter as AnchorWallet;
}

getAnchorWallet(): Wallet {
const adapter = this.getWallet();
return {
publicKey: adapter.publicKey,
signTransaction: adapter.signTransaction.bind(adapter),
signAllTransactions: adapter.signAllTransactions.bind(adapter),
payer: adapter as any, // Add this line to satisfy the NodeWallet type
};
generateKeypair(): Keypair {
return Keypair.generate();
}
}

export const walletManager = WalletManager.getInstance();

export { WalletAdapter };
6 changes: 3 additions & 3 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { PublicKey } from "@solana/web3.js";

export interface Config {
OPENAI_API_KEY?: string;
JUPITER_REFERRAL_ACCOUNT?: string;
JUPITER_FEE_BPS?: number;
OPENAI_API_KEY?: string | undefined;
JUPITER_REFERRAL_ACCOUNT?: string | undefined;
JUPITER_FEE_BPS?: number | undefined;
}

export interface Creator {
Expand Down
41 changes: 41 additions & 0 deletions src/types/walletTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// walletTypes.ts
import { Keypair, PublicKey, Transaction, VersionedTransaction } from "@solana/web3.js";


export interface Config {
OPENAI_API_KEY?: string;
}

export interface WalletConfig extends Config {
label?: string;
persistKey?: boolean;
autoGenerate?: boolean;
}

export interface WalletIdentifier {
id: string;
label?: string;
publicKey: PublicKey;
}

export interface WalletAdapter {
publicKey: PublicKey;
signTransaction<T extends Transaction | VersionedTransaction>(transaction: T): Promise<T>;
signAllTransactions<T extends Transaction | VersionedTransaction>(transactions: T[]): Promise<T[]>;
}

export interface WalletState {
wallet: WalletAdapter;
keypair?: Keypair;
config: WalletConfig;
lastUsed: number;
}

export class WalletError extends Error {
constructor(message: string, public code: string) {
super(message);
this.name = 'WalletError';
}
}

export interface AnchorWallet extends WalletAdapter {}
7 changes: 4 additions & 3 deletions src/utils/keypair.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// keypair.ts
import { Keypair } from "@solana/web3.js";
import bs58 from "bs58";
import { walletManager } from "../tools/walletManager";

export const keypair = Keypair.generate();
const wallet = walletManager.connectWallet(keypair);
const walletId = keypair.publicKey.toString();
const wallet = walletManager.connectWallet(walletId, keypair);

console.log(keypair.publicKey.toString());
console.log(bs58.encode(keypair.secretKey));

export { wallet };

export { wallet };

0 comments on commit 00fddff

Please sign in to comment.