Skip to content

Commit

Permalink
fix(sdk): Add existential deposits for foreign assets ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldev5 committed Dec 6, 2024
1 parent 73622dd commit b673296
Show file tree
Hide file tree
Showing 41 changed files with 2,763 additions and 1,260 deletions.
11 changes: 8 additions & 3 deletions apps/playground/src/components/assets/AssetsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,20 @@ const AssetsForm: FC<Props> = ({ onSubmit, loading }) => {
funcVal === "DECIMALS" ||
funcVal == "HAS_SUPPORT" ||
funcVal === "BALANCE_FOREIGN" ||
funcVal === "ASSET_BALANCE";
funcVal === "ASSET_BALANCE" ||
funcVal === "MAX_FOREIGN_TRANSFERABLE_AMOUNT";

const supportsCurrencyType =
funcVal === "BALANCE_FOREIGN" || funcVal === "ASSET_BALANCE";
funcVal === "BALANCE_FOREIGN" ||
funcVal === "ASSET_BALANCE" ||
funcVal === "MAX_FOREIGN_TRANSFERABLE_AMOUNT";

const showAddressInput =
funcVal === "BALANCE_FOREIGN" ||
funcVal === "BALANCE_NATIVE" ||
funcVal === "ASSET_BALANCE";
funcVal === "ASSET_BALANCE" ||
funcVal === "MAX_NATIVE_TRANSFERABLE_AMOUNT" ||
funcVal === "MAX_FOREIGN_TRANSFERABLE_AMOUNT";

const onSubmitInternal = (formValues: FormValues) => {
const { func } = formValues;
Expand Down
24 changes: 23 additions & 1 deletion apps/playground/src/components/assets/AssetsQueries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import AssetsForm from "./AssetsForm";
import type {
TCurrencyCore,
TMultiLocation,
TNodeDotKsmWithRelayChains,
TNodePolkadotKusama,
} from "@paraspell/sdk";
import {
Expand Down Expand Up @@ -94,6 +95,17 @@ const AssetsQueries = () => {
node: node as TNodePolkadotKusama,
currency: resolvedCurrency,
});
case "MAX_NATIVE_TRANSFERABLE_AMOUNT":
return Sdk.getMaxNativeTransferableAmount({
address,
node: node as TNodeDotKsmWithRelayChains,
});
case "MAX_FOREIGN_TRANSFERABLE_AMOUNT":
return Sdk.getMaxForeignTransferableAmount({
address,
node: node as TNodePolkadotKusama,
currency: resolvedCurrency,
});
}
};

Expand Down Expand Up @@ -129,6 +141,14 @@ const AssetsQueries = () => {
return apiType === "PAPI"
? `/balance/${node}/asset-papi`
: `/balance/${node}/asset`;
case "MAX_NATIVE_TRANSFERABLE_AMOUNT":
return apiType === "PAPI"
? `/balance/${node}/max-native-transferable-amount-papi`
: `/balance/${node}/max-native-transferable-amount`;
case "MAX_FOREIGN_TRANSFERABLE_AMOUNT":
return apiType === "PAPI"
? `/balance/${node}/max-foreign-transferable-amount-papi`
: `/balance/${node}/max-foreign-transferable-amount`;
}
};

Expand All @@ -149,7 +169,9 @@ const AssetsQueries = () => {
const isBalanceQuery =
func === "BALANCE_FOREIGN" ||
func === "BALANCE_NATIVE" ||
func === "ASSET_BALANCE";
func === "ASSET_BALANCE" ||
func === "MAX_NATIVE_TRANSFERABLE_AMOUNT" ||
func === "MAX_FOREIGN_TRANSFERABLE_AMOUNT";
const resolvedCurrency = resolveCurrency(formValues);
if (useApi) {
return fetchFromApi(
Expand Down
2 changes: 2 additions & 0 deletions apps/playground/src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export const ASSET_QUERIES = [
"BALANCE_NATIVE",
"BALANCE_FOREIGN",
"ASSET_BALANCE",
"MAX_NATIVE_TRANSFERABLE_AMOUNT",
"MAX_FOREIGN_TRANSFERABLE_AMOUNT",
] as const;

export const PALLETS_QUERIES = ["ALL_PALLETS", "DEFAULT_PALLET"] as const;
2 changes: 2 additions & 0 deletions apps/xcm-api/src/analytics/EventName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export enum EventName {
GET_BALANCE_NATIVE = 'Get Balance Native',
GET_BALANCE_NATIVE_PAPI = 'Get Balance Native Papi',
GET_BALANCE_FOREIGN = 'Get Balance Foreign',
GET_MAX_NATIVE_TRANSFERABLE_AMOUNT = 'Get Max Native Transferable Amount',
GET_MAX_FOREIGN_TRANSFERABLE_AMOUNT = 'Get Max Foreign Transferable Amount',
GENERATE_XCM_CALL = 'Generate XCM Call',
GENERATE_XCM_CALL_BATCH = 'Generate XCM Call Batch',
GENERATE_XCM_CALL_BATCH_PAPI = 'Generate XCM Call Batch Papi',
Expand Down
123 changes: 123 additions & 0 deletions apps/xcm-api/src/balance/balance.controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ describe('BalanceController', () => {
getBalanceNative: vi.fn(),
getBalanceForeign: vi.fn(),
getAssetBalance: vi.fn(),
getMaxForeignTransferableAmount: vi.fn(),
getMaxNativeTransferableAmount: vi.fn(),
},
},
{
Expand Down Expand Up @@ -183,4 +185,125 @@ describe('BalanceController', () => {
expect(result).toEqual(balanceForeignMock);
});
});

describe('getMaxForeignTransferableAmount', () => {
it('should track analytics and call BalanceService for max foreign transferable amount PAPI', async () => {
const node = 'Acala';
const params: BalanceForeignDto = {
address: '0x1234567890',
currency: { symbol: 'UNQ' },
};
const req = {} as Request;

const maxForeignAmountMock = BigInt('10000');
const balanceServiceSpy = vi
.spyOn(balanceService, 'getMaxForeignTransferableAmount')
.mockResolvedValue(maxForeignAmountMock);
const analyticsServiceSpy = vi.spyOn(analyticsService, 'track');

const result =
await balanceController.getMaxForeignTransferableAmountPapi(
node,
params,
req,
);

expect(analyticsServiceSpy).toHaveBeenCalledWith(
EventName.GET_MAX_FOREIGN_TRANSFERABLE_AMOUNT,
req,
{ node },
);
expect(balanceServiceSpy).toHaveBeenCalledWith(node, params, true);
expect(result).toEqual(maxForeignAmountMock);
});

it('should track analytics and call BalanceService for max foreign transferable amount PJS', async () => {
const node = 'Acala';
const params: BalanceForeignDto = {
address: '0x1234567890',
currency: { symbol: 'UNQ' },
};
const req = {} as Request;

const maxForeignAmountMock = BigInt('10000');
const balanceServiceSpy = vi
.spyOn(balanceService, 'getMaxForeignTransferableAmount')
.mockResolvedValue(maxForeignAmountMock);
const analyticsServiceSpy = vi.spyOn(analyticsService, 'track');

const result = await balanceController.getMaxForeignTransferableAmount(
node,
params,
req,
);

expect(analyticsServiceSpy).toHaveBeenCalledWith(
EventName.GET_MAX_FOREIGN_TRANSFERABLE_AMOUNT,
req,
{ node },
);
expect(balanceServiceSpy).toHaveBeenCalledWith(node, params);
expect(result).toEqual(maxForeignAmountMock);
});
});

describe('getMaxNativeTransferableAmount', () => {
it('should track analytics and call BalanceService for max native transferable amount PAPI', async () => {
const node = 'Acala';
const params: BalanceForeignDto = {
address: '0x1234567890',
currency: { symbol: 'UNQ' },
};
const req = {} as Request;

const maxNativeAmountMock = BigInt('20000');
const balanceServiceSpy = vi
.spyOn(balanceService, 'getMaxNativeTransferableAmount')
.mockResolvedValue(maxNativeAmountMock);
const analyticsServiceSpy = vi.spyOn(analyticsService, 'track');

const result = await balanceController.getMaxNativeTransferableAmountPapi(
node,
params,
req,
);

expect(analyticsServiceSpy).toHaveBeenCalledWith(
EventName.GET_MAX_FOREIGN_TRANSFERABLE_AMOUNT, // event name matches the code snippet
req,
{ node },
);
expect(balanceServiceSpy).toHaveBeenCalledWith(node, params, true);
expect(result).toEqual(maxNativeAmountMock);
});

it('should track analytics and call BalanceService for max native transferable amount PJS', async () => {
const node = 'Acala';
const params: BalanceForeignDto = {
address: '0x1234567890',
currency: { symbol: 'UNQ' },
};
const req = {} as Request;

const maxNativeAmountMock = BigInt('20000');
const balanceServiceSpy = vi
.spyOn(balanceService, 'getMaxNativeTransferableAmount')
.mockResolvedValue(maxNativeAmountMock);
const analyticsServiceSpy = vi.spyOn(analyticsService, 'track');

const result = await balanceController.getMaxNativeTransferableAmount(
node,
params,
req,
);

expect(analyticsServiceSpy).toHaveBeenCalledWith(
EventName.GET_MAX_FOREIGN_TRANSFERABLE_AMOUNT, // event name matches the code snippet
req,
{ node },
);
expect(balanceServiceSpy).toHaveBeenCalledWith(node, params);
expect(result).toEqual(maxNativeAmountMock);
});
});
});
76 changes: 76 additions & 0 deletions apps/xcm-api/src/balance/balance.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,80 @@ export class BalanceController {
});
return this.balanceService.getAssetBalance(node, params);
}

@Post(':node/max-foreign-transferable-amount-papi')
getMaxForeignTransferableAmountPapi(
@Param('node') node: string,
@Body(new ZodValidationPipe(BalanceForeignDtoSchema))
params: BalanceForeignDto,
@Req() req: Request,
) {
this.analyticsService.track(
EventName.GET_MAX_FOREIGN_TRANSFERABLE_AMOUNT,
req,
{
node,
},
);
return this.balanceService.getMaxForeignTransferableAmount(
node,
params,
true,
);
}

@Post(':node/max-foreign-transferable-amount')
getMaxForeignTransferableAmount(
@Param('node') node: string,
@Body(new ZodValidationPipe(BalanceForeignDtoSchema))
params: BalanceForeignDto,
@Req() req: Request,
) {
this.analyticsService.track(
EventName.GET_MAX_FOREIGN_TRANSFERABLE_AMOUNT,
req,
{
node,
},
);
return this.balanceService.getMaxForeignTransferableAmount(node, params);
}

@Post(':node/max-foreign-transferable-amount-papi')
getMaxNativeTransferableAmountPapi(
@Param('node') node: string,
@Body(new ZodValidationPipe(BalanceForeignDtoSchema))
params: BalanceForeignDto,
@Req() req: Request,
) {
this.analyticsService.track(
EventName.GET_MAX_FOREIGN_TRANSFERABLE_AMOUNT,
req,
{
node,
},
);
return this.balanceService.getMaxNativeTransferableAmount(
node,
params,
true,
);
}

@Post(':node/max-foreign-transferable-amount')
getMaxNativeTransferableAmount(
@Param('node') node: string,
@Body(new ZodValidationPipe(BalanceForeignDtoSchema))
params: BalanceForeignDto,
@Req() req: Request,
) {
this.analyticsService.track(
EventName.GET_MAX_FOREIGN_TRANSFERABLE_AMOUNT,
req,
{
node,
},
);
return this.balanceService.getMaxNativeTransferableAmount(node, params);
}
}
Loading

0 comments on commit b673296

Please sign in to comment.