Skip to content

Commit

Permalink
fix: remove redundant, verbose in query cache
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Oct 30, 2024
1 parent 775e7b5 commit d4af92f
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { AddressTransactionsWithTransfersListResponse } from '@stacks/stacks-blockchain-api-types';
import { describe, expect, it } from 'vitest';

import { filterVerboseUnusedTransactionWithTransferData } from './transactions-with-transfers.query';

describe('filterVerboseUnusedTransactionWithTransferData', () => {
it('should redact the source_code for smart_contract transactions', () => {
const input = {
results: [
{
tx: {
tx_type: 'smart_contract',
smart_contract: {
source_code: 'some_source_code',
},
},
},
],
} as AddressTransactionsWithTransfersListResponse;

const result = filterVerboseUnusedTransactionWithTransferData(input as any) as any;
expect(result.results[0].tx?.smart_contract?.source_code).toBe('redacted');
});

it('should redact function_args and tx_result for contract_call transactions', () => {
const input = {
results: [
{
tx: {
tx_type: 'contract_call',
contract_call: {
function_args: [
{ hex: '0x123', repr: 'arg1' },
{ hex: '0x456', repr: 'arg2' },
],
},
tx_result: { hex: '0xresult', repr: 'result_repr' },
},
},
],
} as AddressTransactionsWithTransfersListResponse;

const result = filterVerboseUnusedTransactionWithTransferData(input) as any;

expect(result.results[0].tx.contract_call?.function_args).toEqual([
{ hex: 'redacted', repr: 'redacted' },
{ hex: 'redacted', repr: 'redacted' },
]);
expect(result.results[0].tx.tx_result).toEqual({ hex: 'redacted', repr: 'redacted' });
});

it('should handle contract_call transactions without function_args', () => {
const input = {
results: [
{
tx: {
tx_type: 'contract_call',
contract_call: {
function_args: [],
contract_id: '',
function_name: '',
function_signature: '',
},
tx_result: { hex: '0xresult', repr: 'result_repr' },
},
},
],
} as unknown as AddressTransactionsWithTransfersListResponse;

const result = filterVerboseUnusedTransactionWithTransferData(input);

// function_args should remain undefined and tx_result should be redacted
expect(result.results[0].tx.tx_result).toEqual({ hex: 'redacted', repr: 'redacted' });
});

it('should not modify transactions that are not smart_contract or contract_call type', () => {
const input = {
results: [{ tx: { tx_type: 'transfer' } }],
} as unknown as AddressTransactionsWithTransfersListResponse;

const result = filterVerboseUnusedTransactionWithTransferData(input);

expect(result.results[0].tx).toEqual({ tx_type: 'transfer' });
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AddressTransactionsWithTransfersListResponse } from '@stacks/stacks-blockchain-api-types';
import { QueryFunctionContext, useQuery } from '@tanstack/react-query';

import { useCurrentNetworkState } from '../../leather-query-provider';
Expand All @@ -12,6 +13,26 @@ const queryOptions = {
refetchOnWindowFocus: true,
} as const;

export function filterVerboseUnusedTransactionWithTransferData(
resp: AddressTransactionsWithTransfersListResponse
) {
const parsedResults = resp.results.map(result => {
if (result.tx.tx_type === 'smart_contract')
result.tx.smart_contract = { ...result.tx.smart_contract, source_code: 'redacted' };

if (result.tx.tx_type === 'contract_call' && result.tx.contract_call.function_args) {
result.tx.contract_call.function_args = result.tx.contract_call.function_args.map(fnArgs => ({
...fnArgs,
hex: 'redacted',
repr: 'redacted',
}));
result.tx.tx_result = { ...result.tx.tx_result, hex: 'redacted', repr: 'redacted' };
}
return result;
});
return { ...resp, results: parsedResults };
}

interface CreateGetAccountTransactionsWithTransfersQueryOptionsArgs {
address: string;
client: StacksClient;
Expand All @@ -25,8 +46,12 @@ export function createGetAccountTransactionsWithTransfersQueryOptions({
return {
enabled: !!address && !!network,
queryKey: [StacksQueryPrefixes.GetAccountTxsWithTransfers, address, network],
queryFn: ({ signal }: QueryFunctionContext) =>
client.getAccountTransactionsWithTransfers(address, signal),
queryFn: async ({ signal }: QueryFunctionContext) => {
const resp = await client.getAccountTransactionsWithTransfers(address, signal);
// transactions_with_transfers is deprecated. When removing this **make
// sure that the filtering is used on the new endpoints**
return filterVerboseUnusedTransactionWithTransferData(resp);
},
...queryOptions,
} as const;
}
Expand Down

0 comments on commit d4af92f

Please sign in to comment.