Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
feat: add transfer and useTransfer
Browse files Browse the repository at this point in the history
  • Loading branch information
DoubleOTheven authored and Sam Ruberti committed Jun 9, 2023
1 parent abc0860 commit 37ba1e9
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/substrate/balances/mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './getBalance.ts';
export * from './transfer.ts';

19 changes: 19 additions & 0 deletions core/substrate/balances/transfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
AddressOrPair,
ApiPromise,
Hash,
SignerOptions,
} from '../../types/mod.ts';

export const transfer = async (
api: ApiPromise | undefined,
to: string | undefined,
amount: number,
signer: AddressOrPair,
options?: SignerOptions,
): Promise<Hash | undefined> => {
if (!api?.tx?.balances?.transfer || !to) return;
const transfer = api.tx.balances.transfer(to, amount);

return await transfer.signAndSend(signer, options);
};
2 changes: 2 additions & 0 deletions core/types/substrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type {
DispatchError,
EventRecord,
ExtrinsicStatus,
Hash,
Header,
RuntimeDispatchInfo,
StorageDeposit,
Expand All @@ -31,6 +32,7 @@ export type {
} from '@polkadot/types/interfaces';

export type {
AddressOrPair,
ApiBase,
QueryableModuleCalls,
SignerOptions,
Expand Down
1 change: 1 addition & 0 deletions react/hooks/substrate/mod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './useApi.ts';
export * from './useBalance.ts';
export * from './useBlockHeader.ts';
export * from './useTransfer.ts';
47 changes: 47 additions & 0 deletions react/hooks/substrate/useTransfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { useCallback, useMemo, useState } from 'react';
import { useApi } from './useApi.ts';
import { ChainId } from '../../../chains/mod.ts';
import { useChain, useWallet } from '../mod.ts';
import { Hash, SignerOptions, transfer } from '../../../core/mod.ts';

export type SignAndSendTransfer = (
to: string,
amount: number,
options?: SignerOptions,
) => void;

export interface TransferState {
signAndSend: SignAndSendTransfer;
hash: Hash | undefined;
error: unknown | undefined;
resetState: () => void;
isSubmitting: boolean;
}

export const useTransfer = (chainId?: ChainId): TransferState | undefined => {
const [hash, setHash] = useState<Hash>();
const [error, setError] = useState<unknown>();
const [isSubmitting, setIsSubmitting] = useState(false);
const { account } = useWallet();
const chainConfig = useChain(chainId);
const chain = useApi(chainConfig?.id);

const resetState = useCallback(() => {
setHash(undefined);
setError(undefined);
}, []);

const signAndSend: SignAndSendTransfer = useMemo(
() => (to, amount, options) => {
if (!chain?.api || !account || !account.address || !account.wallet) {
return;
}
setIsSubmitting(true);
transfer(chain.api, to, amount, account?.wallet.extension.signer, options)
.then(setHash).catch(setError).finally(() => setIsSubmitting(false));
},
[chain?.api, account?.address],
);

return { signAndSend, error, hash, resetState, isSubmitting };
};

0 comments on commit 37ba1e9

Please sign in to comment.