@chain-registry/keplr
Version:
Chain Registry to Keplr
174 lines (173 loc) • 7.54 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.chainRegistryChainToKeplr = void 0;
exports.extractVersion = extractVersion;
const cosmos_1 = require("@keplr-wallet/cosmos");
const semver_1 = __importDefault(require("semver"));
const getRpc = (chain) => chain.apis?.rpc?.[0]?.address ?? '';
const getRest = (chain) => chain.apis?.rest?.[0]?.address ?? '';
const getExplr = (chain) => chain.explorers?.[0]?.url ?? '';
const versionPatterns = {
fullSemver: /v?(\d+\.\d+\.\d+)(?=-[\w.-]+|$)/, // Matches complete semver patterns like '0.47.20' or 'v0.47.20'
partialSemver: /v?(\d+\.\d+)(?=(?:\.\d+)?(?=-[\w.-]+|$))/, // Matches partial semver patterns like '0.47' or 'v0.47'
basicVersion: /v?(\d+)(?=(?:\.\d+)?(?:\.\d+)?(?=-[\w.-]+|$))/, // Matches basic versions like '0' or 'v0'
tagged: /@v(\d+\.\d+)(?:\.x)?(?=-[\w.-]+|$)/, // Specific for tagged formats
embedded: /\/v(\d+\.\d+\.\d+)(?=-[\w.-]+|$)/, // For versions embedded in paths
simple: /v?(\d+)(?:\.(\d+))?(?:\.(\d+))?$/, // General simple versions
complexEmbedded: /[\w-]+\/[\w-]+ v(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-\d+[\w.-]*)/, // Complex formats with namespaces
taggedVersion: /[\w-]+\/[\w-]+ v(\d+)(?:\.(\d+))?(?:\.(\d+))?(?=-[\w.-]+|$)/ // Versions with descriptive tags
};
function extractVersion(input) {
let version = null;
// Check each pattern in turn
if (versionPatterns.fullSemver.test(input)) {
version = input.match(versionPatterns.fullSemver)?.[1];
}
else if (versionPatterns.partialSemver.test(input)) {
version = input.match(versionPatterns.partialSemver)?.[1];
}
else if (versionPatterns.basicVersion.test(input)) {
version = input.match(versionPatterns.basicVersion)?.[1];
}
else if (versionPatterns.taggedVersion.test(input)) {
version = input.match(versionPatterns.taggedVersion)?.[1];
}
else if (versionPatterns.complexEmbedded.test(input)) {
version = input.match(versionPatterns.complexEmbedded)?.[1];
}
else if (versionPatterns.simple.test(input)) {
version = input.match(versionPatterns.simple)?.[1];
}
else if (versionPatterns.tagged.test(input)) {
version = input.match(versionPatterns.tagged)?.[1];
}
else if (versionPatterns.embedded.test(input)) {
version = input.match(versionPatterns.embedded)?.[1];
}
return version ? normalizeVersion(version) : null;
}
function normalizeVersion(version) {
// Ensures the version is normalized to the 'x.y.z' format, if applicable
const parts = version.split('.');
while (parts.length < 3) {
parts.push('0');
}
return parts.join('.');
}
const chainRegistryChainToKeplr = (chain, assets, options = {
getRpcEndpoint: getRpc,
getRestEndpoint: getRest,
getExplorer: getExplr
}) => {
if (!options.getRestEndpoint)
options.getRestEndpoint = getRest;
if (!options.getRpcEndpoint)
options.getRpcEndpoint = getRpc;
if (!options.getExplorer)
options.getExplorer = getExplr;
const features = [];
// check if it's a Cosmos SDK chain (either new or old format)
const isCosmosSDKChain = chain.codebase?.sdk?.type === 'cosmos' || !!chain.codebase?.cosmosSdkVersion;
if (isCosmosSDKChain) {
// determine SDK version
let sdkVer;
if (chain.codebase?.sdk?.type === 'cosmos' && chain.codebase.sdk.version) {
sdkVer = extractVersion(chain.codebase.sdk.version);
}
else if (chain.codebase?.cosmosSdkVersion) {
sdkVer = extractVersion(chain.codebase.cosmosSdkVersion);
}
else {
// if NOT specified, we assume stargate, sorry not sorry
sdkVer = '0.40.0';
}
// stargate
if (semver_1.default.satisfies(sdkVer, '>=0.40'))
features.push('stargate');
// no-legacy-stdTx
if (semver_1.default.satisfies(sdkVer, '>=0.43'))
features.push('no-legacy-stdTx');
// until further notice, assume 'ibc-transfer'
features.push('ibc-transfer');
// ibc-go
if (semver_1.default.satisfies(sdkVer, '>=0.45'))
features.push('ibc-go');
// CosmWasm feature detection
const cosmwasmEnabled = chain.codebase?.cosmwasm?.enabled ?? chain.codebase?.cosmwasmEnabled ?? false;
if (cosmwasmEnabled) {
features.push('cosmwasm');
const wasmVer = extractVersion(chain.codebase?.cosmwasm?.version ?? chain.codebase?.cosmwasmVersion ?? '0.24.0');
if (semver_1.default.satisfies(wasmVer, '>=0.24.0'))
features.push('wasmd_0.24+');
}
}
const chainAssets = assets.find((asset) => asset.chainName === chain.chainName)?.assets || [];
const feeDenoms = chain.fees?.feeTokens.map((feeToken) => feeToken.denom) || [];
const gasPriceSteps = chain.fees?.feeTokens?.reduce((m, feeToken) => {
m[feeToken.denom] = {
low: feeToken.lowGasPrice ?? 0.01,
average: feeToken.averageGasPrice ?? 0.025,
high: feeToken.highGasPrice ?? 0.04
};
return m;
}, {});
const stakingDenoms = chain.staking?.stakingTokens.map((stakingToken) => stakingToken.denom) || [];
const currencies = chainAssets.map((currency) => {
return {
coinDenom: currency.symbol,
coinMinimalDenom: currency.base,
coinDecimals: currency.denomUnits.filter((denomUnit) => denomUnit.denom === currency.display)[0]?.exponent,
coinGeckoId: currency.coingeckoId || undefined,
coinImageUrl: currency.logoURIs?.svg ?? currency.logoURIs?.png
};
});
const stakeCurrency = currencies.find((currency) => stakingDenoms.includes(currency.coinDenom)) ??
currencies[0];
const feeCurrencies = currencies
// USE THE FEE DENOMS
.filter((currency) => feeDenoms.includes(currency.coinMinimalDenom))
.map((feeCurrency) => {
if (!(feeCurrency.coinMinimalDenom in gasPriceSteps)) {
return feeCurrency;
}
// has gas
const gasPriceStep = gasPriceSteps[feeCurrency.coinMinimalDenom];
return {
...feeCurrency,
gasPriceStep
};
});
const feeCurrenciesDefault = currencies
// USE THE STAKE CURRENCY
.filter((currency) => stakeCurrency.coinDenom === currency.coinDenom)
.map((feeCurrency) => {
if (!(feeCurrency.coinMinimalDenom in gasPriceSteps)) {
return feeCurrency;
}
// has gas
const gasPriceStep = gasPriceSteps[feeCurrency.coinMinimalDenom];
return {
...feeCurrency,
gasPriceStep
};
});
const chainInfo = {
rpc: options.getRpcEndpoint(chain),
rest: options.getRestEndpoint(chain),
chainId: chain.chainId,
chainName: chain.prettyName,
bip44: {
coinType: chain.slip44
},
bech32Config: cosmos_1.Bech32Address.defaultBech32Config(chain.bech32Prefix),
currencies: currencies,
stakeCurrency: stakeCurrency || currencies[0],
feeCurrencies: feeCurrencies.length !== 0 ? feeCurrencies : feeCurrenciesDefault,
features
};
return chainInfo;
};
exports.chainRegistryChainToKeplr = chainRegistryChainToKeplr;