Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"graphql-ws": "^5.13.1",
"gt3-server-node-express-sdk": "https://github.com/GaloyMoney/gt3-server-node-express-bypass#master",
"i18n": "^0.15.1",
"ibex-client": "^3.0.0",
"ibex-client": "^3.1.0",
"invoices": "^3.0.0",
"ioredis": "^5.3.2",
"ioredis-cache": "^2.0.0",
Expand Down Expand Up @@ -182,11 +182,11 @@
"eslint-plugin-jest": "^27.4.0",
"eslint-plugin-prettier": "^5.0.0",
"graphql-subscriptions": "^2.0.0",
"grpc_tools_node_protoc_ts": "^5.3.3",
"grpc-tools": "^1.12.4",
"grpc_tools_node_protoc_ts": "^5.3.3",
"jest": "^29.7.0",
"jest_workaround": "^0.79.19",
"jest-junit": "^16.0.0",
"jest_workaround": "^0.79.19",
"lodash.difference": "^4.5.0",
"lodash.find": "^4.6.0",
"madge": "^6.1.0",
Expand Down
69 changes: 19 additions & 50 deletions src/services/ibex/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import IbexClient, {
SendToAddressCopyBodyParam,
SendToAddressCopyResponse200,
IbexUrls,
CryptoReceiveInfo,
CryptoReceiveOption,
} from "ibex-client"

import { IbexConfig } from "@config"
Expand All @@ -39,15 +41,11 @@ import {
IbexFeeEstimation,
IbexInvoiceArgs,
PayInvoiceArgs,
CryptoReceiveOption,
CryptoReceiveInfo,
CreateCryptoReceiveInfoRequest,
CreateCryptoSendInfoBodyParam,
CryptoSendBodyParam,
CryptoSendInfo,
CryptoSendRequirements,
CryptoSendResponse,
IbexCurrency,
UsdWalletAmount,
} from "./types"

Expand Down Expand Up @@ -433,40 +431,14 @@ const getCryptoReceiveBalance = async (
}
}

const getCryptoReceiveOptions = async (): Promise<CryptoReceiveOption[] | IbexError> => {
try {
const token = await getIbexToken()
if (token instanceof IbexError) return token
const data = await ibexGet<CryptoReceiveOption[]>(
token,
"/crypto/receive-infos/options",
)

if (data instanceof IbexError) return data
return data
} catch (err) {
return new IbexError(err instanceof Error ? err : new Error(String(err)))
}
}
const getCryptoReceiveOptions = async (): Promise<CryptoReceiveOption[] | IbexError> =>
Ibex.getCryptoReceiveOptions().then(errorHandler)

const createCryptoReceiveInfo = async (
accountId: IbexAccountId,
option: Pick<CryptoReceiveOption, "name" | "network">,
option: CryptoReceiveOption,
): Promise<CryptoReceiveInfo | IbexError> => {
try {
const token = await getIbexToken()
if (token instanceof IbexError) return token
const data = await ibexPost<CryptoReceiveInfo>(
token,
`/accounts/${accountId}/crypto/receive-infos`,
{ name: option.name, network: option.network } as CreateCryptoReceiveInfoRequest,
)
if (data instanceof IbexError) return data
if (!data.data.address) return new UnexpectedIbexResponse("Address not found")
return data
} catch (err) {
return new IbexError(err instanceof Error ? err : new Error(String(err)))
}
return Ibex.createCryptoReceiveInfo(accountId, option).then(errorHandler)
}

const getTronUsdtOption = async (): Promise<CryptoReceiveOption | IbexError> => {
Expand All @@ -485,16 +457,13 @@ const getTronUsdtOption = async (): Promise<CryptoReceiveOption | IbexError> =>
return tronUsdt
}

const getEthereumUsdtOption = async (): Promise<CryptoReceiveOption | IbexError> => {
const options = await getCryptoReceiveOptions()
if (options instanceof IbexError) return options

const UsdtCurrencyId = await getIbexCurrencyId(WalletCurrency.Usdt)
if (UsdtCurrencyId instanceof IbexError) return UsdtCurrencyId as IbexError

const findEthereumUsdtReceiveOption = (
options: CryptoReceiveOption[],
): CryptoReceiveOption | IbexError => {
const ethereumUsdt = options.find(
(opt) =>
opt.currencyId === UsdtCurrencyId && opt.network.toLowerCase() === "ethereum",
opt.currencyId === USDTAmount.currencyId &&
opt.network.toLowerCase() === "ethereum",
)

if (!ethereumUsdt) {
Expand All @@ -504,17 +473,17 @@ const getEthereumUsdtOption = async (): Promise<CryptoReceiveOption | IbexError>
return ethereumUsdt
}

const getEthereumUsdtOption = async (): Promise<CryptoReceiveOption | IbexError> => {
const options = await getCryptoReceiveOptions()
if (options instanceof IbexError) return options

return findEthereumUsdtReceiveOption(options)
}

const getIbexCurrencyId = async (
currency: WalletCurrency,
): Promise<IbexCurrencyId | IbexError> => {
const token = await getIbexToken()
if (token instanceof IbexError) return token

const data = await ibexGet<{ currencies: IbexCurrency[] }>(token, "/currency/all")
if (data instanceof IbexError) return data
const currencyId = data.currencies.find((c) => c.name === currency)?.id
if (!currencyId) return new IbexError(new Error(`Currency ${currency} not found`))
return currencyId
return (Ibex.getCurrencyId(currency) as Promise<IbexCurrencyId>).then(errorHandler)
}

// const sendBetweenAccounts = async (
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8867,10 +8867,10 @@ i18n@^0.15.1:
math-interval-parser "^2.0.1"
mustache "^4.2.0"

ibex-client@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/ibex-client/-/ibex-client-3.0.0.tgz#963ced9561b1e2cea0cec876ebf282f62ef69540"
integrity sha512-ATfB1qptJLv2C7K0bzzZCjTSYgJO/ihx059FCT+DjngfSkijJLOdFdFepQTcy7Cadh1ij5TdFa5+jYbr3ZiViw==
ibex-client@^3.1.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/ibex-client/-/ibex-client-3.1.1.tgz#8fe11b40f2b51d03fb6f5f613f558a06097d525b"
integrity sha512-9L2aYU+40GU1u0fQ1Ag0C1f3ncfJL0+d7YzkBEDSw1uqfaILc+Zeg23B9U3jssurJZJ+pJN2QFCWN0Ww0bsJZg==
dependencies:
api "^6.1.2"
node-cache "^5.1.2"
Expand Down
Loading