mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-19 19:55:51 +00:00
Convert from Flow to Typescript (#816)
* convert to typescript * add docs for custom node typing * update webpack, gulpfile
This commit is contained in:
committed by
Elliot Lee
parent
5979ff6197
commit
8204f6c648
62
src/ledger/balances.ts
Normal file
62
src/ledger/balances.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import * as utils from './utils'
|
||||
import {validate} from '../common'
|
||||
import {Connection} from '../common'
|
||||
import {TrustlinesOptions, Trustline} from './trustlines-types'
|
||||
|
||||
|
||||
type Balance = {
|
||||
value: string,
|
||||
currency: string,
|
||||
counterparty?: string
|
||||
}
|
||||
|
||||
type GetBalances = Array<Balance>
|
||||
|
||||
function getTrustlineBalanceAmount(trustline: Trustline) {
|
||||
return {
|
||||
currency: trustline.specification.currency,
|
||||
counterparty: trustline.specification.counterparty,
|
||||
value: trustline.state.balance
|
||||
}
|
||||
}
|
||||
|
||||
function formatBalances(options, balances) {
|
||||
const result = balances.trustlines.map(getTrustlineBalanceAmount)
|
||||
if (!(options.counterparty ||
|
||||
(options.currency && options.currency !== 'XRP')
|
||||
)) {
|
||||
const xrpBalance = {
|
||||
currency: 'XRP',
|
||||
value: balances.xrp
|
||||
}
|
||||
result.unshift(xrpBalance)
|
||||
}
|
||||
if (options.limit && result.length > options.limit) {
|
||||
const toRemove = result.length - options.limit
|
||||
result.splice(-toRemove, toRemove)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
function getLedgerVersionHelper(connection: Connection, optionValue?: number
|
||||
): Promise<number> {
|
||||
if (optionValue !== undefined && optionValue !== null) {
|
||||
return Promise.resolve(optionValue)
|
||||
}
|
||||
return connection.getLedgerVersion()
|
||||
}
|
||||
|
||||
function getBalances(address: string, options: TrustlinesOptions = {}
|
||||
): Promise<GetBalances> {
|
||||
validate.getTrustlines({address, options})
|
||||
|
||||
return Promise.all([
|
||||
getLedgerVersionHelper(this.connection, options.ledgerVersion).then(
|
||||
ledgerVersion =>
|
||||
utils.getXRPBalance(this.connection, address, ledgerVersion)),
|
||||
this.getTrustlines(address, options)
|
||||
]).then(results =>
|
||||
formatBalances(options, {xrp: results[0], trustlines: results[1]}))
|
||||
}
|
||||
|
||||
export default getBalances
|
||||
Reference in New Issue
Block a user