@moonbeam-network/xcm-utils
Version:
Moonbeam XCM utilities
162 lines (157 loc) • 4.86 kB
JavaScript
// src/format/address.ts
function isHexString(asset) {
return typeof asset === "string" && asset.startsWith("0x");
}
function isEthAddress(address) {
return address.length === 42 && address.startsWith("0x");
}
// src/format/asset.ts
function formatAssetIdToERC20(id) {
if (id.startsWith("0x")) {
return id;
}
if (!/^\d{38,39}$/.test(id)) {
throw new Error(`Asset id: ${id} must be a string and have 38-39 digits`);
}
return `0xffffffff${BigInt(id).toString(16).padStart(32, "0")}`;
}
function convertAddressTo32Bytes(address) {
if (!/^0x[a-fA-F0-9]{40}$/.test(address)) {
throw new Error(`Invalid address: ${address}`);
}
return `0x000000000000000000000000${address.substring(2)}`;
}
// src/numbers/decimals.ts
import Big from "big.js";
Big.NE = -18;
function toDecimal(number, decimals, maxDecimal = 6, roundType) {
const dividend = Big(number.toString().replace(/[^0-9]/g, ""));
const divisor = Big(10).pow(decimals);
const result = dividend.div(divisor).round(maxDecimal, roundType);
return result.toString();
}
function toBigInt(amount, decimals) {
if (typeof amount === "bigint") {
return amount;
}
const multiplier = Big(10).pow(decimals);
const result = Big(amount).mul(multiplier);
return BigInt(result.toFixed(0, Big.roundDown));
}
function convertDecimals(number, decimals, targetDecimals) {
const decimalNumber = toDecimal(number, decimals, decimals);
return toBigInt(decimalNumber.toString(), targetDecimals);
}
function hasDecimalOverflow(fl, maxDecimal) {
const parts = fl.toString().split(".");
return parts.length > 1 && parts[1].length > maxDecimal;
}
// src/polkadot/polkadot.address.ts
import {
bnToU8a,
compactToU8a,
hexToU8a,
stringToU8a,
u8aToHex
} from "@polkadot/util";
import { blake2AsU8a, decodeAddress } from "@polkadot/util-crypto";
function getSovereignAccountAddresses(paraId) {
const paraIdU8a = bnToU8a(paraId, { bitLength: 32 });
const relay = u8aToHex(
new Uint8Array([...stringToU8a("para"), ...paraIdU8a])
).padEnd(66, "0");
const generic = u8aToHex(
new Uint8Array([...stringToU8a("sibl"), ...paraIdU8a])
).padEnd(66, "0");
const moonbeam = generic.slice(0, 42);
return {
generic,
moonbeam,
relay
};
}
function getMultilocationDerivedAddresses({
paraId,
address,
isParents = false
}) {
const parents = isParents ? 1 : 0;
const accType = isEthAddress(address) ? "AccountKey20" : "AccountId32";
const decodedAddress = isEthAddress(address) ? hexToU8a(address) : decodeAddress(address);
const family = parents === 0 && paraId ? "ChildChain" : parents === 1 && !paraId ? "ParentChain" : "SiblingChain";
const blake = blake2AsU8a(
new Uint8Array([
...stringToU8a(family),
...paraId ? compactToU8a(paraId) : [],
...compactToU8a(accType.length + (isEthAddress(address) ? 20 : 32)),
...stringToU8a(accType),
...decodedAddress
])
);
const address20 = u8aToHex(blake.slice(0, 20));
const address32 = u8aToHex(blake.slice(0, 32));
return {
address20,
address32
};
}
// src/polkadot/polkadot.api.ts
import { ApiPromise, WsProvider } from "@polkadot/api";
import { typesBundle } from "@polkadot/apps-config";
import { LRUCache } from "lru-cache";
var MRLTypes = /* @__PURE__ */ ((MRLTypes2) => {
MRLTypes2["XcmVersionedMultiLocation"] = "XcmVersionedMultiLocation";
MRLTypes2["XcmVersionedLocation"] = "XcmVersionedLocation";
MRLTypes2["XcmRoutingUserAction"] = "XcmRoutingUserAction";
MRLTypes2["VersionedUserAction"] = "VersionedUserAction";
return MRLTypes2;
})(MRLTypes || {});
var cache = new LRUCache({
max: 20,
dispose: async (promise) => {
const api = await promise;
if (api.isConnected) {
api.disconnect();
}
}
});
async function getPolkadotApi(ws) {
const key = Array.isArray(ws) ? ws.join(";") : ws;
const promise = cache.get(key) || ApiPromise.create({
noInitWarn: true,
provider: new WsProvider(ws),
types: {
["XcmRoutingUserAction" /* XcmRoutingUserAction */]: {
destination: "XcmVersionedLocation" /* XcmVersionedLocation */
},
["VersionedUserAction" /* VersionedUserAction */]: {
_enum: { V1: "XcmRoutingUserAction" /* XcmRoutingUserAction */ }
}
},
typesBundle
});
cache.set(key, promise);
const api = await promise;
await api.isReady;
return api;
}
// src/polkadot/polkadot.apps.ts
function getPolkadotAppsUrl(ws) {
return `https://polkadot.js.org/apps/?rpc=${encodeURIComponent(ws)}#/explorer/query`;
}
export {
MRLTypes,
convertAddressTo32Bytes,
convertDecimals,
formatAssetIdToERC20,
getMultilocationDerivedAddresses,
getPolkadotApi,
getPolkadotAppsUrl,
getSovereignAccountAddresses,
hasDecimalOverflow,
isEthAddress,
isHexString,
toBigInt,
toDecimal
};
//# sourceMappingURL=index.mjs.map