Skip to content

Commit 1b556af

Browse files
committed
verify tx already exists
1 parent 31efe4a commit 1b556af

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

src/__tests__/unit/tx-history.controller.unit.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe('TxHistoryController', () => {
1818
let rskNodeService: RskNodeService;
1919
let storeTransaction: sinon.SinonStub;
2020
let getTransactionHistoryByAddress: sinon.SinonStub;
21+
let getTransactionByHash: sinon.SinonStub;
2122
let getTx: sinon.SinonStub;
2223
let getRskTransaction: sinon.SinonStub;
2324
let context: any;
@@ -33,9 +34,11 @@ describe('TxHistoryController', () => {
3334
getRskTransaction = rskNodeService.getTransaction as sinon.SinonStub;
3435
storeTransaction = txHistoryService.storeTransaction as sinon.SinonStub;
3536
getTransactionHistoryByAddress = txHistoryService.getTransactionHistoryByAddress as sinon.SinonStub;
37+
getTransactionByHash = txHistoryService.getTransactionByHash as sinon.SinonStub;
3638

3739
getTx.resolves({});
3840
getRskTransaction.resolves({});
41+
getTransactionByHash.resolves(null);
3942

4043
controller = new TxHistoryController(
4144
bitcoinService,
@@ -115,6 +118,32 @@ describe('TxHistoryController', () => {
115118
sinon.assert.notCalled(storeTransaction);
116119
expect(response.statusCode).to.equal(200);
117120
});
121+
122+
it('should return 200 and not store when transaction already exists in database', async () => {
123+
const txHistory: TxHistory = {
124+
userAddress: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb0',
125+
txHash: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd',
126+
providerHash: 'ExistingTxHash',
127+
fromTokenName: 'BTC',
128+
fromNetworkName: 'Bitcoin',
129+
toTokenName: 'RBTC',
130+
toNetworkName: 'Rootstock',
131+
fromAmount: '0.001',
132+
toAmount: '0.0009',
133+
date: new Date('2024-01-03'),
134+
sdkProvider: 'FLYOVER',
135+
} as TxHistory;
136+
137+
getTransactionByHash.resolves(txHistory);
138+
139+
const response = await controller.storeTransaction(txHistory);
140+
141+
sinon.assert.calledOnce(getTransactionByHash);
142+
sinon.assert.notCalled(getTx);
143+
sinon.assert.notCalled(getRskTransaction);
144+
sinon.assert.notCalled(storeTransaction);
145+
expect(response.statusCode).to.equal(200);
146+
});
118147
});
119148

120149
describe('getTransactionHistory', () => {

src/controllers/tx-history.controller.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,16 @@ export class TxHistoryController {
5252
txHistory: TxHistory,
5353
): Promise<Response> {
5454
try {
55-
const transactionExists = await this.verifyTransactionExists(txHistory);
55+
const transactionExists = await this.verifyTransactionExistsOnBlockchain(txHistory);
5656
if (!transactionExists) {
5757
return this.response.status(200).send();
5858
}
5959

60+
const transactionExistsInDatabase = await this.verifyTransactionExistsInDatabase(txHistory.txHash);
61+
if (transactionExistsInDatabase) {
62+
return this.response.status(200).send();
63+
}
64+
6065
const result = await this.txHistoryService.storeTransaction(txHistory);
6166
if (result) {
6267
return this.response.status(200).send();
@@ -73,7 +78,12 @@ export class TxHistoryController {
7378
}
7479
}
7580

76-
private async verifyTransactionExists(txHistory: TxHistory): Promise<boolean> {
81+
private async verifyTransactionExistsInDatabase(txHash: string): Promise<boolean> {
82+
const transaction = await this.txHistoryService.getTransactionByHash(txHash);
83+
return !!transaction;
84+
}
85+
86+
private async verifyTransactionExistsOnBlockchain(txHistory: TxHistory): Promise<boolean> {
7787
const sourceNetwork = txHistory.fromNetworkName?.trim().toLowerCase();
7888

7989
try {
@@ -83,6 +93,8 @@ export class TxHistoryController {
8393
await this.rskNodeService.getTransaction(txHistory.txHash);
8494
}
8595

96+
97+
8698
return true;
8799
} catch (error: unknown) {
88100
const errorMessage = error instanceof Error ? error.message : String(error);

src/models/register-payload.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class RegisterPayload extends Model {
77
type: 'string',
88
required: true,
99
jsonSchema: {
10-
pattern: '^(0x[a-fA-F0-9]{64}|[a-fA-F0-9]{64}|[A-Za-z0-9]{12})$',
10+
pattern: '^(0x[a-fA-F0-9]{64}|[a-fA-F0-9]{64}|[a-zA-Z0-9]+)$',
1111
errorMessage: 'Must be a valid transaction hash or provider identifier'
1212
}
1313
})

0 commit comments

Comments
 (0)