Skip to content

Commit

Permalink
fix: guard against read properties of undefined errors in stx helper …
Browse files Browse the repository at this point in the history
…function (#213)
  • Loading branch information
teebszet authored Aug 15, 2023
1 parent 3e3dc93 commit f1cd2da
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
32 changes: 21 additions & 11 deletions api/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ export function parseMempoolStxTransactionsData({
return parsedTx;
}

/**
* parseStxTransactionData
* @param responseTx StxTransactionDataResponse
* @param stxAddress string
* @returns StxTransactionData parsed for display
*/
export function parseStxTransactionData({
responseTx,
stxAddress,
Expand Down Expand Up @@ -329,23 +335,27 @@ export function parseStxTransactionData({

// add token transfer data if type is token transfer
if (parsedTx.txType === 'token_transfer') {
const amount = new BigNumber(responseTx.token_transfer?.amount ?? 0);
parsedTx.tokenTransfer = {
recipientAddress: responseTx.token_transfer.recipient_address,
amount: new BigNumber(responseTx.token_transfer.amount),
memo: responseTx.token_transfer.memo,
recipientAddress: responseTx.token_transfer?.recipient_address,
amount,
memo: responseTx.token_transfer?.memo,
};
const amount = new BigNumber(responseTx.token_transfer.amount);
parsedTx.amount = amount;
}
if (parsedTx.txType === 'contract_call') {
parsedTx.contractCall = responseTx.contract_call;
if (responseTx.contract_call?.function_name === 'transfer') {
if (responseTx.post_conditions && responseTx.post_conditions.length > 0) {
parsedTx.tokenType = responseTx.post_conditions.find((x) => x !== undefined)?.type;
parsedTx.amount = new BigNumber(responseTx.post_conditions.find((x) => x !== undefined)?.amount ?? 0);
parsedTx.tokenName = responseTx.post_conditions.find((x) => x !== undefined)?.asset.asset_name;
if (responseTx.post_conditions.find((x) => x !== undefined)?.asset_value)
parsedTx.assetId = responseTx.post_conditions.find((x) => x !== undefined)?.asset_value?.repr.substring(1);
if (
responseTx.contract_call?.function_name === 'transfer' &&
responseTx.post_conditions &&
responseTx.post_conditions.length > 0
) {
const firstPostCondition = responseTx.post_conditions.find(Boolean);
parsedTx.tokenType = firstPostCondition?.type;
parsedTx.amount = new BigNumber(firstPostCondition?.amount ?? 0);
parsedTx.tokenName = firstPostCondition?.asset?.asset_name;
if (firstPostCondition?.asset_value) {
parsedTx.assetId = firstPostCondition.asset_value.repr?.substring(1);
}
}
}
Expand Down
31 changes: 29 additions & 2 deletions tests/api/helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { describe, expect, it } from 'vitest';
import { getUniquePendingTx } from 'api/helper';
import { StxMempoolTransactionData, StxTransactionData } from 'types/*';
import { getUniquePendingTx, parseStxTransactionData } from 'api/helper';
import {
StxMempoolTransactionData,
StxMempoolTransactionDataResponse,
StxTransactionData,
StxTransactionDataResponse,
} from 'types/*';
import { TransactionType } from 'types/api/shared/transaction';

describe('getUniquePendingTx', () => {
[
Expand Down Expand Up @@ -66,3 +72,24 @@ describe('getUniquePendingTx', () => {
});
});
});

describe('parseStxTransactionData', () => {
it('does not throw if post condition has no asset', () => {
const inputs = {
responseTx: {
tx_type: 'contract_call' as TransactionType,
contract_call: {
function_name: 'transfer',
},
post_conditions: [
{
type: '',
amount: 0,
},
],
} as unknown as StxTransactionDataResponse,
stxAddress: 'address1',
};
expect(() => parseStxTransactionData(inputs)).not.toThrow();
});
});
3 changes: 2 additions & 1 deletion types/api/shared/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export type TransactionType =
| 'poison_microblock'
| 'bitcoin'
| 'unsupported'
| 'brc20';
| 'brc20'
| 'message_sign';

export type TransactionStatus =
| 'pending'
Expand Down

0 comments on commit f1cd2da

Please sign in to comment.