Skip to content

Commit

Permalink
Merge pull request kodadot#765 from kodadot/755-just-hash
Browse files Browse the repository at this point in the history
Transactions can return hashes
  • Loading branch information
yangwao authored Sep 21, 2021
2 parents 6dbf7f0 + 55d229b commit c20d615
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
7 changes: 4 additions & 3 deletions src/components/transfer/Transfer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
42 changes: 27 additions & 15 deletions src/utils/transactionExecutor.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) => {
Expand Down

0 comments on commit c20d615

Please sign in to comment.