Skip to content

Instantly share code, notes, and snippets.

@wilsonianb
Last active June 29, 2018 19:37
Show Gist options
  • Save wilsonianb/20114824c787efb37ec7ccb2b541da52 to your computer and use it in GitHub Desktop.
Save wilsonianb/20114824c787efb37ec7ccb2b541da52 to your computer and use it in GitHub Desktop.
const RippleAPI = require('ripple-lib').RippleAPI;
const api = new RippleAPI({
server: 'wss://s2.ripple.com'
});
async function getChannelHistory(previousTxnID) {
let prevId = previousTxnID
let txs = []
while (1) {
const rawTx = await api.request('tx', {
transaction: prevId
})
tx = {
id: rawTx.hash,
account: rawTx.Account,
type: rawTx.TransactionType,
result: rawTx.meta.TransactionResult
}
for (let node of rawTx.meta.AffectedNodes) {
let finalFields
if (node.ModifiedNode &&
node.ModifiedNode.LedgerEntryType === 'PayChannel') {
finalFields = node.ModifiedNode.FinalFields
prevId = node.ModifiedNode.PreviousTxnID
} else if (node.CreatedNode &&
node.CreatedNode.LedgerEntryType === 'PayChannel') {
finalFields = node.CreatedNode.NewFields
} else {
continue
}
tx.final_fields = {
amount: finalFields.Amount,
balance: finalFields.Balance,
expiration: finalFields.Expiration
}
break
}
txs.unshift(tx)
if (rawTx.TransactionType==='PaymentChannelCreate')
break
}
return txs
}
async function getPaymentChannels(account) {
const resp = await api.getAccountObjects(account, {
type: 'payment_channel'
})
let channels = {}
for (let channel of resp.account_objects) {
channels[channel.index] = {
account: channel.Account,
destination: channel.Destination,
public_key: channel.PublicKey,
amount: channel.Amount,
balance: channel.Balance,
settle_delay: channel.SettleDelay,
cancel_after: channel.CancelAfter,
expiration: channel.Expiration,
history: await getChannelHistory(channel.PreviousTxnID)
}
}
return channels
}
async function getXRPChannelInfo (account) {
try {
await api.connect()
const accountInfo = await api.getAccountInfo(account)
return {
account: account,
balance: accountInfo.xrpBalance,
reserve: 20 + 5 * accountInfo.ownerCount,
channels: await getPaymentChannels(account)
}
} catch(err) {
console.log(err)
}
}
getXRPChannelInfo('rEgrbBGK2V2unCFFLf8eq4s8iQUjZ4gH4F').then(info => {
console.log(JSON.stringify(info, null, 2))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment