operations/create_passive_sell_offer.js

  1. import xdr from '../xdr';
  2. /**
  3. * Returns a XDR CreatePasiveSellOfferOp. A "create passive offer" operation creates an
  4. * offer that won't consume a counter offer that exactly matches this offer. This is
  5. * useful for offers just used as 1:1 exchanges for path payments. Use manage offer
  6. * to manage this offer after using this operation to create it.
  7. * @function
  8. * @alias Operation.createPassiveSellOffer
  9. * @param {object} opts Options object
  10. * @param {Asset} opts.selling - What you're selling.
  11. * @param {Asset} opts.buying - What you're buying.
  12. * @param {string} opts.amount - The total amount you're selling. If 0, deletes the offer.
  13. * @param {number|string|BigNumber|Object} opts.price - Price of 1 unit of `selling` in terms of `buying`.
  14. * @param {number} opts.price.n - If `opts.price` is an object: the price numerator
  15. * @param {number} opts.price.d - If `opts.price` is an object: the price denominator
  16. * @param {string} [opts.source] - The source account (defaults to transaction source).
  17. * @throws {Error} Throws `Error` when the best rational approximation of `price` cannot be found.
  18. * @returns {xdr.CreatePassiveSellOfferOp} Create Passive Sell Offer operation
  19. */
  20. export function createPassiveSellOffer(opts) {
  21. const attributes = {};
  22. attributes.selling = opts.selling.toXDRObject();
  23. attributes.buying = opts.buying.toXDRObject();
  24. if (!this.isValidAmount(opts.amount)) {
  25. throw new TypeError(this.constructAmountRequirementsError('amount'));
  26. }
  27. attributes.amount = this._toXDRAmount(opts.amount);
  28. if (opts.price === undefined) {
  29. throw new TypeError('price argument is required');
  30. }
  31. attributes.price = this._toXDRPrice(opts.price);
  32. const createPassiveSellOfferOp = new xdr.CreatePassiveSellOfferOp(attributes);
  33. const opAttributes = {};
  34. opAttributes.body = xdr.OperationBody.createPassiveSellOffer(
  35. createPassiveSellOfferOp
  36. );
  37. this.setSourceAccount(opAttributes, opts);
  38. return new xdr.Operation(opAttributes);
  39. }