From 62903ed2073434e5c8e8b79547f4c7aa168e0549 Mon Sep 17 00:00:00 2001 From: ngundotra Date: Mon, 3 Feb 2025 00:31:15 +0000 Subject: [PATCH] remove async interface for getNetwork, require genesisHash instead --- .../svmKeypairWalletProvider.ts | 15 +++++++----- .../src/wallet-providers/walletProvider.ts | 23 +++++++++---------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/typescript/agentkit/src/wallet-providers/svmKeypairWalletProvider.ts b/typescript/agentkit/src/wallet-providers/svmKeypairWalletProvider.ts index 0c09954f1..b2d3f526f 100644 --- a/typescript/agentkit/src/wallet-providers/svmKeypairWalletProvider.ts +++ b/typescript/agentkit/src/wallet-providers/svmKeypairWalletProvider.ts @@ -7,6 +7,7 @@ import { SOLANA_MAINNET_CHAIN_ID, SOLANA_MAINNET_NETWORK_ID, SOLANA_PROTOCOL_FAM export class SvmKeypairWalletProvider extends SvmWalletProvider { #keypair: Keypair; #connection: Connection; + #genesisHash: string; /** * Creates a new SvmKeypairWalletProvider @@ -16,42 +17,44 @@ export class SvmKeypairWalletProvider extends SvmWalletProvider { constructor({ keypair, rpcUrl, + genesisHash, }: { keypair: Uint8Array | string, rpcUrl: string, + genesisHash: string, }) { super(); this.#keypair = typeof keypair === "string" ? Keypair.fromSecretKey(bs58.decode(keypair)) : Keypair.fromSecretKey(keypair); this.#connection = new Connection(rpcUrl); + this.#genesisHash = genesisHash; } getAddress(): string { return this.#keypair.publicKey.toBase58(); } - async getNetwork(): Promise { - const hash = await this.#connection.getGenesisHash(); - if (hash === SOLANA_MAINNET_GENESIS_BLOCK_HASH) { + getNetwork(): Network { + if (this.#genesisHash === SOLANA_MAINNET_GENESIS_BLOCK_HASH) { return { protocolFamily: SOLANA_PROTOCOL_FAMILY, chainId: String(SOLANA_MAINNET_CHAIN_ID), networkId: SOLANA_MAINNET_NETWORK_ID, }; - } else if (hash === SOLANA_TESTNET_GENESIS_BLOCK_HASH) { + } else if (this.#genesisHash === SOLANA_TESTNET_GENESIS_BLOCK_HASH) { return { protocolFamily: SOLANA_PROTOCOL_FAMILY, chainId: String(SOLANA_TESTNET_CHAIN_ID), networkId: SOLANA_TESTNET_NETWORK_ID, }; - } else if (hash === SOLANA_DEVNET_GENESIS_BLOCK_HASH) { + } else if (this.#genesisHash === SOLANA_DEVNET_GENESIS_BLOCK_HASH) { return { protocolFamily: SOLANA_PROTOCOL_FAMILY, chainId: String(SOLANA_DEVNET_CHAIN_ID), networkId: SOLANA_DEVNET_NETWORK_ID, }; } else { - throw new Error(`Unknown network with genesis hash: ${hash}`); + throw new Error(`Unknown network with genesis hash: ${this.#genesisHash}`); } } diff --git a/typescript/agentkit/src/wallet-providers/walletProvider.ts b/typescript/agentkit/src/wallet-providers/walletProvider.ts index 678af8291..ad92b6b40 100644 --- a/typescript/agentkit/src/wallet-providers/walletProvider.ts +++ b/typescript/agentkit/src/wallet-providers/walletProvider.ts @@ -22,17 +22,16 @@ export abstract class WalletProvider { */ private trackInitialization() { try { - this.getNetwork().then(network => { - sendAnalyticsEvent({ - name: "agent_initialization", - action: "initialize_wallet_provider", - component: "wallet_provider", - wallet_provider: this.getName(), - wallet_address: this.getAddress(), - network_id: network.networkId, - chain_id: network.chainId, - protocol_family: network.protocolFamily, - }); + + sendAnalyticsEvent({ + name: "agent_initialization", + action: "initialize_wallet_provider", + component: "wallet_provider", + wallet_provider: this.getName(), + wallet_address: this.getAddress(), + network_id: this.getNetwork().networkId, + chain_id: this.getNetwork().chainId, + protocol_family: this.getNetwork().protocolFamily, }); } catch (error) { console.warn("Failed to track wallet provider initialization:", error); @@ -51,7 +50,7 @@ export abstract class WalletProvider { * * @returns The network of the wallet provider. */ - abstract getNetwork(): Promise; + abstract getNetwork(): Network; /** * Get the name of the wallet provider.