-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: transaction note from tx origin
Null -> undefined
- Loading branch information
Showing
6 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...outes/transactions/mappers/multisig-transactions/multisig-transaction-note.mapper.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
src/routes/transactions/mappers/multisig-transactions/multisig-transaction-note.mapper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters