Skip to content

Commit

Permalink
feat(sdk)!: Refactor transfer Builder to explicitly include from, to …
Browse files Browse the repository at this point in the history
…parameters for relaychains

Move amount closer to currency

Add symbol selection for multiassets
  • Loading branch information
michaeldev5 committed Dec 17, 2024
1 parent aebaffd commit 395b45e
Show file tree
Hide file tree
Showing 155 changed files with 2,425 additions and 2,908 deletions.
6 changes: 4 additions & 2 deletions apps/playground/src/components/TransferInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ const TransferInfo = () => {
destination: formValues.to,
accountOrigin: originAddress,
accountDestination: formValues.destinationAddress,
currency,
amount: formValues.amount,
currency: {
...currency,
amount: formValues.amount,
},
});
}
};
Expand Down
42 changes: 18 additions & 24 deletions apps/playground/src/components/XcmTransfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import ErrorAlert from "./ErrorAlert";
import type { FormValuesTransformed } from "./TransferForm";
import TransferForm from "./TransferForm";
import { useDisclosure, useScrollIntoView } from "@mantine/hooks";
import type { TCurrencyInputWithAmount } from "@paraspell/sdk";
import {
isForeignAsset,
Override,
type Extrinsic,
type TCurrencyInput,
type TMultiLocation,
type TNode,
type TNodePolkadotKusama,
} from "@paraspell/sdk";
import type { TPapiTransaction } from "@paraspell/sdk/papi";
Expand Down Expand Up @@ -49,32 +48,37 @@ const XcmTransfer = () => {
customCurrency,
customCurrencyType,
currency,
}: FormValuesTransformed): TCurrencyInput => {
amount,
}: FormValuesTransformed): TCurrencyInputWithAmount => {
if (isCustomCurrency) {
if (customCurrencyType === "id") {
return {
id: customCurrency,
amount,
};
} else if (customCurrencyType === "symbol") {
return {
symbol: customCurrency,
amount,
};
} else if (customCurrencyType === "overridenMultilocation") {
return {
multilocation: Override(JSON.parse(customCurrency) as TMultiLocation),
amount,
};
} else {
return {
multilocation: JSON.parse(customCurrency) as TMultiLocation,
amount,
};
}
} else if (currency) {
if (isForeignAsset(currency) && ethers.isAddress(currency.assetId)) {
return { symbol: currency.symbol ?? "" };
return { symbol: currency.symbol ?? "", amount };
}

if (!isForeignAsset(currency)) {
return { symbol: currency.symbol ?? "" };
return { symbol: currency.symbol ?? "", amount };
}

const hasDuplicateIds = isRelayChain(from)
Expand All @@ -83,17 +87,18 @@ const XcmTransfer = () => {
(asset) => asset.assetId === currency.assetId,
).length > 1;
return hasDuplicateIds
? { symbol: currency.symbol ?? "" }
? { symbol: currency.symbol ?? "", amount }
: {
id: currency.assetId ?? "",
amount,
};
} else {
throw Error("Currency is required");
}
};

const submit = async (formValues: FormValuesTransformed) => {
const { from, to, amount, address, ahAddress, useApi } = formValues;
const { from, to, address, ahAddress, useApi } = formValues;

if (!selectedAccount) {
alert("No account selected, connect wallet first");
Expand Down Expand Up @@ -136,23 +141,12 @@ const XcmTransfer = () => {
);
} else {
const builder = Sdk.Builder(api as ApiPromise & PolkadotClient);
if (from === "Polkadot" || from === "Kusama") {
tx = await builder
.to(to as TNode)
.amount(amount)
.address(address)
.build();
} else if (to === "Polkadot" || to === "Kusama") {
tx = await builder.from(from).amount(amount).address(address).build();
} else {
tx = await builder
.from(from)
.to(to)
.currency(determineCurrency(formValues))
.amount(amount)
.address(address, ahAddress)
.build();
}
tx = await builder
.from(from)
.to(to)
.currency(determineCurrency(formValues))
.address(address, ahAddress)
.build();
}

if (apiType === "PAPI") {
Expand Down
24 changes: 6 additions & 18 deletions apps/visualizator-fe/src/components/SendXcm/SendXcm.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState } from 'react';
import { Stack, Title, Box, Group, Button, Modal } from '@mantine/core';
import { useDisclosure } from '@mantine/hooks';
import type { TNode } from '@paraspell/sdk';
import { Builder, createApiInstanceForNode } from '@paraspell/sdk';
import type { ApiPromise } from '@polkadot/api';
import { web3Accounts, web3Enable, web3FromAddress } from '@polkadot/extension-dapp';
Expand Down Expand Up @@ -45,23 +44,12 @@ const SendXcm = () => {
{ from, to, amount, address, currency }: FormValues,
api: ApiPromise
) => {
if (from === 'Polkadot' || from === 'Kusama') {
return Builder(api)
.to(to as TNode)
.amount(amount)
.address(address)
.build();
} else if (to === 'Polkadot' || to === 'Kusama') {
return Builder(api).from(from).amount(amount).address(address).build();
} else {
return Builder(api)
.from(from)
.to(to)
.currency({ symbol: currency })
.amount(amount)
.address(address)
.build();
}
return Builder(api)
.from(from)
.to(to)
.currency({ symbol: currency, amount })
.address(address)
.build();
};

const submitUsingSdk = async (
Expand Down
11 changes: 2 additions & 9 deletions apps/xcm-api/src/transfer-info/dto/transfer-info.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { CurrencyCoreSchema } from '../../x-transfer/dto/XTransferDto.js';
import { CurrencyCoreWithAmountSchema } from '../../x-transfer/dto/XTransferDto.js';
import { z } from 'zod';
import { validateAmount } from '../../utils/validateAmount.js';

export const TransferInfoSchema = z.object({
origin: z.string(),
Expand All @@ -9,13 +8,7 @@ export const TransferInfoSchema = z.object({
accountDestination: z
.string()
.min(1, { message: 'Destination address is required' }),
currency: CurrencyCoreSchema,
amount: z.union([
z.string().refine(validateAmount, {
message: 'Amount must be a positive number',
}),
z.number().positive({ message: 'Amount must be a positive number' }),
]),
currency: CurrencyCoreWithAmountSchema,
});

export type TransferInfoDto = z.infer<typeof TransferInfoSchema>;
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ describe('TransferInfoController', () => {
destination: 'Basilisk',
accountOrigin: '5F5586mfsnM6durWRLptYt3jSUs55KEmahdodQ5tQMr9iY96',
accountDestination: '5F5586mfsnM6durWRLptYt3jSUs55KEmahdodQ5tQMr9iY96',
currency: { symbol: 'DOT' },
amount: 100,
currency: { symbol: 'DOT', amount: 100 },
};
const mockResult = {} as TTransferInfo;
const spy = vi
Expand All @@ -62,8 +61,7 @@ describe('TransferInfoController', () => {
destination: 'Basilisk',
accountOrigin: '5F5586mfsnM6durWRLptYt3jSUs55KEmahdodQ5tQMr9iY96',
accountDestination: '5F5586mfsnM6durWRLptYt3jSUs55KEmahdodQ5tQMr9iY96',
currency: { symbol: 'DOT' },
amount: 100,
currency: { symbol: 'DOT', amount: 100 },
};
const mockResult = {} as TTransferInfo;
const spy = vi
Expand Down
3 changes: 1 addition & 2 deletions apps/xcm-api/src/transfer-info/transfer-info.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ export class TransferInfoController {
req: Request,
params: TransferInfoDto,
) {
const { origin, destination, currency, amount } = params;
const { origin, destination, currency } = params;
this.analyticsService.track(eventName, req, {
origin,
destination,
currency: JSON.stringify(currency),
amount,
});
}

Expand Down
24 changes: 8 additions & 16 deletions apps/xcm-api/src/transfer-info/transfer-info.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ describe('TransferInfoService', () => {
destination: 'Kusama',
accountOrigin: '0x123',
accountDestination: '0x456',
currency: { symbol: 'DOT' },
amount: '1000',
currency: { symbol: 'DOT', amount: '1000' },
}),
).rejects.toThrow(BadRequestException);
});
Expand All @@ -51,8 +50,7 @@ describe('TransferInfoService', () => {
destination: 'InvalidNode',
accountOrigin: '0x123',
accountDestination: '0x456',
currency: { symbol: 'DOT' },
amount: '1000',
currency: { symbol: 'DOT', amount: '1000' },
}),
).rejects.toThrow(BadRequestException);
});
Expand All @@ -65,8 +63,7 @@ describe('TransferInfoService', () => {
destination: 'Kusama',
accountOrigin: '0x123',
accountDestination: '0x456',
currency: { symbol: 'DOT' },
amount: '1000',
currency: { symbol: 'DOT', amount: '1000' },
}),
).rejects.toThrow(BadRequestException);
});
Expand Down Expand Up @@ -106,8 +103,7 @@ describe('TransferInfoService', () => {
destination: 'Kusama',
accountOrigin: '0x123',
accountDestination: '0x456',
currency: { symbol: 'DOT' },
amount: '1000',
currency: { symbol: 'DOT', amount: '1000' },
});
expect(result).toEqual(mockTransferInfo);
});
Expand Down Expand Up @@ -148,8 +144,7 @@ describe('TransferInfoService', () => {
destination: 'Kusama',
accountOrigin: '0x123',
accountDestination: '0x456',
currency: { symbol: 'DOT' },
amount: '1000',
currency: { symbol: 'DOT', amount: '1000' },
},
true,
);
Expand All @@ -166,8 +161,7 @@ describe('TransferInfoService', () => {
destination: 'Kusama',
accountOrigin: '0x123',
accountDestination: '0x456',
currency: { symbol: 'DOT' },
amount: '1000',
currency: { symbol: 'DOT', amount: '1000' },
}),
).rejects.toThrow(BadRequestException);
});
Expand All @@ -180,8 +174,7 @@ describe('TransferInfoService', () => {
destination: 'Kusama',
accountOrigin: '0x123',
accountDestination: '0x456',
currency: { symbol: 'DOT' },
amount: '1000',
currency: { symbol: 'DOT', amount: '1000' },
}),
).rejects.toThrow(InternalServerErrorException);
});
Expand All @@ -196,8 +189,7 @@ describe('TransferInfoService', () => {
destination: 'Kusama',
accountOrigin: '0x123',
accountDestination: '0x456',
currency: { symbol: 'DOT' },
amount: '1000',
currency: { symbol: 'DOT', amount: '1000' },
}),
).rejects.toThrow('Invalid destination wallet address.');
});
Expand Down
2 changes: 0 additions & 2 deletions apps/xcm-api/src/transfer-info/transfer-info.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export class TransferInfoService {
accountOrigin,
accountDestination,
currency,
amount,
}: TransferInfoDto,
usePapi = false,
) {
Expand Down Expand Up @@ -58,7 +57,6 @@ export class TransferInfoService {
accountOrigin,
accountDestination,
currency,
amount: amount.toString(),
});
} catch (e) {
if (e instanceof InvalidCurrencyError) {
Expand Down
Loading

0 comments on commit 395b45e

Please sign in to comment.