test editing offers over grpc api (#396)

This commit is contained in:
woodser 2025-10-11 10:51:39 -04:00 committed by GitHub
parent 0781837c1a
commit 93b7696c16
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 60 additions and 2 deletions

View file

@ -1538,7 +1538,7 @@ test("Can prepare for trading (Test, CI)", async () => {
await prepareForTrading(5, user1, user2);
});
test("Can post, deactivate, activate, and remove an offer (Test, CI, sanity check)", async () => {
test("Can post, deactivate, activate, edit, and remove an offer (Test, CI, sanity check)", async () => {
// wait for user1 to have unlocked balance to post offer
await waitForAvailableBalance(250000000000n * 2n, user1);
@ -1619,6 +1619,28 @@ test("Can post, deactivate, activate, and remove an offer (Test, CI, sanity chec
if (!peerOffer) throw new Error("Offer " + offer.getId() + " was not found in peer's offers after posted");
testOffer(peerOffer, ctx, false);
// edit offer
ctx.extraInfo = "My edited extra info";
offer = await user1.editOffer(offer.getId(),
undefined, // currency code
170, // price
undefined, // market price margin pct
undefined, // trigger price
undefined, // payment account id
ctx.extraInfo // extra info
);
assert.equal(offer.getState(), "AVAILABLE");
assert.equal(parseFloat(offer.getPrice()), 170);
expect(offer.getExtraInfo()).toContain("My edited extra info");
// peer sees edited offer
await wait(TestConfig.trade.maxTimePeerNoticeMs);
peerOffer = getOffer(await user2.getOffers(assetCode, TestConfig.trade.direction), offer.getId());
if (!peerOffer) throw new Error("Offer " + offer.getId() + " was not found in peer's offers after posted");
testOffer(peerOffer, ctx, false);
expect(peerOffer.getExtraInfo()).toContain("My edited extra info");
expect(parseFloat(peerOffer.getPrice())).toEqual(170);
// cancel offer
await user1.removeOffer(offer.getId());
@ -3163,6 +3185,7 @@ async function makeOffer(ctxP?: Partial<TradeContext>): Promise<OfferInfo> {
extraInfo: ctx.extraInfo,
sourceOfferId: ctx.sourceOfferId
});
HavenoUtils.log(0, "Posted offer " + offer.getId() + " for " + ctx.assetCode + " at index " + ctx.index);
// test offer
testOffer(offer, ctx, true);

View file

@ -21,7 +21,7 @@ import HavenoUtils from "./utils/HavenoUtils";
import TaskLooper from "./utils/TaskLooper";
import type * as grpcWeb from "grpc-web";
import { GetTradeStatisticsClient, GetVersionClient, AccountClient, XmrConnectionsClient, DisputesClient, DisputeAgentsClient, NotificationsClient, WalletsClient, PriceClient, OffersClient, PaymentAccountsClient, TradesClient, ShutdownServerClient, XmrNodeClient } from './protobuf/GrpcServiceClientPb';
import { GetTradeStatisticsRequest, GetTradeStatisticsReply, GetVersionRequest, GetVersionReply, IsAppInitializedRequest, IsAppInitializedReply, RegisterDisputeAgentRequest, UnregisterDisputeAgentRequest, MarketPriceRequest, MarketPriceReply, MarketPricesRequest, MarketPricesReply, MarketPriceInfo, MarketDepthRequest, MarketDepthReply, MarketDepthInfo, GetBalancesRequest, GetBalancesReply, XmrBalanceInfo, GetMyOfferRequest, GetMyOfferReply, GetOffersRequest, GetOffersReply, OfferInfo, GetPaymentMethodsRequest, GetPaymentMethodsReply, GetPaymentAccountFormRequest, CreatePaymentAccountRequest, ValidateFormFieldRequest, CreatePaymentAccountReply, GetPaymentAccountFormReply, GetPaymentAccountsRequest, GetPaymentAccountsReply, CreateCryptoCurrencyPaymentAccountRequest, CreateCryptoCurrencyPaymentAccountReply, DeletePaymentAccountRequest, DeletePaymentAccountReply, PostOfferRequest, PostOfferReply, DeactivateOfferRequest, ActivateOfferRequest, CancelOfferRequest, TakeOfferRequest, TakeOfferReply, TradeInfo, GetTradeRequest, GetTradeReply, GetTradesRequest, GetTradesReply, GetXmrSeedRequest, GetXmrSeedReply, GetXmrPrimaryAddressRequest, GetXmrPrimaryAddressReply, GetXmrNewSubaddressRequest, GetXmrNewSubaddressReply, ConfirmPaymentSentRequest, ConfirmPaymentReceivedRequest, CompleteTradeRequest, XmrTx, GetXmrTxsRequest, GetXmrTxsReply, XmrDestination, CreateXmrTxRequest, CreateXmrTxReply, CreateXmrSweepTxsRequest, CreateXmrSweepTxsReply, RelayXmrTxsRequest, RelayXmrTxsReply, CreateAccountRequest, AccountExistsRequest, AccountExistsReply, DeleteAccountRequest, OpenAccountRequest, IsAccountOpenRequest, IsAccountOpenReply, CloseAccountRequest, ChangePasswordRequest, BackupAccountRequest, BackupAccountReply, RestoreAccountRequest, StopRequest, NotificationMessage, RegisterNotificationListenerRequest, SendNotificationRequest, UrlConnection, AddConnectionRequest, RemoveConnectionRequest, GetConnectionRequest, GetConnectionsRequest, SetConnectionRequest, CheckConnectionRequest, CheckConnectionsReply, CheckConnectionsRequest, StartCheckingConnectionRequest, StopCheckingConnectionRequest, GetBestConnectionRequest, SetAutoSwitchRequest, GetAutoSwitchRequest, CheckConnectionReply, GetConnectionsReply, GetConnectionReply, GetBestConnectionReply, GetDisputeRequest, GetDisputeReply, GetDisputesRequest, GetDisputesReply, OpenDisputeRequest, ResolveDisputeRequest, SendDisputeChatMessageRequest, SendChatMessageRequest, GetChatMessagesRequest, GetChatMessagesReply, StartXmrNodeRequest, StopXmrNodeRequest, IsXmrNodeOnlineRequest, IsXmrNodeOnlineReply, GetXmrNodeSettingsRequest, GetXmrNodeSettingsReply } from "./protobuf/grpc_pb";
import { GetTradeStatisticsRequest, GetTradeStatisticsReply, GetVersionRequest, GetVersionReply, IsAppInitializedRequest, IsAppInitializedReply, RegisterDisputeAgentRequest, UnregisterDisputeAgentRequest, MarketPriceRequest, MarketPriceReply, MarketPricesRequest, MarketPricesReply, MarketPriceInfo, MarketDepthRequest, MarketDepthReply, MarketDepthInfo, GetBalancesRequest, GetBalancesReply, XmrBalanceInfo, GetMyOfferRequest, GetMyOfferReply, GetOffersRequest, GetOffersReply, OfferInfo, GetPaymentMethodsRequest, GetPaymentMethodsReply, GetPaymentAccountFormRequest, CreatePaymentAccountRequest, ValidateFormFieldRequest, CreatePaymentAccountReply, GetPaymentAccountFormReply, GetPaymentAccountsRequest, GetPaymentAccountsReply, CreateCryptoCurrencyPaymentAccountRequest, CreateCryptoCurrencyPaymentAccountReply, DeletePaymentAccountRequest, DeletePaymentAccountReply, PostOfferRequest, PostOfferReply, EditOfferRequest, EditOfferReply, DeactivateOfferRequest, ActivateOfferRequest, CancelOfferRequest, TakeOfferRequest, TakeOfferReply, TradeInfo, GetTradeRequest, GetTradeReply, GetTradesRequest, GetTradesReply, GetXmrSeedRequest, GetXmrSeedReply, GetXmrPrimaryAddressRequest, GetXmrPrimaryAddressReply, GetXmrNewSubaddressRequest, GetXmrNewSubaddressReply, ConfirmPaymentSentRequest, ConfirmPaymentReceivedRequest, CompleteTradeRequest, XmrTx, GetXmrTxsRequest, GetXmrTxsReply, XmrDestination, CreateXmrTxRequest, CreateXmrTxReply, CreateXmrSweepTxsRequest, CreateXmrSweepTxsReply, RelayXmrTxsRequest, RelayXmrTxsReply, CreateAccountRequest, AccountExistsRequest, AccountExistsReply, DeleteAccountRequest, OpenAccountRequest, IsAccountOpenRequest, IsAccountOpenReply, CloseAccountRequest, ChangePasswordRequest, BackupAccountRequest, BackupAccountReply, RestoreAccountRequest, StopRequest, NotificationMessage, RegisterNotificationListenerRequest, SendNotificationRequest, UrlConnection, AddConnectionRequest, RemoveConnectionRequest, GetConnectionRequest, GetConnectionsRequest, SetConnectionRequest, CheckConnectionRequest, CheckConnectionsReply, CheckConnectionsRequest, StartCheckingConnectionRequest, StopCheckingConnectionRequest, GetBestConnectionRequest, SetAutoSwitchRequest, GetAutoSwitchRequest, CheckConnectionReply, GetConnectionsReply, GetConnectionReply, GetBestConnectionReply, GetDisputeRequest, GetDisputeReply, GetDisputesRequest, GetDisputesReply, OpenDisputeRequest, ResolveDisputeRequest, SendDisputeChatMessageRequest, SendChatMessageRequest, GetChatMessagesRequest, GetChatMessagesReply, StartXmrNodeRequest, StopXmrNodeRequest, IsXmrNodeOnlineRequest, IsXmrNodeOnlineReply, GetXmrNodeSettingsRequest, GetXmrNodeSettingsReply } from "./protobuf/grpc_pb";
import { TradeStatistics3, OfferDirection, PaymentMethod, PaymentAccountForm, PaymentAccountFormField, PaymentAccount, PaymentAccountPayload, AvailabilityResult, Attachment, DisputeResult, Dispute, ChatMessage, XmrNodeSettings } from "./protobuf/pb_pb";
@ -1116,6 +1116,41 @@ export default class HavenoClient {
}
}
/**
* Edit an existing offer.
*
* @param {string} offerId - id of the offer to edit
* @param {string} currencyCode - traded asset code (optional, default to existing)
* @param {number} price - trade price (optional, default to market price)
* @param {number} marketPriceMarginPct - if using market price, % from market price to accept (optional, default 0%)
* @param {number} triggerPrice - price to remove offer (optional)
* @param {string} paymentAccountId - payment account id (optional, default to existing)
* @param {string} extraInfo - set the offer's extra information (optional, default to none)
* @return {OfferInfo} the edited offer
*/
async editOffer(offerId: string,
currencyCode: string | undefined,
price: number | undefined,
marketPriceMarginPct: number | undefined,
triggerPrice: number | undefined,
paymentAccountId: string | undefined,
extraInfo: string | undefined): Promise<OfferInfo> {
try {
const request = new EditOfferRequest();
request.setOfferId(offerId);
if (currencyCode) request.setCurrencyCode(currencyCode!);
if (price) request.setPrice(price!.toString());
request.setUseMarketBasedPrice(price === undefined);
request.setMarketPriceMarginPct(marketPriceMarginPct ? marketPriceMarginPct : 0);
if (triggerPrice) request.setTriggerPrice(triggerPrice!.toString());
if (paymentAccountId) request.setPaymentAccountId(paymentAccountId!);
request.setExtraInfo(extraInfo ? extraInfo : ""); // clear existing extra info if undefined
return (await this._offersClient.editOffer(request, {password: this._password})).getOffer()!;
} catch (e: any) {
throw new HavenoError(e.message, e.code);
}
}
/**
* Deactivate an offer.
*