Skip to content

Commit

Permalink
fix: add functionality for sponsored transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarkhanzadian committed Feb 5, 2024
1 parent 37ad2da commit 3e7af19
Showing 1 changed file with 32 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useMemo, useState } from 'react';
import toast from 'react-hot-toast';
import { useNavigate } from 'react-router-dom';

import { StacksTransaction } from '@stacks/transactions';
import { AuthType, StacksTransaction } from '@stacks/transactions';

import { finalizeTxSignature } from '@shared/actions/finalize-tx-signature';
import { logger } from '@shared/logger';
Expand All @@ -14,11 +14,16 @@ import { useDefaultRequestParams } from '@app/common/hooks/use-default-request-s
import { LoadingKeys } from '@app/common/hooks/use-loading';
import { useSubmitTransactionCallback } from '@app/common/hooks/use-submit-stx-transaction';
import { stacksTransactionToHex } from '@app/common/transactions/stacks/transaction.utils';
import { delay } from '@app/common/utils';
import { useTransactionRequest } from '@app/store/transactions/requests.hooks';
import { useSignStacksTransaction } from '@app/store/transactions/transaction.hooks';

import { useStacksTransactionSummary } from './use-stacks-transaction-summary';

async function simulateShortDelayToAvoidUndefinedTabId() {
await delay(1000);
}

export function useStacksBroadcastTransaction(token: CryptoCurrencies, decimals?: number) {
const signStacksTransaction = useSignStacksTransaction();
const [isBroadcasting, setIsBroadcasting] = useState(false);
Expand All @@ -32,7 +37,7 @@ export function useStacksBroadcastTransaction(token: CryptoCurrencies, decimals?
});

return useMemo(() => {
function handlePreviewSuccess(txId: string, signedTx: StacksTransaction) {
function handlePreviewSuccess(signedTx: StacksTransaction, txId?: string) {
if (requestToken && tabId) {
finalizeTxSignature({
requestPayload: requestToken,
Expand All @@ -43,13 +48,15 @@ export function useStacksBroadcastTransaction(token: CryptoCurrencies, decimals?
},
});
}
navigate(
RouteUrls.SentStxTxSummary.replace(':symbol', token.toLowerCase()).replace(
':txId',
`${txId}`
),
formSentSummaryTxState(txId, signedTx, decimals)
);
if (txId) {
navigate(
RouteUrls.SentStxTxSummary.replace(':symbol', token.toLowerCase()).replace(
':txId',
`${txId}`
),
formSentSummaryTxState(txId, signedTx, decimals)
);
}
}

async function broadcastTransactionAction(signedTx: StacksTransaction) {
Expand All @@ -60,16 +67,22 @@ export function useStacksBroadcastTransaction(token: CryptoCurrencies, decimals?
}
try {
setIsBroadcasting(true);
await broadcastTransactionFn({
onError(e: Error | string) {
const message = isString(e) ? e : e.message;
navigate(RouteUrls.TransactionBroadcastError, { state: { message } });
},
onSuccess(txId) {
handlePreviewSuccess(txId, signedTx);
},
replaceByFee: false,
})(signedTx);
const isSponsored = signedTx.auth?.authType === AuthType.Sponsored;
if (isSponsored) {
await simulateShortDelayToAvoidUndefinedTabId();
handlePreviewSuccess(signedTx);
} else {
await broadcastTransactionFn({
onError(e: Error | string) {
const message = isString(e) ? e : e.message;
navigate(RouteUrls.TransactionBroadcastError, { state: { message } });
},
onSuccess(txId) {
handlePreviewSuccess(signedTx, txId);
},
replaceByFee: false,
})(signedTx);
}
} catch (e) {
navigate(RouteUrls.TransactionBroadcastError, {
state: { message: e instanceof Error ? e.message : 'Unknown error' },
Expand Down

0 comments on commit 3e7af19

Please sign in to comment.