@moonbeam-network/xcm-utils 1.0.0-dev.210 → 1.0.0-dev.211
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 +1 -2
- package/build/index.mjs +1 -22
- package/build/index.mjs.map +1 -1
- package/package.json +4 -4
package/build/index.d.ts
CHANGED
|
@@ -39,8 +39,7 @@ declare enum MRLTypes {
|
|
|
39
39
|
VersionedUserAction = "VersionedUserAction"
|
|
40
40
|
}
|
|
41
41
|
declare function getPolkadotApi(ws: string | string[]): Promise<ApiPromise>;
|
|
42
|
-
declare function getPolkadotHttpApi(rpc: string): Promise<ApiPromise>;
|
|
43
42
|
|
|
44
43
|
declare function getPolkadotAppsUrl(ws: string): string;
|
|
45
44
|
|
|
46
|
-
export { MRLTypes, convertAddressTo32Bytes, convertDecimals, formatAssetIdToERC20, getMultilocationDerivedAddresses, getPolkadotApi, getPolkadotAppsUrl,
|
|
45
|
+
export { MRLTypes, convertAddressTo32Bytes, convertDecimals, formatAssetIdToERC20, getMultilocationDerivedAddresses, getPolkadotApi, getPolkadotAppsUrl, getSovereignAccountAddresses, hasDecimalOverflow, isEthAddress, isHexString, toBigInt, toDecimal };
|
package/build/index.mjs
CHANGED
|
@@ -100,7 +100,7 @@ function getMultilocationDerivedAddresses({
|
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
// src/polkadot/polkadot.api.ts
|
|
103
|
-
import { ApiPromise,
|
|
103
|
+
import { ApiPromise, WsProvider } from "@polkadot/api";
|
|
104
104
|
import { typesBundle } from "@polkadot/apps-config";
|
|
105
105
|
import { LRUCache } from "lru-cache";
|
|
106
106
|
var MRLTypes = /* @__PURE__ */ ((MRLTypes2) => {
|
|
@@ -139,26 +139,6 @@ async function getPolkadotApi(ws) {
|
|
|
139
139
|
await api.isReady;
|
|
140
140
|
return api;
|
|
141
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
142
|
|
|
163
143
|
// src/polkadot/polkadot.apps.ts
|
|
164
144
|
function getPolkadotAppsUrl(ws) {
|
|
@@ -172,7 +152,6 @@ export {
|
|
|
172
152
|
getMultilocationDerivedAddresses,
|
|
173
153
|
getPolkadotApi,
|
|
174
154
|
getPolkadotAppsUrl,
|
|
175
|
-
getPolkadotHttpApi,
|
|
176
155
|
getSovereignAccountAddresses,
|
|
177
156
|
hasDecimalOverflow,
|
|
178
157
|
isEthAddress,
|
package/build/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,
|
|
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, 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"],"mappings":";AAAO,SAAS,YAAY,OAAyB;AACnD,SAAO,OAAO,UAAU,YAAY,MAAM,WAAW,IAAI;AAC3D;AAEO,SAAS,aAAa,SAA0B;AACrD,SAAO,QAAQ,WAAW,MAAM,QAAQ,WAAW,IAAI;AACzD;;;ACNO,SAAS,qBAAqB,IAAY;AAC/C,MAAI,GAAG,WAAW,IAAI,GAAG;AACvB,WAAO;AAAA,EACT;AAEA,MAAI,CAAC,cAAc,KAAK,EAAE,GAAG;AAC3B,UAAM,IAAI,MAAM,aAAa,EAAE,yCAAyC;AAAA,EAC1E;AAEA,SAAO,aAAa,OAAO,EAAE,EAAE,SAAS,EAAE,EAAE,SAAS,IAAI,GAAG,CAAC;AAC/D;AAEO,SAAS,wBAAwB,SAAyB;AAC/D,MAAI,CAAC,sBAAsB,KAAK,OAAO,GAAG;AACxC,UAAM,IAAI,MAAM,oBAAoB,OAAO,EAAE;AAAA,EAC/C;AAEA,SAAO,6BAA6B,QAAQ,UAAU,CAAC,CAAC;AAC1D;;;AClBA,OAAO,SAAgC;AAEvC,IAAI,KAAK;AAEF,SAAS,UACd,QACA,UACA,aAAa,GACb,WACQ;AACR,QAAM,WAAW,IAAI,OAAO,SAAS,EAAE,QAAQ,WAAW,EAAE,CAAC;AAC7D,QAAM,UAAU,IAAI,EAAE,EAAE,IAAI,QAAQ;AACpC,QAAM,SAAS,SAAS,IAAI,OAAO,EAAE,MAAM,YAAY,SAAS;AAEhE,SAAO,OAAO,SAAS;AACzB;AAEO,SAAS,SACd,QACA,UACQ;AACR,MAAI,OAAO,WAAW,UAAU;AAC9B,WAAO;AA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,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;;;AClDO,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.
|
|
3
|
+
"version": "1.0.0-dev.211",
|
|
4
4
|
"description": "Moonbeam XCM utilities",
|
|
5
5
|
"author": "moonbeam-foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -38,10 +38,10 @@
|
|
|
38
38
|
"@types/big.js": "^6.2.2"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
|
-
"@polkadot/api": "16.
|
|
41
|
+
"@polkadot/api": "16.3.1",
|
|
42
42
|
"@polkadot/apps-config": "0.158.1",
|
|
43
|
-
"@polkadot/util": "13.5.
|
|
44
|
-
"@polkadot/util-crypto": "13.5.
|
|
43
|
+
"@polkadot/util": "13.5.3",
|
|
44
|
+
"@polkadot/util-crypto": "13.5.3"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
47
|
"build": "tsup",
|