Skip to content

Commit

Permalink
fix(dex): make sure users eth is fetched
Browse files Browse the repository at this point in the history
  • Loading branch information
jjBlockchain committed Jun 27, 2023
1 parent cfc4349 commit 2996148
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { actions, model, selectors } from 'data'
import { Analytics } from 'data/types'
import { promptForSecondPassword } from 'services/sagas'

import quoteMock from './mocks/quote.json'
import * as S from './selectors'
import { actions as A } from './slice'
import type { DexSwapForm } from './types'
Expand Down Expand Up @@ -41,15 +40,13 @@ export default ({ api }: { api: APIType }) => {
})
)

const walletAddress = nonCustodialCoinAccounts[token]?.[0].address
if (!walletAddress) {
yield* put(A.fetchUserEligibilityFailure('No user wallet address'))
if (!nonCustodialCoinAccounts[token]?.length) {
yield put(actions.core.data.eth.fetchData())
yield put(actions.core.data.eth.fetchErc20Data())
}

yield put(A.fetchUserEligibilityLoading())
const userEligibility = yield* call(api.getDexUserEligibility, {
walletAddress: `${walletAddress}`
})
const userEligibility = yield* call(api.getDexUserEligibility)
yield* put(A.fetchUserEligibilitySuccess(userEligibility))
} catch (e) {
yield put(
Expand Down
4 changes: 2 additions & 2 deletions packages/blockchain-wallet-v4/src/network/api/dex/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import {

export type { DexChain, DexSwapQuote, DexToken, DexTokenWithBalance } from './types'

export default ({ apiUrl, authorizedGet, authorizedPost, get, post }) => {
export default ({ apiUrl, authorizedGet, authorizedPost, get, nabuUrl, post }) => {
return {
buildDexTx: buildDexTx({ apiUrl, authorizedPost }),
getDexChainTokens: getDexChainTokens({ apiUrl, get }), // can't get this to work
getDexChains: getDexChains({ apiUrl, get }),
getDexSwapQuote: getDexSwapQuote({ apiUrl, authorizedPost }),
getDexTokenAllowance: getDexTokenAllowance({ apiUrl, post }),
getDexUserEligibility: getDexUserEligibility({ apiUrl, authorizedGet }),
getDexUserEligibility: getDexUserEligibility({ authorizedGet, nabuUrl }),
searchDexTokens: searchDexTokens({ apiUrl, get })
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import type { RequestConfig } from '../../http'
import { DEX_NABU_GATEWAY_PREFIX } from '../constants'
import { DexUserEligibilitySchema } from '../schemas'

export const getDexUserEligibility =
({
apiUrl,
authorizedGet
authorizedGet,
nabuUrl
}: {
apiUrl: string
authorizedGet: (config: RequestConfig) => Promise<unknown>
nabuUrl: string
}) =>
({ walletAddress }: { walletAddress: string }): Promise<boolean> =>
(): Promise<boolean> =>
authorizedGet({
contentType: 'application/json',
endPoint: `${DEX_NABU_GATEWAY_PREFIX}/eligible`,
params: { product: 'DEX', walletAddress },
url: apiUrl
endPoint: `/products`,
url: nabuUrl
}).then((data) => {
try {
return DexUserEligibilitySchema.parse(data)
Expand Down
10 changes: 8 additions & 2 deletions packages/blockchain-wallet-v4/src/network/api/dex/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,17 @@ export const DexSwapQuoteSchema: z.ZodSchema<DexSwapQuote, z.ZodTypeDef, unknown
...result
}))

const DexEnabledSchema: z.ZodSchema<boolean, z.ZodTypeDef, unknown> = z
.object({
enabled: z.boolean()
})
.transform(({ enabled }) => enabled)

export const DexUserEligibilitySchema: z.ZodSchema<boolean, z.ZodTypeDef, unknown> = z
.object({
eligible: z.boolean()
dex: DexEnabledSchema
})
.transform(({ eligible }) => eligible)
.transform(({ dex }) => dex)

export const DexTokenAllowanceSchema: z.ZodSchema<DexTokenAllowance, z.ZodTypeDef, unknown> =
z.object({
Expand Down
1 change: 1 addition & 0 deletions packages/blockchain-wallet-v4/src/network/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const api = ({ apiKey, getAuthCredentials, networks, options, reauthenticate }:
apiUrl,
authorizedGet: authorizedHttp.get,
authorizedPost: authorizedHttp.post,
nabuUrl,
...http
}),
...earn({
Expand Down

0 comments on commit 2996148

Please sign in to comment.