use config interface to edit offers

This commit is contained in:
woodser 2025-12-09 18:46:34 -05:00 committed by woodser
parent 52e6db20dd
commit 8a78e70eae
2 changed files with 40 additions and 34 deletions

View file

@ -1623,14 +1623,12 @@ test("Can post, deactivate, activate, edit, and remove an offer (Test, CI, sanit
ctx.extraInfo = "My edited extra info"; ctx.extraInfo = "My edited extra info";
ctx.price = undefined; ctx.price = undefined;
ctx.marketPriceMarginPct = 0.15; ctx.marketPriceMarginPct = 0.15;
offer = await user1.editOffer(offer.getId(), offer = await user1.editOffer({
undefined, // currency code offerId: offer.getId(),
ctx.price, // price price: ctx.price,
ctx.marketPriceMarginPct, // market price margin pct marketPriceMarginPct: ctx.marketPriceMarginPct,
undefined, // trigger price extraInfo: ctx.extraInfo
undefined, // payment account id });
ctx.extraInfo // extra info
);
assert.equal(offer.getState(), "AVAILABLE"); assert.equal(offer.getState(), "AVAILABLE");
if (ctx.marketPriceMarginPct) assert.equal(offer.getMarketPriceMarginPct(), ctx.marketPriceMarginPct); if (ctx.marketPriceMarginPct) assert.equal(offer.getMarketPriceMarginPct(), ctx.marketPriceMarginPct);
if (ctx.price) expect(parseFloat(offer.getPrice())).toEqual(ctx.price); if (ctx.price) expect(parseFloat(offer.getPrice())).toEqual(ctx.price);

View file

@ -26,9 +26,9 @@ import { TradeStatistics3, OfferDirection, PaymentMethod, PaymentAccountForm, Pa
/** /**
* Configuration to post, clone, or edit an offer. * Configuration to post or clone an offer.
*/ */
export interface OfferConfig { export interface PostOfferConfig {
direction?: OfferDirection; direction?: OfferDirection;
amount?: bigint; amount?: bigint;
minAmount?: bigint; minAmount?: bigint;
@ -45,6 +45,19 @@ export interface OfferConfig {
sourceOfferId?: string; sourceOfferId?: string;
} }
/**
* Configuration to edit an offer.
*/
export interface EditOfferConfig {
offerId: string;
assetCode?: string;
paymentAccountId?: string;
price?: number;
marketPriceMarginPct?: number;
triggerPrice?: number;
extraInfo?: string;
}
/** /**
* Haveno daemon client. * Haveno daemon client.
*/ */
@ -1074,7 +1087,7 @@ export default class HavenoClient {
/** /**
* Post or clone an offer. * Post or clone an offer.
* *
* @param {OfferConfig} config - configures the offer to post or clone * @param {PostOfferConfig} config - configures the offer to post or clone
* @param {OfferDirection} [config.direction] - specifies to buy or sell xmr (default buy) * @param {OfferDirection} [config.direction] - specifies to buy or sell xmr (default buy)
* @param {bigint} [config.amount] - amount of XMR to trade * @param {bigint} [config.amount] - amount of XMR to trade
* @param {string} [config.assetCode] - asset code to trade for XMR * @param {string} [config.assetCode] - asset code to trade for XMR
@ -1091,7 +1104,7 @@ export default class HavenoClient {
* @param {string} [config.sourceOfferId] - create a clone of a source offer which shares the same reserved funds. overrides other fields which are immutable or unspecified (optional) * @param {string} [config.sourceOfferId] - create a clone of a source offer which shares the same reserved funds. overrides other fields which are immutable or unspecified (optional)
* @return {OfferInfo} the posted offer * @return {OfferInfo} the posted offer
*/ */
async postOffer(config: OfferConfig): Promise<OfferInfo> { async postOffer(config: PostOfferConfig): Promise<OfferInfo> {
console.log("Posting offer with security deposit %: " + config.securityDepositPct) console.log("Posting offer with security deposit %: " + config.securityDepositPct)
try { try {
const request = new PostOfferRequest(); const request = new PostOfferRequest();
@ -1119,32 +1132,27 @@ export default class HavenoClient {
/** /**
* Edit an existing offer. * Edit an existing offer.
* *
* @param {string} offerId - id of the offer to edit * @param {EditOfferConfig} config - configures the offer to edit
* @param {string} currencyCode - traded asset code (optional, default to existing) * @param {string} [config.offerId] - id of the offer to edit
* @param {number} price - trade price (optional, default to market price) * @param {string} [config.assetCode] - traded asset code (optional, default to existing)
* @param {number} marketPriceMarginPct - if using market price, % from market price to accept (optional, default 0%) * @param {number} [config.price] - trade price (optional, default to market price)
* @param {number} triggerPrice - price to remove offer (optional) * @param {number} [config.marketPriceMarginPct] - if using market price, % from market price to accept (optional, default 0%)
* @param {string} paymentAccountId - payment account id (optional, default to existing) * @param {number} [config.triggerPrice] - price to remove offer (optional)
* @param {string} extraInfo - set the offer's extra information (optional, default to none) * @param {string} [config.paymentAccountId] - payment account id (optional, default to existing)
* @param {string} [config.extraInfo] - set the offer's extra information (optional, default to none)
* @return {OfferInfo} the edited offer * @return {OfferInfo} the edited offer
*/ */
async editOffer(offerId: string, async editOffer(config: EditOfferConfig): Promise<OfferInfo> {
currencyCode: string | undefined,
price: number | undefined,
marketPriceMarginPct: number | undefined,
triggerPrice: number | undefined,
paymentAccountId: string | undefined,
extraInfo: string | undefined): Promise<OfferInfo> {
try { try {
const request = new EditOfferRequest(); const request = new EditOfferRequest();
request.setOfferId(offerId); request.setOfferId(config.offerId);
if (currencyCode) request.setCurrencyCode(currencyCode!); if (config.assetCode) request.setCurrencyCode(config.assetCode);
if (price) request.setPrice(price!.toString()); if (config.price) request.setPrice(config.price.toString());
request.setUseMarketBasedPrice(price === undefined); request.setUseMarketBasedPrice(config.price === undefined);
request.setMarketPriceMarginPct(marketPriceMarginPct ? marketPriceMarginPct : 0); request.setMarketPriceMarginPct(config.marketPriceMarginPct ? config.marketPriceMarginPct : 0);
if (triggerPrice) request.setTriggerPrice(triggerPrice!.toString()); if (config.triggerPrice) request.setTriggerPrice(config.triggerPrice.toString());
if (paymentAccountId) request.setPaymentAccountId(paymentAccountId!); if (config.paymentAccountId) request.setPaymentAccountId(config.paymentAccountId);
request.setExtraInfo(extraInfo ? extraInfo : ""); // clear existing extra info if undefined request.setExtraInfo(config.extraInfo ? config.extraInfo : ""); // clear existing extra info if undefined
return (await this._offersClient.editOffer(request, {password: this._password})).getOffer()!; return (await this._offersClient.editOffer(request, {password: this._password})).getOffer()!;
} catch (e: any) { } catch (e: any) {
throw new HavenoError(e.message, e.code); throw new HavenoError(e.message, e.code);