Source

js-stellar-base/src/liquidity_pool_asset.js

  1. import xdr from './xdr';
  2. import { Asset } from './asset';
  3. import {
  4. LiquidityPoolFeeV18,
  5. getLiquidityPoolId
  6. } from './get_liquidity_pool_id';
  7. /**
  8. * LiquidityPoolAsset class represents a liquidity pool trustline change.
  9. *
  10. * @constructor
  11. * @param {Asset} assetA – The first asset in the Pool, it must respect the rule assetA < assetB. See {@link Asset.compare} for more details on how assets are sorted.
  12. * @param {Asset} assetB – The second asset in the Pool, it must respect the rule assetA < assetB. See {@link Asset.compare} for more details on how assets are sorted.
  13. * @param {number} fee – The liquidity pool fee. For now the only fee supported is `30`.
  14. */
  15. export class LiquidityPoolAsset {
  16. constructor(assetA, assetB, fee) {
  17. if (!assetA || !(assetA instanceof Asset)) {
  18. throw new Error('assetA is invalid');
  19. }
  20. if (!assetB || !(assetB instanceof Asset)) {
  21. throw new Error('assetB is invalid');
  22. }
  23. if (Asset.compare(assetA, assetB) !== -1) {
  24. throw new Error('Assets are not in lexicographic order');
  25. }
  26. if (!fee || fee !== LiquidityPoolFeeV18) {
  27. throw new Error('fee is invalid');
  28. }
  29. this.assetA = assetA;
  30. this.assetB = assetB;
  31. this.fee = fee;
  32. }
  33. /**
  34. * Returns a liquidity pool asset object from its XDR ChangeTrustAsset object
  35. * representation.
  36. * @param {xdr.ChangeTrustAsset} ctAssetXdr - The asset XDR object.
  37. * @returns {LiquidityPoolAsset}
  38. */
  39. static fromOperation(ctAssetXdr) {
  40. const assetType = ctAssetXdr.switch();
  41. if (assetType === xdr.AssetType.assetTypePoolShare()) {
  42. const liquidityPoolParameters = ctAssetXdr
  43. .liquidityPool()
  44. .constantProduct();
  45. return new this(
  46. Asset.fromOperation(liquidityPoolParameters.assetA()),
  47. Asset.fromOperation(liquidityPoolParameters.assetB()),
  48. liquidityPoolParameters.fee()
  49. );
  50. }
  51. throw new Error(`Invalid asset type: ${assetType.name}`);
  52. }
  53. /**
  54. * Returns the `xdr.ChangeTrustAsset` object for this liquidity pool asset.
  55. *
  56. * Note: To convert from an {@link Asset `Asset`} to `xdr.ChangeTrustAsset`
  57. * please refer to the
  58. * {@link Asset.toChangeTrustXDRObject `Asset.toChangeTrustXDRObject`} method.
  59. *
  60. * @returns {xdr.ChangeTrustAsset} XDR ChangeTrustAsset object.
  61. */
  62. toXDRObject() {
  63. const lpConstantProductParamsXdr =
  64. new xdr.LiquidityPoolConstantProductParameters({
  65. assetA: this.assetA.toXDRObject(),
  66. assetB: this.assetB.toXDRObject(),
  67. fee: this.fee
  68. });
  69. const lpParamsXdr = new xdr.LiquidityPoolParameters(
  70. 'liquidityPoolConstantProduct',
  71. lpConstantProductParamsXdr
  72. );
  73. return new xdr.ChangeTrustAsset('assetTypePoolShare', lpParamsXdr);
  74. }
  75. /**
  76. * @returns {LiquidityPoolParameters} Liquidity pool parameters.
  77. */
  78. getLiquidityPoolParameters() {
  79. return {
  80. ...this,
  81. assetA: this.assetA,
  82. assetB: this.assetB,
  83. fee: this.fee
  84. };
  85. }
  86. /**
  87. * @see [Assets concept](https://developers.stellar.org/docs/glossary/assets/)
  88. * @returns {AssetType.liquidityPoolShares} asset type. Can only be `liquidity_pool_shares`.
  89. */
  90. getAssetType() {
  91. return 'liquidity_pool_shares';
  92. }
  93. /**
  94. * @param {LiquidityPoolAsset} other the LiquidityPoolAsset to compare
  95. * @returns {boolean} `true` if this asset equals the given asset.
  96. */
  97. equals(other) {
  98. return (
  99. this.assetA.equals(other.assetA) &&
  100. this.assetB.equals(other.assetB) &&
  101. this.fee === other.fee
  102. );
  103. }
  104. toString() {
  105. const poolId = getLiquidityPoolId(
  106. 'constant_product',
  107. this.getLiquidityPoolParameters()
  108. ).toString('hex');
  109. return `liquidity_pool:${poolId}`;
  110. }
  111. }