Skip to content

Instantly share code, notes, and snippets.

@gnurag
Forked from WietseWind/index.js
Created December 14, 2021 19:48
Show Gist options
  • Save gnurag/e398656bdf162e7ddef2aa9ab1698641 to your computer and use it in GitHub Desktop.
Save gnurag/e398656bdf162e7ddef2aa9ab1698641 to your computer and use it in GitHub Desktop.
account_objects (type: state) to account_lines responses: get lines based on reserve claim
const { XrplClient } = require('xrpl-client')
const client = new XrplClient()
const myAccount = 'rwietsevLFg8XSmG3bEZzFein1g8RBqWDZ'
const objectFlags = {
lsfLowReserve: 0x00010000,
lsfHighReserve: 0x00020000,
lsfLowNoRipple: 0x00100000,
lsfHighNoRipple: 0x00200000
}
/**
* This function returns account_lines line results
* based on account_objects (type = state) results,
* » Returns only the account_lines to show based on:
* - Counts towards your reserve
*/
const accountObjectsToAccountLines = (accountObjectArray, suppressIncoming) => {
const notInDefaultState = accountObjectArray.filter(obj => {
return obj.Flags & objectFlags[obj.HighLimit.issuer === myAccount ? 'lsfHighReserve' : 'lsfLowReserve']
})
const accountLinesFormatted = notInDefaultState.map(obj => {
const parties = [obj.HighLimit, obj.LowLimit]
const [self, counterparty] = obj.HighLimit.issuer === myAccount
? parties
: parties.reverse()
const ripplingFlags = [(objectFlags.lsfHighNoRipple & obj.Flags) == objectFlags.lsfHighNoRipple, (objectFlags.lsfLowNoRipple & obj.Flags) == objectFlags.lsfLowNoRipple]
const [no_ripple, no_ripple_peer] = obj.HighLimit.issuer === myAccount
? ripplingFlags
: ripplingFlags.reverse()
const balance = obj.Balance.value === '0'
? obj.Balance.value
: obj.Balance.value.slice(1)
return {
account: counterparty.issuer,
balance,
currency: self.currency,
limit: self.value,
limit_peer: counterparty.value,
no_ripple,
no_ripple_peer
}
})
return accountLinesFormatted.filter(l => {
if (suppressIncoming) {
if (l.limit === '0' && (l.balance === '0' || l.balance.slice(0, 1) === '-')) {
return false
}
}
return true
})
}
const main = async () => {
// Note: can be paged, first get all (marker), then pass to accountObjectsToAccountLines fn:
const query = await client.send({
command: 'account_objects',
account: myAccount,
type: 'state'
})
console.log(accountObjectsToAccountLines(query.account_objects, false))
client.close()
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment