Skip to content

Commit e6c5d0e

Browse files
authored
refactor: convert new TS files to async/await (#1270)
## High Level Overview of Change This PR is a fast follow from #1269 to refactor the files converted to TS to use proper async/await structure instead of `then` structure. There is no functionality change, only refactors. ### Context of Change Best practices, easier to read/write ### Type of Change - [x] Refactor (non-breaking change that only restructures code) ### Codebase Modernization Already done ;) ## Before / After No changes ## Test Plan CI passes.
1 parent ae6bb27 commit e6c5d0e

File tree

9 files changed

+503
-504
lines changed

9 files changed

+503
-504
lines changed

src/rippled/accountTransactions.ts

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface AccountTransactionsResult {
1717
marker?: any
1818
}
1919

20-
const getAccountTransactions = (
20+
const getAccountTransactions = async (
2121
account: string,
2222
currency: string | undefined,
2323
marker: any,
@@ -62,35 +62,39 @@ const getAccountTransactions = (
6262
}
6363

6464
log.info(`get transactions: ${account} -> ${classicAddress}`)
65-
return getAccountTxs(rippledSocket, classicAddress, limit, marker)
66-
.then((data) => {
67-
const transactions = data.transactions
68-
.map((tx: any) => {
69-
const txn = formatTransaction(tx)
70-
return summarize(txn, true)
71-
})
72-
.filter((tx: any) => {
73-
// No filter - return all transactions
74-
if (!currency) {
75-
return true
76-
}
65+
try {
66+
const data = await getAccountTxs(
67+
rippledSocket,
68+
classicAddress,
69+
limit,
70+
marker,
71+
)
72+
const transactions = data.transactions
73+
.map((tx: any) => {
74+
const txn = formatTransaction(tx)
75+
return summarize(txn, true)
76+
})
77+
.filter((tx: any) => {
78+
// No filter - return all transactions
79+
if (!currency) {
80+
return true
81+
}
7782

78-
// Filter by currency (IOU) or MPT issuance ID (passed as currency)
79-
const txString = JSON.stringify(tx)
80-
return (
81-
txString.includes(`"currency":"${currency.toUpperCase()}"`) ||
82-
txString.includes(`"${currency}"`)
83-
)
84-
})
85-
return {
86-
transactions,
87-
marker: data.marker,
88-
}
89-
})
90-
.catch((error: any) => {
91-
log.error(error.toString())
92-
throw error
93-
})
83+
// Filter by currency (IOU) or MPT issuance ID (passed as currency)
84+
const txString = JSON.stringify(tx)
85+
return (
86+
txString.includes(`"currency":"${currency.toUpperCase()}"`) ||
87+
txString.includes(`"${currency}"`)
88+
)
89+
})
90+
return {
91+
transactions,
92+
marker: data.marker,
93+
}
94+
} catch (error: any) {
95+
log.error(error.toString())
96+
throw error
97+
}
9498
}
9599

96100
export default getAccountTransactions

src/rippled/index.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
import getAccountState from './accountState'
2-
import getAccountTransactions from './accountTransactions'
3-
import getLedger from './ledgers'
4-
import getTransaction from './transactions'
5-
import getQuorum from './quorum'
6-
import getNegativeUNL from './nUNL'
7-
import getOffers from './offers'
8-
9-
export {
10-
getAccountState,
11-
getAccountTransactions,
12-
getLedger,
13-
getTransaction,
14-
getQuorum,
15-
getNegativeUNL,
16-
getOffers,
17-
}
1+
export { default as getAccountState } from './accountState'
2+
export { default as getAccountTransactions } from './accountTransactions'
3+
export { default as getLedger } from './ledgers'
4+
export { default as getTransaction } from './transactions'
5+
export { default as getQuorum } from './quorum'
6+
export { default as getNegativeUNL } from './nUNL'
7+
export { default as getOffers } from './offers'
188

199
export { getAccountInfo, getAMMInfoByAssets } from './lib/rippled'

src/rippled/ledgers.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import type { ExplorerXrplClient } from '../containers/shared/SocketContext'
55

66
const log = logger({ name: 'ledgers' })
77

8-
const getLedger = (
8+
const getLedger = async (
99
identifier: string | number,
1010
rippledSocket: ExplorerXrplClient,
11-
) => {
11+
): Promise<any> => {
1212
const parameters: any = {}
1313
if (!isNaN(Number(identifier))) {
1414
parameters.ledger_index = Number(identifier)
@@ -22,13 +22,13 @@ const getLedger = (
2222
}
2323

2424
log.info(`get ledger: ${JSON.stringify(parameters)}`)
25-
return getRippledLedger(rippledSocket, parameters)
26-
.then((ledger) => summarizeLedger(ledger, true))
27-
.then((data) => data)
28-
.catch((error: any) => {
29-
log.error(error.toString())
30-
throw error
31-
})
25+
try {
26+
const ledger = await getRippledLedger(rippledSocket, parameters)
27+
return summarizeLedger(ledger, true)
28+
} catch (error: any) {
29+
log.error(error.toString())
30+
throw error
31+
}
3232
}
3333

3434
export default getLedger

0 commit comments

Comments
 (0)