Skip to content

Commit d4af92f

Browse files
committed
fix: remove redundant, verbose in query cache
1 parent 775e7b5 commit d4af92f

File tree

2 files changed

+112
-2
lines changed

2 files changed

+112
-2
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { AddressTransactionsWithTransfersListResponse } from '@stacks/stacks-blockchain-api-types';
2+
import { describe, expect, it } from 'vitest';
3+
4+
import { filterVerboseUnusedTransactionWithTransferData } from './transactions-with-transfers.query';
5+
6+
describe('filterVerboseUnusedTransactionWithTransferData', () => {
7+
it('should redact the source_code for smart_contract transactions', () => {
8+
const input = {
9+
results: [
10+
{
11+
tx: {
12+
tx_type: 'smart_contract',
13+
smart_contract: {
14+
source_code: 'some_source_code',
15+
},
16+
},
17+
},
18+
],
19+
} as AddressTransactionsWithTransfersListResponse;
20+
21+
const result = filterVerboseUnusedTransactionWithTransferData(input as any) as any;
22+
expect(result.results[0].tx?.smart_contract?.source_code).toBe('redacted');
23+
});
24+
25+
it('should redact function_args and tx_result for contract_call transactions', () => {
26+
const input = {
27+
results: [
28+
{
29+
tx: {
30+
tx_type: 'contract_call',
31+
contract_call: {
32+
function_args: [
33+
{ hex: '0x123', repr: 'arg1' },
34+
{ hex: '0x456', repr: 'arg2' },
35+
],
36+
},
37+
tx_result: { hex: '0xresult', repr: 'result_repr' },
38+
},
39+
},
40+
],
41+
} as AddressTransactionsWithTransfersListResponse;
42+
43+
const result = filterVerboseUnusedTransactionWithTransferData(input) as any;
44+
45+
expect(result.results[0].tx.contract_call?.function_args).toEqual([
46+
{ hex: 'redacted', repr: 'redacted' },
47+
{ hex: 'redacted', repr: 'redacted' },
48+
]);
49+
expect(result.results[0].tx.tx_result).toEqual({ hex: 'redacted', repr: 'redacted' });
50+
});
51+
52+
it('should handle contract_call transactions without function_args', () => {
53+
const input = {
54+
results: [
55+
{
56+
tx: {
57+
tx_type: 'contract_call',
58+
contract_call: {
59+
function_args: [],
60+
contract_id: '',
61+
function_name: '',
62+
function_signature: '',
63+
},
64+
tx_result: { hex: '0xresult', repr: 'result_repr' },
65+
},
66+
},
67+
],
68+
} as unknown as AddressTransactionsWithTransfersListResponse;
69+
70+
const result = filterVerboseUnusedTransactionWithTransferData(input);
71+
72+
// function_args should remain undefined and tx_result should be redacted
73+
expect(result.results[0].tx.tx_result).toEqual({ hex: 'redacted', repr: 'redacted' });
74+
});
75+
76+
it('should not modify transactions that are not smart_contract or contract_call type', () => {
77+
const input = {
78+
results: [{ tx: { tx_type: 'transfer' } }],
79+
} as unknown as AddressTransactionsWithTransfersListResponse;
80+
81+
const result = filterVerboseUnusedTransactionWithTransferData(input);
82+
83+
expect(result.results[0].tx).toEqual({ tx_type: 'transfer' });
84+
});
85+
});

packages/query/src/stacks/transactions/transactions-with-transfers.query.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AddressTransactionsWithTransfersListResponse } from '@stacks/stacks-blockchain-api-types';
12
import { QueryFunctionContext, useQuery } from '@tanstack/react-query';
23

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

16+
export function filterVerboseUnusedTransactionWithTransferData(
17+
resp: AddressTransactionsWithTransfersListResponse
18+
) {
19+
const parsedResults = resp.results.map(result => {
20+
if (result.tx.tx_type === 'smart_contract')
21+
result.tx.smart_contract = { ...result.tx.smart_contract, source_code: 'redacted' };
22+
23+
if (result.tx.tx_type === 'contract_call' && result.tx.contract_call.function_args) {
24+
result.tx.contract_call.function_args = result.tx.contract_call.function_args.map(fnArgs => ({
25+
...fnArgs,
26+
hex: 'redacted',
27+
repr: 'redacted',
28+
}));
29+
result.tx.tx_result = { ...result.tx.tx_result, hex: 'redacted', repr: 'redacted' };
30+
}
31+
return result;
32+
});
33+
return { ...resp, results: parsedResults };
34+
}
35+
1536
interface CreateGetAccountTransactionsWithTransfersQueryOptionsArgs {
1637
address: string;
1738
client: StacksClient;
@@ -25,8 +46,12 @@ export function createGetAccountTransactionsWithTransfersQueryOptions({
2546
return {
2647
enabled: !!address && !!network,
2748
queryKey: [StacksQueryPrefixes.GetAccountTxsWithTransfers, address, network],
28-
queryFn: ({ signal }: QueryFunctionContext) =>
29-
client.getAccountTransactionsWithTransfers(address, signal),
49+
queryFn: async ({ signal }: QueryFunctionContext) => {
50+
const resp = await client.getAccountTransactionsWithTransfers(address, signal);
51+
// transactions_with_transfers is deprecated. When removing this **make
52+
// sure that the filtering is used on the new endpoints**
53+
return filterVerboseUnusedTransactionWithTransferData(resp);
54+
},
3055
...queryOptions,
3156
} as const;
3257
}

0 commit comments

Comments
 (0)