Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solana-contrib: Refactor generateTXLink #675

Merged
merged 13 commits into from Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.`);
}
}