Skip to content

Commit

Permalink
(registry) feat: review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Atatakai authored and Atatakai committed Aug 19, 2024
1 parent c44e1cb commit 70e54e9
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Address } from 'viem';
import { getPendingIncentives, useClaimableIncentives } from 'libs/common-contract-functions/src';
import { UNICODE_SYMBOLS } from 'libs/util-constants/src/lib/symbols';
import { TOKENOMICS } from 'libs/util-contracts/src';
import { notifyError, notifySuccess } from 'libs/util-functions/src';
import { areAddressesEqual, notifyError, notifySuccess } from 'libs/util-functions/src';

import { getEthersProviderForEthereum, getTokenomicsEthersContract } from 'common-util/Contracts';
import { claimOwnerIncentivesRequest } from 'common-util/functions/requests';
Expand Down Expand Up @@ -78,9 +78,9 @@ export const RewardsSection: FC<RewardsSectionProps> = ({ ownerAddress, id, type

const {
reward: claimableReward,
rewardEth: claimableRewardEth,
rewardWei: claimableRewardWei,
topUp: claimableTopUp,
topUpEth: claimableTopUpEth,
topUpWei: claimableTopUpWei,
isFetching: isClaimableIncentivesLoading,
refetch,
} = useClaimableIncentives(
Expand Down Expand Up @@ -142,11 +142,11 @@ export const RewardsSection: FC<RewardsSectionProps> = ({ ownerAddress, id, type
]);

const canClaim = useMemo(() => {
if (account !== ownerAddress) return false;
if (claimableRewardEth === undefined || claimableTopUpEth === undefined) return false;
if (!areAddressesEqual(account.toString(), ownerAddress)) return false;
if (claimableRewardWei === undefined || claimableTopUpWei === undefined) return false;

return claimableRewardEth > 0 || claimableTopUpEth > 0;
}, [account, ownerAddress, claimableRewardEth, claimableTopUpEth]);
return claimableRewardWei > 0 || claimableTopUpWei > 0;
}, [account, ownerAddress, claimableRewardWei, claimableTopUpWei]);

const handleClaim = useCallback(async () => {
if (!account) return;
Expand Down Expand Up @@ -181,7 +181,7 @@ export const RewardsSection: FC<RewardsSectionProps> = ({ ownerAddress, id, type
pagination={false}
style={{ maxWidth: '550px' }}
/>
<a href="https://tokenomics.olas.network/donate">
<a href="https://tokenomics.olas.network/donate" target="_blank" rel="noopener noreferrer">
Make donation {UNICODE_SYMBOLS.EXTERNAL_LINK}
</a>
</Flex>
Expand Down
4 changes: 2 additions & 2 deletions apps/autonolas-registry/common-util/Details/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Button, Col, Row, Typography } from 'antd';
import capitalize from 'lodash/capitalize';
import get from 'lodash/get';
import { FC, useCallback, useState } from 'react';
import { Address } from 'viem';

import { GenericObject, Loader, NA } from '@autonolas/frontend-library';

Expand All @@ -14,14 +15,13 @@ import { DetailsSubInfo } from './DetailsSubInfo';
import { NftImage } from './NFTImage';
import { DetailsTitle, Header } from './styles';
import { useDetails } from './useDetails';
import { Address } from 'viem';

const { Text } = Typography;

type DetailsProps = {
id: string;
type: NavTypesValues;
getDetails: (id: string) => Promise<{ unitHash: Address, dependencies: string[]}>;
getDetails: (id: string) => Promise<{ unitHash: Address; dependencies: string[] }>;
getTokenUri: (id: string) => Promise<string>;
getOwner: (id: string) => Promise<string>;
handleUpdate?: () => void;
Expand Down
8 changes: 4 additions & 4 deletions apps/autonolas-registry/common-util/Details/useDetails.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import { Address } from 'viem';

import { NA, areAddressesEqual, notifyError } from '@autonolas/frontend-library';
import { NA } from 'libs/util-constants/src/lib/symbols';
import { areAddressesEqual, notifyError } from 'libs/util-functions/src';

import { NavTypesValues } from 'util/constants';

Expand Down Expand Up @@ -38,7 +39,6 @@ export const useDetails = ({

try {
const tempDetails = await getDetails(id);
console.log('tempDetails', tempDetails);
setInfo(tempDetails);

const ownerAccount = await getOwner(id);
Expand Down Expand Up @@ -73,13 +73,13 @@ export const useDetails = ({
const isOwner = useMemo(() => {
if (isSvm) {
if (walletPublicKey && ownerAddress) {
return areAddressesEqual(`${walletPublicKey}`, ownerAddress);
return areAddressesEqual(walletPublicKey.toString(), ownerAddress);
}
return false;
}

if (account && ownerAddress) {
return areAddressesEqual(`${account}`, ownerAddress);
return areAddressesEqual(account.toString(), ownerAddress);
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion apps/autonolas-registry/common-util/Login/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const EVM_SUPPORTED_CHAINS = SUPPORTED_CHAINS.map((chain) => {
};
});

type SolanaChain = {
export type SolanaChain = {
id: number | null;
networkDisplayName: string;
networkName: string;
Expand Down
70 changes: 39 additions & 31 deletions apps/autonolas-registry/common-util/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
// TODO: provide all the rest types
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import { PublicKey } from '@solana/web3.js';
import { Contract, ethers } from 'ethers';
import { RuleObject } from 'antd/es/form';
import { StoreValue } from 'antd/es/form/interface';
import { Contract, FallbackProvider, JsonRpcProvider, ethers } from 'ethers';
import { isString } from 'lodash';
import { Address } from 'viem';



import { getChainIdOrDefaultToMainnet as getChainIdOrDefaultToMainnetFn, getIsValidChainId as getIsValidChainIdFn, isValidAddress, notifyError, notifyWarning } from '@autonolas/frontend-library';


import {
getChainIdOrDefaultToMainnet as getChainIdOrDefaultToMainnetFn,
getIsValidChainId as getIsValidChainIdFn,
isValidAddress,
notifyError,
notifyWarning,
} from '@autonolas/frontend-library';

import { sendTransaction as sendTransactionFn } from 'libs/util-functions/src';


import { RpcUrl } from 'libs/util-functions/src/lib/sendTransaction/types';

import { VM_TYPE } from '../../util/constants';
import { RPC_URLS } from '../Contracts';
import { SUPPORTED_CHAINS } from '../Login';
import { EVM_SUPPORTED_CHAINS, SVM_SUPPORTED_CHAINS } from '../Login/config';
import { EVM_SUPPORTED_CHAINS, SVM_SUPPORTED_CHAINS, SolanaChain } from '../Login/config';

// TODO: provide types for MODAL_PROVIDER
export const getModalProvider = () => (window as any)?.MODAL_PROVIDER;

Check failure on line 25 in apps/autonolas-registry/common-util/functions/index.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

export const getModalProvider = () => window?.MODAL_PROVIDER;

export const getWindowEthereum = () => window?.ethereum;
export const getWindowEthereum = () => (window as any)?.ethereum;

Check failure on line 27 in apps/autonolas-registry/common-util/functions/index.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

export const getChainId = (chainId = null) => {
if (chainId) return chainId;
Expand All @@ -43,7 +43,7 @@ export const getChainId = (chainId = null) => {

export const getProvider = () => {
const defaultChainId = getChainId();
const rpcUrl = RPC_URLS[defaultChainId];
const rpcUrl = typeof defaultChainId === 'number' ? (RPC_URLS as RpcUrl)[defaultChainId] : null;

if (!rpcUrl) {
throw new Error(`No RPC URL found for chainId: ${defaultChainId}`);
Expand Down Expand Up @@ -105,7 +105,10 @@ export const getChainIdOrDefaultToMainnet = (chainId: string | number) => {
* @param {object} builderIns - The object to check.
* @returns {boolean} - True if the object is a MethodsBuilder object, false otherwise.
*/
const isMethodsBuilderInstance = (builderIns, registryAddress) => {
const isMethodsBuilderInstance = (
builderIns: object & { _args?: any }, // TODO: provide better type

Check failure on line 109 in apps/autonolas-registry/common-util/functions/index.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
registryAddress: Address,
) => {
if (typeof builderIns !== 'object' || builderIns === null) {
throw new Error('sendTransaction: Input must be an object.');
}
Expand Down Expand Up @@ -139,10 +142,10 @@ const isMethodsBuilderInstance = (builderIns, registryAddress) => {
export const sendTransaction = (
method: Contract,
account: Address,
extra?: { vmType: string; registryAddress: string },
extra?: { vmType: string; registryAddress: Address },
) => {
const { vmType, registryAddress } = extra || {};
if (vmType === VM_TYPE.SVM) {
if (vmType === VM_TYPE.SVM && registryAddress) {
// Check if something resembling an SVM method is being passed
if (!isMethodsBuilderInstance(method, registryAddress)) {
notifyError('Invalid method object');
Expand All @@ -153,20 +156,23 @@ export const sendTransaction = (

return sendTransactionFn(method, account, {
supportedChains: SUPPORTED_CHAINS,
rpcUrls: RPC_URLS,
rpcUrls: RPC_URLS as RpcUrl,
});
};

export const addressValidator = () => ({
validator(_, value) {
validator(_: RuleObject, value: StoreValue) {
return isValidAddress(value)
? Promise.resolve()
: Promise.reject(new Error('Please enter valid addresses.'));
},
});

// check if the provider is gnosis safe
export const checkIfGnosisSafe = async (account, provider) => {
export const checkIfGnosisSafe = async (
account: Address,
provider: JsonRpcProvider | FallbackProvider,
) => {
try {
if (provider && provider.getCode) {
// TODO: getCode has some issues and throws error in console
Expand All @@ -177,6 +183,7 @@ export const checkIfGnosisSafe = async (account, provider) => {
console.error(error);
return false;
}
return false;
};

/**
Expand All @@ -185,12 +192,12 @@ export const checkIfGnosisSafe = async (account, provider) => {
* but now all networks have service manager token. Hence, this function
* defaults to true BUT can be overridden for specific networks in the future.
*/
export const doesNetworkHaveValidServiceManagerTokenFn = (chainId) => !!chainId;
export const doesNetworkHaveValidServiceManagerTokenFn = (chainId: number) => !!chainId;

const doesPathIncludesComponents = (path) => !!path?.includes('components');
const doesPathIncludesAgents = (path) => !!path?.includes('agents');
export const doesPathIncludesServices = (path) => !!path?.includes('services');
export const doesPathIncludesComponentsOrAgents = (path) => {
const doesPathIncludesComponents = (path: string) => !!path?.includes('components');
const doesPathIncludesAgents = (path: string) => !!path?.includes('agents');
export const doesPathIncludesServices = (path: string) => !!path?.includes('services');
export const doesPathIncludesComponentsOrAgents = (path: string) => {
if (!path) return false;
return doesPathIncludesComponents(path) || doesPathIncludesAgents(path);
};
Expand All @@ -200,19 +207,20 @@ export const notifyWrongNetwork = () => {
};

// functions for solana
export const isPageWithSolana = (path) => {
export const isPageWithSolana = (path: string) => {
if (!path) return false;
if (!isString(path)) return false;

const checkPath = (chain) => path.toLowerCase().includes(chain.networkName.toLowerCase());
const checkPath = (chain: SolanaChain) =>
path.toLowerCase().includes(chain.networkName.toLowerCase());
return SVM_SUPPORTED_CHAINS.some(checkPath);
};

export const isValidSolanaPublicKey = (publicKey) => {
export const isValidSolanaPublicKey = (publicKey: PublicKey) => {
try {
const isValid = PublicKey.isOnCurve(publicKey);
return isValid;
} catch (e) {
return false;
}
};
};
6 changes: 2 additions & 4 deletions apps/autonolas-registry/common-util/functions/requests.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Address, TransactionReceipt } from 'viem';

Check failure on line 1 in apps/autonolas-registry/common-util/functions/requests.ts

View workflow job for this annotation

GitHub Actions / build

'TransactionReceipt' is defined but never used

import { notifyError, sendTransaction } from '@autonolas/frontend-library';

import { getEstimatedGasLimit } from 'libs/util-functions/src';
import { getEstimatedGasLimit, notifyError, sendTransaction } from 'libs/util-functions/src';

import { SUPPORTED_CHAINS } from 'common-util/Login';

Expand Down Expand Up @@ -72,7 +70,7 @@ export const claimOwnerIncentivesRequest = async ({
supportedChains: SUPPORTED_CHAINS,
rpcUrls: RPC_URLS as Record<string, string>,
});
return (response as TransactionReceipt)?.transactionHash;
return response?.transactionHash;
} catch (error) {
window.console.log('Error occurred on claiming owner incentives');
throw error;
Expand Down
Loading

0 comments on commit 70e54e9

Please sign in to comment.