-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(trading): add buy middleware and thunks
- Loading branch information
1 parent
2f6dda3
commit 012bb64
Showing
19 changed files
with
328 additions
and
20 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
packages/suite/src/hooks/wallet/trading/form/common/useTradingInitializer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
packages/suite/src/hooks/wallet/trading/form/common/useTradingPreviousRoute.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/suite/src/views/wallet/trading/common/TradingHeader/TradingHeader.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
suite-common/trading/src/middlewares/tradingMiddleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { createMiddleware } from '@suite-common/redux-utils'; | ||
|
||
import { tradingBuyActions } from '../actions/buyActions'; | ||
import * as tradingActions from '../actions/tradingActions'; | ||
import { INVITY_API_RELOAD_DATA_AFTER_MS } from '../constants'; | ||
import { invityAPI } from '../invityAPI'; | ||
import { TradingRootState, selectState } from '../selectors/tradingSelectors'; | ||
import { buyThunks } from '../thunks/buyThunks'; | ||
import { getTradeTypeByRoute } from '../utils'; | ||
|
||
const LOCATION_CHANGE = '@router/location-change'; | ||
|
||
/** | ||
* In the Sell and Swap section an account can be changed by a user in the select | ||
*/ | ||
export const getAccountAccordingToRoute = (state: TradingRootState) => { | ||
const { | ||
selectedAccount: { account }, | ||
// accounts, | ||
} = state.wallet; | ||
|
||
return account; | ||
}; | ||
|
||
export const tradingMiddleware = createMiddleware(async (action, { dispatch, next, getState }) => { | ||
const state = selectState(getState()); | ||
const { isLoading, lastLoadedTimestamp } = state.wallet.trading; | ||
const isRouteChange = action.type === LOCATION_CHANGE; | ||
|
||
if (action.type === tradingActions.LOAD_DATA) { | ||
const account = getAccountAccordingToRoute(state); | ||
const { platforms, coins } = state.wallet.trading.info; | ||
const { buyInfo } = state.wallet.trading.buy; | ||
|
||
const currentAccountDescriptor = invityAPI.getCurrentAccountDescriptor(); | ||
const isDifferentAccount = currentAccountDescriptor !== account?.descriptor; | ||
|
||
if ( | ||
account && | ||
!isLoading && | ||
(isDifferentAccount || | ||
lastLoadedTimestamp + INVITY_API_RELOAD_DATA_AFTER_MS < Date.now()) | ||
) { | ||
dispatch(tradingActions.tradingActions.setLoading(true)); | ||
|
||
const { invityServerEnvironment } = state.suite.settings.debug; | ||
if (invityServerEnvironment) { | ||
invityAPI.setInvityServersEnvironment(invityServerEnvironment); | ||
} | ||
|
||
const tradeType = getTradeTypeByRoute(state.router.route?.name); | ||
if (tradeType) { | ||
dispatch(tradingActions.tradingActions.setTradingActiveSection(tradeType)); | ||
} | ||
|
||
invityAPI.createInvityAPIKey(account.descriptor); | ||
|
||
if (isDifferentAccount || !platforms || !coins) { | ||
const info = await invityAPI.getInfo(); | ||
|
||
dispatch(tradingActions.tradingActions.saveInfo(info)); | ||
} | ||
|
||
if (isDifferentAccount || !buyInfo) { | ||
const buyInfoData = await dispatch(buyThunks.loadInfoThunk()).unwrap(); | ||
|
||
dispatch(tradingBuyActions.saveBuyInfo(buyInfoData)); | ||
} | ||
|
||
dispatch(tradingActions.tradingActions.setLoading(false, Date.now())); | ||
} | ||
} | ||
|
||
next(action); | ||
|
||
// get the new state after the action has been processed | ||
const newState = selectState(getState()); | ||
|
||
// TODO: native route change control? | ||
if (isRouteChange) { | ||
const routeName = newState.router.route?.name; | ||
const isBuy = routeName === 'wallet-trading-buy'; | ||
const isSell = routeName === 'wallet-trading-sell'; | ||
const isExchange = routeName === 'wallet-trading-exchange'; | ||
|
||
if (isBuy) { | ||
dispatch(tradingActions.tradingActions.setTradingActiveSection('buy')); | ||
} | ||
|
||
const wasBuy = state.router.route?.name === 'wallet-trading-buy'; | ||
const wasSell = state.router.route?.name === 'wallet-trading-sell'; | ||
const isBuyToSell = wasBuy && isSell; | ||
const isSellToBuy = wasSell && isBuy; | ||
|
||
const cleanupPrefilledFromCryptoId = | ||
!!newState.wallet.trading.prefilledFromCryptoId && | ||
((!isSell && !isExchange && !isBuy) || isBuyToSell || isSellToBuy); | ||
|
||
if (cleanupPrefilledFromCryptoId) { | ||
dispatch(tradingActions.tradingActions.setTradingFromPrefilledCryptoId(undefined)); | ||
} | ||
} | ||
|
||
return action; | ||
}); |
2 changes: 1 addition & 1 deletion
2
suite-common/trading/src/reducers/__fixtures__/buyTradingReducer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Route } from '@suite-common/suite-types/src/route'; | ||
import { Account, SelectedAccountStatus } from '@suite-common/wallet-types'; | ||
import { AddressDisplayOptions } from '@suite-common/wallet-types/src/settings'; | ||
|
||
import type { TradingState } from '../reducers/tradingReducer'; | ||
import { InvityServerEnvironment } from '../types'; | ||
|
||
// partial copy of Suite state | ||
export type TradingRootState = { | ||
wallet: { | ||
selectedAccount: SelectedAccountStatus; | ||
accounts: Account[]; | ||
trading: TradingState; | ||
}; | ||
suite: { | ||
settings: { | ||
addressDisplayType: AddressDisplayOptions; | ||
debug: { | ||
invityServerEnvironment: InvityServerEnvironment; | ||
}; | ||
}; | ||
}; | ||
router: { | ||
url: string; | ||
pathname: string; | ||
route: { | ||
name: Route['name']; | ||
}; | ||
}; | ||
}; | ||
|
||
export const selectState = (state: TradingRootState) => state; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { BuyProviderInfo, CryptoId, FiatCurrencyCode } from 'invity-api'; | ||
|
||
import { createThunk } from '@suite-common/redux-utils'; | ||
|
||
import { invityAPI } from '../invityAPI'; | ||
import { BuyInfo } from '../reducers/buyReducer'; | ||
import { regional } from '../regional'; | ||
import { TradingFiatCurrenciesProps } from '../types'; | ||
|
||
const loadInfoThunk = createThunk<BuyInfo>('buy/loadInfo', async () => { | ||
const buyInfo = await invityAPI.getBuyList(); | ||
const defaultAmountsOfFiatCurrencies: TradingFiatCurrenciesProps = new Map(); | ||
|
||
if (!buyInfo || !buyInfo.providers) { | ||
return { | ||
buyInfo: { | ||
country: regional.UNKNOWN_COUNTRY, | ||
providers: [], | ||
defaultAmountsOfFiatCurrencies, | ||
}, | ||
providerInfos: {}, | ||
supportedFiatCurrencies: new Set(), | ||
supportedCryptoCurrencies: new Set(), | ||
}; | ||
} | ||
|
||
const providerInfos: { [name: string]: BuyProviderInfo } = {}; | ||
|
||
buyInfo.providers.forEach(e => (providerInfos[e.name] = e)); | ||
|
||
const tradedFiatCurrencies: string[] = []; | ||
const tradedCoins: CryptoId[] = []; | ||
buyInfo.providers.forEach(p => { | ||
tradedFiatCurrencies.push(...p.tradedFiatCurrencies.map(c => c.toLowerCase())); | ||
tradedCoins.push(...p.tradedCoins); | ||
}); | ||
const supportedFiatCurrencies = new Set(tradedFiatCurrencies); | ||
const supportedCryptoCurrencies = new Set(tradedCoins); | ||
|
||
if (buyInfo.defaultAmountsOfFiatCurrencies) { | ||
Object.entries(buyInfo.defaultAmountsOfFiatCurrencies).forEach(([key, value]) => { | ||
defaultAmountsOfFiatCurrencies.set(key as FiatCurrencyCode, value.toString()); | ||
}); | ||
} | ||
|
||
return { | ||
buyInfo: { | ||
...buyInfo, | ||
defaultAmountsOfFiatCurrencies, | ||
}, | ||
providerInfos, | ||
supportedFiatCurrencies, | ||
supportedCryptoCurrencies, | ||
}; | ||
}); | ||
|
||
export const buyThunks = { | ||
loadInfoThunk, | ||
}; |
Oops, something went wrong.