UNPKG

@moonbeam-network/xcm-types

Version:
354 lines (345 loc) 7.41 kB
// src/asset/Asset.ts var Asset = class _Asset { key; originSymbol; constructor({ key, originSymbol }) { this.key = key; this.originSymbol = originSymbol; } copyWith(params) { return new _Asset({ ...this, ...params }); } isEqual(asset) { return this.key === asset.key && this.originSymbol === asset.originSymbol; } }; // src/asset/AssetAmount.ts import { convertDecimals, toBigInt as toBigInt2, toDecimal } from "@moonbeam-network/xcm-utils"; import Big from "big.js"; // src/asset/ChainAsset.ts import { toBigInt } from "@moonbeam-network/xcm-utils"; var ChainAsset = class _ChainAsset extends Asset { address; decimals; ids; min; symbol; constructor({ address, decimals, ids, min, symbol, ...other }) { super(other); this.address = address; this.decimals = decimals; this.ids = ids; this.min = min ? toBigInt(min, decimals) : void 0; this.symbol = symbol; } static fromAsset(asset, params) { return new _ChainAsset({ ...asset, ...params }); } copyWith(params) { return new _ChainAsset({ ...this, ...params }); } getSymbol() { return this.symbol || this.originSymbol; } getAssetId() { return this.ids?.id ?? this.originSymbol; } getAssetRegisteredId() { return this.ids?.id; } getBalanceAssetId() { return this.ids?.balanceId ?? this.getAssetId(); } getMinAssetId() { return this.ids?.minId ?? this.getAssetId(); } getAssetPalletInstance() { if (!this.ids?.palletInstance) { throw new Error( `Pallet instance is not defined for ${this.originSymbol}` ); } return this.ids?.palletInstance; } getAssetMin() { return this.min ?? 0n; } getGeneralKey() { return this.ids?.generalKey; } hasOnlyAddress() { return !!this.address && !this.ids?.id; } }; // src/asset/AssetAmount.ts Big.NE = -18; var AssetAmount = class _AssetAmount extends ChainAsset { amount; constructor({ amount, ...other }) { super(other); this.amount = toBigInt2(amount, other.decimals); } static fromChainAsset(asset, params) { return new _AssetAmount({ ...asset, ...params }); } isSame(asset) { return super.isEqual(asset) && this.decimals === asset.decimals; } isEqual(asset) { return this.isSame(asset) && this.amount === asset.amount; } copyWith(params) { return new _AssetAmount({ ...this, ...params }); } convertDecimals(decimals) { if (this.decimals === decimals) { return this.copyWith({}); } return this.copyWith({ amount: convertDecimals(this.amount, this.decimals, decimals), decimals }); } toBig() { return Big(this.amount.toString()); } toBigDecimal(maxDecimal, roundType) { return Big(this.toDecimal(maxDecimal, roundType)); } toDecimal(maxDecimal, roundType) { return toDecimal(this.amount, this.decimals, maxDecimal, roundType); } }; // src/chain/Chain.ts var Chain = class { assets; ecosystem; explorer; isTestChain; key; name; #nativeAsset; wh; constructor({ assets, ecosystem, explorer, isTestChain = false, key, name, nativeAsset, wh }) { this.assets = assets instanceof Map ? assets : new Map(assets?.map((data) => [data.key, data])); this.ecosystem = ecosystem; this.explorer = explorer; this.isTestChain = isTestChain; this.key = key; this.name = name; this.#nativeAsset = nativeAsset; this.wh = wh; } get nativeAsset() { return this.getChainAsset(this.#nativeAsset); } isEqual(chain) { return this.key === chain.key; } getChainAsset(keyOrAsset) { const key = typeof keyOrAsset === "string" ? keyOrAsset : keyOrAsset.key; const chainAsset = this.assets.get(key); if (!chainAsset) { throw new Error( `No ChainAsset found by the key ${key} for chain ${this.name} (${this.key})` ); } return chainAsset; } getWormholeName() { if (!this.wh?.name) { throw new Error(`Chain ${this.name} does not have a wormhole name`); } return this.wh.name; } }; // src/chain/Chain.interfaces.ts var Ecosystem = /* @__PURE__ */ ((Ecosystem2) => { Ecosystem2["Polkadot"] = "polkadot"; Ecosystem2["Kusama"] = "kusama"; Ecosystem2["AlphanetRelay"] = "alphanet-relay"; Ecosystem2["StagenetRelay"] = "stagenet-relay"; return Ecosystem2; })(Ecosystem || {}); // src/chain/Chain.utils.ts import { defineChain } from "viem"; function getViemChain({ id, name, nativeAsset, rpc }) { return defineChain({ id, name, nativeCurrency: { decimals: nativeAsset.decimals, name: nativeAsset.originSymbol, symbol: nativeAsset.originSymbol }, rpcUrls: { default: { http: [rpc] } } }); } // src/chain/EvmChain.ts var EvmChain = class _EvmChain extends Chain { id; rpc; static is(obj) { return obj instanceof _EvmChain; } constructor({ id, rpc, ...others }) { super(others); this.id = id; this.rpc = rpc; } getViemChain() { return getViemChain(this); } copyWith(params) { return new _EvmChain({ ...this, ...params }); } }; // src/chain/parachain/Parachain.ts var Parachain = class _Parachain extends Chain { checkSovereignAccountBalances; genesisHash; isRelay; parachainId; ss58Format; usesChainDecimals; relayGenesisHash; weight; ws; static is(obj) { return obj instanceof _Parachain; } constructor({ checkSovereignAccountBalances, genesisHash, isRelay, parachainId, relayGenesisHash, usesChainDecimals, ss58Format, weight, ws, ...others }) { super(others); this.checkSovereignAccountBalances = !!checkSovereignAccountBalances; this.genesisHash = genesisHash; this.isRelay = !!isRelay; this.parachainId = parachainId; this.relayGenesisHash = relayGenesisHash; this.ss58Format = ss58Format; this.usesChainDecimals = !!usesChainDecimals; this.weight = weight; this.ws = ws; } copyWith(params) { return new _Parachain({ ...this, ...params }); } }; // src/chain/parachain/EvmParachain.ts var EvmParachain = class _EvmParachain extends Parachain { id; rpc; isEvmSigner; contracts; ws; static is(obj) { return obj instanceof _EvmParachain; } static isAnyParachain(obj) { return obj instanceof _EvmParachain || obj instanceof Parachain; } static isAnyEvmChain(obj) { return obj instanceof _EvmParachain || obj instanceof EvmChain; } constructor({ id, rpc, isEvmSigner = false, contracts, ...others }) { super(others); if (isEvmSigner) { if (!id || !rpc) { throw new Error( `'id' and 'rpc' must be provided for ${this.name} if 'isEvmSigner' is true` ); } } this.contracts = contracts; this.id = id ?? 0; this.rpc = rpc ?? ""; this.isEvmSigner = isEvmSigner; this.ws = others.ws; } getViemChain() { return getViemChain(this); } copyWith(params) { return new _EvmParachain({ ...this, ...params }); } }; export { Asset, AssetAmount, Chain, ChainAsset, Ecosystem, EvmChain, EvmParachain, Parachain }; //# sourceMappingURL=index.mjs.map