diff --git a/src/components/transfer/Transfer.vue b/src/components/transfer/Transfer.vue index 82217fe221..6b024109db 100644 --- a/src/components/transfer/Transfer.vue +++ b/src/components/transfer/Transfer.vue @@ -154,7 +154,7 @@ export default class Transfer extends Mixins( const tx = await exec(this.accountId, '', cb, arg, txCb( async blockHash => { - execResultValue(tx); + this.transactionValue = execResultValue(tx); const header = await api.rpc.chain.getHeader(blockHash); const blockNumber = header.number.toString(); @@ -166,10 +166,11 @@ export default class Transfer extends Mixins( this.destinationAddress = ''; this.price = 0; this.usdValue = 0; - this.$router.push(this.$route.path); + if (this.$route.query) { + this.$router.push(this.$route.path); + } this.isLoading = false; - this.transactionValue = blockHash.toHex(); }, dispatchError => { execResultValue(tx); diff --git a/src/utils/transactionExecutor.ts b/src/utils/transactionExecutor.ts index 9a38edd7ca..8278da8f08 100644 --- a/src/utils/transactionExecutor.ts +++ b/src/utils/transactionExecutor.ts @@ -1,18 +1,18 @@ import keyring from '@polkadot/ui-keyring'; import { KeyringAccount } from '@/types'; -import { SubmittableExtrinsic } from '@polkadot/api/types'; +import { AddressOrPair, SubmittableExtrinsic } from '@polkadot/api/types'; import { Callback, ISubmittableResult } from '@polkadot/types/types'; import { getAddress } from '@/extension'; import { toDefaultAddress } from '@/utils/account' import { DispatchError, Hash } from '@polkadot/types/interfaces'; -export type ExecResult = (() => void) | string +export type ExecResult = UnsubscribeFn | string export type Extrinsic = SubmittableExtrinsic<'promise'> +export type UnsubscribeFn = () => string; export const execResultValue = (execResult: ExecResult): string => { if (typeof execResult === 'function') { - execResult() - return '' + return execResult() } return execResult @@ -23,24 +23,36 @@ const exec = async (account: KeyringAccount | string, password: string | null, c const transfer = await callback(...params); const address = typeof account === 'string' ? account : account.address; const injector = await getAddress(toDefaultAddress(address)); - if (injector) { - const h = await transfer.signAndSend(address, { signer: injector.signer }, statusCb); - return h - } + const hasCallback = typeof statusCb === 'function'; - const alicePair = keyring.getPair(address); - if (password) { - alicePair.decodePkcs8(password); - } - const hash = statusCb ? await transfer.signAndSend(alicePair, statusCb) : await transfer.signAndSend(alicePair); - return typeof hash === 'function' ? hash : hash.toHex(); + const options = injector ? { signer: injector.signer } : undefined; + const signer: AddressOrPair = injector ? address : extractFromKeyring(address, password); + + const tx = await transfer.signAsync(signer, options); + const hash = hasCallback ? await tx.send(statusCb) : await transfer.send(); + return typeof hash === 'function' ? constructCallback(hash, tx.hash.toHex()) : hash.toHex(); } catch (err) { throw err; } - }; +const extractFromKeyring = (address: string, password: string | null) => { + const alicePair = keyring.getPair(address); + if (password) { + alicePair.decodePkcs8(password); + } + + return alicePair; +} + +const constructCallback = (cb: () => void, result: string) => { + return () => { + cb(); + return result; + }; +} + export const txCb = (onSuccess: (blockHash: Hash) => void, onError: (err: DispatchError) => void, onResult: (result: ISubmittableResult) => void = console.log) => (result: ISubmittableResult) => {