Skip to content

Instantly share code, notes, and snippets.

@shortthefomo
Last active November 24, 2025 13:36
Show Gist options
  • Select an option

  • Save shortthefomo/93db22ade141c3ceaf4faae7bf88e534 to your computer and use it in GitHub Desktop.

Select an option

Save shortthefomo/93db22ade141c3ceaf4faae7bf88e534 to your computer and use it in GitHub Desktop.
Derive Balance Change
/**
* @tx: transaction result from the XRPL
* @raddress: the raddress you want to get the balance changes for
*/
deriveBalanceChange(tx, raddress) {
let change = {}
if (tx.meta === undefined && tx.metaData === undefined) { return change }
for (let affected of (tx.meta || tx.metaData).AffectedNodes) {
let node = affected.ModifiedNode || affected.DeletedNode
if (!node) { continue }
if (node.LedgerEntryType === 'AccountRoot') {
if (node.FinalFields?.Account === raddress) {
let prev = node.PreviousFields.Balance
let final = node.FinalFields.Balance
change.XRP = new decimal(prev).sub(new decimal(final))
}
}
if (node.LedgerEntryType === 'RippleState') {
if (node.FinalFields?.LowLimit?.issuer === raddress) {
change[node.FinalFields.LowLimit.currency] = new decimal(node.PreviousFields.Balance.value).sub(new decimal(node.FinalFields.Balance.value))
}
}
}
return change == {} ? undefined : change
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment