Skip to content

Commit

Permalink
feat: leverage current RUNE rewards default AccountId
Browse files Browse the repository at this point in the history
  • Loading branch information
gomesalexandre committed Aug 19, 2024
1 parent 7ec45ac commit 95d613a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
21 changes: 16 additions & 5 deletions src/pages/RFOX/components/AddressSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import {
Input,
Stack,
} from '@chakra-ui/react'
import { fromAccountId, thorchainAssetId, thorchainChainId } from '@shapeshiftoss/caip'
import { fromAccountId, thorchainAssetId, thorchainChainId, toAccountId } from '@shapeshiftoss/caip'
import type { FC } from 'react'
import { useCallback, useMemo, useState } from 'react'
import { useForm, useFormContext } from 'react-hook-form'
import { useTranslate } from 'react-polyglot'
import { AccountDropdown } from 'components/AccountDropdown/AccountDropdown'
import { validateAddress } from 'lib/address/address'

import { selectRuneAddress } from '../helpers'
import { useRFOXContext } from '../hooks/useRfoxContext'
import { useStakingInfoQuery } from '../hooks/useStakingInfoQuery'
import type { AddressSelectionValues } from '../types'

type AddressSelectionProps = {
Expand Down Expand Up @@ -51,8 +53,6 @@ export const AddressSelection: FC<AddressSelectionProps> = ({
}) => {
const translate = useTranslate()

const { runeMatchingAccountId } = useRFOXContext()

// Local controller in case consumers don't have a form context
const _methods = useForm<AddressSelectionValues>()
const methods = useFormContext<AddressSelectionValues>()
Expand All @@ -61,6 +61,14 @@ export const AddressSelection: FC<AddressSelectionProps> = ({
const formState = methods?.formState ?? _methods.formState
const { errors } = formState

const { stakingAssetAccountId } = useRFOXContext()
const { data: currentRuneAddress } = useStakingInfoQuery({
stakingAssetAccountAddress: stakingAssetAccountId
? fromAccountId(stakingAssetAccountId).account
: undefined,
select: selectRuneAddress,
})

const [isManualAddress, setIsManualAddress] = useState(false)

const handleAccountIdChange = useCallback(
Expand Down Expand Up @@ -138,17 +146,20 @@ export const AddressSelection: FC<AddressSelectionProps> = ({

const accountSelection = useMemo(() => {
if (isManualAddress) return null
const maybeRuneAccountId = currentRuneAddress
? toAccountId({ account: currentRuneAddress, chainId: thorchainChainId })
: undefined

return (
<AccountDropdown
defaultAccountId={runeMatchingAccountId}
defaultAccountId={maybeRuneAccountId}
assetId={thorchainAssetId}
onChange={handleAccountIdChange}
boxProps={boxProps}
buttonProps={buttonProps}
/>
)
}, [handleAccountIdChange, isManualAddress, runeMatchingAccountId])
}, [currentRuneAddress, handleAccountIdChange, isManualAddress])

const addressSelectionLabel = useMemo(
() =>
Expand Down
18 changes: 1 addition & 17 deletions src/pages/RFOX/hooks/useRfoxContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
type AssetId,
foxOnArbitrumOneAssetId,
fromAssetId,
thorchainChainId,
} from '@shapeshiftoss/caip'
import React, { createContext, useContext, useMemo, useState } from 'react'
import {
Expand All @@ -19,7 +18,6 @@ type RFOXContextType = {
selectedAssetId: AssetId
stakingAssetAccountId: AccountId | undefined
stakingAssetId: AssetId
runeMatchingAccountId: AccountId | undefined
setSelectedAssetId: (assetId: AssetId) => void
setStakingAssetAccountId: React.Dispatch<React.SetStateAction<AccountId | undefined>>
}
Expand Down Expand Up @@ -65,13 +63,6 @@ export const RFOXProvider: React.FC<React.PropsWithChildren<{ stakingAssetId: As
return matchingAccountId
}, [accountIdsByAccountNumberAndChainId, filter, selectedAssetId, stakingAssetAccountNumber])

const runeMatchingAccountId = useMemo(() => {
if (!(filter && stakingAssetAccountNumber !== undefined)) return
const accountNumberAccountIds = accountIdsByAccountNumberAndChainId[stakingAssetAccountNumber]
const runeAccountId = accountNumberAccountIds?.[thorchainChainId]
return runeAccountId
}, [accountIdsByAccountNumberAndChainId, filter, stakingAssetAccountNumber])

const value: RFOXContextType = useMemo(
() => ({
selectedAssetAccountId,
Expand All @@ -80,15 +71,8 @@ export const RFOXProvider: React.FC<React.PropsWithChildren<{ stakingAssetId: As
selectedAssetId,
stakingAssetAccountId,
stakingAssetId,
runeMatchingAccountId,
}),
[
runeMatchingAccountId,
selectedAssetAccountId,
selectedAssetId,
stakingAssetAccountId,
stakingAssetId,
],
[selectedAssetAccountId, selectedAssetId, stakingAssetAccountId, stakingAssetId],
)

return <RFOXContext.Provider value={value}>{children}</RFOXContext.Provider>
Expand Down

0 comments on commit 95d613a

Please sign in to comment.