|
1 | | -// file: src/transfer.ts |
| 1 | +import type { ApiPromise, ISubmittableResult, SubmittableExtrinsic } from '@autonomys/auto-utils' |
| 2 | +import { chainToChainIdCodec } from './transforms' |
| 3 | +import type { Chain } from './types' |
2 | 4 |
|
3 | | -import type { ApiPromise, Codec } from '@autonomys/auto-utils' |
4 | | - |
5 | | -export const initiateChannel = async (api: ApiPromise, destination: Codec) => { |
6 | | - return await api.tx.messenger.initiateChannel(destination) |
| 5 | +/** |
| 6 | + * Creates a transaction to initiate a channel to a destination chain. |
| 7 | + * |
| 8 | + * This function creates a transaction but does not submit it. The returned transaction |
| 9 | + * must be signed and sent using `signAndSendTx` or similar methods. |
| 10 | + * |
| 11 | + * Before initiating a channel, ensure the destination chain is in the allowlist. |
| 12 | + * Use `chainAllowlist()` to check which chains can open channels with the current chain. |
| 13 | + * |
| 14 | + * @param api - The API instance for the source chain |
| 15 | + * @param destination - The destination chain: 'consensus' or { domainId: number } |
| 16 | + * @returns A transaction that can be signed and submitted |
| 17 | + * |
| 18 | + * @example |
| 19 | + * ```typescript |
| 20 | + * // Initiate channel to consensus chain |
| 21 | + * const tx = initiateChannel(api, 'consensus') |
| 22 | + * await signAndSendTx(keyringPair, tx, {}, [], false) |
| 23 | + * |
| 24 | + * // Initiate channel to domain 0 |
| 25 | + * const tx = initiateChannel(api, { domainId: 0 }) |
| 26 | + * await signAndSendTx(keyringPair, tx, {}, [], false) |
| 27 | + * |
| 28 | + * // Check if destination is allowed before initiating |
| 29 | + * const allowlist = await chainAllowlist(api) |
| 30 | + * const canOpenToDomain0 = allowlist.some(chain => chain !== 'consensus' && chain.domainId === 0) |
| 31 | + * if (canOpenToDomain0) { |
| 32 | + * const tx = initiateChannel(api, { domainId: 0 }) |
| 33 | + * await signAndSendTx(keyringPair, tx, {}, [], false) |
| 34 | + * } |
| 35 | + * ``` |
| 36 | + */ |
| 37 | +export const initiateChannel = ( |
| 38 | + api: ApiPromise, |
| 39 | + destination: Chain, |
| 40 | +): SubmittableExtrinsic<'promise', ISubmittableResult> => { |
| 41 | + const destinationChainId = chainToChainIdCodec(api, destination) |
| 42 | + return api.tx.messenger.initiateChannel(destinationChainId) |
7 | 43 | } |
8 | 44 |
|
9 | | -export const closeChannel = async (api: ApiPromise, chainId: Codec, channelId: string) => { |
10 | | - return await api.tx.messenger.closeChannel(chainId, channelId) |
| 45 | +/** |
| 46 | + * Creates a transaction to close an existing channel to a destination chain. |
| 47 | + * |
| 48 | + * This function creates a transaction but does not submit it. The returned transaction |
| 49 | + * must be signed and sent using `signAndSendTx` or similar methods. |
| 50 | + * |
| 51 | + * Closing a channel stops further message exchanges on that channel. You can query |
| 52 | + * existing channels using `channels()` to find the channel ID to close. |
| 53 | + * |
| 54 | + * @param api - The API instance for the chain where the channel exists |
| 55 | + * @param destination - The destination chain of the channel: 'consensus' or { domainId: number } |
| 56 | + * @param channelId - The channel ID to close (number, string, or bigint) |
| 57 | + * @returns A transaction that can be signed and submitted |
| 58 | + * |
| 59 | + * @example |
| 60 | + * ```typescript |
| 61 | + * // Close channel 0 to domain 0 |
| 62 | + * const tx = closeChannel(api, { domainId: 0 }, 0) |
| 63 | + * await signAndSendTx(keyringPair, tx, {}, [], false) |
| 64 | + * |
| 65 | + * // Close channel 1 to consensus chain |
| 66 | + * const tx = closeChannel(api, 'consensus', 1) |
| 67 | + * await signAndSendTx(keyringPair, tx, {}, [], false) |
| 68 | + * |
| 69 | + * // Query channel before closing to verify it exists |
| 70 | + * const channel = await channels(api, { domainId: 0 }, 0) |
| 71 | + * if (channel && channel.state === 'Open') { |
| 72 | + * const tx = closeChannel(api, { domainId: 0 }, 0) |
| 73 | + * await signAndSendTx(keyringPair, tx, {}, [], false) |
| 74 | + * } |
| 75 | + * ``` |
| 76 | + */ |
| 77 | +export const closeChannel = ( |
| 78 | + api: ApiPromise, |
| 79 | + destination: Chain, |
| 80 | + channelId: number | string | bigint, |
| 81 | +): SubmittableExtrinsic<'promise', ISubmittableResult> => { |
| 82 | + const destinationChainId = chainToChainIdCodec(api, destination) |
| 83 | + const channelIdCodec = api.createType('U256', channelId) |
| 84 | + return api.tx.messenger.closeChannel(destinationChainId, channelIdCodec) |
11 | 85 | } |
0 commit comments