Skip to content

Commit

Permalink
fix: use cancell accepted webhook sell
Browse files Browse the repository at this point in the history
  • Loading branch information
sarneijim committed Sep 23, 2024
1 parent 312bf48 commit 6c00990
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 38 deletions.
38 changes: 13 additions & 25 deletions lib/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

/**
Expand All @@ -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,
});
}
Expand Down Expand Up @@ -89,19 +81,17 @@ export type ConfirmSwapRequest = {

export type ConfirmSellRequest = {
provider: string;
sellId: string;
quoteId: string;
transactionId: string;
type: string;
};

export async function confirmSwap(payload: ConfirmSwapRequest) {
await swapAxiosClient.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);
const { quoteId, ...payload } = data
await buyAxiosClient.post(`/webhook/v1/transaction/${quoteId}/accepted`, payload);
}

export type CancelSwapRequest = {
Expand All @@ -118,20 +108,18 @@ export type CancelSwapRequest = {

export type CancelSellRequest = {
provider: string;
sellId: string;
quoteId: string;
statusCode?: string;
errorMessage?: string;
type: string;
};

export async function cancelSwap(payload: CancelSwapRequest) {
await swapAxiosClient.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);
const {quoteId, ...payload} = data
await buyAxiosClient.post(`/webhook/v1/transaction/${quoteId}/cancelled`, payload);
}

type SwapBackendResponse = {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand Down
20 changes: 7 additions & 13 deletions lib/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,7 @@ export class ExchangeSDK {
.catch(async (error) => {
await this.cancelSellOnError({
error,
sellId: quoteId,
type
quoteId,
});

this.handleError(error);
Expand All @@ -397,8 +396,7 @@ export class ExchangeSDK {
.catch(async(error: Error) => {
await this.cancelSellOnError({
error,
sellId: quoteId,
type
quoteId,
});

if (error.name === "DisabledTransactionBroadcastError") {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit 6c00990

Please sign in to comment.