Skip to content

Commit 555b905

Browse files
authored
solana-contrib: Refactor generateTXLink (#675)
* 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
1 parent 212548c commit 555b905

File tree

4 files changed

+26
-21
lines changed

4 files changed

+26
-21
lines changed

packages/solana-contrib/src/transaction/PendingTransaction.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
import promiseRetry from "promise-retry";
1010
import type { WrapOptions } from "retry";
1111

12+
import { generateTXLink } from "../utils/txLink.js";
1213
import { TransactionReceipt } from "./TransactionReceipt.js";
1314

1415
/**
@@ -165,6 +166,6 @@ export class PendingTransaction {
165166
* @returns
166167
*/
167168
generateSolanaExplorerLink(cluster: Cluster = "mainnet-beta"): string {
168-
return `https://explorer.solana.com/tx/${this.signature}?cluster=${cluster}`;
169+
return generateTXLink(this.signature, cluster);
169170
}
170171
}

packages/solana-contrib/src/transaction/TransactionReceipt.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { default as invariant } from "tiny-invariant";
88
import type { Event, EventParser } from "../interfaces.js";
99
import type { PromiseOrValue } from "../utils/misc.js";
1010
import { valueAsPromise } from "../utils/misc.js";
11+
import { generateTXLink } from "../utils/txLink.js";
1112
import { PendingTransaction } from "./PendingTransaction.js";
1213
import type { TransactionEnvelope } from "./TransactionEnvelope.js";
1314

@@ -19,11 +20,6 @@ export type TransactionLike =
1920
| PendingTransaction
2021
| TransactionReceipt;
2122

22-
export enum ExplorerType {
23-
SOLANA_EXPLORER = "solana-explorer",
24-
SOLSCAN = "solscan",
25-
}
26-
2723
/**
2824
* Confirms a transaction, returning its receipt.
2925
*
@@ -102,20 +98,6 @@ export class TransactionReceipt {
10298
* @returns
10399
*/
104100
generateSolanaExplorerLink(cluster: Cluster = "mainnet-beta"): string {
105-
return this.generateTXLink(cluster);
106-
}
107-
108-
generateTXLink(
109-
cluster: Cluster = "mainnet-beta",
110-
explorerType: string = ExplorerType.SOLANA_EXPLORER
111-
): string {
112-
switch (explorerType) {
113-
case ExplorerType.SOLANA_EXPLORER:
114-
return `https://explorer.solana.com/tx/${this.signature}?cluster=${cluster}`;
115-
case ExplorerType.SOLSCAN:
116-
return `https://solscan.io/tx/${this.signature}?cluster=${cluster}`;
117-
default:
118-
throw new Error(`Explorer type ${explorerType} is not supported.`);
119-
}
101+
return generateTXLink(this.signature, cluster);
120102
}
121103
}

packages/solana-contrib/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export * from "./pubkeyCache.js";
66
export * from "./publicKey.js";
77
export * from "./simulateTransactionWithCommitment.js";
88
export * from "./time.js";
9+
export * from "./txLink.js";
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { Cluster } from "@solana/web3.js";
2+
3+
export enum ExplorerType {
4+
SOLANA_EXPLORER = "solana-explorer",
5+
SOLSCAN = "solscan",
6+
}
7+
8+
export function generateTXLink(
9+
signature: string,
10+
cluster: Cluster = "mainnet-beta",
11+
explorerType: string = ExplorerType.SOLANA_EXPLORER
12+
): string {
13+
switch (explorerType) {
14+
case ExplorerType.SOLANA_EXPLORER:
15+
return `https://explorer.solana.com/tx/${signature}?cluster=${cluster}`;
16+
case ExplorerType.SOLSCAN:
17+
return `https://solscan.io/tx/${signature}?cluster=${cluster}`;
18+
default:
19+
throw new Error(`Explorer type ${explorerType} is not supported.`);
20+
}
21+
}

0 commit comments

Comments
 (0)