diff --git a/package-lock.json b/package-lock.json index 78b416ab..0882d4b5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9131,7 +9131,7 @@ }, "packages/utils": { "name": "@moonbeam-network/xcm-utils", - "version": "0.0.2", + "version": "0.0.3", "license": "MIT", "dependencies": { "big.js": "^6.2.1", diff --git a/packages/sdk/src/polkadot/PolkadotService.ts b/packages/sdk/src/polkadot/PolkadotService.ts index 0c7ce88d..9a2eb0d5 100644 --- a/packages/sdk/src/polkadot/PolkadotService.ts +++ b/packages/sdk/src/polkadot/PolkadotService.ts @@ -10,6 +10,7 @@ import { MoonChain, XcmConfigBuilder, } from '@moonbeam-network/xcm-config'; +import { getPolkadotApi } from '@moonbeam-network/xcm-utils'; import { ApiPromise } from '@polkadot/api'; import { QueryableStorageMultiArg, @@ -23,7 +24,6 @@ import { PalletAssetsAssetMetadata, } from '@polkadot/types/lookup'; import _ from 'lodash'; -import { getPolkadotApi } from './polkadot.api'; import { AssetBalanceInfo, XCMType } from './polkadot.interfaces'; import { calculateMin } from './polkadot.utils'; diff --git a/packages/sdk/src/polkadot/index.ts b/packages/sdk/src/polkadot/index.ts index b0da2f88..7f0ebfd5 100644 --- a/packages/sdk/src/polkadot/index.ts +++ b/packages/sdk/src/polkadot/index.ts @@ -1,4 +1,3 @@ -export * from './polkadot.api'; export * from './polkadot.factory'; export * from './polkadot.interfaces'; export * from './PolkadotService'; diff --git a/packages/sdk/src/polkadot/polkadot.api.ts b/packages/sdk/src/polkadot/polkadot.api.ts deleted file mode 100644 index 73563cbf..00000000 --- a/packages/sdk/src/polkadot/polkadot.api.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { ApiPromise, WsProvider } from '@polkadot/api'; -import LRU from 'lru-cache'; - -const tenMin = 10 * 60 * 1000; - -const cache = new LRU>({ - max: 20, - ttl: tenMin, - updateAgeOnGet: true, - ttlAutopurge: true, - dispose: async (promise: Promise) => { - const api = await promise; - - if (api.isConnected) { - api.disconnect(); - } - }, -}); - -export async function getPolkadotApi(ws: string): Promise { - const promise = - cache.get(ws) || - ApiPromise.create({ - provider: new WsProvider(ws), - }); - - cache.set(ws, promise); - - const api = await promise; - - await api.isReady; - - return api; -} diff --git a/packages/sdk/src/sdk/index.ts b/packages/sdk/src/sdk/index.ts index b8def6f2..19820623 100644 --- a/packages/sdk/src/sdk/index.ts +++ b/packages/sdk/src/sdk/index.ts @@ -1,3 +1,8 @@ export * from './sdk'; export * from './sdk.interfaces'; -export { isXcmSdkDeposit, isXcmSdkWithdraw } from './sdk.utils'; +export { + isDepositTransferData, + isWithdrawTransferData, + isXcmSdkDeposit, + isXcmSdkWithdraw, +} from './sdk.utils'; diff --git a/packages/sdk/src/sdk/sdk.deposit.ts b/packages/sdk/src/sdk/sdk.deposit.ts index 6399530a..edcd16d6 100644 --- a/packages/sdk/src/sdk/sdk.deposit.ts +++ b/packages/sdk/src/sdk/sdk.deposit.ts @@ -167,7 +167,7 @@ export async function getDepositData< : undefined, sourceMinBalance, getFee: async (amount = sourceBalance): Promise => { - const extrinsic = await createExtrinsic(amount); + const extrinsic = createExtrinsic(amount); const info = await extrinsic.paymentInfo(sourceAccount); return info.partialFee.toBigInt(); @@ -176,7 +176,7 @@ export async function getDepositData< amount: bigint, cb?: ExtrinsicEventsCallback, ): Promise => { - const extrinsic = await createExtrinsic(amount); + const extrinsic = createExtrinsic(amount); const hash = await extrinsic.signAndSend( sourceAccount, { diff --git a/packages/sdk/src/sdk/sdk.utils.ts b/packages/sdk/src/sdk/sdk.utils.ts index c9710f09..deea7877 100644 --- a/packages/sdk/src/sdk/sdk.utils.ts +++ b/packages/sdk/src/sdk/sdk.utils.ts @@ -32,7 +32,7 @@ export function isWithdrawTransferData( return !!(data as WithdrawTransferData)?.destination; } -export function useIsDepositTransferData( +export function isDepositTransferData( data?: WithdrawTransferData | DepositTransferData, ): data is DepositTransferData { return !!(data as DepositTransferData)?.moonChainFee; diff --git a/packages/utils/package.json b/packages/utils/package.json index 792da31a..16dc215f 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -1,6 +1,6 @@ { "name": "@moonbeam-network/xcm-utils", - "version": "0.0.2", + "version": "0.0.3", "description": "Moonbeam XCM utilities", "scripts": { "build": "tsup", diff --git a/packages/utils/src/api/polkadot.api.ts b/packages/utils/src/api/polkadot.api.ts index 73563cbf..31f70963 100644 --- a/packages/utils/src/api/polkadot.api.ts +++ b/packages/utils/src/api/polkadot.api.ts @@ -1,13 +1,8 @@ import { ApiPromise, WsProvider } from '@polkadot/api'; import LRU from 'lru-cache'; -const tenMin = 10 * 60 * 1000; - const cache = new LRU>({ max: 20, - ttl: tenMin, - updateAgeOnGet: true, - ttlAutopurge: true, dispose: async (promise: Promise) => { const api = await promise; @@ -17,7 +12,10 @@ const cache = new LRU>({ }, }); -export async function getPolkadotApi(ws: string): Promise { +export async function getPolkadotApi( + ws: string, + onDisconnect?: VoidFunction, +): Promise { const promise = cache.get(ws) || ApiPromise.create({ @@ -30,5 +28,9 @@ export async function getPolkadotApi(ws: string): Promise { await api.isReady; + if (onDisconnect) { + api.once('disconnected', onDisconnect); + } + return api; } diff --git a/packages/utils/src/numbers/decimals.test.ts b/packages/utils/src/numbers/decimals.test.ts index aadeb656..5da6bfc4 100644 --- a/packages/utils/src/numbers/decimals.test.ts +++ b/packages/utils/src/numbers/decimals.test.ts @@ -64,5 +64,14 @@ describe('utils - decimals', () => { expect(toBigInt('0.372', 12)).toBe(372_000_000_000n); expect(toBigInt('0.372209875392', 12)).toBe(372_209_875_392n); }); + + it('should convert to BigInt from number', () => { + expect(toBigInt(52.912391, 12)).toBe(52_912_391_000_000n); + expect(toBigInt(80, 12)).toBe(80_000_000_000_000n); + expect(toBigInt(370, 12)).toBe(370_000_000_000_000n); + expect(toBigInt(0.78, 12)).toBe(780_000_000_000n); + expect(toBigInt(0.372, 12)).toBe(372_000_000_000n); + expect(toBigInt(0.372209875392, 12)).toBe(372_209_875_392n); + }); }); }); diff --git a/packages/utils/src/numbers/decimals.ts b/packages/utils/src/numbers/decimals.ts index 4fa74d05..0d65bb07 100644 --- a/packages/utils/src/numbers/decimals.ts +++ b/packages/utils/src/numbers/decimals.ts @@ -12,7 +12,7 @@ export function toDecimal( return result.toNumber(); } -export function toBigInt(amount: string, decimals: number): bigint { +export function toBigInt(amount: string | number, decimals: number): bigint { const multiplier = Big(10).pow(decimals); const result = Big(amount).mul(multiplier);