Skip to content

Commit 438a8c6

Browse files
author
Elmar Kenigs
authored
Fixes for getPolkadotApi and other (#51)
1 parent d54a1b8 commit 438a8c6

File tree

11 files changed

+30
-49
lines changed

11 files changed

+30
-49
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/sdk/src/polkadot/PolkadotService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
MoonChain,
1111
XcmConfigBuilder,
1212
} from '@moonbeam-network/xcm-config';
13+
import { getPolkadotApi } from '@moonbeam-network/xcm-utils';
1314
import { ApiPromise } from '@polkadot/api';
1415
import {
1516
QueryableStorageMultiArg,
@@ -23,7 +24,6 @@ import {
2324
PalletAssetsAssetMetadata,
2425
} from '@polkadot/types/lookup';
2526
import _ from 'lodash';
26-
import { getPolkadotApi } from './polkadot.api';
2727
import { AssetBalanceInfo, XCMType } from './polkadot.interfaces';
2828
import { calculateMin } from './polkadot.utils';
2929

packages/sdk/src/polkadot/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from './polkadot.api';
21
export * from './polkadot.factory';
32
export * from './polkadot.interfaces';
43
export * from './PolkadotService';

packages/sdk/src/polkadot/polkadot.api.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

packages/sdk/src/sdk/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
export * from './sdk';
22
export * from './sdk.interfaces';
3-
export { isXcmSdkDeposit, isXcmSdkWithdraw } from './sdk.utils';
3+
export {
4+
isDepositTransferData,
5+
isWithdrawTransferData,
6+
isXcmSdkDeposit,
7+
isXcmSdkWithdraw,
8+
} from './sdk.utils';

packages/sdk/src/sdk/sdk.deposit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export async function getDepositData<
167167
: undefined,
168168
sourceMinBalance,
169169
getFee: async (amount = sourceBalance): Promise<bigint> => {
170-
const extrinsic = await createExtrinsic(amount);
170+
const extrinsic = createExtrinsic(amount);
171171
const info = await extrinsic.paymentInfo(sourceAccount);
172172

173173
return info.partialFee.toBigInt();
@@ -176,7 +176,7 @@ export async function getDepositData<
176176
amount: bigint,
177177
cb?: ExtrinsicEventsCallback,
178178
): Promise<string> => {
179-
const extrinsic = await createExtrinsic(amount);
179+
const extrinsic = createExtrinsic(amount);
180180
const hash = await extrinsic.signAndSend(
181181
sourceAccount,
182182
{

packages/sdk/src/sdk/sdk.utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function isWithdrawTransferData(
3232
return !!(data as WithdrawTransferData)?.destination;
3333
}
3434

35-
export function useIsDepositTransferData(
35+
export function isDepositTransferData(
3636
data?: WithdrawTransferData | DepositTransferData,
3737
): data is DepositTransferData {
3838
return !!(data as DepositTransferData)?.moonChainFee;

packages/utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@moonbeam-network/xcm-utils",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "Moonbeam XCM utilities",
55
"scripts": {
66
"build": "tsup",

packages/utils/src/api/polkadot.api.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import { ApiPromise, WsProvider } from '@polkadot/api';
22
import LRU from 'lru-cache';
33

4-
const tenMin = 10 * 60 * 1000;
5-
64
const cache = new LRU<string, Promise<ApiPromise>>({
75
max: 20,
8-
ttl: tenMin,
9-
updateAgeOnGet: true,
10-
ttlAutopurge: true,
116
dispose: async (promise: Promise<ApiPromise>) => {
127
const api = await promise;
138

@@ -17,7 +12,10 @@ const cache = new LRU<string, Promise<ApiPromise>>({
1712
},
1813
});
1914

20-
export async function getPolkadotApi(ws: string): Promise<ApiPromise> {
15+
export async function getPolkadotApi(
16+
ws: string,
17+
onDisconnect?: VoidFunction,
18+
): Promise<ApiPromise> {
2119
const promise =
2220
cache.get(ws) ||
2321
ApiPromise.create({
@@ -30,5 +28,9 @@ export async function getPolkadotApi(ws: string): Promise<ApiPromise> {
3028

3129
await api.isReady;
3230

31+
if (onDisconnect) {
32+
api.once('disconnected', onDisconnect);
33+
}
34+
3335
return api;
3436
}

packages/utils/src/numbers/decimals.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,14 @@ describe('utils - decimals', () => {
6464
expect(toBigInt('0.372', 12)).toBe(372_000_000_000n);
6565
expect(toBigInt('0.372209875392', 12)).toBe(372_209_875_392n);
6666
});
67+
68+
it('should convert to BigInt from number', () => {
69+
expect(toBigInt(52.912391, 12)).toBe(52_912_391_000_000n);
70+
expect(toBigInt(80, 12)).toBe(80_000_000_000_000n);
71+
expect(toBigInt(370, 12)).toBe(370_000_000_000_000n);
72+
expect(toBigInt(0.78, 12)).toBe(780_000_000_000n);
73+
expect(toBigInt(0.372, 12)).toBe(372_000_000_000n);
74+
expect(toBigInt(0.372209875392, 12)).toBe(372_209_875_392n);
75+
});
6776
});
6877
});

packages/utils/src/numbers/decimals.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function toDecimal(
1212
return result.toNumber();
1313
}
1414

15-
export function toBigInt(amount: string, decimals: number): bigint {
15+
export function toBigInt(amount: string | number, decimals: number): bigint {
1616
const multiplier = Big(10).pow(decimals);
1717
const result = Big(amount).mul(multiplier);
1818

0 commit comments

Comments
 (0)