From 9704b894acb0ebd5bdb646b21ae785f5ac8cc7d6 Mon Sep 17 00:00:00 2001 From: Ian Macalinao Date: Fri, 7 Oct 2022 15:25:54 -0500 Subject: [PATCH] feat(solana-contrib): add shortenPubkey function --- packages/solana-contrib/src/utils/misc.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/solana-contrib/src/utils/misc.ts b/packages/solana-contrib/src/utils/misc.ts index b4a17f554..7f87c4f22 100644 --- a/packages/solana-contrib/src/utils/misc.ts +++ b/packages/solana-contrib/src/utils/misc.ts @@ -1,3 +1,5 @@ +import type { PublicKey } from "./publicKey"; + export * from "@saberhq/option-utils"; const noop = () => { @@ -60,3 +62,15 @@ export const valueAsPromise = async ( } return awaitable; }; + +/** + * Shortens a pubkey. + * @param pubkey + * @returns + */ +export const shortenPubkey = (pubkey: PublicKey): string => { + const str = pubkey.toString(); + return str.length > 20 + ? `${str.substring(0, 7)}.....${str.substring(str.length - 7, str.length)}` + : str; +};