-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add MainNeedsFunds component to display funding requirements an…
…d integrate with low funds alert
- Loading branch information
1 parent
ead9d08
commit 7cb42a7
Showing
7 changed files
with
201 additions
and
157 deletions.
There are no files selected for viewing
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: 2 additions & 0 deletions
2
frontend/components/MainPage/sections/AlertSections/LowFunds/LowFunds.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
111 changes: 111 additions & 0 deletions
111
frontend/components/MainPage/sections/AlertSections/LowFunds/MainNeedsFunds.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { Flex, Typography } from 'antd'; | ||
import { useEffect, useMemo } from 'react'; | ||
|
||
import { CustomAlert } from '@/components/Alert'; | ||
import { getNativeTokenSymbol } from '@/config/tokens'; | ||
import { UNICODE_SYMBOLS } from '@/constants/symbols'; | ||
import { TokenSymbol } from '@/enums/Token'; | ||
import { useElectronApi } from '@/hooks/useElectronApi'; | ||
import { useNeedsFunds } from '@/hooks/useNeedsFunds'; | ||
import { useServices } from '@/hooks/useServices'; | ||
import { useStakingProgram } from '@/hooks/useStakingProgram'; | ||
|
||
import { InlineBanner } from './InlineBanner'; | ||
import { useLowFundsDetails } from './useLowFunds'; | ||
|
||
const { Text, Title } = Typography; | ||
|
||
export const MainNeedsFunds = () => { | ||
const { selectedStakingProgramId } = useStakingProgram(); | ||
|
||
const { | ||
hasEnoughEthForInitialFunding, | ||
hasEnoughOlasForInitialFunding, | ||
serviceFundRequirements, | ||
isInitialFunded, | ||
needsInitialFunding, | ||
} = useNeedsFunds(selectedStakingProgramId); | ||
|
||
const { selectedAgentConfig } = useServices(); | ||
const { evmHomeChainId: homeChainId } = selectedAgentConfig; | ||
const nativeTokenSymbol = getNativeTokenSymbol(homeChainId); | ||
const { chainName, masterSafeAddress } = useLowFundsDetails(); | ||
|
||
const electronApi = useElectronApi(); | ||
|
||
useEffect(() => { | ||
if ( | ||
hasEnoughEthForInitialFunding && | ||
hasEnoughOlasForInitialFunding && | ||
!isInitialFunded | ||
) { | ||
electronApi.store?.set?.('isInitialFunded', true); | ||
} | ||
}, [ | ||
electronApi.store, | ||
hasEnoughEthForInitialFunding, | ||
hasEnoughOlasForInitialFunding, | ||
isInitialFunded, | ||
]); | ||
|
||
const olasRequired = useMemo(() => { | ||
if (hasEnoughOlasForInitialFunding) return null; | ||
const olas = serviceFundRequirements[homeChainId][TokenSymbol.OLAS]; | ||
return `${UNICODE_SYMBOLS.OLAS}${olas} OLAS `; | ||
}, [homeChainId, hasEnoughOlasForInitialFunding, serviceFundRequirements]); | ||
|
||
const nativeTokenRequired = useMemo(() => { | ||
if (hasEnoughEthForInitialFunding) return null; | ||
const native = serviceFundRequirements[homeChainId][nativeTokenSymbol]; | ||
return `${native} ${nativeTokenSymbol}`; | ||
}, [ | ||
homeChainId, | ||
hasEnoughEthForInitialFunding, | ||
serviceFundRequirements, | ||
nativeTokenSymbol, | ||
]); | ||
|
||
if (!needsInitialFunding) return null; | ||
|
||
return ( | ||
<CustomAlert | ||
fullWidth | ||
showIcon | ||
message={ | ||
<Flex vertical gap={8} align="flex-start"> | ||
<Title level={5} style={{ margin: 0 }}> | ||
Fund your agent | ||
</Title> | ||
|
||
<Text> | ||
To activate your agent, add these amounts on {chainName} chain to | ||
your safe: | ||
</Text> | ||
|
||
<Flex gap={0} vertical> | ||
{!hasEnoughOlasForInitialFunding && ( | ||
<div> | ||
{UNICODE_SYMBOLS.BULLET} <Text strong>{olasRequired}</Text> - | ||
for staking. | ||
</div> | ||
)} | ||
{!hasEnoughEthForInitialFunding && ( | ||
<div> | ||
{UNICODE_SYMBOLS.BULLET}{' '} | ||
<Text strong>{nativeTokenRequired}</Text> - for trading. | ||
</div> | ||
)} | ||
</Flex> | ||
|
||
{masterSafeAddress && ( | ||
<InlineBanner | ||
text="Your safe address" | ||
address={masterSafeAddress} | ||
/> | ||
)} | ||
</Flex> | ||
} | ||
type="primary" | ||
/> | ||
); | ||
}; |
31 changes: 31 additions & 0 deletions
31
frontend/components/MainPage/sections/AlertSections/LowFunds/useLowFunds.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,31 @@ | ||
import { useMemo } from 'react'; | ||
|
||
import { useChainDetails } from '@/hooks/useChainDetails'; | ||
import { useServices } from '@/hooks/useServices'; | ||
import { useMasterWalletContext } from '@/hooks/useWallet'; | ||
|
||
export const useLowFundsDetails = () => { | ||
const { selectedAgentConfig } = useServices(); | ||
const homeChainId = selectedAgentConfig.evmHomeChainId; | ||
|
||
const { masterSafes } = useMasterWalletContext(); | ||
|
||
// master safe details | ||
const selectedMasterSafe = useMemo(() => { | ||
if (!masterSafes) return; | ||
if (!homeChainId) return; | ||
|
||
return masterSafes.find( | ||
(masterSafe) => masterSafe.evmChainId === homeChainId, | ||
); | ||
}, [masterSafes, homeChainId]); | ||
|
||
// current chain details | ||
const { name: chainName, symbol: tokenSymbol } = useChainDetails(homeChainId); | ||
|
||
return { | ||
chainName, | ||
tokenSymbol, | ||
masterSafeAddress: selectedMasterSafe?.address, | ||
}; | ||
}; |
Oops, something went wrong.