|
1 | | -import type { Api, KnownChainId } from "@polkadot-agent-kit/common" |
| 1 | +import type { TDestination, TNodeDotKsmWithRelayChains, TPapiTransaction } from "@paraspell/sdk" |
| 2 | +import { Builder } from "@paraspell/sdk" |
| 3 | +import type { KnownChainId } from "@polkadot-agent-kit/common" |
2 | 4 | import { getAllSupportedChains, getChainById } from "@polkadot-agent-kit/common" |
3 | | -import type { TxResult } from "@substrate/asset-transfer-api" |
4 | | -import { AssetTransferApi } from "@substrate/asset-transfer-api" |
5 | | - |
6 | | -import { constructApiPromiseWithTimeout } from "../../utils" |
7 | 5 |
|
8 | 6 | /** |
9 | | - * Transfers a native asset to a destination address on a destination chain via xcm |
10 | | - * @param api - The API instance to use for the query |
11 | | - * @param to - The destination address |
12 | | - * @param amount - The amount to transfer |
13 | | - * @param destinationChain - The destination chain (RelayChain or Parachain) |
14 | | - * @returns The transaction result |
| 7 | + * Builds an XCM transaction to transfer a native asset from one chain to another. |
| 8 | + * |
| 9 | + * This function uses the \@paraspell/sdk Builder to construct a cross-chain (XCM) transfer |
| 10 | + * of the native token from a source chain to a destination chain. It supports both relay chains |
| 11 | + * and parachains, and abstracts away the complexity of XCM message construction. |
| 12 | + * |
| 13 | + * @param srcChain - The source chain ID (relay chain or parachain) from which the asset is sent |
| 14 | + * @param destChain - The destination chain ID (relay chain or parachain) to which the asset is sent |
| 15 | + * @param from - The sender's address on the source chain |
| 16 | + * @param to - The recipient's address on the destination chain |
| 17 | + * @param amount - The amount of the native asset to transfer (as bigint, in base units) |
| 18 | + * @returns A Promise resolving to a TPapiTransaction object representing the unsigned XCM transaction |
| 19 | + * |
| 20 | + * @example |
| 21 | + * const tx = await xcmTransferNativeAsset( |
| 22 | + * 'polkadot', |
| 23 | + * 'hydra', |
| 24 | + * 'senderAddress', |
| 25 | + * 'recipientAddress', |
| 26 | + * 10000000000n |
| 27 | + * ) |
| 28 | + * // tx can then be signed and submitted using the appropriate transaction handler |
15 | 29 | */ |
| 30 | + |
16 | 31 | export const xcmTransferNativeAsset = async ( |
17 | | - api: Api<KnownChainId>, |
| 32 | + srcChain: KnownChainId, |
| 33 | + destChain: KnownChainId, |
| 34 | + from: string, |
18 | 35 | to: string, |
19 | | - amount: bigint, |
20 | | - destinationChain: KnownChainId |
21 | | -): Promise<TxResult<"submittable">> => { |
22 | | - const sourceChain = getChainById(api.chainId, getAllSupportedChains()) |
23 | | - const destChain = getChainById(destinationChain, getAllSupportedChains()) |
24 | | - const { |
25 | | - api: xcmApi, |
26 | | - specName, |
27 | | - safeXcmVersion |
28 | | - } = await constructApiPromiseWithTimeout(sourceChain.wsUrls) |
29 | | - |
30 | | - const assetApi = new AssetTransferApi(xcmApi, specName, safeXcmVersion) |
31 | | - let callInfo: TxResult<"submittable"> |
32 | | - try { |
33 | | - callInfo = await assetApi.createTransferTransaction( |
34 | | - destChain.chainId?.toString() || "", |
35 | | - to, |
36 | | - [sourceChain.symbol], |
37 | | - [amount.toString()], |
38 | | - { |
39 | | - format: "submittable", |
40 | | - xcmVersion: safeXcmVersion |
41 | | - } |
42 | | - ) |
43 | | - return callInfo |
44 | | - } catch (e) { |
45 | | - throw Error(e as string) |
46 | | - } |
| 36 | + amount: bigint |
| 37 | +): Promise<TPapiTransaction> => { |
| 38 | + const sourceChain = getChainById(srcChain, getAllSupportedChains()) |
| 39 | + const destinationChain = getChainById(destChain, getAllSupportedChains()) |
| 40 | + const url = sourceChain.wsUrls |
| 41 | + // XCM transfer native tokken |
| 42 | + const tx = await Builder(url) |
| 43 | + .from(sourceChain.name as TNodeDotKsmWithRelayChains) |
| 44 | + .senderAddress(from) |
| 45 | + .to(destinationChain.name as TDestination) |
| 46 | + .currency({ symbol: sourceChain.symbol, amount: amount }) |
| 47 | + .address(to) |
| 48 | + .build() |
47 | 49 |
|
48 | | - return callInfo |
| 50 | + return tx |
49 | 51 | } |
0 commit comments