Skip to content

Commit ff1419f

Browse files
bmzigfusmanii
andauthored
improve: define Solana block explorer formatting (#1191)
Signed-off-by: bennett <bennett@umaproject.org> Co-authored-by: Faisal Usmani <faisal.of.usmani@gmail.com>
1 parent 586fafb commit ff1419f

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

src/utils/BlockExplorerUtils.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { ethers } from "ethers";
22
import { PUBLIC_NETWORKS } from "../constants";
3-
import { createShortHexString } from "./FormattingUtils";
3+
import { createShortenedString } from "./FormattingUtils";
4+
import { chainIsEvm } from "./NetworkUtils";
45
import { isDefined } from "./TypeGuards";
6+
import bs58 from "bs58";
57

68
/**
79
* Creates a block explorer link for a transaction or address on a given network.
@@ -49,32 +51,37 @@ function constructURL(domain: string, parts: string[]): string {
4951
* @param chainId The chainId to link to.
5052
* @returns A formatted markdown block explorer link to the given transaction hash or address on the given chainId.
5153
*/
52-
function _createBlockExplorerLinkMarkdown(hex: string, chainId = 1): string | null {
54+
function _createBlockExplorerLinkMarkdown(addr: string, chainId = 1): string | null {
5355
// Attempt to resolve the block explorer domain for the given chainId.
5456
const explorerDomain = resolveBlockExplorerDomain(chainId);
5557
// If the chainId is not supported, return an unsupported link.
5658
if (!isDefined(explorerDomain)) {
57-
return `<unsupported chain/hash ${chainId}:${hex}>}`;
59+
return `<unsupported chain/hash ${chainId}:${addr}>}`;
5860
}
5961
// Ensure that the first two characters are "0x". If they are not, append them.
60-
if (hex.substring(0, 2) !== "0x") {
61-
hex = `0x${hex}`;
62-
}
63-
// Ensure that the hex string is a valid hexadecimal string.
64-
if (!ethers.utils.isHexString(hex)) {
65-
return null;
62+
if (addr.substring(0, 2) !== "0x" && chainIsEvm(chainId)) {
63+
addr = `0x${addr}`;
64+
if (!ethers.utils.isHexString(addr)) {
65+
return null;
66+
}
6667
}
6768
// Resolve the short URL string.
68-
const shortURLString = createShortHexString(hex);
69-
// Iterate over the two possible hex lengths.
70-
for (const [length, route] of [
71-
[66, "tx"],
72-
[42, "address"],
73-
] as [number, string][]) {
74-
// If the hex string is the correct length, return the link.
75-
if (hex.length === length) {
76-
return `<${constructURL(explorerDomain, [route, hex])} | ${shortURLString}>`;
69+
const shortURLString = createShortenedString(addr);
70+
if (chainIsEvm(chainId)) {
71+
// Iterate over the two possible addr lengths.
72+
for (const [length, route] of [
73+
[66, "tx"],
74+
[42, "address"],
75+
] as [number, string][]) {
76+
// If the hex string is the correct length, return the link.
77+
if (addr.length === length) {
78+
return `<${constructURL(explorerDomain, [route, addr])} | ${shortURLString}>`;
79+
}
7780
}
81+
} else {
82+
const addrLength = bs58.decode(addr).length;
83+
const route = addrLength === 32 ? "account" : "tx";
84+
return `<${constructURL(explorerDomain, [route, addr])} | ${shortURLString}>`;
7885
}
7986
return null;
8087
}

src/utils/FormattingUtils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ export const formatFeePct = (relayerFeePct: BN): string => {
6262
* @param maxLength The maximum length of the shortened string. Defaults to 8.
6363
* @param delimiter The delimiter to use in the middle of the shortened string. Defaults to "...".
6464
* @returns The shortened hexadecimal string.
65-
* @example createShortHexString("0x772871a444c6e4e9903d8533a5a13101b74037158123e6709470f0afbf6e7d94") -> "0x7787...7d94"
65+
* @example createShortenedString("0x772871a444c6e4e9903d8533a5a13101b74037158123e6709470f0afbf6e7d94") -> "0x7787...7d94"
6666
*/
67-
export function createShortHexString(hex: string, maxLength = 8, delimiter = ".."): string {
67+
export function createShortenedString(hex: string, maxLength = 8, delimiter = ".."): string {
6868
// If we have more maxLength then the hex size, we can simply
6969
// return the hex directly.
7070
if (hex.length <= maxLength) {
@@ -134,10 +134,10 @@ export function formatGwei(weiVal: string): string {
134134
* Shortens a list of addresses to a shorter version with only the first 10 characters.
135135
* @param addresses A list of addresses to shorten.
136136
* @returns A list of shortened addresses.
137-
* @see createShortHexString
137+
* @see createShortenedString
138138
*/
139139
export function shortenHexStrings(addresses: string[]): string[] {
140-
return addresses.map((h) => createShortHexString(h));
140+
return addresses.map((h) => createShortenedString(h));
141141
}
142142

143143
// formatWei converts a string or BN instance from Wei to Ether, e.g., 1e19 -> 10.

0 commit comments

Comments
 (0)