Skip to content

Commit

Permalink
remove async interface for getNetwork, require genesisHash instead
Browse files Browse the repository at this point in the history
  • Loading branch information
ngundotra committed Feb 3, 2025
1 parent 8829294 commit 62903ed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Network> {
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}`);
}
}

Expand Down
23 changes: 11 additions & 12 deletions typescript/agentkit/src/wallet-providers/walletProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -51,7 +50,7 @@ export abstract class WalletProvider {
*
* @returns The network of the wallet provider.
*/
abstract getNetwork(): Promise<Network>;
abstract getNetwork(): Network;

/**
* Get the name of the wallet provider.
Expand Down

0 comments on commit 62903ed

Please sign in to comment.