Skip to content
Open
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
25 changes: 25 additions & 0 deletions src/modules/csv-export/v1/csv-export.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,31 @@ describe('CsvExportService', () => {
);
});

it('should stream null payment fields when fee is paid from signer wallet', async () => {
const mockTransactionExportNoFee = transactionExportBuilder()
.with('payment', null)
.with('gasToken', null)
.with('gasTokenSymbol', null)
.with('gasTokenDecimals', null)
.build();

const mockPageNoFee = pageBuilder()
.with('results', [mockTransactionExportNoFee])
.with('next', null)
.build();

mockExportApi.export.mockResolvedValueOnce(rawify(mockPageNoFee));

await service.export(exportArgs);

expect(streamData).toHaveLength(1);
expect(streamData[0]).toEqual(
transformTransactionExport(mockTransactionExportNoFee),
);
expect(streamData[0].payment).toBeNull();
expect(streamData[0].gasTokenSymbol).toBeNull();
});

it('should handle pagination with default values', async () => {
const exportArgsNoPagination = {
...exportArgs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,28 @@ export function transactionExportBuilder(): IBuilder<TransactionExport> {
.with('note', faker.lorem.sentence())
.with('transactionHash', faker.string.hexadecimal({ length: 64 }) as Hash)
.with('contractAddress', getAddress(faker.finance.ethereumAddress()))
.with('nonce', faker.number.int().toString());
.with('nonce', faker.number.int().toString())
.with('gasToken', getAddress(faker.finance.ethereumAddress()))
.with('payment', faker.number.bigInt().toString())
.with('gasTokenSymbol', faker.finance.currencyCode())
.with('gasTokenDecimals', faker.number.int({ min: 0, max: 18 }));
}

/**
* Transforms transaction export's amount field to user-friendly format
* Transforms transaction export's amount and payment and gasToken fields to user-friendly format
*/
export function transformTransactionExport(
data: TransactionExport,
): TransactionExport {
const { amount, assetDecimals, ...rest } = data;
const convertedAmount = formatUnits(BigInt(amount), assetDecimals ?? 0);
return { ...rest, amount: convertedAmount, assetDecimals };
const { amount, assetDecimals, payment, gasTokenDecimals, ...rest } = data;
return {
...rest,
assetDecimals,
amount: formatUnits(BigInt(amount), assetDecimals ?? 0),
gasTokenDecimals: gasTokenDecimals ?? null,
payment:
payment != null
? formatUnits(BigInt(payment), gasTokenDecimals ?? 0)
: null,
};
}
3 changes: 3 additions & 0 deletions src/modules/csv-export/v1/entities/csv-export.options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// SPDX-License-Identifier: FSL-1.1-MIT
import type { CsvOptions } from '@/modules/csv-export/csv-utils/csv.service';

export const CSV_OPTIONS: CsvOptions = {
Expand All @@ -17,6 +18,8 @@ export const CSV_OPTIONS: CsvOptions = {
{ key: 'proposerAddress', header: 'Proposer Address' },
{ key: 'executorAddress', header: 'Executor Address' },
{ key: 'note', header: 'Note' },
{ key: 'payment', header: 'Amount Gas' },
{ key: 'gasTokenSymbol', header: 'Gas token' },
],
cast: {
date: (value: Date): string => value.toISOString(),
Expand Down
100 changes: 100 additions & 0 deletions src/modules/csv-export/v1/entities/transaction-export.entity.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: FSL-1.1-MIT
import { faker } from '@faker-js/faker';
import omit from 'lodash/omit';
import type { Address, Hex } from 'viem';
import { getAddress } from 'viem';
import { transactionExportBuilder } from '@/modules/csv-export/v1/entities/__tests__/transaction-export.builder';
Expand Down Expand Up @@ -94,6 +95,10 @@ describe('TransactionExportSchema', () => {
.with('note', null)
.with('contractAddress', null)
.with('nonce', null)
.with('gasToken', null)
.with('payment', null)
.with('gasTokenSymbol', null)
.with('gasTokenDecimals', null)
.build();

const result = TransactionExportSchema.safeParse(transactionExport);
Expand All @@ -110,6 +115,29 @@ describe('TransactionExportSchema', () => {
expect(result.data.note).toBeNull();
expect(result.data.contractAddress).toBeNull();
expect(result.data.nonce).toBeNull();
expect(result.data.gasToken).toBeNull();
expect(result.data.payment).toBeNull();
expect(result.data.gasTokenSymbol).toBeNull();
expect(result.data.gasTokenDecimals).toBeNull();
}
});

it('should parse successfully when gas fee fields are absent (backwards compatibility)', () => {
const rawData = omit(transactionExportBuilder().build(), [
'gasToken',
'payment',
'gasTokenSymbol',
'gasTokenDecimals',
]);

const result = TransactionExportSchema.safeParse(rawData);

expect(result.success).toBe(true);
if (result.success) {
expect(result.data.gasToken).toBeUndefined();
expect(result.data.payment).toBeNull();
expect(result.data.gasTokenSymbol).toBeUndefined();
expect(result.data.gasTokenDecimals).toBeNull();
}
});

Expand Down Expand Up @@ -282,4 +310,76 @@ describe('TransactionExportSchema', () => {
received: 'Invalid Date',
});
});

it('should transform payment using gasTokenDecimals', () => {
const rawPayment = '1000000000000000000'; // 1 token in wei
const expectedPayment = '1';

const transactionExport = transactionExportBuilder()
.with('payment', rawPayment)
.with('gasTokenDecimals', 18)
.build();

const result = TransactionExportSchema.safeParse(transactionExport);

expect(result.success).toBe(true);
expect(result.data?.payment).toBe(expectedPayment);
});

it('should set payment to null when payment is null', () => {
const transactionExport = transactionExportBuilder()
.with('payment', null)
.with('gasTokenDecimals', null)
.build();

const result = TransactionExportSchema.safeParse(transactionExport);

expect(result.success).toBe(true);
expect(result.data?.payment).toBeNull();
});

it('should not transform payment when gasTokenDecimals is null (defaults to 0)', () => {
const rawPayment = '100';
const expectedPayment = '100';

const transactionExport = transactionExportBuilder()
.with('payment', rawPayment)
.with('gasTokenDecimals', null)
.build();

const result = TransactionExportSchema.safeParse(transactionExport);

expect(result.success).toBe(true);
expect(result.data?.payment).toBe(expectedPayment);
});

it('should validate gasToken as a valid address', () => {
const transactionExport = transactionExportBuilder()
.with('gasToken', '0x123' as Address)
.build();

const result = TransactionExportSchema.safeParse(transactionExport);

expect(!result.success && result.error.issues.length).toBe(1);
expect(result.error?.issues[0]).toStrictEqual({
code: 'custom',
message: 'Invalid address',
path: ['gasToken'],
});
});

it('should validate payment as numeric string', () => {
const transactionExport = transactionExportBuilder()
.with('payment', 'not-numeric')
.build();

const result = TransactionExportSchema.safeParse(transactionExport);

expect(!result.success && result.error.issues.length).toBe(1);
expect(result.error?.issues[0]).toStrictEqual({
code: 'custom',
message: 'Invalid base-10 numeric string',
path: ['payment'],
});
});
});
21 changes: 16 additions & 5 deletions src/modules/csv-export/v1/entities/transaction-export.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,23 @@ export const TransactionExportSchema = z
transactionHash: HexSchema,
contractAddress: AddressSchema.nullable(),
nonce: z.string().nullable(),
gasToken: AddressSchema.nullish(),
payment: NumericStringSchema.nullish(),
gasTokenSymbol: z.string().nullish(),
gasTokenDecimals: z.number().nullish(),
})
.transform(({ amount, assetDecimals, ...rest }) => ({
...rest,
assetDecimals,
amount: formatUnits(BigInt(amount), assetDecimals ?? 0),
}));
.transform(
({ amount, assetDecimals, payment, gasTokenDecimals, ...rest }) => ({
...rest,
assetDecimals,
amount: formatUnits(BigInt(amount), assetDecimals ?? 0),
gasTokenDecimals: gasTokenDecimals ?? null,
payment:
payment != null
? formatUnits(BigInt(payment), gasTokenDecimals ?? 0)
: null,
}),
);

export const TransactionExportPageSchema = buildPageSchema(
TransactionExportSchema,
Expand Down