import { getEnv } from "@ledgerhq/live-env"; import { AccessDeniedError, CurrencyDisabledAsInputError, CurrencyDisabledAsOutputError, CurrencyDisabledError, CurrencyNotSupportedByProviderError, CurrencyNotSupportedError, JSONDecodeError, JSONRPCResponseError, NoIPHeaderError, NotImplementedError, SwapGenericAPIError, TradeMethodNotSupportedError, UnexpectedError, ValidationError, } from "../../errors"; import getCompleteSwapHistory from "./getCompleteSwapHistory"; import { postSwapAccepted, postSwapCancelled } from "./postSwapState"; import { getIncompatibleCurrencyKeys } from "./getIncompatibleCurrencyKeys"; export { getAvailableProviders } from "../providers"; export const operationStatusList = { finishedOK: ["finished"], finishedKO: ["refunded"], }; // A swap operation is considered pending if it is not in a finishedOK or finishedKO state export const isSwapOperationPending: (status: string) => boolean = status => !operationStatusList.finishedOK.includes(status) && !operationStatusList.finishedKO.includes(status); const getSwapAPIBaseURL: () => string = () => getEnv("SWAP_API_BASE"); const getSwapUserIP = () => { const SWAP_USER_IP = getEnv("SWAP_USER_IP"); if (SWAP_USER_IP) { return { "X-Forwarded-For": SWAP_USER_IP }; } return undefined; }; const SWAP_API_BASE_PATTERN = /.*\/v(?\d+)\/*$/; const getSwapAPIVersion: () => number = () => { const version = Number(getSwapAPIBaseURL().match(SWAP_API_BASE_PATTERN)?.groups?.version); if (version == null || isNaN(version)) { throw new SwapGenericAPIError( "Configured swap API base URL is invalid, should end with /v", ); } return version; }; const USStates = { AL: "Alabama", AK: "Alaska", AZ: "Arizona", AR: "Arkansas", CA: "California", CO: "Colorado", CT: "Connecticut", DE: "Delaware", DC: "District Of Columbia", FL: "Florida", GA: "Georgia", HI: "Hawaii", ID: "Idaho", IL: "Illinois", IN: "Indiana", IA: "Iowa", KS: "Kansas", KY: "Kentucky", LA: "Louisiana", ME: "Maine", MD: "Maryland", MA: "Massachusetts", MI: "Michigan", MN: "Minnesota", MS: "Mississippi", MO: "Missouri", MT: "Montana", NE: "Nebraska", NV: "Nevada", NH: "New Hampshire", NJ: "New Jersey", NM: "New Mexico", NY: "New York", NC: "North Carolina", ND: "North Dakota", OH: "Ohio", OK: "Oklahoma", OR: "Oregon", PA: "Pennsylvania", RI: "Rhode Island", SC: "South Carolina", SD: "South Dakota", TN: "Tennessee", TX: "Texas", UT: "Utah", VT: "Vermont", VA: "Virginia", WA: "Washington", WV: "West Virginia", WI: "Wisconsin", WY: "Wyoming", }; const countries = { US: "United States", }; const swapBackendErrorCodes = { "100": JSONRPCResponseError, "101": JSONDecodeError, "200": NoIPHeaderError, "300": CurrencyNotSupportedError, "301": CurrencyDisabledError, "302": CurrencyDisabledAsInputError, "303": CurrencyDisabledAsOutputError, "304": CurrencyNotSupportedByProviderError, "400": TradeMethodNotSupportedError, "500": UnexpectedError, "600": NotImplementedError, "700": ValidationError, "701": AccessDeniedError, }; export const getSwapAPIError = (errorCode: number, errorMessage?: string) => { if (errorCode in swapBackendErrorCodes) return new swapBackendErrorCodes[errorCode](errorMessage); return new Error(errorMessage); }; export { getSwapAPIBaseURL, getSwapUserIP, getSwapAPIVersion, getCompleteSwapHistory, postSwapAccepted, postSwapCancelled, USStates, countries, getIncompatibleCurrencyKeys, };