Skip to content

Commit

Permalink
JSDoc updates
Browse files Browse the repository at this point in the history
  • Loading branch information
MananTank committed Feb 19, 2024
1 parent 4f5c003 commit 523ff5d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
31 changes: 26 additions & 5 deletions packages/thirdweb/src/react/ui/TransactionButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,31 @@ import { useEffect, useState } from "react";
import { formatEther } from "../../../utils/units.js";

export type TransactionButtonProps = React.PropsWithChildren<{
/**
* The transaction to be sent when the button is clicked
*/
transaction: PreparedTransaction;
/**
* Callback to be called when the transaction is successful
* @param transactionHash - The object of type [`WaitForReceiptOptions`](https://portal.thirdweb.com/references/typescript/v5/WaitForReceiptOptions)
*/
onSuccess?: (transactionHash: WaitForReceiptOptions) => void;
/**
* The Error thrown when trying to send the transaction
* @param error - The `Error` object thrown
*/
onError?: (error: Error) => void;
/**
* Callback to be called when the button is clicked
*/
onSubmit?: () => void;
/**
* The className to apply to the button element for custom styling
*/
className?: string;
/**
* The style to apply to the button element for custom styling
*/
style?: React.CSSProperties;
}>;

Expand All @@ -31,7 +51,7 @@ export type TransactionButtonProps = React.PropsWithChildren<{
* It handles switching chains if the connected wallet is on a different chain than the transaction.
* It also estimates gas and displays a loading spinner while the transaction is pending.
* @param props - The props for this component.
* @returns The rendered component.
* Refer to [TransactionButtonProps](https://portal.thirdweb.com/references/typescript/v5/TransactionButtonProps) for details.
* @example
* ```tsx
* <TransactionButton
Expand All @@ -42,6 +62,7 @@ export type TransactionButtonProps = React.PropsWithChildren<{
* Confirm Transaction
* </TransactionButton>
* ```
* @component
*/
export const TransactionButton: React.FC<TransactionButtonProps> = (props) => {
const {
Expand All @@ -61,7 +82,7 @@ export const TransactionButton: React.FC<TransactionButtonProps> = (props) => {
const sendTransaction = useSendTransaction();
const chainQuery = useChainQuery(txChain.id);

const [gasCost, setGasCost] = useState<string | undefined>();
const [totalCost, setTotalCost] = useState<string | undefined>();
useEffect(() => {
// TODO this should be a new core action: estimateGasCost
estimateGas({ transaction, wallet })
Expand All @@ -72,7 +93,7 @@ export const TransactionButton: React.FC<TransactionButtonProps> = (props) => {
: transaction.value) || BigInt(0);
// TODO fix this - should fetch price from eth_getGasPrice
const gasCostWei = value * BigInt(10 ** 9);
setGasCost(formatEther(gasCostWei + txValueWei, "wei"));
setTotalCost(formatEther(gasCostWei + txValueWei, "wei"));
})
.catch((error) => {
console.error("Error estimating gas", error);
Expand All @@ -94,7 +115,7 @@ export const TransactionButton: React.FC<TransactionButtonProps> = (props) => {
);
}

if (gasCost && chainQuery.data && !sendTransaction.isPending) {
if (totalCost && chainQuery.data && !sendTransaction.isPending) {
return (
<Button
gap="xs"
Expand Down Expand Up @@ -123,7 +144,7 @@ export const TransactionButton: React.FC<TransactionButtonProps> = (props) => {
paddingLeft: spacing.xs,
}}
>
{gasCost} {chainQuery.data.nativeCurrency.symbol}
{totalCost} {chainQuery.data.nativeCurrency.symbol}
</span>
</Button>
);
Expand Down
2 changes: 2 additions & 0 deletions packages/thirdweb/src/transaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ export {
type BaseTransactionOptions,
isBaseTransactionOptions,
} from "./types.js";

export type { WaitForReceiptOptions } from "./actions/wait-for-tx-receipt.js";

0 comments on commit 523ff5d

Please sign in to comment.