Source

js-stellar-base/src/operations/clawback.js

  1. import xdr from '../xdr';
  2. import { decodeAddressToMuxedAccount } from '../util/decode_encode_muxed_account';
  3. /**
  4. * Creates a clawback operation.
  5. *
  6. * @function
  7. * @alias Operation.clawback
  8. *
  9. * @param {object} opts - Options object
  10. * @param {Asset} opts.asset - The asset being clawed back.
  11. * @param {string} opts.amount - The amount of the asset to claw back.
  12. * @param {string} opts.from - The public key of the (optionally-muxed)
  13. * account to claw back from.
  14. *
  15. * @param {string} [opts.source] - The source account for the operation.
  16. * Defaults to the transaction's source account.
  17. *
  18. * @return {xdr.ClawbackOp}
  19. *
  20. * @see https://github.com/stellar/stellar-protocol/blob/master/core/cap-0035.md#clawback-operation
  21. */
  22. export function clawback(opts) {
  23. const attributes = {};
  24. if (!this.isValidAmount(opts.amount)) {
  25. throw new TypeError(this.constructAmountRequirementsError('amount'));
  26. }
  27. attributes.amount = this._toXDRAmount(opts.amount);
  28. attributes.asset = opts.asset.toXDRObject();
  29. try {
  30. attributes.from = decodeAddressToMuxedAccount(opts.from);
  31. } catch (e) {
  32. throw new Error('from address is invalid');
  33. }
  34. const opAttributes = {
  35. body: xdr.OperationBody.clawback(new xdr.ClawbackOp(attributes))
  36. };
  37. this.setSourceAccount(opAttributes, opts);
  38. return new xdr.Operation(opAttributes);
  39. }