Skip to content

Commit

Permalink
fix(swap): make swap coins more dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
plondon committed Jul 15, 2021
1 parent 7077346 commit 6804a99
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const SUPPORTED_COINS: Array<CoinType | string> = [

// used in the coin/account selector in Swap
export const SWAP_ACCOUNTS_SELECTOR: CoinAccountSelectorType = {
coins: SUPPORTED_COINS,
nonCustodialAccounts: true,
tradingAccounts: true
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,7 @@ export const getCoinAccounts = (state: RootState, ownProps: CoinAccountSelectorT
{ [key in CoinType]: Array<SwapAccountType> }
> = getCoinAccountsR(state)

const accounts = accountsR.getOrElse(
SUPPORTED_COINS.reduce((result, item) => {
result[item] = []
return result
}, {} as { [key in CoinType]: Array<SwapAccountType> })
)
const accounts = accountsR.getOrElse({})

return accounts
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import { CoinType, SwapOrderType } from 'blockchain-wallet-v4/src/types'

export const FALLBACK_DELAY = 10_000 * 2

export const getInput = (order: SwapOrderType) =>
order.pair.split('-')[0] as CoinType
export const getInput = (order: SwapOrderType) => order.pair.split('-')[0] as CoinType

export const getOutput = (order: SwapOrderType) =>
order.pair.split('-')[1] as CoinType
export const getOutput = (order: SwapOrderType) => order.pair.split('-')[1] as CoinType

export const getInputFromPair = (pair: string) => pair.split('-')[0] as CoinType

export const getOutputFromPair = (pair: string) =>
pair.split('-')[1] as CoinType
export const getOutputFromPair = (pair: string) => pair.split('-')[1] as CoinType
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,8 @@ export default ({ api, coreSagas, networks }: { api: APIType; coreSagas; network
return
}

const accounts = getCoinAccounts(yield select(), SWAP_ACCOUNTS_SELECTOR)
const coins = S.getCoins()
const accounts = getCoinAccounts(yield select(), { coins, ...SWAP_ACCOUNTS_SELECTOR })
const baseAccount = accounts[initSwapFormValues.BASE.coin].find(
(val) => val.label === initSwapFormValues.BASE?.label
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,6 @@ export const getIncomingAmount = (state: RootState) => {
}
})(quoteR)
}

export const getCoins = () =>
Object.keys(window.coins).filter((coin) => !window.coins[coin].coinfig.type.isFiat)
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export const getData = (state) => {
const walletCurrencyR = selectors.core.settings.getCurrency(state)

// for sell, get 'swap' accounts
const accounts = getCoinAccounts(state, SWAP_ACCOUNTS_SELECTOR)
const coins = selectors.components.swap.getCoins()
const accounts = getCoinAccounts(state, { coins, ...SWAP_ACCOUNTS_SELECTOR })

return lift(
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class CoinSelection extends PureComponent<Props> {
)}
</Text>
</StickyHeaderFlyoutWrapper>
{SUPPORTED_COINS.map((coin) => {
{coins?.map((coin) => {
const accounts = (this.props.accounts[coin] as Array<SwapAccountType>) || []
return accounts.map((account) => {
const isAccountSelected = this.checkAccountSelected(this.props.side, values, account)
Expand Down Expand Up @@ -170,6 +170,7 @@ class CoinSelection extends PureComponent<Props> {
}

const mapStateToProps = (state: RootState, ownProps: OwnProps) => ({
coins: selectors.components.swap.getCoins(),
custodialEligibility: selectors.components.swap.getCustodialEligibility(state).getOrElse(false),
values: selectors.form.getFormValues('initSwap')(state) as InitSwapFormValuesType,
...getData(state, ownProps)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { RootState } from 'data/rootReducer'
import { OwnProps } from '.'

export const getData = (state: RootState, { side }: OwnProps) => {
const accounts = getCoinAccounts(state, SWAP_ACCOUNTS_SELECTOR)
const coins = selectors.components.swap.getCoins()
const accounts = getCoinAccounts(state, { coins, ...SWAP_ACCOUNTS_SELECTOR })
const pairs = selectors.components.swap.getPairs(state).getOrElse([])
let coinsForSide

Expand All @@ -22,17 +23,14 @@ export const getData = (state: RootState, { side }: OwnProps) => {
coinsForSide = uniq(pairs.map(sideF))

// This will only work if the coin is disabled for to and from
const accountsForSide = coinsForSide.reduce(
(prevValue, curValue: CoinType) => {
if (!prevValue[curValue]) {
return {
...prevValue,
[curValue]: accounts[curValue]
}
const accountsForSide = coinsForSide.reduce((prevValue, curValue: CoinType) => {
if (!prevValue[curValue]) {
return {
...prevValue,
[curValue]: accounts[curValue]
}
},
{}
) as { [key in CoinType]: Array<SwapAccountType> }
}
}, {}) as { [key in CoinType]: Array<SwapAccountType> }

return { accounts: accountsForSide }
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const getData = (state: RootState) => {
state
)
const walletCurrencyR = selectors.core.settings.getCurrency(state)
const accounts = getCoinAccounts(state, SWAP_ACCOUNTS_SELECTOR)
const coins = selectors.components.swap.getCoins()
const accounts = getCoinAccounts(state, { coins, ...SWAP_ACCOUNTS_SELECTOR })
return lift(
(
incomingAmount: ExtractSuccess<typeof incomingAmountR>,
Expand All @@ -39,7 +40,7 @@ export const getData = (state: RootState) => {
limits,
payment: paymentR.getOrElse(undefined),
quote,
walletCurrency,
walletCurrency
})
)(incomingAmountR, limitsR, quoteR, baseRatesR, walletCurrencyR)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { createDeepEqualSelector } from 'blockchain-wallet-v4/src/utils'
import { selectors } from 'data'
import { SWAP_ACCOUNTS_SELECTOR } from 'data/coins/model/swap'
import { getCoinAccounts } from 'data/coins/selectors'
import { RootState } from 'data/rootReducer'

export const getData = createDeepEqualSelector(
[(state: RootState) => getCoinAccounts(state, SWAP_ACCOUNTS_SELECTOR)],
accounts => {
return { accounts }
}
)
export const getData = (state: RootState) => {
const coins = selectors.components.swap.getCoins()
const accounts = getCoinAccounts(state, { coins, ...SWAP_ACCOUNTS_SELECTOR })
return { accounts }
}

export default getData

0 comments on commit 6804a99

Please sign in to comment.