Skip to content

Instantly share code, notes, and snippets.

@shortthefomo
Last active April 12, 2022 02:12
Show Gist options
  • Save shortthefomo/8847411aa75085002297f1d51b868400 to your computer and use it in GitHub Desktop.
Save shortthefomo/8847411aa75085002297f1d51b868400 to your computer and use it in GitHub Desktop.
derive offer off any offer that partially / fully consumed a transaction
const deriveOffer = async (tx, address) => {
let hash = tx.hash || tx.transaction.hash
let taker = tx.Account || tx.transaction.Account
for(let affected of (tx.meta || tx.metaData).AffectedNodes){
let node = affected.ModifiedNode || affected.DeletedNode || affected.CreatedNode
if(!node || node.LedgerEntryType !== 'Offer')
continue
if(node.NewFields)
return hash
if(!node.PreviousTxnID && !node.FinalFields.PreviousTxnID)
continue
let maker = node.FinalFields.Account
if(maker !== address && taker !== address)
continue
let previousTxnID = (!node.PreviousTxnID) ? node.FinalFields.PreviousTxnID : node.PreviousTxnID
let request = {
"id": 1,
"command": "tx",
"transaction": previousTxnID,
"binary": false
}
let ledger_result = await client.send(request)
return deriveOffer(ledger_result, address)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment