From f6e060535d3bf1312eb5579582d439107a6cb161 Mon Sep 17 00:00:00 2001 From: sarneijim Date: Fri, 20 Sep 2024 14:04:58 +0100 Subject: [PATCH 01/11] fix: change call endpoint --- lib/src/api.ts | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/src/api.ts b/lib/src/api.ts index 7f689c3..dfdb393 100644 --- a/lib/src/api.ts +++ b/lib/src/api.ts @@ -5,18 +5,34 @@ import { decodeSellPayload } from "@ledgerhq/hw-app-exchange"; import { BEData } from "./sdk"; const SWAP_BACKEND_URL = "https://swap.ledger.com/v5/swap"; -const SELL_BACKEND_URL = "https://buy.api.aws.prd.ldg-tech.com/sell/v1"; +const SELL_BACKEND_URL = "https://buy.api.aws.prd.ldg-tech.com/sell/v1/remit"; +const CARD_BACKEND_URL = "https://buy.api.aws.prd.ldg-tech.com/card/v1/remit"; -let axiosClient = axios.create({ + +let swapAxiosClient = axios.create({ baseURL: SWAP_BACKEND_URL, }); +let sellAxiosClient = axios.create({ + baseURL: SELL_BACKEND_URL, +}); + +let cardAxiosClient = axios.create({ + baseURL: CARD_BACKEND_URL, +}); + /** * Override the default axios client base url environment (default is production) * @param {string} url */ export function setBackendUrl(url: string) { - axiosClient = axios.create({ + swapAxiosClient = axios.create({ + baseURL: url, + }); + sellAxiosClient = axios.create({ + baseURL: url, + }); + cardAxiosClient = axios.create({ baseURL: url, }); } @@ -57,7 +73,7 @@ export async function retriveSwapPayload( amountFromInSmallestDenomination: Number(data.amountInAtomicUnit), rateId: data.quoteId, }; - const res = await axiosClient.post("", request); + const res = await swapAxiosClient.post("", request); return parseSwapBackendInfo(res.data); } @@ -72,7 +88,7 @@ export type ConfirmSwapRequest = { }; export async function confirmSwap(payload: ConfirmSwapRequest) { - await axiosClient.post("accepted", payload); + await swapAxiosClient.post("accepted", payload); } export type CancelSwapRequest = { @@ -88,7 +104,7 @@ export type CancelSwapRequest = { }; export async function cancelSwap(payload: CancelSwapRequest) { - await axiosClient.post("cancelled", payload); + await swapAxiosClient.post("cancelled", payload); } type SwapBackendResponse = { @@ -176,7 +192,7 @@ export async function retriveSellPayload(data: SellRequestPayload) { amountTo: data.amountTo, nonce: data.nonce, }; - const res = await axiosClient.post("/sell", request); + const res = await sellAxiosClient.post("/sell/v1/remit", request); return parseSellBackendInfo(res.data); } @@ -205,7 +221,7 @@ export async function decodeSellPayloadAndPost( referralFee: null, }; - axiosClient.post("/forgeTransaction/offRamp", payload); + sellAxiosClient.post("/forgeTransaction/offRamp", payload); } catch (e) { console.log("Error decoding payload", e); } From cb663d4c30d389742837710fdae8ec163c12e768 Mon Sep 17 00:00:00 2001 From: sarneijim Date: Fri, 20 Sep 2024 14:06:21 +0100 Subject: [PATCH 02/11] fix: misspelling retrieve --- lib/src/api.test.ts | 6 +++--- lib/src/api.ts | 4 ++-- lib/src/sdk.test.ts | 10 +++++----- lib/src/sdk.ts | 10 +++++----- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/src/api.test.ts b/lib/src/api.test.ts index d41257d..8f50a68 100644 --- a/lib/src/api.test.ts +++ b/lib/src/api.test.ts @@ -10,7 +10,7 @@ const mockPost = jest.fn(); }; }); -import { cancelSwap, confirmSwap, retriveSwapPayload } from "./api"; +import { cancelSwap, confirmSwap, retrieveSwapPayload } from "./api"; describe("retrieveSwapPayload", () => { afterEach(() => { @@ -32,7 +32,7 @@ describe("retrieveSwapPayload", () => { mockPost.mockResolvedValueOnce({ data: responseData }); // WHEN - const result = await retriveSwapPayload(data); + const result = await retrieveSwapPayload(data); // THEN const expectedResult = { @@ -72,7 +72,7 @@ describe("retrieveSwapPayload", () => { mockPost.mockResolvedValueOnce({ data: responseData }); // WHEN - const result = await retriveSwapPayload(data); + const result = await retrieveSwapPayload(data); // THEN expect(result).not.toBeUndefined(); diff --git a/lib/src/api.ts b/lib/src/api.ts index dfdb393..203a305 100644 --- a/lib/src/api.ts +++ b/lib/src/api.ts @@ -59,7 +59,7 @@ export type SwapPayloadResponse = { payinExtraId?: string; }; -export async function retriveSwapPayload( +export async function retrieveSwapPayload( data: SwapPayloadRequestData ): Promise { const request = { @@ -181,7 +181,7 @@ const parseSellBackendInfo = (response: SellResponsePayload) => { }; }; -export async function retriveSellPayload(data: SellRequestPayload) { +export async function retrieveSellPayload(data: SellRequestPayload) { const request = { quoteId: data.quoteId, provider: data.provider, diff --git a/lib/src/sdk.test.ts b/lib/src/sdk.test.ts index b37bb31..e8502c8 100644 --- a/lib/src/sdk.test.ts +++ b/lib/src/sdk.test.ts @@ -9,10 +9,10 @@ import { ExchangeModule } from "@ledgerhq/wallet-api-exchange-module"; import { AccountModule } from "@ledgerhq/wallet-api-client/lib/modules/Account"; import { CurrencyModule } from "@ledgerhq/wallet-api-client/lib/modules/Currency"; import { - retriveSwapPayload, + retrieveSwapPayload, confirmSwap, cancelSwap, - retriveSellPayload, + retrieveSellPayload, } from "./api"; import { ExchangeSDK, FeeStrategy } from "./sdk"; import { getCustomModule } from "./wallet-api"; @@ -89,7 +89,7 @@ beforeEach(() => { describe("swap", () => { beforeAll(() => { - (retriveSwapPayload as jest.Mock).mockResolvedValue({ + (retrieveSwapPayload as jest.Mock).mockResolvedValue({ binaryPayload: "", signature: "", payinAddress: "", @@ -265,8 +265,8 @@ describe("sell", () => { ]; mockCurrenciesList.mockResolvedValue(currencies as any); - // Mock `retriveSellPayload` since `getSellPayload` is not provided - (retriveSellPayload as jest.Mock).mockResolvedValue({ + // Mock `retrieveSellPayload` since `getSellPayload` is not provided + (retrieveSellPayload as jest.Mock).mockResolvedValue({ payinAddress: "0xfff", providerSig: { payload: Buffer.from(""), diff --git a/lib/src/sdk.ts b/lib/src/sdk.ts index 714b5af..c3c11e3 100644 --- a/lib/src/sdk.ts +++ b/lib/src/sdk.ts @@ -13,8 +13,8 @@ import { cancelSwap, confirmSwap, decodeSellPayloadAndPost, - retriveSellPayload, - retriveSwapPayload, + retrieveSellPayload, + retrieveSwapPayload, setBackendUrl, } from "./api"; import { @@ -31,7 +31,7 @@ import walletApiDecorator, { getCustomModule, } from "./wallet-api"; -export type GetSwapPayload = typeof retriveSwapPayload; +export type GetSwapPayload = typeof retrieveSwapPayload; /** * Swap information required to request a user's swap transaction. @@ -202,7 +202,7 @@ export class ExchangeSDK { this.logger.debug("DeviceTransactionId retrieved:", deviceTransactionId); // Step 2: Ask for payload creation - const payloadRequest = getSwapPayload ?? retriveSwapPayload; + const payloadRequest = getSwapPayload ?? retrieveSwapPayload; const { binaryPayload, signature, payinAddress, swapId, payinExtraId } = await payloadRequest({ provider: this.providerId, @@ -494,7 +494,7 @@ export class ExchangeSDK { signature = data.signature; beData = data.beData; } else { - const data = await retriveSellPayload({ + const data = await retrieveSellPayload({ quoteId: quoteId!, provider: this.providerId, fromCurrency: account.currency, From 7acd939d82379929bcbccb6757ccd4056620ee5b Mon Sep 17 00:00:00 2001 From: sarneijim Date: Fri, 20 Sep 2024 14:10:04 +0100 Subject: [PATCH 03/11] refactor: unify method to detect the flow type --- lib/src/sdk.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/sdk.ts b/lib/src/sdk.ts index c3c11e3..b09fcb3 100644 --- a/lib/src/sdk.ts +++ b/lib/src/sdk.ts @@ -343,7 +343,7 @@ export class ExchangeSDK { initialAtomicAmount, }); - if (this.providerId === "coinify") { + if (getSellPayload) { await decodeSellPayloadAndPost( binaryPayload as string, beData as BEData, From 75f2839582fcda38e8cab6770b48c106ec20a3b2 Mon Sep 17 00:00:00 2001 From: sarneijim Date: Fri, 20 Sep 2024 15:17:17 +0100 Subject: [PATCH 04/11] fix: add accepted and cancelled sell calls --- lib/src/api.ts | 21 +++++++++++++++++++++ lib/src/sdk.ts | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/lib/src/api.ts b/lib/src/api.ts index 203a305..bd9595d 100644 --- a/lib/src/api.ts +++ b/lib/src/api.ts @@ -87,10 +87,20 @@ export type ConfirmSwapRequest = { hardwareWalletType?: string; }; +export type ConfirmSellRequest = { + provider: string; + sellId: string; + transactionId: string; +}; + export async function confirmSwap(payload: ConfirmSwapRequest) { await swapAxiosClient.post("accepted", payload); } +export async function confirmSell(payload: ConfirmSellRequest) { + await sellAxiosClient.post("accepted", payload); +} + export type CancelSwapRequest = { provider: string; swapId: string; @@ -103,10 +113,21 @@ export type CancelSwapRequest = { swapStep?: string; }; +export type CancelSellRequest = { + provider: string; + sellId: string; + statusCode?: string; + errorMessage?: string; +}; + export async function cancelSwap(payload: CancelSwapRequest) { await swapAxiosClient.post("cancelled", payload); } +export async function cancelSell(payload: CancelSellRequest) { + await sellAxiosClient.post("cancelled", payload); +} + type SwapBackendResponse = { provider: string; swapId: string; diff --git a/lib/src/sdk.ts b/lib/src/sdk.ts index b09fcb3..7382472 100644 --- a/lib/src/sdk.ts +++ b/lib/src/sdk.ts @@ -12,6 +12,8 @@ import { ExchangeModule } from "@ledgerhq/wallet-api-exchange-module"; import { cancelSwap, confirmSwap, + cancelSell, + confirmSell, decodeSellPayloadAndPost, retrieveSellPayload, retrieveSwapPayload, @@ -367,6 +369,15 @@ export class ExchangeSDK { amount: fromAmountAtomic, currency, customFeeConfig, + }) + .catch(async (error) => { + await this.cancelSellOnError({ + error, + sellId: quoteId, + }); + + this.handleError(error); + throw error; }); const tx = await this.exchangeModule @@ -378,15 +389,30 @@ export class ExchangeSDK { signature, feeStrategy, }) - .catch((error: Error) => { + .catch(async(error: Error) => { + await this.cancelSellOnError({ + error, + sellId: quoteId, + }); + + if (error.name === "DisabledTransactionBroadcastError") { + throw error; + } + const err = new SignatureStepError(error); - this.logger.error(err); + this.handleError(err); throw err; }); this.logger.log("Transaction sent:", tx); this.logger.log("*** End Sell ***"); - + await confirmSell({ + provider: this.providerId, + sellId: quoteId ?? "", + transactionId: tx, + }).catch((error: Error) => { + this.logger.error(error); + }); return tx; } @@ -452,6 +478,23 @@ export class ExchangeSDK { }); } + private async cancelSellOnError({ + error, + sellId, + }: { + error: Error, + sellId?: string, + }) { + await cancelSell({ + provider: this.providerId, + sellId: sellId ?? "", + statusCode: error.name, + errorMessage: error.message, + }).catch((cancelError: Error) => { + this.logger.error(cancelError); + }); + } + private async sellPayloadRequest({ account, getSellPayload, From 034d8d4254e948b3e362d38c1e49c4096cf57923 Mon Sep 17 00:00:00 2001 From: sarneijim Date: Fri, 20 Sep 2024 15:28:49 +0100 Subject: [PATCH 05/11] fix: update post call --- lib/src/api.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/api.ts b/lib/src/api.ts index bd9595d..5c262e2 100644 --- a/lib/src/api.ts +++ b/lib/src/api.ts @@ -213,7 +213,7 @@ export async function retrieveSellPayload(data: SellRequestPayload) { amountTo: data.amountTo, nonce: data.nonce, }; - const res = await sellAxiosClient.post("/sell/v1/remit", request); + const res = await sellAxiosClient.post("", request); return parseSellBackendInfo(res.data); } From 388af32e5d5e1b1a9b44af50ff762aed94439166 Mon Sep 17 00:00:00 2001 From: sarneijim Date: Fri, 20 Sep 2024 16:04:56 +0100 Subject: [PATCH 06/11] feature: add card --- lib/src/api.ts | 21 ++++++++++++++------- lib/src/sdk.ts | 43 ++++++++++++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/lib/src/api.ts b/lib/src/api.ts index 5c262e2..828c9a1 100644 --- a/lib/src/api.ts +++ b/lib/src/api.ts @@ -2,7 +2,7 @@ import axios from "axios"; import { Account } from "@ledgerhq/wallet-api-client"; import BigNumber from "bignumber.js"; import { decodeSellPayload } from "@ledgerhq/hw-app-exchange"; -import { BEData } from "./sdk"; +import { BEData, ExchangeType } from "./sdk"; const SWAP_BACKEND_URL = "https://swap.ledger.com/v5/swap"; const SELL_BACKEND_URL = "https://buy.api.aws.prd.ldg-tech.com/sell/v1/remit"; @@ -91,14 +91,17 @@ export type ConfirmSellRequest = { provider: string; sellId: string; transactionId: string; + type: string; }; export async function confirmSwap(payload: ConfirmSwapRequest) { await swapAxiosClient.post("accepted", payload); } -export async function confirmSell(payload: ConfirmSellRequest) { - await sellAxiosClient.post("accepted", payload); +export async function confirmSell(data: ConfirmSellRequest) { + const { type, ...payload } = data + const axiosClient = type === ExchangeType.SELL ? sellAxiosClient : cardAxiosClient; + await axiosClient.post("accepted", payload); } export type CancelSwapRequest = { @@ -118,14 +121,17 @@ export type CancelSellRequest = { sellId: string; statusCode?: string; errorMessage?: string; + type: string; }; export async function cancelSwap(payload: CancelSwapRequest) { await swapAxiosClient.post("cancelled", payload); } -export async function cancelSell(payload: CancelSellRequest) { - await sellAxiosClient.post("cancelled", payload); +export async function cancelSell(data: CancelSellRequest) { + const { type, ...payload } = data + const axiosClient = type === ExchangeType.SELL ? sellAxiosClient : cardAxiosClient; + await axiosClient.post("cancelled", payload); } type SwapBackendResponse = { @@ -176,6 +182,7 @@ export interface SellRequestPayload { amountFrom: number; amountTo: number; nonce: string; + type: string; } export interface SellResponsePayload { @@ -213,8 +220,8 @@ export async function retrieveSellPayload(data: SellRequestPayload) { amountTo: data.amountTo, nonce: data.nonce, }; - const res = await sellAxiosClient.post("", request); - + const axiosClient = data.type === ExchangeType.SELL ? sellAxiosClient : cardAxiosClient; + const res = await axiosClient.post("", request); return parseSellBackendInfo(res.data); } diff --git a/lib/src/sdk.ts b/lib/src/sdk.ts index 7382472..324d5f2 100644 --- a/lib/src/sdk.ts +++ b/lib/src/sdk.ts @@ -33,6 +33,22 @@ import walletApiDecorator, { getCustomModule, } from "./wallet-api"; +export type FeeStrategy = "SLOW" | "MEDIUM" | "FAST" | "CUSTOM"; + +enum FeeStrategyEnum { + SLOW = "SLOW", + MEDIUM = "MEDIUM", + FAST = "FAST", + CUSTOM = "CUSTOM", +} + +export const ExchangeType = { + FUND: "FUND", + SELL: "SELL", + SWAP: "SWAP", + CARD: "CARD" +} as const; + export type GetSwapPayload = typeof retrieveSwapPayload; /** @@ -80,23 +96,9 @@ export type SellInfo = { rate?: number; customFeeConfig?: { [key: string]: BigNumber }; getSellPayload?: GetSellPayload; + type?: string; }; -export type FeeStrategy = "SLOW" | "MEDIUM" | "FAST" | "CUSTOM"; - -enum FeeStrategyEnum { - SLOW = "SLOW", - MEDIUM = "MEDIUM", - FAST = "FAST", - CUSTOM = "CUSTOM", -} - -export const ExchangeType = { - FUND: "FUND", - SELL: "SELL", - SWAP: "SWAP", -} as const; - /** * ExchangeSDK allows you to send a swap request to a Ledger Device through a Ledger Live request. * Under the hood, it relies on {@link https://github.com/LedgerHQ/wallet-api WalletAPI}. @@ -311,6 +313,7 @@ export class ExchangeSDK { rate, toFiat, getSellPayload, + type = ExchangeType.CARD } = info; const { account, currency } = @@ -343,6 +346,7 @@ export class ExchangeSDK { account, deviceTransactionId, initialAtomicAmount, + type }); if (getSellPayload) { @@ -374,6 +378,7 @@ export class ExchangeSDK { await this.cancelSellOnError({ error, sellId: quoteId, + type }); this.handleError(error); @@ -393,6 +398,7 @@ export class ExchangeSDK { await this.cancelSellOnError({ error, sellId: quoteId, + type }); if (error.name === "DisabledTransactionBroadcastError") { @@ -410,6 +416,7 @@ export class ExchangeSDK { provider: this.providerId, sellId: quoteId ?? "", transactionId: tx, + type }).catch((error: Error) => { this.logger.error(error); }); @@ -481,15 +488,18 @@ export class ExchangeSDK { private async cancelSellOnError({ error, sellId, + type }: { error: Error, sellId?: string, + type: string }) { await cancelSell({ provider: this.providerId, sellId: sellId ?? "", statusCode: error.name, errorMessage: error.message, + type }).catch((cancelError: Error) => { this.logger.error(cancelError); }); @@ -504,6 +514,7 @@ export class ExchangeSDK { amount, deviceTransactionId, initialAtomicAmount, + type, }: { amount: BigNumber; getSellPayload?: GetSellPayload; @@ -513,6 +524,7 @@ export class ExchangeSDK { quoteId?: string; rate?: number; toFiat?: string; + type: string; }) { let recipientAddress: string; let binaryPayload: string; @@ -546,6 +558,7 @@ export class ExchangeSDK { amountFrom: amount.toNumber(), amountTo: rate! * amount.toNumber(), nonce: deviceTransactionId, + type, }).catch((error: Error) => { const err = new PayloadStepError(error); this.handleError(err); From 312bf48744c4cd0d97ed9e98baadc2f1960b8b32 Mon Sep 17 00:00:00 2001 From: sarneijim Date: Fri, 20 Sep 2024 16:10:54 +0100 Subject: [PATCH 07/11] fix: fix remit call --- lib/src/api.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/src/api.ts b/lib/src/api.ts index 828c9a1..fd03475 100644 --- a/lib/src/api.ts +++ b/lib/src/api.ts @@ -5,8 +5,8 @@ import { decodeSellPayload } from "@ledgerhq/hw-app-exchange"; import { BEData, ExchangeType } from "./sdk"; const SWAP_BACKEND_URL = "https://swap.ledger.com/v5/swap"; -const SELL_BACKEND_URL = "https://buy.api.aws.prd.ldg-tech.com/sell/v1/remit"; -const CARD_BACKEND_URL = "https://buy.api.aws.prd.ldg-tech.com/card/v1/remit"; +const SELL_BACKEND_URL = "https://buy.api.aws.prd.ldg-tech.com/sell/v1"; +const CARD_BACKEND_URL = "https://buy.api.aws.prd.ldg-tech.com/card/v1"; let swapAxiosClient = axios.create({ @@ -221,7 +221,7 @@ export async function retrieveSellPayload(data: SellRequestPayload) { nonce: data.nonce, }; const axiosClient = data.type === ExchangeType.SELL ? sellAxiosClient : cardAxiosClient; - const res = await axiosClient.post("", request); + const res = await axiosClient.post("/remit", request); return parseSellBackendInfo(res.data); } From 6c009900e1390adce7e828a29b050ab7c8fe9728 Mon Sep 17 00:00:00 2001 From: sarneijim Date: Mon, 23 Sep 2024 06:46:31 +0100 Subject: [PATCH 08/11] fix: use cancell accepted webhook sell --- lib/src/api.ts | 38 +++++++++++++------------------------- lib/src/sdk.ts | 20 +++++++------------- 2 files changed, 20 insertions(+), 38 deletions(-) diff --git a/lib/src/api.ts b/lib/src/api.ts index fd03475..51c6fc9 100644 --- a/lib/src/api.ts +++ b/lib/src/api.ts @@ -5,20 +5,15 @@ import { decodeSellPayload } from "@ledgerhq/hw-app-exchange"; import { BEData, ExchangeType } from "./sdk"; const SWAP_BACKEND_URL = "https://swap.ledger.com/v5/swap"; -const SELL_BACKEND_URL = "https://buy.api.aws.prd.ldg-tech.com/sell/v1"; -const CARD_BACKEND_URL = "https://buy.api.aws.prd.ldg-tech.com/card/v1"; +const BUY_BACKEND_URL = "https://buy.api.aws.prd.ldg-tech.com/"; let swapAxiosClient = axios.create({ baseURL: SWAP_BACKEND_URL, }); -let sellAxiosClient = axios.create({ - baseURL: SELL_BACKEND_URL, -}); - -let cardAxiosClient = axios.create({ - baseURL: CARD_BACKEND_URL, +let buyAxiosClient = axios.create({ + baseURL: BUY_BACKEND_URL, }); /** @@ -29,10 +24,7 @@ export function setBackendUrl(url: string) { swapAxiosClient = axios.create({ baseURL: url, }); - sellAxiosClient = axios.create({ - baseURL: url, - }); - cardAxiosClient = axios.create({ + buyAxiosClient = axios.create({ baseURL: url, }); } @@ -89,9 +81,8 @@ export type ConfirmSwapRequest = { export type ConfirmSellRequest = { provider: string; - sellId: string; + quoteId: string; transactionId: string; - type: string; }; export async function confirmSwap(payload: ConfirmSwapRequest) { @@ -99,9 +90,8 @@ export async function confirmSwap(payload: ConfirmSwapRequest) { } export async function confirmSell(data: ConfirmSellRequest) { - const { type, ...payload } = data - const axiosClient = type === ExchangeType.SELL ? sellAxiosClient : cardAxiosClient; - await axiosClient.post("accepted", payload); + const { quoteId, ...payload } = data + await buyAxiosClient.post(`/webhook/v1/transaction/${quoteId}/accepted`, payload); } export type CancelSwapRequest = { @@ -118,10 +108,9 @@ export type CancelSwapRequest = { export type CancelSellRequest = { provider: string; - sellId: string; + quoteId: string; statusCode?: string; errorMessage?: string; - type: string; }; export async function cancelSwap(payload: CancelSwapRequest) { @@ -129,9 +118,8 @@ export async function cancelSwap(payload: CancelSwapRequest) { } export async function cancelSell(data: CancelSellRequest) { - const { type, ...payload } = data - const axiosClient = type === ExchangeType.SELL ? sellAxiosClient : cardAxiosClient; - await axiosClient.post("cancelled", payload); + const {quoteId, ...payload} = data + await buyAxiosClient.post(`/webhook/v1/transaction/${quoteId}/cancelled`, payload); } type SwapBackendResponse = { @@ -220,8 +208,8 @@ export async function retrieveSellPayload(data: SellRequestPayload) { amountTo: data.amountTo, nonce: data.nonce, }; - const axiosClient = data.type === ExchangeType.SELL ? sellAxiosClient : cardAxiosClient; - const res = await axiosClient.post("/remit", request); + const pathname = data.type === ExchangeType.SELL ? "sell/v1/remit" : "card/v1/remit"; + const res = await buyAxiosClient.post(pathname, request); return parseSellBackendInfo(res.data); } @@ -249,7 +237,7 @@ export async function decodeSellPayloadAndPost( referralFee: null, }; - sellAxiosClient.post("/forgeTransaction/offRamp", payload); + buyAxiosClient.post("/forgeTransaction/offRamp", payload); } catch (e) { console.log("Error decoding payload", e); } diff --git a/lib/src/sdk.ts b/lib/src/sdk.ts index 324d5f2..f890b51 100644 --- a/lib/src/sdk.ts +++ b/lib/src/sdk.ts @@ -377,8 +377,7 @@ export class ExchangeSDK { .catch(async (error) => { await this.cancelSellOnError({ error, - sellId: quoteId, - type + quoteId, }); this.handleError(error); @@ -397,8 +396,7 @@ export class ExchangeSDK { .catch(async(error: Error) => { await this.cancelSellOnError({ error, - sellId: quoteId, - type + quoteId, }); if (error.name === "DisabledTransactionBroadcastError") { @@ -414,12 +412,11 @@ export class ExchangeSDK { this.logger.log("*** End Sell ***"); await confirmSell({ provider: this.providerId, - sellId: quoteId ?? "", + quoteId: quoteId ?? "", transactionId: tx, - type }).catch((error: Error) => { this.logger.error(error); - }); + }); return tx; } @@ -487,19 +484,16 @@ export class ExchangeSDK { private async cancelSellOnError({ error, - sellId, - type + quoteId, }: { error: Error, - sellId?: string, - type: string + quoteId?: string, }) { await cancelSell({ provider: this.providerId, - sellId: sellId ?? "", + quoteId: quoteId ?? "", statusCode: error.name, errorMessage: error.message, - type }).catch((cancelError: Error) => { this.logger.error(cancelError); }); From d096585b073ea609a4edcf1cc7a49a1bcebc141b Mon Sep 17 00:00:00 2001 From: sarneijim Date: Mon, 23 Sep 2024 07:04:43 +0100 Subject: [PATCH 09/11] fix: change default value --- lib/src/sdk.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/sdk.ts b/lib/src/sdk.ts index f890b51..fad5e24 100644 --- a/lib/src/sdk.ts +++ b/lib/src/sdk.ts @@ -313,7 +313,7 @@ export class ExchangeSDK { rate, toFiat, getSellPayload, - type = ExchangeType.CARD + type = ExchangeType.SELL } = info; const { account, currency } = From 49e7c4078ccf9a247ef40bca9edb27421d318e9a Mon Sep 17 00:00:00 2001 From: sarneijim Date: Mon, 23 Sep 2024 07:21:14 +0100 Subject: [PATCH 10/11] fix: fix tests --- lib/src/api.ts | 2 +- lib/src/sdk.test.ts | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/src/api.ts b/lib/src/api.ts index 51c6fc9..a0d5d0a 100644 --- a/lib/src/api.ts +++ b/lib/src/api.ts @@ -188,7 +188,7 @@ export interface SellResponsePayload { const parseSellBackendInfo = (response: SellResponsePayload) => { return { - sellId: response.sellId, + quoteId: response.sellId, payinAddress: response.payinAddress, providerSig: { payload: response.providerSig.payload, diff --git a/lib/src/sdk.test.ts b/lib/src/sdk.test.ts index e8502c8..cd4b43c 100644 --- a/lib/src/sdk.test.ts +++ b/lib/src/sdk.test.ts @@ -13,6 +13,8 @@ import { confirmSwap, cancelSwap, retrieveSellPayload, + confirmSell, + cancelSell, } from "./api"; import { ExchangeSDK, FeeStrategy } from "./sdk"; import { getCustomModule } from "./wallet-api"; @@ -217,6 +219,16 @@ describe("swap", () => { }); describe("sell", () => { + beforeAll(() => { + (retrieveSellPayload as jest.Mock).mockResolvedValue({ + binaryPayload: "", + signature: "", + payinAddress: "", + quoteId: "sell-id", + }); + (confirmSell as jest.Mock).mockResolvedValue({}); + (cancelSell as jest.Mock).mockResolvedValue({}); + }); it("sends back the 'transactionId' from the WalletAPI", async () => { // GIVEN const currencies: Array> = [ From 6a19e0385db1efb33d064de36d6dee5ed72935d2 Mon Sep 17 00:00:00 2001 From: sarneijim Date: Mon, 23 Sep 2024 07:25:37 +0100 Subject: [PATCH 11/11] refactor: naming sell --- lib/src/api.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/src/api.ts b/lib/src/api.ts index a0d5d0a..10c8221 100644 --- a/lib/src/api.ts +++ b/lib/src/api.ts @@ -5,15 +5,15 @@ import { decodeSellPayload } from "@ledgerhq/hw-app-exchange"; import { BEData, ExchangeType } from "./sdk"; const SWAP_BACKEND_URL = "https://swap.ledger.com/v5/swap"; -const BUY_BACKEND_URL = "https://buy.api.aws.prd.ldg-tech.com/"; +const SELL_BACKEND_URL = "https://buy.api.aws.prd.ldg-tech.com/"; let swapAxiosClient = axios.create({ baseURL: SWAP_BACKEND_URL, }); -let buyAxiosClient = axios.create({ - baseURL: BUY_BACKEND_URL, +let sellAxiosClient = axios.create({ + baseURL: SELL_BACKEND_URL, }); /** @@ -24,7 +24,7 @@ export function setBackendUrl(url: string) { swapAxiosClient = axios.create({ baseURL: url, }); - buyAxiosClient = axios.create({ + sellAxiosClient = axios.create({ baseURL: url, }); } @@ -91,7 +91,7 @@ export async function confirmSwap(payload: ConfirmSwapRequest) { export async function confirmSell(data: ConfirmSellRequest) { const { quoteId, ...payload } = data - await buyAxiosClient.post(`/webhook/v1/transaction/${quoteId}/accepted`, payload); + await sellAxiosClient.post(`/webhook/v1/transaction/${quoteId}/accepted`, payload); } export type CancelSwapRequest = { @@ -119,7 +119,7 @@ export async function cancelSwap(payload: CancelSwapRequest) { export async function cancelSell(data: CancelSellRequest) { const {quoteId, ...payload} = data - await buyAxiosClient.post(`/webhook/v1/transaction/${quoteId}/cancelled`, payload); + await sellAxiosClient.post(`/webhook/v1/transaction/${quoteId}/cancelled`, payload); } type SwapBackendResponse = { @@ -209,7 +209,7 @@ export async function retrieveSellPayload(data: SellRequestPayload) { nonce: data.nonce, }; const pathname = data.type === ExchangeType.SELL ? "sell/v1/remit" : "card/v1/remit"; - const res = await buyAxiosClient.post(pathname, request); + const res = await sellAxiosClient.post(pathname, request); return parseSellBackendInfo(res.data); } @@ -237,7 +237,7 @@ export async function decodeSellPayloadAndPost( referralFee: null, }; - buyAxiosClient.post("/forgeTransaction/offRamp", payload); + sellAxiosClient.post("/forgeTransaction/offRamp", payload); } catch (e) { console.log("Error decoding payload", e); }