@rarible/types
Version:
`@rarible/types` is a TypeScript library that provides type definitions and interfaces for the Rarible ecosystem. This package is designed to facilitate seamless integration with Rarible's APIs and services, ensuring type safety and improved developer exp
40 lines (39 loc) • 1.42 kB
JavaScript
import { CustomError } from "@rarible/utils";
import { L1BlockchainByBlockchainDictionary, blockchains, blockchainsLayer1 } from "./domain.js";
export const withLayer1BlockchainRegExp = new RegExp(`^(${blockchainsLayer1.join("|")}):(.*?)$`);
export function isBlockchainSpecified(value) {
return withLayer1BlockchainRegExp.test(value);
}
export function withLayer1Blockchain(blockchain, raw) {
return `${blockchain}:${raw}`;
}
export const withBlockchainRegExp = new RegExp(`^(${blockchains.join("|")}):(.*?)$`);
export function isRealBlockchainSpecified(value) {
return withBlockchainRegExp.test(value);
}
export function withBlockchain(blockchain, raw) {
return `${blockchain}:${raw}`;
}
export function toLayerOneBlockchain(blockchain) {
const converted = L1BlockchainByBlockchainDictionary[blockchain];
if (converted)
return converted;
throw new Error(`Unknown blockchain - ${blockchain}`);
}
export function parseBlockchain(value) {
const safe = parseBlockchainSafe(value);
if (!safe)
throw new BlockchainParseError(value);
return safe;
}
export function parseBlockchainSafe(value) {
const match = withBlockchainRegExp.exec(value);
if (!match)
return undefined;
return [match[1], match[2]];
}
export class BlockchainParseError extends CustomError {
constructor(value) {
super(`Can't parse blockchain from value ${value}`);
}
}