Skip to content

Commit

Permalink
Fix variable naming conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
truemiller committed Feb 22, 2024
1 parent 49ef8ec commit 62ac05e
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 62 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion frontend/components/Modals/QRModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type QRModalProps = {
};

export const QRModal = ({
data: { amount, chainId, address, open, isERC20 },
data: { amount, chainId, address, open, isErc20: isERC20 },
}: QRModalProps) => {
const { setQrModalData } = useModals();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { copyToClipboard } from '@/helpers/copyToClipboard';
import { copyToClipboard } from '@/common-util/copyToClipboard';
import { useModals, useServices } from '@/hooks';
import { Address } from '@/types';
import { FundsReceivedMap } from '@/types';
Expand Down Expand Up @@ -70,7 +70,12 @@ export const FundRequirement = ({

const handleQr = useCallback(
(): void =>
qrModalOpen({ amount: requirement, chainId: 100, address, isERC20 }), // hardcoded chainId for now
qrModalOpen({
amount: requirement,
chainId: 100,
address,
isErc20: isERC20,
}), // hardcoded chainId for now
[address, isERC20, qrModalOpen, requirement],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type FundRequirementERC20Props = {
};

export const FundRequirementERC20 = (props: FundRequirementERC20Props) => {
const { getERC20Balance } = useEthers();
const { getErc20Balance: getERC20Balance } = useEthers();
return (
<FundRequirement getBalance={getERC20Balance} isERC20={true} {...props} />
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type FundRequirementETHProps = {
};

export const FundRequirementETH = (props: FundRequirementETHProps) => {
const { getETHBalance } = useEthers();
const { getEthBalance: getETHBalance } = useEthers();
return (
<FundRequirement getBalance={getETHBalance} isERC20={false} {...props} />
);
Expand Down
39 changes: 36 additions & 3 deletions frontend/components/Spawn/SpawnRPC.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
useMemo,
useState,
} from 'react';
import { useSpawn, useEthers } from '@/hooks';
import { useSpawn, useEthers, useAppInfo } from '@/hooks';
import { SpawnScreenState } from '@/enums';
import { CheckSquareTwoTone, WarningFilled } from '@ant-design/icons';
import { InputStatus } from 'antd/es/_util/statusUtils';
Expand All @@ -37,7 +37,8 @@ type SpawnRPCProps = {

export const SpawnRPC = ({ rpc, setRpc, nextPage }: SpawnRPCProps) => {
const { setSpawnScreenState } = useSpawn();
const { checkRPC } = useEthers();
const { checkRPC, getEthBalance } = useEthers();
const { userPublicKey } = useAppInfo();

const [isCheckingRpc, setIsCheckingRpc] = useState(false);
const [rpcState, setRpcState] = useState<RPCState>(RPCState.INVALID);
Expand Down Expand Up @@ -82,12 +83,44 @@ export const SpawnRPC = ({ rpc, setRpc, nextPage }: SpawnRPCProps) => {
);

const handleContinue = useCallback(async () => {
if (!userPublicKey) {
message.error(
'Error retrieving user public key, please ensure your master wallet key is set correctly',
);
return;
}

if (!rpc || rpcState !== RPCState.VALID) {
message.error('Invalid RPC');
return;
}

const ethBalance: number | undefined = await getEthBalance(
userPublicKey,
rpc,
).catch(() => undefined);

if (ethBalance === undefined) {
message.error('Failed to get master wallet balance');
return;
}

if (ethBalance < 1) {
message.error(
'Insufficient master wallet balance, you need at least 1 XDAI.',
);
return;
}

setSpawnScreenState(nextPage);
}, [nextPage, rpc, rpcState, setSpawnScreenState]);
}, [
getEthBalance,
nextPage,
rpc,
rpcState,
setSpawnScreenState,
userPublicKey,
]);

const isContinueDisabled: boolean = useMemo(
() => !rpc || rpcState !== RPCState.VALID,
Expand Down
26 changes: 10 additions & 16 deletions frontend/components/Spawn/SpawnStakingCheck.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const SpawnStakingCheck = ({
}: SpawnStakingCheckProps) => {
const { createService } = useServices();
const { userPublicKey } = useAppInfo();
const { getERC20Balance } = useEthers();
const { getErc20Balance } = useEthers();

const [isCreating, setIsCreating] = useState(false);
const [buttonClicked, setButtonClicked] = useState<ButtonOptions>();
Expand Down Expand Up @@ -112,7 +112,7 @@ export const SpawnStakingCheck = ({
if (!userPublicKey) {
return Promise.reject('No public key found');
}
return getERC20Balance(userPublicKey, rpc, TOKENS.gnosis.OLAS)
return getErc20Balance(userPublicKey, rpc, TOKENS.gnosis.OLAS)
.then((olasBalance: number) => {
const { olas_required_to_stake, olas_cost_of_bond } =
serviceTemplate.configuration;
Expand All @@ -132,7 +132,7 @@ export const SpawnStakingCheck = ({
.catch((e) => {
return Promise.reject(e);
});
}, [getERC20Balance, userPublicKey, rpc, serviceTemplate.configuration]);
}, [getErc20Balance, userPublicKey, rpc, serviceTemplate.configuration]);

const handleYes = async () => {
setButtonClicked(ButtonOptions.YES);
Expand Down Expand Up @@ -180,19 +180,13 @@ export const SpawnStakingCheck = ({
setButtonClicked(undefined);
};

const stakingRequirement = useMemo(
() =>
ethers.utils.formatUnits(
`${
serviceTemplate.configuration.olas_cost_of_bond +
serviceTemplate.configuration.olas_required_to_stake
}`,
),
[
serviceTemplate.configuration.olas_cost_of_bond,
serviceTemplate.configuration.olas_required_to_stake,
],
);
const stakingRequirement = useMemo(() => {
const { olas_required_to_stake, olas_cost_of_bond } =
serviceTemplate.configuration;
return ethers.utils.formatUnits(
`${olas_required_to_stake + olas_cost_of_bond}`,
);
}, [serviceTemplate.configuration]);

return (
<Flex gap={8} vertical>
Expand Down
3 changes: 2 additions & 1 deletion frontend/components/YourAgents/HasAgents.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Service } from '@/client';
import { Flex } from 'antd';
import { ServiceCard } from './ServiceCard/ServiceCard';
import { ReactElement } from 'react';

export const HasAgents = ({
services,
}: {
services: Service[];
}): JSX.Element => {
}): ReactElement => {
return (
<Flex vertical gap={16}>
{services.map((service: Service) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useInterval } from 'usehooks-ts';
const BALANCE_POLLING_INTERVAL = 5000;

export const ServiceCardTotalBalance = ({ service }: { service: Service }) => {
const { getETHBalances } = useMulticall();
const { getEthBalances } = useMulticall();
const [hasInitialLoaded, setHasInitialLoaded] = useState(false);
const [balances, setBalances] = useState<BalancesMap>({});

Expand All @@ -27,7 +27,7 @@ export const ServiceCardTotalBalance = ({ service }: { service: Service }) => {
service.chain_data?.multisig &&
service.ledger?.rpc
)
getETHBalances(
getEthBalances(
[...service.chain_data.instances, service.chain_data.multisig],
service.ledger.rpc,
)
Expand Down
4 changes: 2 additions & 2 deletions frontend/context/ModalsProvider/ModalsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const ModalsContext = createContext<ModalsContextProps>({
address: undefined,
amount: undefined,
chainId: undefined,
isERC20: false,
isErc20: false,
},
setQrModalData: () => {},
});
Expand All @@ -35,7 +35,7 @@ export const ModalsProvider = ({ children }: PropsWithChildren) => {
address: undefined,
amount: undefined,
chainId: undefined,
isERC20: false,
isErc20: false,
});
return (
<ModalsContext.Provider
Expand Down
6 changes: 3 additions & 3 deletions frontend/context/TabsProvider/TabsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {

type TabsContextProps = {
activeTab: Tab;
setActiveTab: Dispatch<SetStateAction<string>>;
setActiveTab: Dispatch<SetStateAction<Tab>>;
};

export const TabsContext = createContext<TabsContextProps>({
Expand All @@ -18,9 +18,9 @@ export const TabsContext = createContext<TabsContextProps>({
});

export const TabsProvider = ({ children }: PropsWithChildren) => {
const [activeTab, setActiveTab] = useState<string>(Tab.YOUR_AGENTS);
const [activeTab, setActiveTab] = useState<Tab>(Tab.YOUR_AGENTS);
return (
<TabsContext.Provider value={{ activeTab: activeTab as Tab, setActiveTab }}>
<TabsContext.Provider value={{ activeTab: activeTab, setActiveTab }}>
{children}
</TabsContext.Provider>
);
Expand Down
19 changes: 12 additions & 7 deletions frontend/hooks/useEthers.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address } from '@/types';
import { BigNumber, ethers, providers, utils } from 'ethers';
import { ethers, providers, utils } from 'ethers';

export const useEthers = () => {
/**
Expand All @@ -8,7 +8,7 @@ export const useEthers = () => {
* @param rpc string
* @returns Promise<number>
*/
const getETHBalance = async (
const getEthBalance = async (
address: Address,
rpc: string,
): Promise<number> => {
Expand All @@ -17,9 +17,10 @@ export const useEthers = () => {
name: 'Gnosis',
chainId: 100, // we currently only support Gnosis Trader agent
});
return provider
.getBalance(address)
.then((balance: BigNumber) => Number(utils.formatEther(balance)));
return provider.getBalance(address).then((balance) => {
const formattedBalance = utils.formatEther(balance);
return Number(formattedBalance);
});
} catch (e) {
return Promise.reject('Failed to get ETH balance');
}
Expand All @@ -32,7 +33,7 @@ export const useEthers = () => {
* @param contractAddress Address
* @returns Promise<number>
*/
const getERC20Balance = async (
const getErc20Balance = async (
address: Address,
rpc: string,
contractAddress?: Address,
Expand Down Expand Up @@ -87,5 +88,9 @@ export const useEthers = () => {
}
};

return { getETHBalance, checkRPC, getERC20Balance };
return {
getEthBalance,
checkRPC,
getErc20Balance,
};
};
2 changes: 1 addition & 1 deletion frontend/hooks/useModals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const useModals = () => {
address: undefined,
amount: undefined,
chainId: undefined,
isERC20: false,
isErc20: false,
});

const qrModalOpen = (data: Omit<Required<QRModalData>, 'open'>) =>
Expand Down
6 changes: 3 additions & 3 deletions frontend/hooks/useMulticall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const useMulticall = () => {
* @param rpc
* @returns Promise<{ [address: string]: number }>
*/
const getETHBalances = async (
const getEthBalances = async (
addresses: Address[],
rpc: string,
): Promise<{ [address: Address]: number }> => {
Expand Down Expand Up @@ -45,7 +45,7 @@ export const useMulticall = () => {
* @param contractAddress
* @returns Promise<{ [address: string]: number }>
*/
const getERC20Balances = async (
const getErc20Balances = async (
addresses: Address[],
rpc: string,
contractAddress: Address,
Expand Down Expand Up @@ -75,5 +75,5 @@ export const useMulticall = () => {
);
};

return { getETHBalances, getERC20Balances };
return { getEthBalances, getErc20Balances };
};
4 changes: 3 additions & 1 deletion frontend/hooks/useTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { Tab } from '@/enums';
import { useContext } from 'react';

export const useTabs = () => {
const { activeTab, setActiveTab } = useContext(TabsContext);
const { activeTab, setActiveTab: setTab } = useContext(TabsContext);

const resetTabs = () => setActiveTab(Tab.YOUR_AGENTS);
const setActiveTab = (tab: string) => setTab(tab as Tab); // casting needed to meet context useState requirements

return { activeTab, setActiveTab, resetTabs };
};
30 changes: 13 additions & 17 deletions frontend/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,23 @@ import { YourAgents } from '@/components/YourAgents/YourAgents';
import { Tab } from '@/enums';
import { useTabs } from '@/hooks/useTabs';
import { Tabs, type TabsProps } from 'antd';
import { useMemo } from 'react';

const tabs: TabsProps['items'] = [
{
key: Tab.YOUR_AGENTS,
label: 'Your Agents',
children: <YourAgents />,
},
{
key: Tab.MARKETPLACE,
label: 'Marketplace',
children: <Marketplace />,
},
];

export default function Home() {
const { activeTab, setActiveTab } = useTabs();

const tabs: TabsProps['items'] = useMemo(
() => [
{
key: Tab.YOUR_AGENTS,
label: 'Your Agents',
children: <YourAgents />,
},
{
key: Tab.MARKETPLACE,
label: 'Marketplace',
children: <Marketplace />,
},
],
[],
);

return (
<Layout>
<Tabs
Expand Down
2 changes: 1 addition & 1 deletion frontend/types/QRModalData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export type QRModalData = {
address?: Address;
amount?: number;
chainId?: number;
isERC20: boolean;
isErc20: boolean;
};

0 comments on commit 62ac05e

Please sign in to comment.