UNPKG

@bithomp/xrpl-api

Version:

A Bithomp JavaScript/TypeScript library for interacting with the XRP Ledger

107 lines (106 loc) 4.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseNFTokenOfferChanges = parseNFTokenOfferChanges; const ledger_1 = require("../../models/ledger"); const common_1 = require("../../common"); function parseNFTokenOfferChanges(tx) { return new NFTokenOfferChanges(tx).call(); } class NFTokenOfferChanges { constructor(tx) { this.tx = tx; this.changes = {}; } call() { if (this.hasAffectedNodes() === false) { return this.changes; } for (const affectedNode of this.tx.meta.AffectedNodes) { this.parseAffectedNode(affectedNode); } return this.changes; } addChange(account, change) { if (!this.changes[account]) { this.changes[account] = []; } this.changes[account].push((0, common_1.removeUndefined)(change)); } hasAffectedNodes() { if (this.tx.meta?.AffectedNodes === undefined) { return false; } if (this.tx.meta?.AffectedNodes?.length === 0) { return false; } return true; } parseAffectedNode(affectedNode) { if (this.isNFTokensCreateOfferNode(affectedNode)) { this.parseNFTokensCreateOfferNode(affectedNode); } else if (this.isNFTokensDeleteOfferNode(affectedNode)) { this.parseNFTokensDeleteOfferNode(affectedNode); } } isNFTokensCreateOfferNode(affectedNode) { return affectedNode.CreatedNode?.LedgerEntryType === "NFTokenOffer" && affectedNode.CreatedNode?.NewFields; } parseNFTokensCreateOfferNode(affectedNode) { const status = "created"; const amount = affectedNode.CreatedNode.NewFields.Amount; const flags = affectedNode.CreatedNode.NewFields.Flags; const nftokenID = affectedNode.CreatedNode.NewFields.NFTokenID; const owner = affectedNode.CreatedNode.NewFields.Owner; const index = affectedNode.CreatedNode.LedgerIndex; const destination = affectedNode.CreatedNode.NewFields.Destination; const previousTxnID = affectedNode.CreatedNode.NewFields.PreviousTxnID; const previousTxnLgrSeq = affectedNode.CreatedNode.NewFields.PreviousTxnLgrSeq; let expiration = affectedNode.CreatedNode.NewFields.Expiration; if (typeof expiration === "number") { expiration = (0, ledger_1.ledgerTimeToUnixTime)(expiration); } this.addChange(this.tx.Account, { status, amount, flags, nftokenID, owner, destination, expiration, index, previousTxnID, previousTxnLgrSeq, }); } isNFTokensDeleteOfferNode(affectedNode) { return affectedNode.DeletedNode?.LedgerEntryType === "NFTokenOffer" && affectedNode.DeletedNode?.FinalFields; } parseNFTokensDeleteOfferNode(affectedNode) { const status = "deleted"; const amount = affectedNode.DeletedNode.FinalFields.Amount; const flags = affectedNode.DeletedNode.FinalFields.Flags; const nftokenID = affectedNode.DeletedNode.FinalFields.NFTokenID; const owner = affectedNode.DeletedNode.FinalFields.Owner; const index = affectedNode.DeletedNode.LedgerIndex; const destination = affectedNode.DeletedNode.FinalFields.Destination; const previousTxnID = affectedNode.DeletedNode.FinalFields.PreviousTxnID; const previousTxnLgrSeq = affectedNode.DeletedNode.FinalFields.PreviousTxnLgrSeq; let expiration = affectedNode.DeletedNode.FinalFields.Expiration; if (typeof expiration === "number") { expiration = (0, ledger_1.ledgerTimeToUnixTime)(expiration); } this.addChange(owner, { status, amount, flags, nftokenID, owner, destination, expiration, index, previousTxnID, previousTxnLgrSeq, }); } }