Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resume funding #34

Merged
merged 47 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
411040b
remove Array<>
truemiller Mar 6, 2024
103ac48
update hook usage
truemiller Mar 6, 2024
ac5f00d
reset spawn on error page button click
truemiller Mar 6, 2024
817d8ef
Refactor SpawnMasterWalletFunding component
truemiller Mar 6, 2024
0607876
Fix debounceCheckRpc bug in SpawnRPC component
truemiller Mar 6, 2024
b401f7a
Refactor SpawnStakingCheck component and create service based on stak…
truemiller Mar 6, 2024
8bf1889
Refactor Funding component to use FundingRecord and SpawnData types
truemiller Mar 6, 2024
76a04f3
Refactor setReceivedFunds to setSpawnData in FundRequirement component
truemiller Mar 6, 2024
b089d6c
Update FundRequirementERC20.tsx imports and props
truemiller Mar 6, 2024
ef139dd
Update FundRequirementETH.tsx imports and props
truemiller Mar 6, 2024
f925ec1
Refactor ServiceCard component to use updated hooks
truemiller Mar 6, 2024
305eceb
Refactor SpawnProvider.tsx to use SpawnData type
truemiller Mar 6, 2024
788112a
Update useMarketplace hook to useServiceTemplates hook
truemiller Mar 6, 2024
7e86113
Remove useMarketplace hook
truemiller Mar 6, 2024
cb8224e
Refactor useServices hook to remove unused methods
truemiller Mar 6, 2024
1aa9572
Add useServiceTemplates hook
truemiller Mar 6, 2024
80764d0
Refactor useSpawn hook to improve code structure and add new function…
truemiller Mar 6, 2024
87fea34
Refactor SpawnPage component to handle different screens and add erro…
truemiller Mar 6, 2024
bfb27a1
Add 'SpawnData' type to index.ts
truemiller Mar 6, 2024
7a50a81
Add FundingRecord type to Records.ts
truemiller Mar 6, 2024
488f6a8
Add SpawnData type definition
truemiller Mar 6, 2024
44fd383
Merge remote-tracking branch 'origin/main' into resume-funding
truemiller Mar 6, 2024
c007051
add createMasterFunding
truemiller Mar 6, 2024
dffeef2
Refactor SpawnAgentFunding to destructure spawnData
truemiller Mar 7, 2024
6b37304
Refactor SpawnMasterWalletFunding component to destructure spawnData
truemiller Mar 7, 2024
eb3ec15
Refactor SpawnRPC component to destructure spawnData.rpc
truemiller Mar 7, 2024
e2543a6
Refactor SpawnStakingCheck component
truemiller Mar 7, 2024
a71f96c
Refactor Funding.tsx to use destructuring assignment
truemiller Mar 7, 2024
cdd8d61
Update fund requirement handling in FundRequirement component
truemiller Mar 7, 2024
65f665f
Refactor AppInfoProvider to conditionally render children based on ap…
truemiller Mar 7, 2024
0428f10
Remove unused import and update default value for screen in SpawnProv…
truemiller Mar 7, 2024
5730ccd
Refactor useSpawn hook to load spawn data and get agent fund requirem…
truemiller Mar 7, 2024
aca141e
Refactor SpawnPage component to use new useSpawn hook
truemiller Mar 7, 2024
ce7b226
Update screen property in SpawnData.ts
truemiller Mar 7, 2024
c4a6f85
Update import statement for EthersService in SpawnMasterWalletFunding…
truemiller Mar 7, 2024
a5e9fab
Update EthersService import in SpawnRPC.tsx
truemiller Mar 7, 2024
072e7eb
Update EthersService import in SpawnStakingCheck component
truemiller Mar 7, 2024
ac6738a
Refactor imports in ServiceCard.tsx
truemiller Mar 7, 2024
8f65a16
Export EthersService in Ethers.ts
truemiller Mar 7, 2024
088beec
Add Ethers and Multicall services
truemiller Mar 7, 2024
32ec04d
Update Navbar and ServiceCard components
truemiller Mar 7, 2024
9c3b7c4
Merge remote-tracking branch 'origin/main' into resume-funding
truemiller Mar 7, 2024
53dcee4
Update service imports and add checkServiceIsFunded function
truemiller Mar 7, 2024
c69ef32
fix unneeded condition
truemiller Mar 8, 2024
b6b65e0
remove return undefined
truemiller Mar 8, 2024
a5a2b9a
fixes as per comments
truemiller Mar 8, 2024
11240e1
Update dependencies and fix color imports
truemiller Mar 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions frontend/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ export type UpdateServicePayload = {
};

export type DeleteServicesPayload = {
hashes: Array<ServiceHash>;
hashes: ServiceHash[];
};

export type DeleteServicesResponse = {
hashes: Array<ServiceHash>;
hashes: ServiceHash[];
};

export type AppInfo = {
Expand Down
6 changes: 0 additions & 6 deletions frontend/components/Layout/Navbar/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { Flex } from 'antd';
import { SettingsButton } from './SettingsButton';
import { NotificationButton } from './NotificationButton';
import Image from 'next/image';

export const Navbar = () => {
return (
<Flex vertical={false} justify="space-between" style={{ minWidth: '100%' }}>
<Image src="/olas-logo.png" alt="" width={100} height={25} />
<Flex gap={4}>
<NotificationButton disabled />
<SettingsButton disabled />
</Flex>
</Flex>
);
};
4 changes: 2 additions & 2 deletions frontend/components/Marketplace/Marketplace.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Flex } from 'antd';
import { MarketplaceItemCard } from './MarketplaceItemCard';
import { useMemo } from 'react';
import { useMarketplace } from '@/hooks';
import { useServiceTemplates } from '@/hooks';

export const Marketplace = () => {
const { getServiceTemplates } = useMarketplace();
const { getServiceTemplates } = useServiceTemplates();

const serviceTemplates = useMemo(
() => getServiceTemplates(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { copyToClipboard } from '@/common-util/copyToClipboard';
import { useModals } from '@/hooks';
import { Address, AddressBooleanRecord } from '@/types';
import { Address, SpawnData } from '@/types';
import { Button, Flex, Typography, message } from 'antd';
import {
Dispatch,
Expand All @@ -24,7 +24,7 @@ type FundRequirementProps = {
rpc: string,
contractAddress?: Address,
) => Promise<number>;
setReceivedFunds: Dispatch<SetStateAction<AddressBooleanRecord>>;
setSpawnData: Dispatch<SetStateAction<SpawnData>>;
};

/**
Expand All @@ -41,7 +41,7 @@ export const FundRequirement = ({
hasReceivedFunds,
isErc20,
getBalance,
setReceivedFunds,
setSpawnData,
}: FundRequirementProps) => {
const { qrModalOpen } = useModals();

Expand Down Expand Up @@ -77,11 +77,36 @@ export const FundRequirement = ({
.then((balance: number) => {
if (balance >= requirement) {
setIsPollingBalance(false);
setReceivedFunds((prev: AddressBooleanRecord) => ({
...prev,
[address]: true,
}));
message.success(`Funded ${address}`);
setSpawnData((prev: SpawnData) => {
// update agent fund requirements
if (prev.agentFundRequirements[address]) {
return {
...prev,
agentFundRequirements: {
...prev.agentFundRequirements,
[address]: {
...prev.agentFundRequirements[address],
received: true,
},
},
};
}
// update master wallet fund requirements
if (prev.masterWalletFundRequirements[address]) {
return {
...prev,
masterWalletFundRequirements: {
...prev.masterWalletFundRequirements,
[address]: {
...prev.masterWalletFundRequirements[address],
received: true,
},
},
};
}
// do nothing
return prev;
});
}
})
.catch(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Dispatch, SetStateAction } from 'react';
import { FundRequirement } from './FundRequirement';
import { Address, AddressBooleanRecord } from '@/types';
import EthersService from '@/service/Ethers';
import { Address, SpawnData } from '@/types';
import { EthersService } from '@/service';

type FundRequirementERC20Props = {
address: Address;
Expand All @@ -10,7 +10,7 @@ type FundRequirementERC20Props = {
symbol: string;
contractAddress?: Address;
hasReceivedFunds: boolean;
setReceivedFunds: Dispatch<SetStateAction<AddressBooleanRecord>>;
setSpawnData: Dispatch<SetStateAction<SpawnData>>;
};

export const FundRequirementERC20 = (props: FundRequirementERC20Props) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Dispatch, SetStateAction } from 'react';
import { FundRequirement } from './FundRequirement';
import { Address, AddressBooleanRecord } from '@/types';
import EthersService from '@/service/Ethers';
import { Address, SpawnData } from '@/types';
import { EthersService } from '@/service';

type FundRequirementETHProps = {
address: Address;
symbol: string;
requirement: number;
rpc: string;
hasReceivedFunds: boolean;
setReceivedFunds: Dispatch<SetStateAction<AddressBooleanRecord>>;
setSpawnData: Dispatch<SetStateAction<SpawnData>>;
};

export const FundRequirementETH = (props: FundRequirementETHProps) => {
Expand Down
47 changes: 21 additions & 26 deletions frontend/components/Spawn/Funding/Funding.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { COLOR } from '@/constants';
import { SpawnScreen } from '@/enums';
import { useSpawn } from '@/hooks';
import { Address, AddressNumberRecord } from '@/types';
import { AddressBooleanRecord } from '@/types';
import { TimelineItemProps, Flex, Typography, Timeline } from 'antd';
import { Address, FundingRecord, SpawnData } from '@/types';
import { TimelineItemProps, Flex, Typography, Timeline, theme } from 'antd';
import { isEmpty } from 'lodash';
import {
useState,
useMemo,
useEffect,
SetStateAction,
Expand All @@ -15,7 +12,7 @@ import {
} from 'react';

type FundRequirementComponentProps = {
setReceivedFunds: Dispatch<SetStateAction<AddressBooleanRecord>>;
setSpawnData: Dispatch<SetStateAction<SpawnData>>;
rpc: string;
address: Address;
requirement: number;
Expand All @@ -25,7 +22,7 @@ type FundRequirementComponentProps = {
};

type FundingProps = {
fundRequirements: AddressNumberRecord;
fundRequirements: FundingRecord;
FundRequirementComponent: (
props: FundRequirementComponentProps,
) => ReactElement;
Expand All @@ -43,54 +40,52 @@ export const Funding = ({
symbol,
contractAddress,
}: FundingProps) => {
const { setSpawnData, rpc } = useSpawn();

const [receivedFunds, setReceivedFunds] = useState<AddressBooleanRecord>({
...(Object.keys(fundRequirements) as Address[]).reduce(
(acc: AddressBooleanRecord, address: Address) => {
acc[address] = false;
return acc;
},
{},
),
});
const {
setSpawnData,
spawnData: { rpc },
} = useSpawn();
const { token } = theme.useToken();

const timelineItems: TimelineItemProps[] = useMemo(
() =>
(Object.keys(fundRequirements) as Address[]).map((address) => {
const { required, received } = fundRequirements[address];
return {
children: (
<FundRequirementComponent
setReceivedFunds={setReceivedFunds}
setSpawnData={setSpawnData}
address={address}
requirement={fundRequirements[address]}
requirement={required}
symbol={symbol}
hasReceivedFunds={receivedFunds[address]}
hasReceivedFunds={received}
contractAddress={contractAddress}
rpc={rpc}
/>
),
color: receivedFunds[address] ? COLOR.GREEN_2 : COLOR.RED,
color: received ? token.green : token.red,
};
}) as TimelineItemProps[],
[
FundRequirementComponent,
contractAddress,
fundRequirements,
receivedFunds,
rpc,
setSpawnData,
symbol,
token.green,
token.red,
],
);

const hasSentAllFunds = useMemo(() => {
if (isEmpty(fundRequirements)) return false;
return (Object.keys(receivedFunds) as Address[]).reduce(
(acc: boolean, address) => acc && receivedFunds[address],
return (Object.keys(fundRequirements) as Address[]).reduce(
(acc: boolean, address) => acc && fundRequirements[address].received,
true,
);
}, [fundRequirements, receivedFunds]);
}, [fundRequirements]);

// if all funds have been sent, move to next page
useEffect(() => {
hasSentAllFunds && setSpawnData((prev) => ({ ...prev, screen: nextPage }));
}, [hasSentAllFunds, nextPage, setSpawnData]);
Expand Down
50 changes: 47 additions & 3 deletions frontend/components/Spawn/SpawnAgentFunding.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,61 @@
import { Funding } from './Funding/Funding';
import { SpawnScreen } from '@/enums';
import { FundRequirementETH } from './Funding/FundRequirement/FundRequirementETH';
import { useSpawn } from '@/hooks';
import { useAppInfo, useSpawn } from '@/hooks';
import { Spin } from 'antd';
import { useState, useEffect } from 'react';
import MulticallService from '@/service/Multicall';
import { Address, AddressNumberRecord } from '@/types';

type SpawnAgentFundingProps = {
nextPage: SpawnScreen;
};

export const SpawnAgentFunding = (props: SpawnAgentFundingProps) => {
const { agentFundRequirements: fundRequirements } = useSpawn();
const {
setSpawnData,
spawnData: { rpc, agentFundRequirements },
} = useSpawn();
const { userPublicKey } = useAppInfo();
const [isInitialLoaded, setIsInitialLoaded] = useState(false);

useEffect(() => {
if (!(!isInitialLoaded && userPublicKey)) return;
const agentAddresses = Object.keys(agentFundRequirements) as Address[];
MulticallService.getEthBalances(agentAddresses, rpc).then(
(balances: AddressNumberRecord) => {
setSpawnData((prev) => ({
...prev,
agentFundRequirements: agentAddresses.reduce(
(acc, address) => ({
...acc,
[address]: {
...agentFundRequirements[address],
received: balances[address] > 1,
},
}),
{},
),
}));
setIsInitialLoaded(true);
},
);
}, [
agentFundRequirements,
isInitialLoaded,
rpc,
setSpawnData,
userPublicKey,
]);

// if not inital loaded, show loader
if (agentFundRequirements === undefined || !isInitialLoaded) {
return <Spin />;
}

return (
<Funding
fundRequirements={fundRequirements}
fundRequirements={agentFundRequirements}
statement="Please fund the agent wallets to continue."
symbol={'XDAI'} // hardcoded while only trader is available
FundRequirementComponent={FundRequirementETH}
Expand Down
7 changes: 6 additions & 1 deletion frontend/components/Spawn/SpawnError.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useSpawn } from '@/hooks';
import { Button, Flex, Typography } from 'antd';
import { useRouter } from 'next/router';

Expand All @@ -6,11 +7,15 @@ type SpawnErrorProps = {
};

export const SpawnError = ({ message }: SpawnErrorProps) => {
const { resetSpawn } = useSpawn();
const router = useRouter();
return (
<Flex vertical justify="center" align="center">
<Typography.Text>{message}</Typography.Text>
<Button type="primary" onClick={() => router.push('/')}>
<Button
type="primary"
onClick={() => router.push('/').finally(resetSpawn)}
>
Back to Home
</Button>
</Flex>
Expand Down
Loading
Loading