Skip to content

Commit

Permalink
solana-contrib: Refactor generateTXLink (#675)
Browse files Browse the repository at this point in the history
* move generate tx link logic to /packages/solana-contrib/src/utils

* fix: build error with pass tx to generateTXLink

* pass signature on behalf of tx
  • Loading branch information
h0lme3 committed Aug 21, 2022
1 parent 212548c commit 555b905
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
Expand Up @@ -9,6 +9,7 @@ import type {
import promiseRetry from "promise-retry";
import type { WrapOptions } from "retry";

import { generateTXLink } from "../utils/txLink.js";
import { TransactionReceipt } from "./TransactionReceipt.js";

/**
Expand Down Expand Up @@ -165,6 +166,6 @@ export class PendingTransaction {
* @returns
*/
generateSolanaExplorerLink(cluster: Cluster = "mainnet-beta"): string {
return `https://explorer.solana.com/tx/${this.signature}?cluster=${cluster}`;
return generateTXLink(this.signature, cluster);
}
}
22 changes: 2 additions & 20 deletions packages/solana-contrib/src/transaction/TransactionReceipt.ts
Expand Up @@ -8,6 +8,7 @@ import { default as invariant } from "tiny-invariant";
import type { Event, EventParser } from "../interfaces.js";
import type { PromiseOrValue } from "../utils/misc.js";
import { valueAsPromise } from "../utils/misc.js";
import { generateTXLink } from "../utils/txLink.js";
import { PendingTransaction } from "./PendingTransaction.js";
import type { TransactionEnvelope } from "./TransactionEnvelope.js";

Expand All @@ -19,11 +20,6 @@ export type TransactionLike =
| PendingTransaction
| TransactionReceipt;

export enum ExplorerType {
SOLANA_EXPLORER = "solana-explorer",
SOLSCAN = "solscan",
}

/**
* Confirms a transaction, returning its receipt.
*
Expand Down Expand Up @@ -102,20 +98,6 @@ export class TransactionReceipt {
* @returns
*/
generateSolanaExplorerLink(cluster: Cluster = "mainnet-beta"): string {
return this.generateTXLink(cluster);
}

generateTXLink(
cluster: Cluster = "mainnet-beta",
explorerType: string = ExplorerType.SOLANA_EXPLORER
): string {
switch (explorerType) {
case ExplorerType.SOLANA_EXPLORER:
return `https://explorer.solana.com/tx/${this.signature}?cluster=${cluster}`;
case ExplorerType.SOLSCAN:
return `https://solscan.io/tx/${this.signature}?cluster=${cluster}`;
default:
throw new Error(`Explorer type ${explorerType} is not supported.`);
}
return generateTXLink(this.signature, cluster);
}
}
1 change: 1 addition & 0 deletions packages/solana-contrib/src/utils/index.ts
Expand Up @@ -6,3 +6,4 @@ export * from "./pubkeyCache.js";
export * from "./publicKey.js";
export * from "./simulateTransactionWithCommitment.js";
export * from "./time.js";
export * from "./txLink.js";
21 changes: 21 additions & 0 deletions packages/solana-contrib/src/utils/txLink.ts
@@ -0,0 +1,21 @@
import type { Cluster } from "@solana/web3.js";

export enum ExplorerType {
SOLANA_EXPLORER = "solana-explorer",
SOLSCAN = "solscan",
}

export function generateTXLink(
signature: string,
cluster: Cluster = "mainnet-beta",
explorerType: string = ExplorerType.SOLANA_EXPLORER
): string {
switch (explorerType) {
case ExplorerType.SOLANA_EXPLORER:
return `https://explorer.solana.com/tx/${signature}?cluster=${cluster}`;
case ExplorerType.SOLSCAN:
return `https://solscan.io/tx/${signature}?cluster=${cluster}`;
default:
throw new Error(`Explorer type ${explorerType} is not supported.`);
}
}

0 comments on commit 555b905

Please sign in to comment.