Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/fix sell card calls #84

Merged
merged 11 commits into from
Sep 23, 2024
6 changes: 3 additions & 3 deletions lib/src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const mockPost = jest.fn();
};
});

import { cancelSwap, confirmSwap, retriveSwapPayload } from "./api";
import { cancelSwap, confirmSwap, retrieveSwapPayload } from "./api";

describe("retrieveSwapPayload", () => {
afterEach(() => {
Expand All @@ -32,7 +32,7 @@ describe("retrieveSwapPayload", () => {
mockPost.mockResolvedValueOnce({ data: responseData });

// WHEN
const result = await retriveSwapPayload(data);
const result = await retrieveSwapPayload(data);

// THEN
const expectedResult = {
Expand Down Expand Up @@ -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();
Expand Down
58 changes: 45 additions & 13 deletions lib/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@ 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";
const SELL_BACKEND_URL = "https://buy.api.aws.prd.ldg-tech.com/";

let axiosClient = axios.create({

let swapAxiosClient = axios.create({
baseURL: SWAP_BACKEND_URL,
});

let sellAxiosClient = axios.create({
baseURL: SELL_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,
});
}
Comment on lines -8 to 30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it works, simple way to manage both backend URL
But I really think it should not matter and the live app creating an exchangeSDK instance should always pass the URL needed

Expand All @@ -43,7 +51,7 @@ export type SwapPayloadResponse = {
payinExtraId?: string;
};

export async function retriveSwapPayload(
export async function retrieveSwapPayload(
data: SwapPayloadRequestData
): Promise<SwapPayloadResponse> {
const request = {
Expand All @@ -57,7 +65,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);
}
Expand All @@ -71,8 +79,19 @@ export type ConfirmSwapRequest = {
hardwareWalletType?: string;
};

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

export async function confirmSwap(payload: ConfirmSwapRequest) {
await axiosClient.post("accepted", payload);
await swapAxiosClient.post("accepted", payload);
}

export async function confirmSell(data: ConfirmSellRequest) {
const { quoteId, ...payload } = data
await sellAxiosClient.post(`/webhook/v1/transaction/${quoteId}/accepted`, payload);
}

export type CancelSwapRequest = {
Expand All @@ -87,8 +106,20 @@ export type CancelSwapRequest = {
swapStep?: string;
};

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

export async function cancelSwap(payload: CancelSwapRequest) {
await axiosClient.post("cancelled", payload);
await swapAxiosClient.post("cancelled", payload);
}

export async function cancelSell(data: CancelSellRequest) {
const {quoteId, ...payload} = data
await sellAxiosClient.post(`/webhook/v1/transaction/${quoteId}/cancelled`, payload);
}

type SwapBackendResponse = {
Expand Down Expand Up @@ -139,6 +170,7 @@ export interface SellRequestPayload {
amountFrom: number;
amountTo: number;
nonce: string;
type: string;
}

export interface SellResponsePayload {
Expand All @@ -156,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,
Expand All @@ -165,7 +197,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,
Expand All @@ -176,8 +208,8 @@ export async function retriveSellPayload(data: SellRequestPayload) {
amountTo: data.amountTo,
nonce: data.nonce,
};
const res = await axiosClient.post("/sell", request);

const pathname = data.type === ExchangeType.SELL ? "sell/v1/remit" : "card/v1/remit";
const res = await sellAxiosClient.post(pathname, request);
return parseSellBackendInfo(res.data);
}

Expand Down Expand Up @@ -205,7 +237,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);
}
Expand Down
22 changes: 17 additions & 5 deletions lib/src/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ 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,
confirmSell,
cancelSell,
} from "./api";
import { ExchangeSDK, FeeStrategy } from "./sdk";
import { getCustomModule } from "./wallet-api";
Expand Down Expand Up @@ -89,7 +91,7 @@ beforeEach(() => {

describe("swap", () => {
beforeAll(() => {
(retriveSwapPayload as jest.Mock).mockResolvedValue({
(retrieveSwapPayload as jest.Mock).mockResolvedValue({
binaryPayload: "",
signature: "",
payinAddress: "",
Expand Down Expand Up @@ -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<Partial<Currency>> = [
Expand Down Expand Up @@ -265,8 +277,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(""),
Expand Down
Loading
Loading