Skip to content

Commit 7e8409d

Browse files
committed
add js docs
1 parent c54c7b9 commit 7e8409d

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

packages/create-typink/src/templates/default/ui/src/utils/txToaster.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,35 @@ import { ISubmittableResult, TxStatus } from 'dedot/types';
44
import { useTypink } from 'typink';
55

66
export type TxToaster = {
7-
onTxProgress: (result: ISubmittableResult) => void;
7+
onTxProgress: (progress: ISubmittableResult) => void;
88
onTxError: (e: Error) => void;
99
};
1010

11+
/**
12+
* Creates a toast notification for transaction progress and manages its updates.
13+
*
14+
* This function initializes a toast notification for a transaction and returns
15+
* methods to update the toast based on the transaction's progress or errors.
16+
*
17+
* @param initialMessage - The initial message to display in the toast notification.
18+
* Defaults to 'Signing Transaction...'.
19+
* @returns An object containing two methods:
20+
* - onTxProgress: Updates the toast based on transaction progress.
21+
* - onTxError: Updates the toast when a transaction error occurs.
22+
*/
1123
export function txToaster(initialMessage: string = 'Signing Transaction...'): TxToaster {
1224
const toastId = toast.info(initialMessage, {
1325
autoClose: false, // prettier-end-here
1426
isLoading: true,
1527
closeOnClick: false,
1628
});
1729

18-
const onTxProgress = (result: ISubmittableResult) => {
30+
const onTxProgress = (progress: ISubmittableResult) => {
1931
let toastType: TypeOptions = 'default';
2032
let autoClose: boolean | number = false;
2133
let toastMessage: string = 'Transaction In Progress...';
2234

23-
const { status, dispatchError } = result;
35+
const { status, dispatchError } = progress;
2436
const succeeded = !dispatchError;
2537

2638
if (status.type === 'Finalized') {

packages/typink/src/helpers/balance.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import { GenericContractApi } from 'dedot/contracts';
22
import { ISubstrateClient } from 'dedot';
33
import { BalanceInsufficientError } from '../utils/index.js';
44

5+
/**
6+
* Checks whether the specified account has a sufficient free balance.
7+
* Throws a `BalanceInsufficientError` if the free balance is not enough to perform transaction.
8+
*
9+
* @param client
10+
* @param address
11+
*/
512
export const checkBalanceSufficiency = async <T extends GenericContractApi = GenericContractApi>(
613
client: ISubstrateClient<T['types']['ChainApi']>,
714
address: string,
@@ -11,4 +18,4 @@ export const checkBalanceSufficiency = async <T extends GenericContractApi = Gen
1118
if (balance.data.free <= 0n) {
1219
throw new BalanceInsufficientError(address);
1320
}
14-
};
21+
};

packages/typink/src/utils/errors.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ import { ISubstrateClient } from 'dedot';
1313
*/
1414
export class TypinkError extends DedotError {}
1515

16+
/**
17+
* Represents an error that occurs during contract message execution.
18+
*
19+
* @typeparam T - The type of the error object, extends any.
20+
* @extends TypinkError
21+
*/
1622
export class ContractMessageError<T extends any> extends TypinkError {
1723
constructor(
1824
public error: T,
@@ -22,6 +28,11 @@ export class ContractMessageError<T extends any> extends TypinkError {
2228
}
2329
}
2430

31+
/**
32+
* Represents an error that occurs when the caller has insufficient balance to perform a transaction.
33+
*
34+
* @extends TypinkError
35+
*/
2536
export class BalanceInsufficientError extends TypinkError {
2637
constructor(
2738
public caller: string,
@@ -41,6 +52,12 @@ const extractErrorType = (error: any): string => {
4152
return JSON.stringify(error);
4253
};
4354

55+
/**
56+
* Extract human-readable error message from an Error instance
57+
*
58+
* @param client
59+
* @param error
60+
*/
4461
export const extractHumanReadableError = <T extends GenericContractApi = GenericContractApi, E extends Error = any>(
4562
client: ISubstrateClient<T['types']['ChainApi']>,
4663
error: E,
@@ -132,6 +149,12 @@ export const extractHumanReadableError = <T extends GenericContractApi = Generic
132149
return error.message;
133150
};
134151

152+
/**
153+
* Replace the message of an Error instance with a human-readable error message
154+
*
155+
* @param client
156+
* @param error
157+
*/
135158
export const withReadableErrorMessage = <T extends GenericContractApi = GenericContractApi, E extends Error = any>(
136159
client: ISubstrateClient<T['types']['ChainApi']>,
137160
error: E,

0 commit comments

Comments
 (0)