1- import { Binary , createClient , Transaction } from "polkadot-api" ;
2- import { withPolkadotSdkCompat } from "polkadot-api/polkadot-sdk-compat" ;
3- import { getPolkadotSigner } from "polkadot-api/signer" ;
4- import { getWsProvider } from "polkadot-api/ws-provider/web" ;
5- import { assetHub } from "@polkadot-api/descriptors" ;
6- import { sr25519CreateDerive } from "@polkadot-labs/hdkd" ;
7- import { DEV_PHRASE , entropyToMiniSecret , mnemonicToEntropy } from "@polkadot-labs/hdkd-helpers" ;
1+ import { Binary , createClient , Transaction } from 'polkadot-api' ;
2+ import { withPolkadotSdkCompat } from 'polkadot-api/polkadot-sdk-compat' ;
3+ import { getPolkadotSigner } from 'polkadot-api/signer' ;
4+ import { getWsProvider } from 'polkadot-api/ws-provider/web' ;
5+ import { assetHub } from '@polkadot-api/descriptors' ;
6+ import { sr25519CreateDerive } from '@polkadot-labs/hdkd' ;
7+ import {
8+ DEV_PHRASE ,
9+ entropyToMiniSecret ,
10+ mnemonicToEntropy ,
11+ } from '@polkadot-labs/hdkd-helpers' ;
812
913const toHuman = ( _key : any , value : any ) => {
10- if ( typeof value === " bigint" ) {
11- return Number ( value ) ;
12- }
14+ if ( typeof value === ' bigint' ) {
15+ return Number ( value ) ;
16+ }
1317
14- if ( value && typeof value === " object" && typeof value . asHex === " function" ) {
15- return value . asHex ( ) ;
16- }
18+ if ( value && typeof value === ' object' && typeof value . asHex === ' function' ) {
19+ return value . asHex ( ) ;
20+ }
1721
18- return value ;
22+ return value ;
1923} ;
2024
2125function getSigner ( ) {
22- const entropy = mnemonicToEntropy ( DEV_PHRASE ) ;
23- const miniSecret = entropyToMiniSecret ( entropy ) ;
24- const derive = sr25519CreateDerive ( miniSecret ) ;
25- const alice = derive ( " //Alice" ) ;
26+ const entropy = mnemonicToEntropy ( DEV_PHRASE ) ;
27+ const miniSecret = entropyToMiniSecret ( entropy ) ;
28+ const derive = sr25519CreateDerive ( miniSecret ) ;
29+ const alice = derive ( ' //Alice' ) ;
2630
27- return getPolkadotSigner ( alice . publicKey , " Sr25519" , alice . sign ) ;
31+ return getPolkadotSigner ( alice . publicKey , ' Sr25519' , alice . sign ) ;
2832}
2933
3034async function main ( ) {
31- const provider = withPolkadotSdkCompat ( getWsProvider ( " ws://localhost:8000" ) ) ;
32- const client = createClient ( provider ) ;
33- const api = client . getTypedApi ( assetHub ) ;
34- const aliceSigner = getSigner ( ) ;
35+ const provider = withPolkadotSdkCompat ( getWsProvider ( ' ws://localhost:8000' ) ) ;
36+ const client = createClient ( provider ) ;
37+ const api = client . getTypedApi ( assetHub ) ;
38+ const aliceSigner = getSigner ( ) ;
3539
36- const callData = Binary . fromHex (
37- "0x1f0803010100411f0300010100fc39fcf04a8071b7409823b7c82427ce67910c6ed80aa0e5093aff234624c8200304000002043205011f0092e81d790000000000"
38- ) ;
39- const tx : Transaction < any , string , string , any > = await api . txFromCallData ( callData ) ;
40- console . log ( "👀 Executing XCM:" , JSON . stringify ( tx . decodedCall , toHuman , 2 ) ) ;
40+ const callData = Binary . fromHex (
41+ '0x1f0803010100411f0300010100fc39fcf04a8071b7409823b7c82427ce67910c6ed80aa0e5093aff234624c8200304000002043205011f0092e81d790000000000' ,
42+ ) ;
43+ const tx : Transaction < any , string , string , any > =
44+ await api . txFromCallData ( callData ) ;
45+ console . log ( '👀 Executing XCM:' , JSON . stringify ( tx . decodedCall , toHuman , 2 ) ) ;
4146
42- await new Promise < void > ( ( resolve ) => {
43- const subscription = tx . signSubmitAndWatch ( aliceSigner ) . subscribe ( ( ev ) => {
44- if ( ev . type === "finalized" || ( ev . type === "txBestBlocksState" && ev . found ) ) {
45- console . log ( `📦 Included in block #${ ev . block . number } : ${ ev . block . hash } ` ) ;
47+ await new Promise < void > ( ( resolve ) => {
48+ const subscription = tx . signSubmitAndWatch ( aliceSigner ) . subscribe ( ( ev ) => {
49+ if (
50+ ev . type === 'finalized' ||
51+ ( ev . type === 'txBestBlocksState' && ev . found )
52+ ) {
53+ console . log (
54+ `📦 Included in block #${ ev . block . number } : ${ ev . block . hash } ` ,
55+ ) ;
4656
47- if ( ! ev . ok ) {
48- const dispatchError = ev . dispatchError ;
49- if ( dispatchError . type === "Module" ) {
50- const modErr : any = dispatchError . value ;
51- console . error ( `❌ Dispatch error in module: ${ modErr . type } → ${ modErr . value ?. type } ` ) ;
52- } else {
53- console . error ( "❌ Dispatch error:" , JSON . stringify ( dispatchError , toHuman , 2 ) ) ;
54- }
55- }
57+ if ( ! ev . ok ) {
58+ const dispatchError = ev . dispatchError ;
59+ if ( dispatchError . type === 'Module' ) {
60+ const modErr : any = dispatchError . value ;
61+ console . error (
62+ `❌ Dispatch error in module: ${ modErr . type } → ${ modErr . value ?. type } ` ,
63+ ) ;
64+ } else {
65+ console . error (
66+ '❌ Dispatch error:' ,
67+ JSON . stringify ( dispatchError , toHuman , 2 ) ,
68+ ) ;
69+ }
70+ }
5671
57- for ( const event of ev . events ) {
58- console . log ( "📣 Event:" , event . type , JSON . stringify ( event . value , toHuman , 2 ) ) ;
59- }
72+ for ( const event of ev . events ) {
73+ console . log (
74+ '📣 Event:' ,
75+ event . type ,
76+ JSON . stringify ( event . value , toHuman , 2 ) ,
77+ ) ;
78+ }
6079
61- console . log ( "✅ Process completed, exiting..." ) ;
62- subscription . unsubscribe ( ) ;
63- resolve ( ) ;
64- }
65- } ) ;
80+ console . log ( '✅ Process completed, exiting...' ) ;
81+ subscription . unsubscribe ( ) ;
82+ resolve ( ) ;
83+ }
6684 } ) ;
85+ } ) ;
6786
68- client . destroy ( ) ;
87+ client . destroy ( ) ;
6988}
7089
71- main ( ) . catch ( console . error ) ;
90+ main ( ) . catch ( console . error ) ;
0 commit comments