import { SubmittableResult } from '@polkadot/api' ; import type { SignerOptions } from '@polkadot/api/types' ; import { SubmittableExtrinsic } from '@polkadot/api/types' ; import type { Registry } from '@polkadot/types/types' ; import type { TransactionResponse } from './types' ; export async function buildTx ( registry , extrinsic , signer , options ? ) { const signerAddress = signer ; return new Promise ( ( resolve , reject ) => { const actionStatus = { from : signerAddress . toString ( ) , txHash : extrinsic . hash . toHex ( ) } as Partial < TransactionResponse > ; extrinsic . signAndSend ( signerAddress , { ... options } , ( result ) => { if ( result . status . isInBlock ) { actionStatus . blockHash = result . status . asInBlock . toHex ( ) ; } if ( result . status . isFinalized || result . status . isInBlock ) { result . events . filter ( ( { event : { section } } ) : boolean => section === 'system' ) . forEach ( ( event ) : void => { const { event : { data , method } } = event ; if ( method === 'ExtrinsicFailed' ) { const [ dispatchError ] = data ; let message = dispatchError . type ; if ( dispatchError . isModule ) { try { const mod = dispatchError . asModule ; const error = registry . findMetaError ( new Uint8Array ( [ mod . index . toNumber ( ) , mod . error . toNumber ( ) ] ) ) ; message = `${ error . section }.${ error . name }${ Array . isArray ( error . docs ) ? `(${ error . docs . join ( '' ) })` : error . docs || '' }` ; } catch ( error ) { } } actionStatus . error = { message } ; reject ( actionStatus ) ; } else if ( method === 'ExtrinsicSuccess' ) { actionStatus . result = result ; resolve ( actionStatus as TransactionResponse ) ; } } ) ; } else if ( result . isError ) { actionStatus . error = { data : result } ; actionStatus . events = null ; reject ( actionStatus ) ; } } ) . catch ( ( error ) => { actionStatus . error = { message : error . message } ; reject ( actionStatus ) ; } ) ; } ) ; }