Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: guard against read properties of undefined errors in stx helper function #213

Merged
merged 5 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading