Lens Blocks are currently in beta and not ready for production use!

Utilities Library

Utility functions for interacting with Lens Social Protocol, including formatting and data manipulation.

Installation

Usage

The utilities provided in this module can be imported and used in your project as follows:

Constants

NameDescription
LensChainIdThe chain ID for the Lens Chain mainnet
LensChainNativeTokenThe native token address in Lens Chain
LensChainTestnetIdThe chain ID for the Lens Chain testnet
ZeroAddressA zero (or empty) address in EVM-compatible blockchains

Functions

arweaveUrlToGatewayUrl string

Convert an Arweave URL to a gateway URL

Props

PropDescriptionType
arUrl

The Arweave URL to convert

string
gatewayDomain

The gateway domain to use (default: https://arweave.net/)

string

formatFollowerCount string

Format a follower count as a string, using "k" for thousands and "m" for millions

Props

PropDescriptionType
count

The follower count to format

number

getAudioExtension string

Get the file extension for a given MediaAudioType

Props

PropDescriptionType
mediaAudioType

The MediaAudioType to get the extension for

MediaAudioType

getCidFromIpfsUrl string

Extract the CID from an IPFS URL

Props

PropDescriptionType
ipfsUrl

The IPFS URL to extract the CID from

string

getDisplayName string

Get the display name for an account, prioritizing the metadata name, then the username, and finally truncating the address.

Props

PropDescriptionType
account

The account to get the display name for

Account

getMetadataContent string | undefined

Safely extracts a textual content field from a post's metadata, if present.

This is useful for metadata types that include a content property (e.g., text or article types).

Props

PropDescriptionType
post
  • The post whose metadata content should be read.
AnyPost

getUsernamePath string

Get the path for a Lens username, optionally including the namespace if it's not the default "lens" namespace.

Props

PropDescriptionType
username
  • The full username, including the namespace (e.g., "@lens/username")
string
namespace
  • The namespace address of the username. If not provided, the default namespace will be assumed.
EvmAddress

getVideoExtension string

Get the file extension for a given MediaVideoType

Props

PropDescriptionType
mediaVideoType

The MediaVideoType to get the extension for

MediaVideoType

hasMetadata post is (Post & { metadata: PostMetadata }) | (ReferencedPost & { metadata: PostMetadata })

Type guard that checks whether a post has a metadata field.

Narrows AnyPost to a Post/ReferencedPost with a non-undefined metadata.

Props

PropDescriptionType
post
  • The post to inspect.
AnyPost

ipfsUrlToGatewayUrl string

Convert an IPFS URL to a gateway URL

Props

PropDescriptionType
ipfsUrl

The IPFS URL to convert

string
gatewayDomain

The gateway domain to use (default: https://ipfs.io/ipfs/)

string

isArticlePost post is ArticlePost

Type guard for posts whose metadata is ArticleMetadata.

Props

PropDescriptionType
post
  • The post to inspect.
AnyPost

isAudioPost post is AudioPost

Type guard for posts whose metadata is AudioMetadata.

Props

PropDescriptionType
post
  • The post to inspect.
AnyPost

isImagePost post is ImagePost

Type guard for posts whose metadata is ImageMetadata.

Props

PropDescriptionType
post
  • The post to inspect.
AnyPost

isLinkPost post is LinkPost

Type guard for posts whose metadata is LinkMetadata.

Props

PropDescriptionType
post
  • The post to inspect.
AnyPost

isLiveStreamPost post is LiveStreamPost

Type guard for posts whose metadata is LiveStreamMetadata.

Props

PropDescriptionType
post
  • The post to inspect.
AnyPost

isMintPost post is MintPost

Type guard for posts whose metadata is MintMetadata.

Props

PropDescriptionType
post
  • The post to inspect.
AnyPost

isSpacePost post is SpacePost

Type guard for posts whose metadata is SpaceMetadata.

Props

PropDescriptionType
post
  • The post to inspect.
AnyPost

isStoryPost post is StoryPost

Type guard for posts whose metadata is StoryMetadata.

Props

PropDescriptionType
post
  • The post to inspect.
AnyPost

isTextOnlyPost post is TextOnlyPost

Type guard for posts whose metadata is TextOnlyMetadata.

Props

PropDescriptionType
post
  • The post to inspect.
AnyPost

isThreeDPost post is ThreeDPost

Type guard for posts whose metadata is ThreeDMetadata.

Props

PropDescriptionType
post
  • The post to inspect.
AnyPost

isTransactionPost post is TransactionPost

Type guard for posts whose metadata is TransactionMetadata.

Props

PropDescriptionType
post
  • The post to inspect.
AnyPost

isVideoPost post is VideoPost

Type guard for posts whose metadata is VideoMetadata.

Props

PropDescriptionType
post
  • The post to inspect.
AnyPost

lensUrlToGatewayUrl string

Convert a Lens URL to a gateway URL

Props

PropDescriptionType
lensUrl

The Lens URL to convert

string
gatewayDomain

The gateway domain to use (default: https://api.grove.storage/)

string

parseUri string | undefined

Parse a URI and convert it to a gateway URL if it is an IPFS, Arweave, or Lens URL

Props

PropDescriptionType
uri

The URI to parse

string | undefined

toApiError SigningError | TransactionIndexingError | UnauthenticatedError | UnexpectedError | ValidationError

Converts an unknown error to a Lens API UnexpectedError, or returns the original error if it's a known error.'

Props

PropDescriptionType
eunknown

truncateAddress string

Truncate a string to a maximum length, adding an ellipsis in the middle

Props

PropDescriptionType
address

The string to truncate

string
maxLength

The maximum length of the truncated string, excluding the ellipsis and 0x prefix

number

truncateUrl string

Truncates a given URL to a specified maximum length and removes common prefixes/suffixes.

  • Strips out http://, https://, and leading www. from the URL.
  • Removes trailing / or \ characters.
  • Truncates the cleaned URL to the given maxLength.
  • Appends an ellipsis () if the original URL exceeds maxLength.

Props

PropDescriptionType
url
  • The full URL string to truncate.
string
maxLength
  • The maximum allowed length of the resulting string (default: 30).
number