Skip to content

Commit

Permalink
fix: native token typing
Browse files Browse the repository at this point in the history
  • Loading branch information
moritzkirstein committed Mar 11, 2024
1 parent 72a4967 commit 3a9f219
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 29 deletions.
17 changes: 13 additions & 4 deletions src/@context/Automation/AutomationProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ function AutomationProvider({ children }) {
})

const [balance, setBalance] = useState<UserBalance>({
native: {}
native: {
symbol: 'eth',
balance: '0'
}
})

const [hasDeleteRequest, setHasDeleteRequest] = useState(false)
Expand Down Expand Up @@ -94,10 +97,16 @@ function AutomationProvider({ children }) {
if (!autoWallet) return

try {
const newBalance: UserBalance = { native: {} }
const newBalance: UserBalance = {
native: {
symbol: 'eth',
balance: '0'
}
}
if (balanceNativeToken)
newBalance.native[balanceNativeToken?.symbol.toLowerCase() || 'ETH'] =
balanceNativeToken?.formatted
newBalance.native.symbol =
balanceNativeToken?.symbol.toLowerCase() || 'eth'
newBalance.native.balance = balanceNativeToken?.formatted

if (approvedBaseTokens?.length > 0) {
const approved = await getApprovedTokenBalances(autoWallet?.address)
Expand Down
9 changes: 6 additions & 3 deletions src/@hooks/useBalance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ function useBalance(): BalanceProviderValue {

const [balance, setBalance] = useState<UserBalance>({
native: {
eth: '0'
symbol: 'eth',
balance: '0'
}
})

Expand Down Expand Up @@ -66,10 +67,12 @@ function useBalance(): BalanceProviderValue {
try {
const userBalance = balanceNativeToken?.formatted
const key = balanceNativeToken?.symbol.toLowerCase()
const newNativeBalance: TokenBalances = { [key]: userBalance }

const newBalance: UserBalance = {
native: newNativeBalance,
native: {
symbol: key,
balance: userBalance
},
approved: await getApprovedTokenBalances(address)
}

Expand Down
5 changes: 4 additions & 1 deletion src/@types/TokenBalance.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
interface UserBalance {
native: TokenBalances
native: {
symbol: string
balance: string
}
approved?: TokenBalances
}

Expand Down
10 changes: 5 additions & 5 deletions src/components/Header/UserPreferences/Automation/Balance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export default function Balance(): ReactElement {
<div className={styles.wrapper}>
<div className={styles.balance}>
<ul>
{Object.keys(balance.native).map((symbol) => (
<li key={`automation-balance-${symbol}`}>
<span>{symbol}</span>:{' '}
{Number(balance.native[symbol]).toFixed(4)}
{balance.native && (
<li key={`automation-balance-${balance.native.symbol}`}>
<span>{balance.native.symbol}</span>:{' '}
{Number(balance.native.balance).toFixed(4)}
</li>
))}
)}
</ul>
</div>
<div className={styles.balance}>
Expand Down
8 changes: 4 additions & 4 deletions src/components/Header/UserPreferences/Automation/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export default function Details({
const {
autoWallet,
autoWalletAddress,
balance,
isAutomationEnabled,
nativeBalance,
isLoading,
hasValidEncryptedWallet,
setIsAutomationEnabled
Expand All @@ -84,11 +84,11 @@ export default function Details({
}, [hasValidEncryptedWallet])

useEffect(() => {
if (!automationConfig.roughTxGasEstimate || !nativeBalance?.balance) return
if (!automationConfig.roughTxGasEstimate) return
setRoughTxCountEstimate(
Number(nativeBalance.balance) / automationConfig.roughTxGasEstimate
Number(balance.native.balance) / automationConfig.roughTxGasEstimate
)
}, [nativeBalance?.balance, automationConfig?.roughTxGasEstimate])
}, [balance.native, automationConfig?.roughTxGasEstimate])

return (
<div className={styles.details}>
Expand Down
20 changes: 8 additions & 12 deletions src/components/Header/UserPreferences/Automation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,23 @@ import styles from './index.module.css'
const cx = classNames.bind(styles)

export default function Automation(): ReactElement {
const {
autoWallet,
isAutomationEnabled,
hasValidEncryptedWallet,
balance,
nativeBalance
} = useAutomation()
const { autoWallet, isAutomationEnabled, hasValidEncryptedWallet, balance } =
useAutomation()

const [hasZeroNetworkTokenBalance, setHasZeroNetworkTokenBalance] =
useState<boolean>(false)
const [hasZeroERC20TokenBalances, setHasZeroERC20TokenBalances] =
useState<boolean>(false)

useEffect(() => {
balance &&
balance.approved &&
setHasZeroERC20TokenBalances(
Object.keys(balance)?.filter((token) => Number(balance[token]) <= 0)
.length > 0
Object.keys(balance.approved)?.filter(
(token) => Number(balance.approved[token]) <= 0
).length > 0
)
setHasZeroNetworkTokenBalance(Number(nativeBalance?.balance) <= 0)
}, [balance, nativeBalance])
setHasZeroNetworkTokenBalance(Number(balance.native.balance) <= 0)
}, [balance])

const wrapperClasses = cx({
automation: true,
Expand Down

0 comments on commit 3a9f219

Please sign in to comment.