Last active
April 12, 2022 02:12
-
-
Save shortthefomo/8847411aa75085002297f1d51b868400 to your computer and use it in GitHub Desktop.
derive offer off any offer that partially / fully consumed a transaction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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