operations/liquidity_pool_deposit.js

  1. import xdr from '../xdr';
  2. /**
  3. * Creates a liquidity pool deposit operation.
  4. *
  5. * @function
  6. * @alias Operation.liquidityPoolDeposit
  7. * @see https://developers.stellar.org/docs/start/list-of-operations/#liquidity-pool-deposit
  8. *
  9. * @param {object} opts - Options object
  10. * @param {string} opts.liquidityPoolId - The liquidity pool ID.
  11. * @param {string} opts.maxAmountA - Maximum amount of first asset to deposit.
  12. * @param {string} opts.maxAmountB - Maximum amount of second asset to deposit.
  13. * @param {number|string|BigNumber|Object} opts.minPrice - Minimum depositA/depositB price.
  14. * @param {number} opts.minPrice.n - If `opts.minPrice` is an object: the price numerator
  15. * @param {number} opts.minPrice.d - If `opts.minPrice` is an object: the price denominator
  16. * @param {number|string|BigNumber|Object} opts.maxPrice - Maximum depositA/depositB price.
  17. * @param {number} opts.maxPrice.n - If `opts.maxPrice` is an object: the price numerator
  18. * @param {number} opts.maxPrice.d - If `opts.maxPrice` is an object: the price denominator
  19. * @param {string} [opts.source] - The source account for the operation. Defaults to the transaction's source account.
  20. *
  21. * @returns {xdr.Operation} The resulting operation (xdr.LiquidityPoolDepositOp).
  22. */
  23. export function liquidityPoolDeposit(opts = {}) {
  24. const { liquidityPoolId, maxAmountA, maxAmountB, minPrice, maxPrice } = opts;
  25. const attributes = {};
  26. if (!liquidityPoolId) {
  27. throw new TypeError('liquidityPoolId argument is required');
  28. }
  29. attributes.liquidityPoolId = xdr.PoolId.fromXDR(liquidityPoolId, 'hex');
  30. if (!this.isValidAmount(maxAmountA, true)) {
  31. throw new TypeError(this.constructAmountRequirementsError('maxAmountA'));
  32. }
  33. attributes.maxAmountA = this._toXDRAmount(maxAmountA);
  34. if (!this.isValidAmount(maxAmountB, true)) {
  35. throw new TypeError(this.constructAmountRequirementsError('maxAmountB'));
  36. }
  37. attributes.maxAmountB = this._toXDRAmount(maxAmountB);
  38. if (minPrice === undefined) {
  39. throw new TypeError('minPrice argument is required');
  40. }
  41. attributes.minPrice = this._toXDRPrice(minPrice);
  42. if (maxPrice === undefined) {
  43. throw new TypeError('maxPrice argument is required');
  44. }
  45. attributes.maxPrice = this._toXDRPrice(maxPrice);
  46. const liquidityPoolDepositOp = new xdr.LiquidityPoolDepositOp(attributes);
  47. const opAttributes = {
  48. body: xdr.OperationBody.liquidityPoolDeposit(liquidityPoolDepositOp)
  49. };
  50. this.setSourceAccount(opAttributes, opts);
  51. return new xdr.Operation(opAttributes);
  52. }