Skip to content

Commit

Permalink
Fixes for getPolkadotApi and other (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
Elmar Kenigs authored Dec 16, 2022
1 parent d54a1b8 commit 438a8c6
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 49 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/sdk/src/polkadot/PolkadotService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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';

Expand Down
1 change: 0 additions & 1 deletion packages/sdk/src/polkadot/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './polkadot.api';
export * from './polkadot.factory';
export * from './polkadot.interfaces';
export * from './PolkadotService';
34 changes: 0 additions & 34 deletions packages/sdk/src/polkadot/polkadot.api.ts

This file was deleted.

7 changes: 6 additions & 1 deletion packages/sdk/src/sdk/index.ts
Original file line number Diff line number Diff line change
@@ -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';
4 changes: 2 additions & 2 deletions packages/sdk/src/sdk/sdk.deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export async function getDepositData<
: undefined,
sourceMinBalance,
getFee: async (amount = sourceBalance): Promise<bigint> => {
const extrinsic = await createExtrinsic(amount);
const extrinsic = createExtrinsic(amount);
const info = await extrinsic.paymentInfo(sourceAccount);

return info.partialFee.toBigInt();
Expand All @@ -176,7 +176,7 @@ export async function getDepositData<
amount: bigint,
cb?: ExtrinsicEventsCallback,
): Promise<string> => {
const extrinsic = await createExtrinsic(amount);
const extrinsic = createExtrinsic(amount);
const hash = await extrinsic.signAndSend(
sourceAccount,
{
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/sdk/sdk.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@moonbeam-network/xcm-utils",
"version": "0.0.2",
"version": "0.0.3",
"description": "Moonbeam XCM utilities",
"scripts": {
"build": "tsup",
Expand Down
14 changes: 8 additions & 6 deletions packages/utils/src/api/polkadot.api.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { ApiPromise, WsProvider } from '@polkadot/api';
import LRU from 'lru-cache';

const tenMin = 10 * 60 * 1000;

const cache = new LRU<string, Promise<ApiPromise>>({
max: 20,
ttl: tenMin,
updateAgeOnGet: true,
ttlAutopurge: true,
dispose: async (promise: Promise<ApiPromise>) => {
const api = await promise;

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

export async function getPolkadotApi(ws: string): Promise<ApiPromise> {
export async function getPolkadotApi(
ws: string,
onDisconnect?: VoidFunction,
): Promise<ApiPromise> {
const promise =
cache.get(ws) ||
ApiPromise.create({
Expand All @@ -30,5 +28,9 @@ export async function getPolkadotApi(ws: string): Promise<ApiPromise> {

await api.isReady;

if (onDisconnect) {
api.once('disconnected', onDisconnect);
}

return api;
}
9 changes: 9 additions & 0 deletions packages/utils/src/numbers/decimals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
2 changes: 1 addition & 1 deletion packages/utils/src/numbers/decimals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 438a8c6

Please sign in to comment.