@moonbeam-network/xcm-utils 1.0.0-dev.209 → 1.0.0-dev.210

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/index.d.ts CHANGED
@@ -12,16 +12,6 @@ declare function toBigInt(amount: bigint | string | number, decimals: number): b
12
12
  declare function convertDecimals(number: string | bigint, decimals: number, targetDecimals: number): bigint;
13
13
  declare function hasDecimalOverflow(fl: number | string, maxDecimal: number): boolean;
14
14
 
15
- declare enum MRLTypes {
16
- XcmVersionedMultiLocation = "XcmVersionedMultiLocation",
17
- XcmVersionedLocation = "XcmVersionedLocation",
18
- XcmRoutingUserAction = "XcmRoutingUserAction",
19
- VersionedUserAction = "VersionedUserAction"
20
- }
21
- declare function getPolkadotApi(ws: string | string[]): Promise<ApiPromise>;
22
-
23
- declare function getPolkadotAppsUrl(ws: string): string;
24
-
25
15
  /**
26
16
  * reference: https://github.com/Moonsong-Labs/xcm-tools/blob/main/scripts/calculate-sovereign-account.ts
27
17
  */
@@ -42,4 +32,15 @@ declare function getMultilocationDerivedAddresses({ paraId, address, isParents,
42
32
  address32: `0x${string}`;
43
33
  };
44
34
 
45
- export { MRLTypes, convertAddressTo32Bytes, convertDecimals, formatAssetIdToERC20, getMultilocationDerivedAddresses, getPolkadotApi, getPolkadotAppsUrl, getSovereignAccountAddresses, hasDecimalOverflow, isEthAddress, isHexString, toBigInt, toDecimal };
35
+ declare enum MRLTypes {
36
+ XcmVersionedMultiLocation = "XcmVersionedMultiLocation",
37
+ XcmVersionedLocation = "XcmVersionedLocation",
38
+ XcmRoutingUserAction = "XcmRoutingUserAction",
39
+ VersionedUserAction = "VersionedUserAction"
40
+ }
41
+ declare function getPolkadotApi(ws: string | string[]): Promise<ApiPromise>;
42
+ declare function getPolkadotHttpApi(rpc: string): Promise<ApiPromise>;
43
+
44
+ declare function getPolkadotAppsUrl(ws: string): string;
45
+
46
+ export { MRLTypes, convertAddressTo32Bytes, convertDecimals, formatAssetIdToERC20, getMultilocationDerivedAddresses, getPolkadotApi, getPolkadotAppsUrl, getPolkadotHttpApi, getSovereignAccountAddresses, hasDecimalOverflow, isEthAddress, isHexString, toBigInt, toDecimal };
package/build/index.mjs CHANGED
@@ -49,52 +49,6 @@ function hasDecimalOverflow(fl, maxDecimal) {
49
49
  return parts.length > 1 && parts[1].length > maxDecimal;
50
50
  }
51
51
 
52
- // src/polkadot/polkadot.api.ts
53
- import { ApiPromise, WsProvider } from "@polkadot/api";
54
- import { typesBundle } from "@polkadot/apps-config";
55
- import { LRUCache } from "lru-cache";
56
- var MRLTypes = /* @__PURE__ */ ((MRLTypes2) => {
57
- MRLTypes2["XcmVersionedMultiLocation"] = "XcmVersionedMultiLocation";
58
- MRLTypes2["XcmVersionedLocation"] = "XcmVersionedLocation";
59
- MRLTypes2["XcmRoutingUserAction"] = "XcmRoutingUserAction";
60
- MRLTypes2["VersionedUserAction"] = "VersionedUserAction";
61
- return MRLTypes2;
62
- })(MRLTypes || {});
63
- var cache = new LRUCache({
64
- max: 20,
65
- dispose: async (promise) => {
66
- const api = await promise;
67
- if (api.isConnected) {
68
- api.disconnect();
69
- }
70
- }
71
- });
72
- async function getPolkadotApi(ws) {
73
- const key = Array.isArray(ws) ? ws.join(";") : ws;
74
- const promise = cache.get(key) || ApiPromise.create({
75
- noInitWarn: true,
76
- provider: new WsProvider(ws),
77
- types: {
78
- ["XcmRoutingUserAction" /* XcmRoutingUserAction */]: {
79
- destination: "XcmVersionedLocation" /* XcmVersionedLocation */
80
- },
81
- ["VersionedUserAction" /* VersionedUserAction */]: {
82
- _enum: { V1: "XcmRoutingUserAction" /* XcmRoutingUserAction */ }
83
- }
84
- },
85
- typesBundle
86
- });
87
- cache.set(key, promise);
88
- const api = await promise;
89
- await api.isReady;
90
- return api;
91
- }
92
-
93
- // src/polkadot/polkadot.apps.ts
94
- function getPolkadotAppsUrl(ws) {
95
- return `https://polkadot.js.org/apps/?rpc=${encodeURIComponent(ws)}#/explorer/query`;
96
- }
97
-
98
52
  // src/polkadot/polkadot.address.ts
99
53
  import {
100
54
  bnToU8a,
@@ -144,6 +98,72 @@ function getMultilocationDerivedAddresses({
144
98
  address32
145
99
  };
146
100
  }
101
+
102
+ // src/polkadot/polkadot.api.ts
103
+ import { ApiPromise, HttpProvider, WsProvider } from "@polkadot/api";
104
+ import { typesBundle } from "@polkadot/apps-config";
105
+ import { LRUCache } from "lru-cache";
106
+ var MRLTypes = /* @__PURE__ */ ((MRLTypes2) => {
107
+ MRLTypes2["XcmVersionedMultiLocation"] = "XcmVersionedMultiLocation";
108
+ MRLTypes2["XcmVersionedLocation"] = "XcmVersionedLocation";
109
+ MRLTypes2["XcmRoutingUserAction"] = "XcmRoutingUserAction";
110
+ MRLTypes2["VersionedUserAction"] = "VersionedUserAction";
111
+ return MRLTypes2;
112
+ })(MRLTypes || {});
113
+ var cache = new LRUCache({
114
+ max: 20,
115
+ dispose: async (promise) => {
116
+ const api = await promise;
117
+ if (api.isConnected) {
118
+ api.disconnect();
119
+ }
120
+ }
121
+ });
122
+ async function getPolkadotApi(ws) {
123
+ const key = Array.isArray(ws) ? ws.join(";") : ws;
124
+ const promise = cache.get(key) || ApiPromise.create({
125
+ noInitWarn: true,
126
+ provider: new WsProvider(ws),
127
+ types: {
128
+ ["XcmRoutingUserAction" /* XcmRoutingUserAction */]: {
129
+ destination: "XcmVersionedLocation" /* XcmVersionedLocation */
130
+ },
131
+ ["VersionedUserAction" /* VersionedUserAction */]: {
132
+ _enum: { V1: "XcmRoutingUserAction" /* XcmRoutingUserAction */ }
133
+ }
134
+ },
135
+ typesBundle
136
+ });
137
+ cache.set(key, promise);
138
+ const api = await promise;
139
+ await api.isReady;
140
+ return api;
141
+ }
142
+ async function getPolkadotHttpApi(rpc) {
143
+ const key = rpc;
144
+ const promise = cache.get(key) || ApiPromise.create({
145
+ noInitWarn: true,
146
+ provider: new HttpProvider(rpc),
147
+ types: {
148
+ ["XcmRoutingUserAction" /* XcmRoutingUserAction */]: {
149
+ destination: "XcmVersionedLocation" /* XcmVersionedLocation */
150
+ },
151
+ ["VersionedUserAction" /* VersionedUserAction */]: {
152
+ _enum: { V1: "XcmRoutingUserAction" /* XcmRoutingUserAction */ }
153
+ }
154
+ },
155
+ typesBundle
156
+ });
157
+ cache.set(key, promise);
158
+ const api = await promise;
159
+ await api.isReady;
160
+ return api;
161
+ }
162
+
163
+ // src/polkadot/polkadot.apps.ts
164
+ function getPolkadotAppsUrl(ws) {
165
+ return `https://polkadot.js.org/apps/?rpc=${encodeURIComponent(ws)}#/explorer/query`;
166
+ }
147
167
  export {
148
168
  MRLTypes,
149
169
  convertAddressTo32Bytes,
@@ -152,6 +172,7 @@ export {
152
172
  getMultilocationDerivedAddresses,
153
173
  getPolkadotApi,
154
174
  getPolkadotAppsUrl,
175
+ getPolkadotHttpApi,
155
176
  getSovereignAccountAddresses,
156
177
  hasDecimalOverflow,
157
178
  isEthAddress,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/format/address.ts","../src/format/asset.ts","../src/numbers/decimals.ts","../src/polkadot/polkadot.api.ts","../src/polkadot/polkadot.apps.ts","../src/polkadot/polkadot.address.ts"],"sourcesContent":["export function isHexString(asset: unknown): boolean {\n return typeof asset === 'string' && asset.startsWith('0x');\n}\n\nexport function isEthAddress(address: string): boolean {\n return address.length === 42 && address.startsWith('0x');\n}\n","export function formatAssetIdToERC20(id: string) {\n if (id.startsWith('0x')) {\n return id;\n }\n\n if (!/^\\d{38,39}$/.test(id)) {\n throw new Error(`Asset id: ${id} must be a string and have 38-39 digits`);\n }\n\n return `0xffffffff${BigInt(id).toString(16).padStart(32, '0')}`;\n}\n\nexport function convertAddressTo32Bytes(address: string): string {\n if (!/^0x[a-fA-F0-9]{40}$/.test(address)) {\n throw new Error(`Invalid address: ${address}`);\n }\n\n return `0x000000000000000000000000${address.substring(2)}`;\n}\n","import Big, { type RoundingMode } from 'big.js';\n\nBig.NE = -18;\n\nexport function toDecimal(\n number: bigint | number | string,\n decimals: number,\n maxDecimal = 6,\n roundType?: RoundingMode,\n): string {\n const dividend = Big(number.toString().replace(/[^0-9]/g, ''));\n const divisor = Big(10).pow(decimals);\n const result = dividend.div(divisor).round(maxDecimal, roundType);\n\n return result.toString();\n}\n\nexport function toBigInt(\n amount: bigint | string | number,\n decimals: number,\n): bigint {\n if (typeof amount === 'bigint') {\n return amount;\n }\n\n const multiplier = Big(10).pow(decimals);\n const result = Big(amount).mul(multiplier);\n\n return BigInt(result.toFixed(0, Big.roundDown));\n}\n\nexport function convertDecimals(\n number: string | bigint,\n decimals: number,\n targetDecimals: number,\n): bigint {\n const decimalNumber = toDecimal(number, decimals, decimals);\n\n return toBigInt(decimalNumber.toString(), targetDecimals);\n}\n\nexport function hasDecimalOverflow(fl: number | string, maxDecimal: number) {\n const parts = fl.toString().split('.');\n return parts.length > 1 && parts[1].length > maxDecimal;\n}\n","import { ApiPromise, WsProvider } from '@polkadot/api';\nimport { typesBundle } from '@polkadot/apps-config';\nimport { LRUCache } from 'lru-cache';\n\nexport enum MRLTypes {\n // TODO handle both types according to RT version\n XcmVersionedMultiLocation = 'XcmVersionedMultiLocation',\n XcmVersionedLocation = 'XcmVersionedLocation',\n XcmRoutingUserAction = 'XcmRoutingUserAction',\n VersionedUserAction = 'VersionedUserAction',\n}\n\nconst cache = new LRUCache<string, Promise<ApiPromise>>({\n max: 20,\n dispose: async (promise: Promise<ApiPromise>) => {\n const api = await promise;\n\n if (api.isConnected) {\n api.disconnect();\n }\n },\n});\n\nexport async function getPolkadotApi(\n ws: string | string[],\n): Promise<ApiPromise> {\n const key = Array.isArray(ws) ? ws.join(';') : ws;\n const promise =\n cache.get(key) ||\n ApiPromise.create({\n noInitWarn: true,\n provider: new WsProvider(ws),\n types: {\n [MRLTypes.XcmRoutingUserAction]: {\n destination: MRLTypes.XcmVersionedLocation,\n },\n [MRLTypes.VersionedUserAction]: {\n _enum: { V1: MRLTypes.XcmRoutingUserAction },\n },\n },\n typesBundle,\n });\n\n cache.set(key, promise);\n\n const api = await promise;\n\n await api.isReady;\n\n return api;\n}\n","export function getPolkadotAppsUrl(ws: string) {\n return `https://polkadot.js.org/apps/?rpc=${encodeURIComponent(ws)}#/explorer/query`;\n}\n","import {\n bnToU8a,\n compactToU8a,\n hexToU8a,\n stringToU8a,\n u8aToHex,\n} from '@polkadot/util';\nimport { blake2AsU8a, decodeAddress } from '@polkadot/util-crypto';\nimport { isEthAddress } from '../format';\n\n/**\n * reference: https://github.com/Moonsong-Labs/xcm-tools/blob/main/scripts/calculate-sovereign-account.ts\n */\n\nexport function getSovereignAccountAddresses(paraId: number) {\n const paraIdU8a = bnToU8a(paraId, { bitLength: 32 });\n const relay = u8aToHex(\n new Uint8Array([...stringToU8a('para'), ...paraIdU8a]),\n ).padEnd(66, '0');\n const generic = u8aToHex(\n new Uint8Array([...stringToU8a('sibl'), ...paraIdU8a]),\n ).padEnd(66, '0');\n const moonbeam = generic.slice(0, 42);\n\n return {\n generic,\n moonbeam,\n relay,\n };\n}\n\n/**\n * reference: https://github.com/Moonsong-Labs/xcm-tools/blob/main/scripts/calculate-multilocation-derivative-account.ts\n */\n\nexport function getMultilocationDerivedAddresses({\n paraId,\n address,\n isParents = false,\n}: {\n paraId?: number;\n address: string;\n isParents?: boolean;\n}) {\n const parents = isParents ? 1 : 0;\n const accType = isEthAddress(address) ? 'AccountKey20' : 'AccountId32';\n const decodedAddress = isEthAddress(address)\n ? hexToU8a(address)\n : decodeAddress(address);\n\n // Describe Family\n // https://github.com/paritytech/polkadot/blob/master/xcm/xcm-builder/src/location_conversion.rs#L96-L118\n const family =\n parents === 0 && paraId\n ? 'ChildChain'\n : parents === 1 && !paraId\n ? 'ParentChain'\n : 'SiblingChain';\n\n const blake = blake2AsU8a(\n new Uint8Array([\n ...stringToU8a(family),\n ...(paraId ? compactToU8a(paraId) : []),\n ...compactToU8a(accType.length + (isEthAddress(address) ? 20 : 32)),\n ...stringToU8a(accType),\n ...decodedAddress,\n ]),\n );\n\n const address20 = u8aToH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AG,EAAE,CAAC;AAE7C,SAAO;AAAA,IACL;AAAA,IACA;AAAA,EACF;AACF;","names":["MRLTypes"]}
1
+ {"version":3,"sources":["../src/format/address.ts","../src/format/asset.ts","../src/numbers/decimals.ts","../src/polkadot/polkadot.address.ts","../src/polkadot/polkadot.api.ts","../src/polkadot/polkadot.apps.ts"],"sourcesContent":["export function isHexString(asset: unknown): boolean {\n return typeof asset === 'string' && asset.startsWith('0x');\n}\n\nexport function isEthAddress(address: string): boolean {\n return address.length === 42 && address.startsWith('0x');\n}\n","export function formatAssetIdToERC20(id: string) {\n if (id.startsWith('0x')) {\n return id;\n }\n\n if (!/^\\d{38,39}$/.test(id)) {\n throw new Error(`Asset id: ${id} must be a string and have 38-39 digits`);\n }\n\n return `0xffffffff${BigInt(id).toString(16).padStart(32, '0')}`;\n}\n\nexport function convertAddressTo32Bytes(address: string): string {\n if (!/^0x[a-fA-F0-9]{40}$/.test(address)) {\n throw new Error(`Invalid address: ${address}`);\n }\n\n return `0x000000000000000000000000${address.substring(2)}`;\n}\n","import Big, { type RoundingMode } from 'big.js';\n\nBig.NE = -18;\n\nexport function toDecimal(\n number: bigint | number | string,\n decimals: number,\n maxDecimal = 6,\n roundType?: RoundingMode,\n): string {\n const dividend = Big(number.toString().replace(/[^0-9]/g, ''));\n const divisor = Big(10).pow(decimals);\n const result = dividend.div(divisor).round(maxDecimal, roundType);\n\n return result.toString();\n}\n\nexport function toBigInt(\n amount: bigint | string | number,\n decimals: number,\n): bigint {\n if (typeof amount === 'bigint') {\n return amount;\n }\n\n const multiplier = Big(10).pow(decimals);\n const result = Big(amount).mul(multiplier);\n\n return BigInt(result.toFixed(0, Big.roundDown));\n}\n\nexport function convertDecimals(\n number: string | bigint,\n decimals: number,\n targetDecimals: number,\n): bigint {\n const decimalNumber = toDecimal(number, decimals, decimals);\n\n return toBigInt(decimalNumber.toString(), targetDecimals);\n}\n\nexport function hasDecimalOverflow(fl: number | string, maxDecimal: number) {\n const parts = fl.toString().split('.');\n return parts.length > 1 && parts[1].length > maxDecimal;\n}\n","import {\n bnToU8a,\n compactToU8a,\n hexToU8a,\n stringToU8a,\n u8aToHex,\n} from '@polkadot/util';\nimport { blake2AsU8a, decodeAddress } from '@polkadot/util-crypto';\nimport { isEthAddress } from '../format';\n\n/**\n * reference: https://github.com/Moonsong-Labs/xcm-tools/blob/main/scripts/calculate-sovereign-account.ts\n */\n\nexport function getSovereignAccountAddresses(paraId: number) {\n const paraIdU8a = bnToU8a(paraId, { bitLength: 32 });\n const relay = u8aToHex(\n new Uint8Array([...stringToU8a('para'), ...paraIdU8a]),\n ).padEnd(66, '0');\n const generic = u8aToHex(\n new Uint8Array([...stringToU8a('sibl'), ...paraIdU8a]),\n ).padEnd(66, '0');\n const moonbeam = generic.slice(0, 42);\n\n return {\n generic,\n moonbeam,\n relay,\n };\n}\n\n/**\n * reference: https://github.com/Moonsong-Labs/xcm-tools/blob/main/scripts/calculate-multilocation-derivative-account.ts\n */\n\nexport function getMultilocationDerivedAddresses({\n paraId,\n address,\n isParents = false,\n}: {\n paraId?: number;\n address: string;\n isParents?: boolean;\n}) {\n const parents = isParents ? 1 : 0;\n const accType = isEthAddress(address) ? 'AccountKey20' : 'AccountId32';\n const decodedAddress = isEthAddress(address)\n ? hexToU8a(address)\n : decodeAddress(address);\n\n // Describe Family\n // https://github.com/paritytech/polkadot/blob/master/xcm/xcm-builder/src/location_conversion.rs#L96-L118\n const family =\n parents === 0 && paraId\n ? 'ChildChain'\n : parents === 1 && !paraId\n ? 'ParentChain'\n : 'SiblingChain';\n\n const blake = blake2AsU8a(\n new Uint8Array([\n ...stringToU8a(family),\n ...(paraId ? compactToU8a(paraId) : []),\n ...compactToU8a(accType.length + (isEthAddress(address) ? 20 : 32)),\n ...stringToU8a(accType),\n ...decodedAddress,\n ]),\n );\n\n const address20 = u8aToHex(blake.slice(0, 20));\n const address32 = u8aToHex(blake.slice(0, 32));\n\n return {\n address20,\n address32,\n };\n}\n","import { ApiPromise, HttpProvider, WsProvider } from '@polkadot/api';\nimport { typesBundle } from '@polkadot/apps-config';\nimport { LRUCache } from 'lru-cache';\n\nexport enum MRLTypes {\n // TODO handle both types according to RT version\n XcmVersionedMultiLocation = 'XcmVersionedMultiLocation',\n XcmVersionedLocation = 'XcmVersionedLocation',\n XcmRoutingUserAction = 'XcmRoutingUserAction',\n VersionedUserAction = 'VersionedUserAction',\n}\n\nconst cache = new LRUCache<string, Promise<ApiPromise>>({\n max: 20,\n dispose: async (promise: Promise<ApiPromise>) => {\n const api = await promise;\n\n if (api.isConnected) {\n api.disconnect();\n }\n },\n});\n\nexport async function getPolkadotApi(\n ws: string | string[],\n): Promise<ApiPromise> {\n const key = Array.isArray(ws) ? ws.join(';') : ws;\n const promise =\n cache.get(key) ||\n ApiPromise.create({\n noInitWarn: true,\n provider: new WsProvider(ws),\n types: {\n [MRLTypes.XcmRoutingUserAction]: {\n destination: MRLTypes.XcmVersionedLocation,\n },\n [MRLTypes.VersionedUserAction]: {\n _enum: { V1: MRLTypes.XcmRoutingUserAction },\n },\n },\n typesBundle,\n });\n\n cache.set(key, promise);\n\n const api = await promise;\n\n await api.isReady;\n\n return api;\n}\n\nexport async function getPolkadotHttpApi(rpc: string): Promise<ApiPromise> {\n const key = rpc;\n const promise =\n cache.get(key) ||\n ApiPromise.create({\n noInitWarn: true,\n provider: new HttpProvider(rpc),\n types: {\n [MRLTypes.XcmRoutingUserAction]: {\n destination: MRLTypes.XcmVersionedLocation,\n },\n [MRLTypes.VersionedUserAction]: {\n _enum: { V1: MRLTypes.XcmRoutingUserAction },\n },\n },\n typesBundle,\n });\n\n cache.set(key, promise);\n\n const api = await promise;\n\n await api.isReady;\n\n return api;\n}\n","export function getPolkadotAppsUrl(ws: string) {\n return `https://polkadot.js.org/apps/?rpc=${encodeURIComponent(ws)}#/explorer/query`;\n}\n"],"mappings":";AAAO,SAAS,YAAY,OAAyB;AACnD,SAAO,OAAO,UAAU,YAAY,MAAM,WAAW,IAAI;AAC3D;AAEO,S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AA,MACL,CAAC,iDAA6B,GAAG;AAAA,QAC/B,aAAa;AAAA,MACf;AAAA,MACA,CAAC,+CAA4B,GAAG;AAAA,QAC9B,OAAO,EAAE,IAAI,kDAA8B;AAAA,MAC7C;AAAA,IACF;AAAA,IACA;AAAA,EACF,CAAC;AAEH,QAAM,IAAI,KAAK,OAAO;AAEtB,QAAM,MAAM,MAAM;AAElB,QAAM,IAAI;AAEV,SAAO;AACT;;;AC7EO,SAAS,mBAAmB,IAAY;AAC7C,SAAO,qCAAqC,mBAAmB,EAAE,CAAC;AACpE;","names":["MRLTypes"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moonbeam-network/xcm-utils",
3
- "version": "1.0.0-dev.209",
3
+ "version": "1.0.0-dev.210",
4
4
  "description": "Moonbeam XCM utilities",
5
5
  "author": "moonbeam-foundation",
6
6
  "license": "MIT",
@@ -31,17 +31,17 @@
31
31
  "types": "./build/index.d.ts",
32
32
  "main": "./build/index.mjs",
33
33
  "dependencies": {
34
- "big.js": "^6.2.1",
35
- "lru-cache": "^10.2.2"
34
+ "big.js": "^7.0.1",
35
+ "lru-cache": "^11.1.0"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/big.js": "^6.2.2"
39
39
  },
40
40
  "peerDependencies": {
41
- "@polkadot/api": "16.1.1",
42
- "@polkadot/apps-config": "0.157.1",
43
- "@polkadot/util": "13.5.1",
44
- "@polkadot/util-crypto": "13.5.1"
41
+ "@polkadot/api": "16.2.2",
42
+ "@polkadot/apps-config": "0.158.1",
43
+ "@polkadot/util": "13.5.2",
44
+ "@polkadot/util-crypto": "13.5.2"
45
45
  },
46
46
  "scripts": {
47
47
  "build": "tsup",