Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/staging' into fix/onchain_update…
Browse files Browse the repository at this point in the history
…_flow
  • Loading branch information
jmoreira-valory committed Aug 22, 2024
2 parents 138c553 + c0e4f59 commit 88858e4
Show file tree
Hide file tree
Showing 18 changed files with 279 additions and 164 deletions.
4 changes: 3 additions & 1 deletion .gitleaksignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ d8149e9b5b7bd6a7ed7bc1039900702f1d4f287b:operate/services/manage.py:generic-api-
99c0f139b037da2587708212fcf6d0e20786d0ba:operate/services/manage.py:generic-api-key:406
99c0f139b037da2587708212fcf6d0e20786d0ba:operate/services/manage.py:generic-api-key:454
99c0f139b037da2587708212fcf6d0e20786d0ba:operate/services/manage.py:generic-api-key:455
91ec07457f69e9a29f63693ac8ef887e4b5f49f0:operate/services/manage.py:generic-api-key:454
91ec07457f69e9a29f63693ac8ef887e4b5f49f0:operate/services/manage.py:generic-api-key:454
410bea2bd02ff54da69387fe8f3b58793e09f7b0:operate/services/manage.py:generic-api-key:421
410bea2bd02ff54da69387fe8f3b58793e09f7b0:operate/services/manage.py:generic-api-key:422
2 changes: 1 addition & 1 deletion electron/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const { paths } = require('./constants');
* - use "" (nothing as a suffix) for latest release candidate, for example "0.1.0rc26"
* - use "alpha" for alpha release, for example "0.1.0rc26-alpha"
*/
const OlasMiddlewareVersion = '0.1.0rc112';
const OlasMiddlewareVersion = '0.1.0rc117';

const path = require('path');
const { app } = require('electron');
Expand Down
5 changes: 2 additions & 3 deletions electron/store.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const Store = require('electron-store');
// set schema to validate store data
const schema = {
isInitialFunded: { type: 'boolean', default: false }, // TODO: reconsider this default, can be problematic if user has already funded prior to implementation
Expand All @@ -16,8 +17,6 @@ const schema = {
* @returns {Promise<void>} - A promise that resolves once the store is set up.
*/
const setupStoreIpc = async (ipcMain, mainWindow) => {
const Store = (await import("electron-store")).default;

const store = new Store({ schema });

store.onDidAnyChange((data) => {
Expand All @@ -30,7 +29,7 @@ const setupStoreIpc = async (ipcMain, mainWindow) => {
ipcMain.handle('store-get', (_, key) => store.get(key));
ipcMain.handle('store-set', (_, key, value) => store.set(key, value));
ipcMain.handle('store-delete', (_, key) => store.delete(key));
ipcMain.handle('store-clear', (_) => store.clear());
ipcMain.handle('store-clear', (_) => store.clear());
};

module.exports = { setupStoreIpc };
6 changes: 3 additions & 3 deletions frontend/components/MainPage/header/AgentButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useCallback, useMemo } from 'react';

import { Chain, DeploymentStatus } from '@/client';
import { COLOR } from '@/constants/colors';
import { LOW_BALANCE } from '@/constants/thresholds';
import { StakingProgram } from '@/enums/StakingProgram';
import { useBalance } from '@/hooks/useBalance';
import { useElectronApi } from '@/hooks/useElectronApi';
Expand Down Expand Up @@ -103,6 +102,7 @@ const AgentNotRunningButton = () => {
const {
setIsPaused: setIsBalancePollingPaused,
safeBalance,
isLowBalance,
totalOlasStakedBalance,
totalEthBalance,
} = useBalance();
Expand Down Expand Up @@ -211,7 +211,7 @@ const AgentNotRunningButton = () => {
const isServiceInactive =
serviceStatus === DeploymentStatus.BUILT ||
serviceStatus === DeploymentStatus.STOPPED;
if (isServiceInactive && safeBalance && safeBalance.ETH < LOW_BALANCE) {
if (isServiceInactive && isLowBalance) {
return false;
}

Expand All @@ -237,14 +237,14 @@ const AgentNotRunningButton = () => {
return hasEnoughOlas && hasEnoughEth;
}, [
serviceStatus,
safeBalance,
service,
storeState?.isInitialFunded,
isEligibleForStaking,
isAgentEvicted,
safeOlasBalanceWithStaked,
requiredOlas,
totalEthBalance,
isLowBalance,
]);

const buttonProps: ButtonProps = {
Expand Down
7 changes: 3 additions & 4 deletions frontend/components/MainPage/header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Flex } from 'antd';
import { useCallback, useEffect, useState } from 'react';

import { DeploymentStatus } from '@/client';
import { LOW_BALANCE } from '@/constants/thresholds';
import { useBalance } from '@/hooks/useBalance';
import { useElectronApi } from '@/hooks/useElectronApi';
import { useServices } from '@/hooks/useServices';
Expand All @@ -12,12 +11,12 @@ import { AgentButton } from './AgentButton';
import { AgentHead } from './AgentHead';

const useSetupTrayIcon = () => {
const { safeBalance } = useBalance();
const { isLowBalance } = useBalance();
const { serviceStatus } = useServices();
const { setTrayIcon } = useElectronApi();

useEffect(() => {
if (safeBalance && safeBalance.ETH < LOW_BALANCE) {
if (isLowBalance) {
setTrayIcon?.('low-gas');
} else if (serviceStatus === DeploymentStatus.DEPLOYED) {
setTrayIcon?.('running');
Expand All @@ -26,7 +25,7 @@ const useSetupTrayIcon = () => {
} else if (serviceStatus === DeploymentStatus.BUILT) {
setTrayIcon?.('logged-out');
}
}, [safeBalance, serviceStatus, setTrayIcon]);
}, [isLowBalance, serviceStatus, setTrayIcon]);

return null;
};
Expand Down
18 changes: 10 additions & 8 deletions frontend/components/MainPage/sections/GasBalanceSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { useEffect, useMemo, useState } from 'react';
import styled from 'styled-components';

import { COLOR } from '@/constants/colors';
import { LOW_BALANCE } from '@/constants/thresholds';
import { useBalance } from '@/hooks/useBalance';
import { useElectronApi } from '@/hooks/useElectronApi';
import { useStore } from '@/hooks/useStore';
import { useWallet } from '@/hooks/useWallet';

import { CardSection } from '../../styled/CardSection';
Expand All @@ -33,7 +33,8 @@ const FineDot = styled(Dot)`
`;

const BalanceStatus = () => {
const { isBalanceLoaded, safeBalance } = useBalance();
const { isBalanceLoaded, isLowBalance } = useBalance();
const { storeState } = useStore();
const { showNotification } = useElectronApi();

const [isLowBalanceNotificationShown, setIsLowBalanceNotificationShown] =
Expand All @@ -42,33 +43,34 @@ const BalanceStatus = () => {
// show notification if balance is too low
useEffect(() => {
if (!isBalanceLoaded) return;
if (!safeBalance) return;
if (!showNotification) return;
if (!storeState?.isInitialFunded) return;

if (safeBalance.ETH < LOW_BALANCE && !isLowBalanceNotificationShown) {
if (isLowBalance && !isLowBalanceNotificationShown) {
showNotification('Trading balance is too low.');
setIsLowBalanceNotificationShown(true);
}

// If it has already been shown and the balance has increased,
// should show the notification again if it goes below the threshold.
if (safeBalance.ETH >= LOW_BALANCE && isLowBalanceNotificationShown) {
if (!isLowBalance && isLowBalanceNotificationShown) {
setIsLowBalanceNotificationShown(false);
}
}, [
isBalanceLoaded,
isLowBalanceNotificationShown,
safeBalance,
isLowBalance,
showNotification,
storeState?.isInitialFunded,
]);

const status = useMemo(() => {
if (!safeBalance || safeBalance.ETH < LOW_BALANCE) {
if (isLowBalance) {
return { statusName: 'Too low', StatusComponent: EmptyDot };
}

return { statusName: 'Fine', StatusComponent: FineDot };
}, [safeBalance]);
}, [isLowBalance]);

const { statusName, StatusComponent } = status;
return (
Expand Down
58 changes: 32 additions & 26 deletions frontend/components/MainPage/sections/NeedsFundsSection.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Flex, Typography } from 'antd';
import { formatUnits } from 'ethers/lib/utils';
import { ReactNode, useEffect, useMemo } from 'react';
import styled from 'styled-components';

import { CustomAlert } from '@/components/Alert';
import { CHAINS } from '@/constants/chains';
import { UNICODE_SYMBOLS } from '@/constants/symbols';
import { useBalance } from '@/hooks/useBalance';
Expand All @@ -10,13 +12,18 @@ import { useServiceTemplates } from '@/hooks/useServiceTemplates';
import { useStore } from '@/hooks/useStore';
import { getMinimumStakedAmountRequired } from '@/utils/service';

import { CustomAlert } from '../../Alert';
import { AlertTitle } from '../../Alert/AlertTitle';
import { CardSection } from '../../styled/CardSection';

const { Text, Paragraph } = Typography;
const { Text } = Typography;
const COVER_PREV_BLOCK_BORDER_STYLE = { marginTop: '-1px' };

const FundingValue = styled.div`
font-size: 24px;
font-weight: 700;
line-height: 32px;
letter-spacing: -0.72px;
`;

const useNeedsFunds = () => {
const { getServiceTemplates } = useServiceTemplates();

Expand Down Expand Up @@ -99,29 +106,28 @@ export const MainNeedsFunds = () => {

const message: ReactNode = useMemo(
() => (
<Flex vertical gap={4}>
<AlertTitle>Your agent needs funds</AlertTitle>
<Paragraph className="mb-4">
USE THE ACCOUNT CREDENTIALS PROVIDED IN THE “ADD FUNDS” INSTRUCTIONS
BELOW.
</Paragraph>
<Paragraph className="mb-4">
To run your agent, you must add these amounts to your account:
</Paragraph>
{!hasEnoughOlasForInitialFunding && (
<Text>
<span className="font-weight-600">{`${UNICODE_SYMBOLS.OLAS}${serviceFundRequirements.olas} OLAS `}</span>
- for staking.
</Text>
)}
{!hasEnoughEthForInitialFunding && (
<Text>
<span className="font-weight-600">
{`${serviceFundRequirements.eth} XDAI `}
</span>
- for trading balance.
</Text>
)}
<Flex vertical gap={16}>
<Text className="font-weight-600">Your agent needs funds</Text>
<Flex gap={24}>
{!hasEnoughOlasForInitialFunding && (
<div>
<FundingValue>{`${UNICODE_SYMBOLS.OLAS}${serviceFundRequirements.olas} OLAS `}</FundingValue>
<span className="text-sm">for staking</span>
</div>
)}
{!hasEnoughEthForInitialFunding && (
<div>
<FundingValue>
{`$${serviceFundRequirements.eth} XDAI `}
</FundingValue>
<span className="text-sm">for trading</span>
</div>
)}
</Flex>
<ul className="p-0 m-0 text-sm">
<li>Do not add more than these amounts.</li>
<li>Use the address in the “Add Funds” section below.</li>
</ul>
</Flex>
),
[
Expand Down
11 changes: 6 additions & 5 deletions frontend/components/MainPage/sections/OlasBalanceSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import styled from 'styled-components';
import { CustomAlert } from '@/components/Alert';
import { COLOR } from '@/constants/colors';
import { UNICODE_SYMBOLS } from '@/constants/symbols';
import { LOW_BALANCE } from '@/constants/thresholds';
import { LOW_MASTER_SAFE_BALANCE } from '@/constants/thresholds';
import { useBalance } from '@/hooks/useBalance';
import { useElectronApi } from '@/hooks/useElectronApi';
import { useReward } from '@/hooks/useReward';
Expand Down Expand Up @@ -122,11 +122,12 @@ const MainOlasBalanceAlert = styled.div`
`;

const LowTradingBalanceAlert = () => {
const { isBalanceLoaded, safeBalance } = useBalance();
const { isBalanceLoaded, isLowBalance } = useBalance();
const { storeState } = useStore();

if (!isBalanceLoaded) return null;
if (!safeBalance) return null;
if (safeBalance.ETH >= LOW_BALANCE) return null;
if (!storeState?.isInitialFunded) return;
if (!isLowBalance) return null;

return (
<MainOlasBalanceAlert>
Expand All @@ -140,7 +141,7 @@ const LowTradingBalanceAlert = () => {
Trading balance is too low
</Title>
<Text>
{`To run your agent, add at least $${LOW_BALANCE} XDAI to your account.`}
{`To run your agent, add at least $${LOW_MASTER_SAFE_BALANCE} XDAI to your account.`}
</Text>
<Text>
Do it quickly to avoid your agent missing its targets and getting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export const StakingContractSection = ({
}

if (!hasEnoughOlasToMigrate) {
return 'Insufficient OLAS balance to migrate, need ${}';
return `Insufficient OLAS to migrate, ${minimumOlasRequiredToMigrate} OLAS required in total.`;
}

if (!isAppVersionCompatible) {
Expand Down
2 changes: 1 addition & 1 deletion frontend/constants/serviceTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { StakingProgram } from '@/enums/StakingProgram';
export const SERVICE_TEMPLATES: ServiceTemplate[] = [
{
name: 'Trader Agent',
hash: 'bafybeidgjgjj5ul6xkubicbemppufgsbx5sr5rwhtrwttk2oivp5bkdnce',
hash: 'bafybeicrstlxew36hlxl7pzi73nmd44aibnhwxzkchzlec6t6yhvs7gvhy',
// hash: 'bafybeibbloa4w33vj4bvdkso7pzk6tr3duvxjpecbx4mur4ix6ehnwb5uu', // temporary
description: 'Trader agent for omen prediction markets',
image:
Expand Down
3 changes: 2 additions & 1 deletion frontend/constants/thresholds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export const MIN_ETH_BALANCE_THRESHOLDS = {
},
};

export const LOW_BALANCE = 2;
export const LOW_AGENT_SAFE_BALANCE = 0.5;
export const LOW_MASTER_SAFE_BALANCE = 2;
26 changes: 26 additions & 0 deletions frontend/context/BalanceProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import { useInterval } from 'usehooks-ts';
import { Wallet } from '@/client';
import { CHAINS } from '@/constants/chains';
import { FIVE_SECONDS_INTERVAL } from '@/constants/intervals';
import {
LOW_AGENT_SAFE_BALANCE,
LOW_MASTER_SAFE_BALANCE,
} from '@/constants/thresholds';
import { TOKENS } from '@/constants/tokens';
import { ServiceRegistryL2ServiceState } from '@/enums/ServiceRegistryL2ServiceState';
import { Token } from '@/enums/Token';
Expand Down Expand Up @@ -44,6 +48,7 @@ export const BalanceContext = createContext<{
safeBalance?: ValueOf<WalletAddressNumberRecord>;
totalEthBalance?: number;
totalOlasBalance?: number;
isLowBalance: boolean;
wallets?: Wallet[];
walletBalances: WalletAddressNumberRecord;
updateBalances: () => Promise<void>;
Expand All @@ -59,6 +64,7 @@ export const BalanceContext = createContext<{
safeBalance: undefined,
totalEthBalance: undefined,
totalOlasBalance: undefined,
isLowBalance: false,
wallets: undefined,
walletBalances: {},
updateBalances: async () => {},
Expand Down Expand Up @@ -197,6 +203,25 @@ export const BalanceProvider = ({ children }: PropsWithChildren) => {
() => masterSafeAddress && walletBalances[masterSafeAddress],
[masterSafeAddress, walletBalances],
);
const agentSafeBalance = useMemo(
() =>
services?.[0]?.chain_configs[CHAINS.GNOSIS.chainId].chain_data
?.multisig &&
walletBalances[
services[0].chain_configs[CHAINS.GNOSIS.chainId].chain_data.multisig!
],
[services, walletBalances],
);
const isLowBalance = useMemo(() => {
if (!safeBalance || !agentSafeBalance) return false;
if (
safeBalance.ETH < LOW_MASTER_SAFE_BALANCE &&
// Need to check agentSafe balance as well, because it's auto-funded from safeBalance
agentSafeBalance.ETH < LOW_AGENT_SAFE_BALANCE
)
return true;
return false;
}, [safeBalance, agentSafeBalance]);

useInterval(
() => {
Expand All @@ -217,6 +242,7 @@ export const BalanceProvider = ({ children }: PropsWithChildren) => {
safeBalance,
totalEthBalance,
totalOlasBalance,
isLowBalance,
wallets,
walletBalances,
updateBalances,
Expand Down
Loading

0 comments on commit 88858e4

Please sign in to comment.