Skip to content

Commit

Permalink
Feat: transaction note from tx origin
Browse files Browse the repository at this point in the history
Null -> undefined
  • Loading branch information
katspaugh committed Dec 27, 2024
1 parent 78e5dd6 commit 4877b81
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ export class TransactionDetails {
txHash!: string | null;
@ApiPropertyOptional({ type: SafeAppInfo, nullable: true })
safeAppInfo!: SafeAppInfo | null;
@ApiPropertyOptional({ type: String, nullable: true })
note?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const multisigExecutionDetailsMapper = jest.mocked({
mapMultisigExecutionDetails: jest.fn(),
} as jest.MockedObjectDeep<MultisigTransactionExecutionDetailsMapper>);

const multisigTransactionNoteMapper = jest.mocked({
mapTxNote: jest.fn(),
});

describe('MultisigTransactionDetails mapper (Unit)', () => {
let mapper: MultisigTransactionDetailsMapper;

Expand All @@ -51,6 +55,7 @@ describe('MultisigTransactionDetails mapper (Unit)', () => {
transactionDataMapper,
safeAppInfoMapper,
multisigExecutionDetailsMapper,
multisigTransactionNoteMapper,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { TransactionDataMapper } from '@/routes/transactions/mappers/common/tran
import { MultisigTransactionInfoMapper } from '@/routes/transactions/mappers/common/transaction-info.mapper';
import { MultisigTransactionExecutionDetailsMapper } from '@/routes/transactions/mappers/multisig-transactions/multisig-transaction-execution-details.mapper';
import { MultisigTransactionStatusMapper } from '@/routes/transactions/mappers/multisig-transactions/multisig-transaction-status.mapper';
import { MultisigTransactionNoteMapper } from '@/routes/transactions/mappers/multisig-transactions/multisig-transaction-note.mapper';

@Injectable()
export class MultisigTransactionDetailsMapper {
Expand All @@ -25,6 +26,7 @@ export class MultisigTransactionDetailsMapper {
private readonly transactionDataMapper: TransactionDataMapper,
private readonly safeAppInfoMapper: SafeAppInfoMapper,
private readonly multisigTransactionExecutionDetailsMapper: MultisigTransactionExecutionDetailsMapper,
private readonly noteMapper: MultisigTransactionNoteMapper,
) {}

async mapDetails(
Expand All @@ -33,6 +35,7 @@ export class MultisigTransactionDetailsMapper {
safe: Safe,
): Promise<TransactionDetails> {
const txStatus = this.statusMapper.mapTransactionStatus(transaction, safe);
const note = this.noteMapper.mapTxNote(transaction);
const [
isTrustedDelegateCall,
addressInfoIndex,
Expand Down Expand Up @@ -79,6 +82,7 @@ export class MultisigTransactionDetailsMapper {
txHash: transaction.transactionHash,
detailedExecutionInfo,
safeAppInfo,
note,
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { multisigTransactionBuilder } from '@/domain/safe/entities/__tests__/multisig-transaction.builder';
import { MultisigTransactionNoteMapper } from '@/routes/transactions/mappers/multisig-transactions/multisig-transaction-note.mapper';

const mapper = new MultisigTransactionNoteMapper();

describe('Multisig Transaction note mapper (Unit)', () => {
it('should parse transaction `origin` and return a note', () => {
const transaction = multisigTransactionBuilder()
.with('origin', '{"name":"{\\"note\\":\\"This is a note\\"}"}')
.build();

const note = mapper.mapTxNote(transaction);

expect(note).toBe('This is a note');
});

it('should return undefined if `origin` is not a valid JSON', () => {
const transaction = multisigTransactionBuilder()
.with('origin', 'invalid-json')
.build();

const note = mapper.mapTxNote(transaction);

expect(note).toBeUndefined();
});

it('should return undefined if `origin` does not contain a note', () => {
const transaction = multisigTransactionBuilder()
.with('origin', '{"url":"uniswap.org","name":"Uniswap"}')
.build();

const note = mapper.mapTxNote(transaction);

expect(note).toBeUndefined();
});

it('should return undefined if `origin` is null', () => {
const transaction = multisigTransactionBuilder()
.with('origin', null)
.build();

const note = mapper.mapTxNote(transaction);

expect(note).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Injectable } from '@nestjs/common';
import { MultisigTransaction } from '@/domain/safe/entities/multisig-transaction.entity';

@Injectable()
export class MultisigTransactionNoteMapper {
mapTxNote(transaction: MultisigTransaction): string | undefined {
let note: string | undefined;

if (transaction.origin) {
try {
const origin = JSON.parse(transaction.origin);
const parsedName = origin.name && JSON.parse(String(origin.name));
if (typeof parsedName.note === 'string') {
note = parsedName.note;
}
} catch {
// Ignore, no note
}
}

return note;
}
}
2 changes: 2 additions & 0 deletions src/routes/transactions/transactions.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { MultisigTransactionDetailsMapper } from '@/routes/transactions/mappers/
import { MultisigTransactionExecutionDetailsMapper } from '@/routes/transactions/mappers/multisig-transactions/multisig-transaction-execution-details.mapper';
import { MultisigTransactionExecutionInfoMapper } from '@/routes/transactions/mappers/multisig-transactions/multisig-transaction-execution-info.mapper';
import { MultisigTransactionStatusMapper } from '@/routes/transactions/mappers/multisig-transactions/multisig-transaction-status.mapper';
import { MultisigTransactionNoteMapper } from '@/routes/transactions/mappers/multisig-transactions/multisig-transaction-note.mapper';
import { MultisigTransactionMapper } from '@/routes/transactions/mappers/multisig-transactions/multisig-transaction.mapper';
import { QueuedItemsMapper } from '@/routes/transactions/mappers/queued-items/queued-items.mapper';
import { TransactionPreviewMapper } from '@/routes/transactions/mappers/transaction-preview.mapper';
Expand Down Expand Up @@ -86,6 +87,7 @@ import { Module } from '@nestjs/common';
MultisigTransactionInfoMapper,
MultisigTransactionMapper,
MultisigTransactionStatusMapper,
MultisigTransactionNoteMapper,
NativeCoinTransferMapper,
NativeStakingMapper,
QueuedItemsMapper,
Expand Down

0 comments on commit 4877b81

Please sign in to comment.