api/lib/tx-to-rest-converter.js
function parseOrderFromTx(tx, options) {
  if (!options.account) {
    throw new Error('Internal Error. must supply options.account');
  }
  if (tx.TransactionType !== 'OfferCreate'
      && tx.TransactionType !== 'OfferCancel') {
    throw new Error('Invalid parameter: identifier. The transaction '
      + 'corresponding to the given identifier is not an order');
  }
  if (tx.meta !== undefined && tx.meta.TransactionResult !== undefined) {
    if (tx.meta.TransactionResult === 'tejSecretInvalid') {
      throw new Error('Invalid secret provided.');
    }
  }

  var order;
  var flags = parseFlagsFromResponse(tx.flags, constants.OfferCreateFlags);
  var action = tx.TransactionType === 'OfferCreate'
    ? 'order_create' : 'order_cancel';
  var balance_changes = tx.meta
    ? parseBalanceChanges(tx.meta)[options.account] || [] : [];
  var timestamp = tx.date
    ? new Date(ripple.utils.toTimestamp(tx.date)).toISOString() : '';
  var order_changes = tx.meta ?
    parseOrderBookChanges(tx.meta)[options.account] : [];

  var direction;
  if (options.account === tx.Account) {
    direction = 'outgoing';
  } else if (balance_changes.length && order_changes.length) {
    direction = 'incoming';
  } else {
    direction = 'passthrough';
  }

  if (action === 'order_create') {
    order = {
      account: tx.Account,
      taker_pays: utils.parseCurrencyAmount(tx.TakerPays),
      taker_gets: utils.parseCurrencyAmount(tx.TakerGets),
      passive: flags.passive,
      immediate_or_cancel: flags.immediate_or_cancel,
      fill_or_kill: flags.fill_or_kill,
      type: flags.sell ? 'sell' : 'buy',
      sequence: tx.Sequence
    };
  } else {
    order = {
      account: tx.Account,
      type: 'cancel',
      sequence: tx.Sequence,
      cancel_sequence: tx.OfferSequence
    };
  }

  return {
    hash: tx.hash,
    ledger: tx.ledger_index,
    validated: tx.validated,
    timestamp: timestamp,
    fee: utils.dropsToXrp(tx.Fee),
    action: action,
    direction: direction,
    order: order,
    balance_changes: balance_changes,
    order_changes: order_changes || []
  };
}
function parseOrderFromTx(tx, options) {
  if (!options.account) {
    throw new Error('Internal Error. must supply options.account');
  }
  if (tx.TransactionType !== 'OfferCreate'
      && tx.TransactionType !== 'OfferCancel') {
    throw new Error('Invalid parameter: identifier. The transaction '
      + 'corresponding to the given identifier is not an order');
  }
  if (tx.meta !== undefined && tx.meta.TransactionResult !== undefined) {
    if (tx.meta.TransactionResult === 'tejSecretInvalid') {
      throw new Error('Invalid secret provided.');
    }
  }

  var order;
  var flags = parseFlagsFromResponse(tx.flags, constants.OfferCreateFlags);
  var action = tx.TransactionType === 'OfferCreate'
    ? 'order_create' : 'order_cancel';
  var balance_changes = tx.meta
    ? parseBalanceChanges(tx.meta)[options.account] || [] : [];
  var timestamp = tx.date
    ? new Date(ripple.utils.toTimestamp(tx.date)).toISOString() : '';
  var order_changes = tx.meta ?
    parseOrderBookChanges(tx.meta)[options.account] : [];

  var direction;
  if (options.account === tx.Account) {
    direction = 'outgoing';
  } else if (balance_changes.length && order_changes.length) {
    direction = 'incoming';
  } else {
    direction = 'passthrough';
  }

  if (action === 'order_create') {
    order = {
      account: tx.Account,
      taker_pays: utils.parseCurrencyAmount(tx.TakerPays),
      taker_gets: utils.parseCurrencyAmount(tx.TakerGets),
      passive: flags.passive,
      immediate_or_cancel: flags.immediate_or_cancel,
      fill_or_kill: flags.fill_or_kill,
      type: flags.sell ? 'sell' : 'buy',
      sequence: tx.Sequence
    };
  } else {
    order = {
      account: tx.Account,
      type: 'cancel',
      sequence: tx.Sequence,
      cancel_sequence: tx.OfferSequence
    };
  }

  return {
    hash: tx.hash,
    ledger: tx.ledger_index,
    validated: tx.validated,
    timestamp: timestamp,
    fee: utils.dropsToXrp(tx.Fee),
    action: action,
    direction: direction,
    order: order,
    balance_changes: balance_changes,
    order_changes: order_changes || []
  };
}
function parseOrderFromTx ( tx , options ) { if ( ! options . account ) { throw new Error ( 'Internal Error. must supply options.account' ) ; } if ( tx . TransactionType !== 'OfferCreate' && tx . TransactionType !== 'OfferCancel' ) { throw new Error ( 'Invalid parameter: identifier. The transaction ' + 'corresponding to the given identifier is not an order' ) ; } if ( tx . meta !== undefined && tx . meta . TransactionResult !== undefined ) { if ( tx . meta . TransactionResult === 'tejSecretInvalid' ) { throw new Error ( 'Invalid secret provided.' ) ; } } var order ; var flags = parseFlagsFromResponse ( tx . flags , constants . OfferCreateFlags ) ; var action = tx . TransactionType === 'OfferCreate' ? 'order_create' : 'order_cancel' ; var balance_changes = tx . meta ? parseBalanceChanges ( tx . meta ) [ options . account ] || [ ] : [ ] ; var timestamp = tx . date ? new Date ( ripple . utils . toTimestamp ( tx . date ) ) . toISOString ( ) : '' ; var order_changes = tx . meta ? parseOrderBookChanges ( tx . meta ) [ options . account ] : [ ] ; var direction ; if ( options . account === tx . Account ) { direction = 'outgoing' ; } else if ( balance_changes . length && order_changes . length ) { direction = 'incoming' ; } else { direction = 'passthrough' ; } if ( action === 'order_create' ) { order = { account : tx . Account , taker_pays : utils . parseCurrencyAmount ( tx . TakerPays ) , taker_gets : utils . parseCurrencyAmount ( tx . TakerGets ) , passive : flags . passive , immediate_or_cancel : flags . immediate_or_cancel , fill_or_kill : flags . fill_or_kill , type : flags . sell ? 'sell' : 'buy' , sequence : tx . Sequence } ; } else { order = { account : tx . Account , type : 'cancel' , sequence : tx . Sequence , cancel_sequence : tx . OfferSequence } ; } return { hash : tx . hash , ledger : tx . ledger_index , validated : tx . validated , timestamp : timestamp , fee : utils . dropsToXrp ( tx . Fee ) , action : action , direction : direction , order : order , balance_changes : balance_changes , order_changes : order_changes || [ ] } ; }
Convert an OfferCreate or OfferCancel transaction in rippled tx format
to a ripple-rest order_change

@param {Object} tx
@param {Object} options
@param {String} options.account - The account to use as perspective when
parsing the transaction.

@returns {Promise.<Object,Error>} - resolves to a parsed OrderChange
transaction or an Error
Convert an OfferCreate or OfferCancel transaction in rippled tx format to a ripple - rest order_change