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
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ services:
# - lnd-outside-1
- svix
# - lnd-outside-2
- otel-agent
restart: on-failure:10
integration-deps:
image: busybox
Expand Down
2 changes: 1 addition & 1 deletion src/app/lightning/payment-status-checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const PaymentStatusChecker = async (uncheckedPaymentRequest: string) => {
isExpired,
// invoiceIsPaid should have no awareness of Ibex. TODO: add to Wallets interface
invoiceIsPaid: async (): Promise<boolean | IbexClientError> => {
const ibexResp = await Ibex.invoiceFromHash({ invoice_hash: paymentHash })
const ibexResp = await Ibex().invoiceFromHash({ invoice_hash: paymentHash })
if (ibexResp instanceof IbexClientError) return ibexResp
baseLogger.info(ibexResp.status)
return ibexResp.state?.id === 1 // Invoice states: https://docs.ibexmercado.com/reference/flow#invoice-states-table
Expand Down
2 changes: 1 addition & 1 deletion src/app/wallets/create-on-chain-address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const createOnChainAddress = async ({
const accountValidator = AccountValidator(account)
if (accountValidator instanceof Error) return accountValidator

const resp = await Ibex.generateBitcoinAddress({ accountId: walletId })
const resp = await Ibex().generateBitcoinAddress({ accountId: walletId })
if (resp instanceof IbexClientError) return resp
else if (!resp.address) return new IbexClientError("Address not returned")
else return resp.address
Expand Down
2 changes: 1 addition & 1 deletion src/app/wallets/get-transactions-for-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const getTransactionsForWallets = async ({

// Flash fork: return history from Ibex
const ibexCalls = await Promise.all(walletIds
.map(id => Ibex.getAccountTransactions({
.map(id => Ibex().getAccountTransactions({
account_id: id,
}))
)
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/public/root/mutation/ln-invoice-fee-probe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const LnInvoiceFeeProbeMutation = GT.Field<
// walletId,
// uncheckedPaymentRequest: paymentRequest,
// })
const resp: any | IbexClientError = await Ibex.getFeeEstimation({
const resp: any | IbexClientError = await Ibex().getFeeEstimation({
// walletId, // we are not checking internal payment flow
bolt11: paymentRequest,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const LnInvoicePaymentSendMutation = GT.Field<
*/

if (!domainAccount) throw new Error("Authentication required")
const PayLightningInvoice = await Ibex.payInvoiceV2({
const PayLightningInvoice = await Ibex().payInvoiceV2({
bolt11: paymentRequest,
accountId: walletId,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const LnNoAmountInvoiceCreateMutation = GT.Field({
// FLASH FORK: create IBEX invoice instead of Galoy invoice

// TODO: move this into Wallets.addInvoiceNoAmountForSelf
const resp = await Ibex.addInvoice({
const resp = await Ibex().addInvoice({
amount: 0,
accountId: walletId,
memo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const LnNoAmountUsdInvoiceFeeProbeMutation = GT.Field({
// })

// TODO: Move Ibex call to Payments interface
const resp: any | IbexClientError = await Ibex.getFeeEstimation({
const resp: any | IbexClientError = await Ibex().getFeeEstimation({
bolt11: paymentRequest,
amount: String(amount / 100),
currencyId: "3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const LnNoAmountUsdInvoicePaymentSendMutation = GT.Field<
if (!domainAccount) throw new Error("Authentication required")
// eslint-disable-next-line @typescript-eslint/no-explicit-any

const PayLightningInvoice = await Ibex.payInvoiceV2({
const PayLightningInvoice = await Ibex().payInvoiceV2({
bolt11: paymentRequest,
accountId: walletId,
amount: amount / 100,
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/public/root/mutation/ln-usd-invoice-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const LnUsdInvoiceCreateMutation = GT.Field({
}

// FLASH FORK: create IBEX invoice instead of Galoy invoice
const resp = await Ibex.addInvoice({
const resp = await Ibex().addInvoice({
amount: amount / 100,
accountId: walletId,
memo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const LnUsdInvoiceFeeProbeMutation = GT.Field<
// uncheckedPaymentRequest: paymentRequest,
// })

const resp: any | IbexClientError = await Ibex.getFeeEstimation({
const resp: any | IbexClientError = await Ibex().getFeeEstimation({
// walletId, // we are not checking internal payment flow
bolt11: paymentRequest,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const OnChainAddressCurrentMutation = GT.Field({
// const address = await Wallets.getLastOnChainAddress(walletId)
// eslint-disable-next-line @typescript-eslint/no-explicit-any

const resp = await Ibex.generateBitcoinAddress({
const resp = await Ibex().generateBitcoinAddress({
accountId: walletId,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const OnChainUsdPaymentSendAsBtcDenominatedMutation = GT.Field<
// })
if (!domainAccount) throw new Error("Authentication required")

const resp = await Ibex.sendToAddressV2({
const resp = await Ibex().sendToAddressV2({
accountId: walletId,
address,
amount: toCents(amount),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const OnChainUsdPaymentSendMutation = GT.Field<
// })
if (!domainAccount) throw new Error("Authentication required")

const resp = await Ibex.sendToAddressV2({
const resp = await Ibex().sendToAddressV2({
accountId: walletId,
address,
amount: amount / 100,
Expand Down
6 changes: 3 additions & 3 deletions src/graphql/public/root/query/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const feesConfig = getFeesConfig()
const GlobalsQuery = GT.Field({
type: Globals,
resolve: async () => {
let nodesIds = await Lightning.listNodesPubkeys()
if (nodesIds instanceof Error) nodesIds = []
// let nodesIds = await Lightning.listNodesPubkeys()
// if (nodesIds instanceof Error) nodesIds = []

return {
nodesIds,
nodesIds: [],
network: NETWORK,
lightningAddressDomain: getLightningAddressDomain(),
lightningAddressDomainAliases: getLightningAddressDomainAliases(),
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/public/root/query/on-chain-usd-tx-fee-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const OnChainUsdTxFeeQuery = GT.Field<null, GraphQLPublicContextAuth>({
// speed,
// })

const resp = await Ibex.estimateFeeV2({
const resp = await Ibex().estimateFeeV2({
"currency-id": "3", // ref/create USD enum/constant
address: address,
amount: amount / 100,
Expand Down
32 changes: 13 additions & 19 deletions src/services/ibex/client/authentication/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { IbexApiError, IbexAuthenticationError, IbexClientError } from "../error
import Redis from "./redis-datastore";
import { FetchResponse } from "api/dist/core";
import { CacheServiceError, CacheUndefinedError } from "@domain/cache";
import { baseLogger as log } from "@services/logger";
import { logResponse } from "../errors/logger";
import { wrapAsyncToRunInSpan } from "@services/tracing";

// TODO: Divide this into setAccessToken and setRefreshToken which take Partial<SignInResponse200>
const storeTokens = async (signInResp: SignInResponse200): Promise<void> => {
Expand All @@ -17,30 +16,27 @@ const storeTokens = async (signInResp: SignInResponse200): Promise<void> => {
} = signInResp

if (!accessToken) return Promise.reject(new IbexClientError("No access token found in Ibex response body"))
const atResp = await Redis.setAccessToken(accessToken, accessTokenExpiresAt)
await Redis.setAccessToken(accessToken, accessTokenExpiresAt)
IbexSDK.auth(accessToken)

if (!refreshToken) return Promise.reject(new IbexClientError("No refresh token found in Ibex response body"))
const rtResp = await Redis.setRefreshToken(refreshToken, refreshTokenExpiresAt)

if (atResp instanceof CacheServiceError) log.warn(`IBEX: Failed to write accessToken to redis cache: ${atResp.message}`)
if (rtResp instanceof CacheServiceError) log.warn(`IBEX: Failed to write refreshToken to redis cache: ${rtResp.message}`)
await Redis.setRefreshToken(refreshToken, refreshTokenExpiresAt)
}

const signIn = async (): Promise<void | IbexApiError> => {
log.info("IBEX: Signing in...")
return IbexSDK.signIn({ email: IBEX_EMAIL, password: IBEX_PASSWORD })
.then(_ => _.data)
.then(_ => storeTokens(_))
.catch(e => new IbexApiError(e.status, e.data))
.then(logResponse)
}
const signIn = wrapAsyncToRunInSpan({
namespace: "service.ibex.client",
fnName: "signIn",
fn: async (): Promise<void | IbexApiError> => {
return IbexSDK.signIn({ email: IBEX_EMAIL, password: IBEX_PASSWORD })
.then(_ => _.data)
.then(_ => storeTokens(_))
.catch(e => new IbexApiError(e.status, e.data))
}
})

const refreshAccessToken = async (): Promise<void | IbexAuthenticationError> => {
log.info("IBEX: Refreshing token...")
const tokenOrErr = await Redis.getRefreshToken()
if (tokenOrErr instanceof CacheUndefinedError) {
log.info("IBEX: Refresh token not found.")
return await signIn().catch((e: IbexApiError) => new IbexAuthenticationError(e))
}
if (tokenOrErr instanceof CacheServiceError) return new IbexAuthenticationError(tokenOrErr)
Expand All @@ -60,7 +56,6 @@ export const withAuth = async <S, T>(apiCall: () => Promise<FetchResponse<S, T>>
const atResp = await Redis.getAccessToken()

if (atResp instanceof CacheUndefinedError) {
log.info("IBEX: Access token not found.")
const refreshResp = await refreshAccessToken()
if (refreshResp instanceof IbexAuthenticationError) return refreshResp
} else if (atResp instanceof CacheServiceError) return new IbexAuthenticationError(atResp)
Expand All @@ -70,7 +65,6 @@ export const withAuth = async <S, T>(apiCall: () => Promise<FetchResponse<S, T>>
return (await apiCall()).data
} catch (err: any) {
if (err.status === 401) {
log.info("IBEX: Access token unauthorized.")
const refreshResp = await refreshAccessToken()
if (refreshResp instanceof IbexAuthenticationError) return refreshResp
return (await apiCall()).data
Expand Down
12 changes: 0 additions & 12 deletions src/services/ibex/client/errors/logger.ts

This file was deleted.

Loading