Skip to content

Commit

Permalink
Fix lint and test
Browse files Browse the repository at this point in the history
  • Loading branch information
sleyter93 committed Jun 23, 2023
1 parent 4aa1137 commit 16a10c9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
22 changes: 11 additions & 11 deletions src/controller/httpsAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,25 @@ export class HttpsAPI {
* and to or from params should be our address when we send or receive a cryptocurrency
* (such as RBTC).
*/
const transactions: {data: IApiTransactions[], prev: string, next: string} =
const transactions: {data: IApiTransactions[], prev: string, next: string} =
await dataSource.getTransactionsByAddress(address, limit as string,
prev as string, next as string, blockNumber as string)
.catch(( () => {[]} ))
.catch(nextFunction)
/* We query events to find transactions when we send or receive a token(ERC20)
* such as RIF,RDOC
* Additionally, we query internal transactions because we could send or receive a cryptocurrency
* invoking a smart contract.
* Finally, we filter by blocknumber and duplicates
*/
const hashes: string[] = await Promise.all([dataSource.getEventsByAddress(address, limit as string),
const hashes: string[] = await Promise.all([dataSource.getEventsByAddress(address, limit as string),
dataSource.getInternalTransactionByAddress(address, limit as string)])
.then((promises) =>
.then((promises) =>
promises.flat()
.filter((value: IEvent | IInternalTransaction) =>
isMyTransaction(value, address) && value.blockNumber >= + blockNumber)
.filter((value: IEvent | IInternalTransaction) => !transactions.data.map(tx => tx.hash)
.includes(value.transactionHash))
.map((value: IEvent | IInternalTransaction) => value.transactionHash)
.filter((value: IEvent | IInternalTransaction) =>
isMyTransaction(value, address) && value.blockNumber >= +blockNumber)
.filter((value: IEvent | IInternalTransaction) => !transactions.data.map(tx => tx.hash)
.includes(value.transactionHash))
.map((value: IEvent | IInternalTransaction) => value.transactionHash)
)
.then((hashes: string[]) => Array.from(new Set(hashes)))
.catch(() => [])
Expand All @@ -102,8 +102,8 @@ export class HttpsAPI {
next: transactions.next,
data: [...transactions.data, ...result]
})
.then(this.responseJsonOk(res))
.catch(nextFunction)
.then(this.responseJsonOk(res))
.catch(nextFunction)
}

)
Expand Down
15 changes: 6 additions & 9 deletions src/service/transaction/transactionProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DataSource } from '../../repository/DataSource'
import { IEvent } from '../../rskExplorerApi/types'
import type { Event } from '../../types/event'
import { PollingProvider } from '../AbstractPollingProvider'
import { isMyTransaction } from './utils'
Expand All @@ -17,18 +16,16 @@ export class TransactionProvider extends PollingProvider<Event> {
this.dataSource.getEventsByAddress(this.address.toLowerCase()),
this.dataSource.getInternalTransactionByAddress(this.address.toLowerCase())
])
.then(promises =>
.then(promises =>
promises.flat()
.filter(transaction => isMyTransaction(transaction, address))
.map(transaction => transaction.transactionHash)
)
.then((hashes: string[]) => Array.from(new Set(hashes)))
.catch(() => [])
.filter(transaction => isMyTransaction(transaction, address))
.map(transaction => transaction.transactionHash)
)
.then((hashes: string[]) => Array.from(new Set(hashes)))
.catch(() => [])


return await Promise.all(hashes
.map(hash => this.dataSource.getTransaction(hash)))

}

async getTransactionsPaginated (address: string, limit?: string, prev?: string, next?: string) {
Expand Down
8 changes: 4 additions & 4 deletions src/service/transaction/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { IEvent, IInternalTransaction } from '../../rskExplorerApi/types'

export function isMyTransaction (event: IEvent | IInternalTransaction, address: string) {
if("args" in event) {
if ('args' in event) {
return event.args.some((arg) => arg.toLowerCase() === address.toLowerCase())
}
if("action" in event) {
return event.action.from.toLowerCase() === address.toLowerCase()
|| event.action.to.toLowerCase() === address.toLowerCase()
if ('action' in event) {
return event.action.from.toLowerCase() === address.toLowerCase() ||
event.action.to.toLowerCase() === address.toLowerCase()
}
}
4 changes: 3 additions & 1 deletion test/address.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ const getEventsByAddressMock = jest.fn(() => Promise.resolve(eventResponse))
const getTransactionsByAddressMock = jest.fn(() => Promise.resolve(transactionResponse))
const getTokensByAddressMock = jest.fn(() => Promise.resolve(tokenResponse))
const getTransactionMock = jest.fn(() => Promise.resolve(transactionFromEventResponse))
const getInternalTransactionByAddressMock = jest.fn(() => Promise.resolve({data:[]}))

Check failure on line 29 in test/address.test.ts

View workflow job for this annotation

GitHub Actions / test

A space is required after '{'

Check failure on line 29 in test/address.test.ts

View workflow job for this annotation

GitHub Actions / test

Missing space before value for key 'data'

Check failure on line 29 in test/address.test.ts

View workflow job for this annotation

GitHub Actions / test

A space is required before '}'
const rskExplorerApiMock = {
getEventsByAddress: getEventsByAddressMock,
getTransactionsByAddress: getTransactionsByAddressMock,
getTokensByAddress: getTokensByAddressMock,
getTransaction: getTransactionMock
getTransaction: getTransactionMock,
getInternalTransactionByAddress: getInternalTransactionByAddressMock
} as any
const dataSourceMapping = {}
dataSourceMapping['31'] = rskExplorerApiMock
Expand Down

0 comments on commit 16a10c9

Please sign in to comment.