Skip to content

Commit

Permalink
feat: extend manage wallet with erc20 tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanya-atatakai committed Dec 20, 2024
1 parent 1fa178d commit 05b0164
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const FundsToActivate = ({
(token) => token !== TokenSymbol.OLAS && token !== nativeTokenSymbol,
);

if (additionalTokens.length === 0) [];
if (additionalTokens.length === 0) return [];

return additionalTokens.map((tokenSymbol) => {
const token = serviceFundRequirements[homeChainId][tokenSymbol];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const MainNeedsFunds = () => {
if (
hasEnoughEthForInitialFunding &&
hasEnoughOlasForInitialFunding &&
hasEnoughAdditionalTokensForInitialFunding &&
!isInitialFunded
) {
electronApi.store?.set?.(`isInitialFunded_${selectedAgentType}`, true);
Expand All @@ -38,6 +39,7 @@ export const MainNeedsFunds = () => {
selectedAgentType,
hasEnoughEthForInitialFunding,
hasEnoughOlasForInitialFunding,
hasEnoughAdditionalTokensForInitialFunding,
isInitialFunded,
]);

Expand Down
29 changes: 21 additions & 8 deletions frontend/components/MainPage/sections/OlasBalanceSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const Balance = styled.span`
`;

export const MainOlasBalance = () => {
const { selectedService } = useServices();
const { selectedService, selectedAgentConfig } = useServices();
const { isLoaded: isBalanceLoaded } = useBalanceContext();
const { masterWalletBalances } = useMasterBalances();
const { serviceStakedBalances, serviceWalletBalances } = useServiceBalances(
Expand All @@ -38,8 +38,11 @@ export const MainOlasBalance = () => {
const displayedBalance = useMemo(() => {
// olas across master wallets, safes and eoa
const masterWalletOlasBalance = masterWalletBalances?.reduce(
(acc, { symbol, balance }) => {
if (symbol === TokenSymbol.OLAS) {
(acc, { symbol, balance, evmChainId }) => {
if (
selectedAgentConfig.requiresAgentSafesOn.includes(evmChainId) &&
symbol === TokenSymbol.OLAS
) {
return acc + Number(balance);
}
return acc;
Expand All @@ -49,8 +52,11 @@ export const MainOlasBalance = () => {

// olas across all service wallets
const serviceWalletOlasBalance = serviceWalletBalances?.reduce(
(acc, { symbol, balance }) => {
if (symbol === TokenSymbol.OLAS) {
(acc, { symbol, evmChainId, balance }) => {
if (
selectedAgentConfig.requiresAgentSafesOn.includes(evmChainId) &&
symbol === TokenSymbol.OLAS
) {
return acc + Number(balance);
}
return acc;
Expand All @@ -60,8 +66,10 @@ export const MainOlasBalance = () => {

// olas staked across all services
const serviceStakedOlasBalance = serviceStakedBalances?.reduce(
(acc, { olasBondBalance, olasDepositBalance }) => {
return acc + Number(olasBondBalance) + Number(olasDepositBalance);
(acc, { olasBondBalance, olasDepositBalance, evmChainId }) => {
if (selectedAgentConfig.requiresAgentSafesOn.includes(evmChainId)) {
return acc + Number(olasBondBalance) + Number(olasDepositBalance);
} else return acc;
},
0,
);
Expand All @@ -73,7 +81,12 @@ export const MainOlasBalance = () => {
]);

return balanceFormat(totalOlasBalance, 2);
}, [masterWalletBalances, serviceStakedBalances, serviceWalletBalances]);
}, [
selectedAgentConfig,
masterWalletBalances,
serviceStakedBalances,
serviceWalletBalances,
]);

return (
<CardSection
Expand Down
21 changes: 21 additions & 0 deletions frontend/components/YourWalletPage/YourAgent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,17 @@ const YourAgentWalletBreakdown = () => {
[serviceSafeBalances, evmHomeChainId],
);

const serviceSafeErc20Balances = useMemo(
() =>
serviceSafeBalances?.filter(
({ isNative, symbol, evmChainId }) =>
!isNative &&
symbol !== TokenSymbol.OLAS &&
evmChainId === evmHomeChainId,
),
[serviceSafeBalances, evmHomeChainId],
);

const serviceEoaNativeBalances = useMemo(
() =>
serviceEoaBalances?.filter(
Expand Down Expand Up @@ -266,6 +277,16 @@ const YourAgentWalletBreakdown = () => {
parentStyle={infoBreakdownParentStyle}
/>
)}
{isArray(serviceSafeErc20Balances) && (
<InfoBreakdownList
list={serviceSafeErc20Balances.map((balance) => ({
left: <strong>{balance.symbol}</strong>,
leftClassName: 'text-sm',
right: `${balanceFormat(balance.balance, 2)} ${balance.symbol}`,
}))}
parentStyle={infoBreakdownParentStyle}
/>
)}
{!isNil(serviceEoa) && (
<InfoBreakdownList
list={[
Expand Down
52 changes: 52 additions & 0 deletions frontend/components/YourWalletPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,57 @@ const MasterSafeNativeBalance = () => {
);
};

const MasterSafeErc20Balances = () => {
const { evmHomeChainId, masterSafeAddress, middlewareChain } =
useYourWallet();
const { masterSafeBalances } = useMasterBalances();

const masterSafeErc20Balances = useMemo(() => {
if (isNil(masterSafeAddress)) return;
if (isNil(masterSafeBalances)) return;

return masterSafeBalances
.filter(({ walletAddress, evmChainId, symbol, isNative }) => {
return (
evmChainId === evmHomeChainId && // TODO: address multi chain, need to refactor as per product requirement
!isNative &&
symbol !== TokenSymbol.OLAS &&
walletAddress === masterSafeAddress
);
})
.reduce<{ [tokenSymbol: string]: number }>((acc, { balance, symbol }) => {
if (!acc[symbol]) acc[symbol] = 0;
acc[symbol] += balance;

return acc;
}, {});
}, [masterSafeBalances, masterSafeAddress, evmHomeChainId]);

if (!masterSafeErc20Balances) return null;

return (
<Flex vertical gap={8}>
{Object.entries(masterSafeErc20Balances).map(([symbol, balance]) => (
<InfoBreakdownList
key={symbol}
list={[
{
left: (
<Text strong>
{symbol} ({capitalize(middlewareChain)})
</Text>
),
leftClassName: 'text-light',
right: `${balanceFormat(balance, 2)} ${symbol}`,
},
]}
parentStyle={infoBreakdownParentStyle}
/>
))}
</Flex>
);
};

const MasterEoaSignerNativeBalance = () => {
const { masterEoa } = useMasterWalletContext();
const { masterWalletBalances } = useMasterBalances();
Expand Down Expand Up @@ -223,6 +274,7 @@ export const YourWalletPage = () => {
<Address />
<OlasBalance />
<MasterSafeNativeBalance />
<MasterSafeErc20Balances />
<MasterEoaSignerNativeBalance />
{selectedService && <YourAgentWallet />}
</Container>
Expand Down
2 changes: 1 addition & 1 deletion frontend/hooks/useFeatureFlag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const FEATURES_CONFIG = FeaturesConfigSchema.parse({
'backup-via-safe': true,
},
[AgentType.Modius]: {
'manage-wallet': false,
'manage-wallet': true,
'withdraw-funds': false,
'last-transactions': false,
'rewards-streak': false,
Expand Down
4 changes: 2 additions & 2 deletions frontend/hooks/useNeedsFunds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const useNeedsFunds = (stakingProgramId: Maybe<StakingProgramId>) => {
return chainIds.every((chainId) => {
const nativeTokenSymbol = getNativeTokenSymbol(chainId);
const nativeTokenBalance =
balancesByChain[chainId][nativeTokenSymbol] || 0;
balancesByChain[chainId]?.[nativeTokenSymbol] || 0;
const nativeTokenRequired =
serviceFundRequirements[chainId]?.[nativeTokenSymbol] || 0;

Expand All @@ -132,7 +132,7 @@ export const useNeedsFunds = (stakingProgramId: Maybe<StakingProgramId>) => {
const chainIds = Object.keys(serviceFundRequirements).map(Number);

return chainIds.every((chainId) => {
const olasBalance = balancesByChain[chainId][TokenSymbol.OLAS] || 0;
const olasBalance = balancesByChain[chainId]?.[TokenSymbol.OLAS] || 0;
const olasRequired =
serviceFundRequirements[chainId]?.[TokenSymbol.OLAS] || 0;

Expand Down

0 comments on commit 05b0164

Please sign in to comment.