@okxweb3/coin-stellar
Version:
@ok/coin-stellar is a Stellar SDK for building Web3 wallets and applications. It supports Stellar and PI blockchains, enabling private key management, address generation, transaction signing, trustline creation, and asset transfers
69 lines • 2.83 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LiquidityPoolAsset = void 0;
const xdr_1 = __importDefault(require("./xdr"));
const asset_1 = require("./asset");
const get_liquidity_pool_id_1 = require("./get_liquidity_pool_id");
class LiquidityPoolAsset {
constructor(assetA, assetB, fee) {
if (!assetA || !(assetA instanceof asset_1.Asset)) {
throw new Error('assetA is invalid');
}
if (!assetB || !(assetB instanceof asset_1.Asset)) {
throw new Error('assetB is invalid');
}
if (asset_1.Asset.compare(assetA, assetB) !== -1) {
throw new Error('Assets are not in lexicographic order');
}
if (!fee || fee !== get_liquidity_pool_id_1.LiquidityPoolFeeV18) {
throw new Error('fee is invalid');
}
this.assetA = assetA;
this.assetB = assetB;
this.fee = fee;
}
static fromOperation(ctAssetXdr) {
const assetType = ctAssetXdr.switch();
if (assetType === xdr_1.default.AssetType.assetTypePoolShare()) {
const liquidityPoolParameters = ctAssetXdr
.liquidityPool()
.constantProduct();
return new this(asset_1.Asset.fromOperation(liquidityPoolParameters.assetA()), asset_1.Asset.fromOperation(liquidityPoolParameters.assetB()), liquidityPoolParameters.fee());
}
throw new Error(`Invalid asset type: ${assetType.name}`);
}
toXDRObject() {
const lpConstantProductParamsXdr = new xdr_1.default.LiquidityPoolConstantProductParameters({
assetA: this.assetA.toXDRObject(),
assetB: this.assetB.toXDRObject(),
fee: this.fee
});
const lpParamsXdr = new xdr_1.default.LiquidityPoolParameters('liquidityPoolConstantProduct', lpConstantProductParamsXdr);
return new xdr_1.default.ChangeTrustAsset('assetTypePoolShare', lpParamsXdr);
}
getLiquidityPoolParameters() {
return {
...this,
assetA: this.assetA,
assetB: this.assetB,
fee: this.fee
};
}
getAssetType() {
return 'liquidity_pool_shares';
}
equals(other) {
return (this.assetA.equals(other.assetA) &&
this.assetB.equals(other.assetB) &&
this.fee === other.fee);
}
toString() {
const poolId = (0, get_liquidity_pool_id_1.getLiquidityPoolId)('constant_product', this.getLiquidityPoolParameters()).toString('hex');
return `liquidity_pool:${poolId}`;
}
}
exports.LiquidityPoolAsset = LiquidityPoolAsset;
//# sourceMappingURL=liquidity_pool_asset.js.map