diff --git a/src/HavenoClient.test.ts b/src/HavenoClient.test.ts index b35164d7..1727cab6 100644 --- a/src/HavenoClient.test.ts +++ b/src/HavenoClient.test.ts @@ -682,6 +682,14 @@ test("Can receive push notifications", async () => { } }); +test("Can get supported assets and their payment methods", async() => { + const assetCodes = await alice.getSupportedAssetCodes(); // TODO: replace with getSupportedAssets(): TradeCurrency[] + for (const assetCode of assetCodes) { + const paymentMethods = await alice.getPaymentMethods(assetCode); + expect(paymentMethods.length).toBeGreaterThanOrEqual(0); + } +}); + test("Can get market prices", async () => { // get all market prices @@ -828,6 +836,7 @@ test("Can get payment methods", async () => { expect(paymentMethod.getId().length).toBeGreaterThan(0); expect(BigInt(paymentMethod.getMaxTradeLimit())).toBeGreaterThan(BigInt(0)); expect(BigInt(paymentMethod.getMaxTradePeriod())).toBeGreaterThan(BigInt(0)); + expect(paymentMethod.getSupportedAssetCodesList().length).toBeGreaterThanOrEqual(0); } }); diff --git a/src/HavenoClient.ts b/src/HavenoClient.ts index 519b07e5..ecacb105 100644 --- a/src/HavenoClient.ts +++ b/src/HavenoClient.ts @@ -38,6 +38,7 @@ export default class HavenoClient { _registerNotificationListenerCalled = false; _keepAliveLooper: any; _keepAlivePeriodMs = 60000; + _paymentMethods: PaymentMethod[] | undefined; // cached for performance // constants static readonly _fullyInitializedMessage = "Application fully initialized"; @@ -746,15 +747,28 @@ export default class HavenoClient { }); } + /** + * Get all supported assets codes. + * + * TODO: replace this with getSupportedAssetCodes(): Promise) + * + * @return {Promise} all supported trade assets + */ + async getSupportedAssetCodes(): Promise { + const assetCodes: string[] = []; + for (const price of await this.getPrices()) assetCodes.push(price.getCurrencyCode()); + return assetCodes; + } + /** * Get the current market price per 1 XMR in the given currency. * - * @param {string} currencyCode - currency code (fiat or crypto) to get the price of - * @return {number} the current market price per 1 XMR in the given currency + * @param {string} assetCode - asset code to get the price of + * @return {number} the price of the asset per 1 XMR */ - async getPrice(currencyCode: string): Promise { + async getPrice(assetCode: string): Promise { return new Promise((resolve, reject) => { - this._priceClient.getMarketPrice(new MarketPriceRequest().setCurrencyCode(currencyCode), {password: this._password}, function(err: grpcWeb.RpcError, response: MarketPriceReply) { + this._priceClient.getMarketPrice(new MarketPriceRequest().setCurrencyCode(assetCode), {password: this._password}, function(err: grpcWeb.RpcError, response: MarketPriceReply) { if (err) reject(err); else resolve(response.getPrice()); }); @@ -762,9 +776,9 @@ export default class HavenoClient { } /** - * Get the current market prices of all currencies. + * Get the current market prices of all a. * - * @return {MarketPrice[]} price per 1 XMR in all supported currencies (fiat & crypto) + * @return {MarketPrice[]} prices of the assets per 1 XMR */ async getPrices(): Promise { return new Promise((resolve, reject) => { @@ -793,15 +807,24 @@ export default class HavenoClient { /** * Get payment methods. * + * @param {string} assetCode - get payment methods supporting this asset code (optional) * @return {PaymentMethod[]} the payment methods */ - async getPaymentMethods(): Promise { - return new Promise((resolve, reject) => { - this._paymentAccountsClient.getPaymentMethods(new GetPaymentMethodsRequest(), {password: this._password}, function(err: grpcWeb.RpcError, response: GetPaymentMethodsReply) { - if (err) reject(err); - else resolve(response.getPaymentMethodsList()); + async getPaymentMethods(assetCode?: string): Promise { + if (!this._paymentMethods) { + this._paymentMethods = await new Promise((resolve, reject) => { + this._paymentAccountsClient.getPaymentMethods(new GetPaymentMethodsRequest(), {password: this._password}, function(err: grpcWeb.RpcError, response: GetPaymentMethodsReply) { + if (err) reject(err); + else resolve(response.getPaymentMethodsList()); + }); }); - }); + } + if (!assetCode) return this._paymentMethods!; + const assetPaymentMethods: PaymentMethod[] = []; + for (const paymentMethod of this._paymentMethods!) { + if (paymentMethod.getSupportedAssetCodesList().includes(assetCode)) assetPaymentMethods.push(paymentMethod); + } + return assetPaymentMethods; } /**