Skip to content

Commit

Permalink
fix: breaking change from stacks.js
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Dec 7, 2021
1 parent 083fa84 commit d09244c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
25 changes: 13 additions & 12 deletions src/common/transactions/broadcast-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,27 @@ interface BroadcastTransactionOptions {
}
export async function broadcastTransaction(options: BroadcastTransactionOptions) {
const { txRaw, serialized, isSponsored, attachment, networkUrl } = options;

if (isSponsored) return { txRaw };
const response = await broadcastRawTransaction(
serialized,
`${networkUrl}/v2/transactions`,
attachment ? Buffer.from(attachment, 'hex') : undefined
);

if (typeof response === 'string') {
const isValidTxId = validateTxId(response);
try {
const response = await broadcastRawTransaction(
serialized,
`${networkUrl}/v2/transactions`,
attachment ? Buffer.from(attachment, 'hex') : undefined
);

const isValidTxId = validateTxId(response.txid);
if (isValidTxId)
return {
txId: response,
txRaw,
};
logger.error(`Error broadcasting raw transaction -- ${response}`);
throw new Error(response);
} else {
throw new Error((response as any).error);
} catch (e) {
logger.error('Error broadcasting raw transaction');
const error = `${response.error} - ${response.reason}`;
logger.error(error);
throw new Error(error);
logger.error(e);
throw new Error(e as any);
}
}
8 changes: 2 additions & 6 deletions src/store/transactions/transaction.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { todaysIsoDate } from '@common/date-utils';
import { finalizeTxSignature } from '@common/actions/finalize-tx-signature';
import { useWallet } from '@common/hooks/use-wallet';
import { broadcastTransaction } from '@common/transactions/broadcast-transaction';
import { logger } from '@common/logger';
import { currentAccountState } from '@store/accounts';
import { currentNetworkState } from '@store/network/networks';
import {
Expand Down Expand Up @@ -84,8 +83,6 @@ export function useSignTransactionSoftwareWallet() {
);
}

// TODO: @kyranjamie
// only used for signed transactions, not send form
export function useTransactionBroadcast() {
const { setLatestNonce } = useWallet();
const signSoftwareWalletTx = useSignTransactionSoftwareWallet();
Expand Down Expand Up @@ -119,9 +116,9 @@ export function useTransactionBroadcast() {
attachment,
networkUrl: network.url,
});
typeof nonce !== 'undefined' && (await setLatestNonce(nonce));
if (typeof nonce !== 'undefined') await setLatestNonce(nonce);
finalizeTxSignature(requestToken, result);
if (typeof result.txId !== 'undefined') {
if (typeof result.txId === 'string') {
set(currentAccountLocallySubmittedTxsState, {
[result.txId]: {
rawTx: result.txRaw,
Expand All @@ -130,7 +127,6 @@ export function useTransactionBroadcast() {
});
}
} catch (error) {
logger.error(error);
if (error instanceof Error) set(transactionBroadcastErrorState, error.message);
}
},
Expand Down

0 comments on commit d09244c

Please sign in to comment.