Skip to content

Commit

Permalink
(registry) feat: Next epoch reward estimates are also visible on comp…
Browse files Browse the repository at this point in the history
…onent and agent Registry pages (#59)

* feat: init - add next epoch rewards row

* feat: create new library for common contract functions

* chore: minor improvements

* refactor: Update DetailsSubInfo component to use custom hook for tokenomics unit type

* refactor: Update RewardsSection to fetch and display pending incentives

* refactor: Update RewardsSection to fetch and display pending incentives

* chore: format document autonolas-registry/common-util/Contracts

* feat: move tokenomics.js to tokenomics.ts add export TOKENOMICS to util-contracts library

* chore: declare module for tokenomics.js in util-contracts

* refractor: convert rewards.js to rewards.ts

* refractor: move claimable rewards to hooks (using useReadContract)

* feat: add useClaimableRewards, try to move other rewards to hooks

* refactor: Update RewardsSection to fetch and display pending incentives

* refractor: udpate configs

* fix: broken tests for agent

* test: add test for component details reward section

* refactor: skip test for common-utils/addresses

* refractor: cleanup rewards.ts and add fetch to useRewards

* refractor: remove getMapUnitIncentivesRequest from tokenomics and use from common-contract-functions

* refactor: update test for common-utils/addresses (skip for 2 test-cases as of now)

* refractor: minor improvement

* ,

* refactor: Fix typo in error message for notifications

* cd '/Users/mohandas/work/open-source-valory/autonolas-frontend-mono'

* test: rewards util

* refactor: Improve getMapUnitIncentivesRequest function in rewards.ts by using only biginit

* refactor: Improve getMapUnitIncentivesRequest function in rewards.ts

* style: review changes by Roman
  • Loading branch information
mohandast52 authored Jul 12, 2024
1 parent 0ed9251 commit 9373860
Show file tree
Hide file tree
Showing 37 changed files with 708 additions and 365 deletions.
75 changes: 38 additions & 37 deletions apps/autonolas-registry/common-util/Contracts/index.jsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
import { ethers } from 'ethers';
import Web3 from 'web3';

import { isL1Network } from '@autonolas/frontend-library';

import { TOKENOMICS } from 'libs/util-contracts/src/lib/abiAndAddresses/tokenomics';

import {
REGISTRIES_MANAGER_CONTRACT,
AGENT_REGISTRY_CONTRACT,
COMPONENT_REGISTRY_CONTRACT,
GENERIC_ERC20_CONTRACT,
GNOSIS_SAFE_CONTRACT,
MULTI_SEND_CONTRACT,
OPERATOR_WHITELIST_CONTRACT,
REGISTRIES_MANAGER_CONTRACT,
SERVICE_MANAGER_CONTRACT,
SERVICE_REGISTRY_CONTRACT,
SERVICE_MANAGER_TOKEN_CONTRACT,
SERVICE_REGISTRY_CONTRACT,
SERVICE_REGISTRY_L2,
SERVICE_REGISTRY_TOKEN_UTILITY_CONTRACT,
SIGN_MESSAGE_LIB_CONTRACT,
GNOSIS_SAFE_CONTRACT,
MULTI_SEND_CONTRACT,
GENERIC_ERC20_CONTRACT,
OPERATOR_WHITELIST_CONTRACT,
} from 'common-util/AbiAndAddresses';
import {
doesNetworkHaveValidServiceManagerTokenFn,
getChainId,
getProvider,
doesNetworkHaveValidServiceManagerTokenFn,
} from 'common-util/functions';
import {
LOCAL_FORK_ID,
LOCAL_FORK_ID_GNOSIS,
LOCAL_FORK_ID_POLYGON,
} from 'util/constants';
import { ADDRESSES } from './addresses';
import { LOCAL_FORK_ID, LOCAL_FORK_ID_GNOSIS, LOCAL_FORK_ID_POLYGON } from 'util/constants';

import {
TOKENOMICS
} from 'libs/util-contracts/src/lib/abiAndAddresses/tokenomics'
import { ADDRESSES } from './addresses';

export const RPC_URLS = {
1: process.env.NEXT_PUBLIC_MAINNET_URL,
Expand Down Expand Up @@ -82,10 +79,7 @@ export const getComponentContract = () => {
const { address } = getWeb3Details();
const { componentRegistry } = address;

const contract = getContract(
COMPONENT_REGISTRY_CONTRACT.abi,
componentRegistry,
);
const contract = getContract(COMPONENT_REGISTRY_CONTRACT.abi, componentRegistry);
return contract;
};

Expand All @@ -105,10 +99,7 @@ export const getAgentContract = () => {
export const getMechMinterContract = () => {
const { address } = getWeb3Details();
const { registriesManager } = address;
const contract = getContract(
REGISTRIES_MANAGER_CONTRACT.abi,
registriesManager,
);
const contract = getContract(REGISTRIES_MANAGER_CONTRACT.abi, registriesManager);

return contract;
};
Expand All @@ -121,10 +112,7 @@ export const getServiceContract = () => {
const { address, chainId } = getWeb3Details();
if (isL1Network(chainId)) {
const { serviceRegistry } = address;
const contract = getContract(
SERVICE_REGISTRY_CONTRACT.abi,
serviceRegistry,
);
const contract = getContract(SERVICE_REGISTRY_CONTRACT.abi, serviceRegistry);
return contract;
}

Expand All @@ -140,10 +128,7 @@ export const getServiceManagerContract = () => {
const { address, chainId } = getWeb3Details();
if (doesNetworkHaveValidServiceManagerTokenFn(chainId)) {
const { serviceManagerToken } = address;
const contract = getContract(
SERVICE_MANAGER_TOKEN_CONTRACT.abi,
serviceManagerToken,
);
const contract = getContract(SERVICE_MANAGER_TOKEN_CONTRACT.abi, serviceManagerToken);
return contract;
}

Expand Down Expand Up @@ -171,10 +156,7 @@ export const getServiceRegistryTokenUtilityContract = () => {
export const getOperatorWhitelistContract = () => {
const { address } = getWeb3Details();
const { operatorWhitelist } = address;
const contract = getContract(
OPERATOR_WHITELIST_CONTRACT.abi,
operatorWhitelist,
);
const contract = getContract(OPERATOR_WHITELIST_CONTRACT.abi, operatorWhitelist);
return contract;
};

Expand Down Expand Up @@ -214,6 +196,25 @@ export const getMultiSendContract = (address) => {
* @returns tokenomics proxy contract
*/
export const getTokenomicsContract = (address) => {
const contract = getContract(TOKENOMICS.abi, address);
const web3 = new Web3(getProvider());
const contract = new web3.eth.Contract(TOKENOMICS.abi, address);
return contract;
};

/**
* @returns ethers provider for ethereum
*/
export const getEthersProviderForEthereum = () => {
const provider = new ethers.JsonRpcProvider(RPC_URLS[1]);
return provider;
};

/**
* TODO: Remove this function once migrated to hooks
* @returns tokenomics ethers contract
*/
export const getTokenomicsEthersContract = (address) => {
const provider = getEthersProviderForEthereum();
const contract = new ethers.Contract(address, TOKENOMICS.abi, provider);
return contract;
};
Original file line number Diff line number Diff line change
@@ -1,42 +1,97 @@
import { Col, Flex, Row } from 'antd';
import { FC, memo } from 'react';
import { formatEther } from 'viem';
import { FC, useEffect, useState } from 'react';
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 { getEthersProviderForEthereum, getTokenomicsEthersContract } from 'common-util/Contracts';

import { RewardsStatistic } from '../styles';
import { useTokenomicsUnitType } from './hooks';

/**
* Formats reward values to required decimal places
*/
const rewardsFormatter = (reward: bigint, dp: number) =>
parseFloat(formatEther(reward)).toLocaleString('en', {
maximumFractionDigits: dp,
minimumFractionDigits: dp,
});
type RewardsColumnProps = { title: string; statistic: null | string; loading?: boolean };

const RewardColumn = ({ title, statistic }: { title: string; statistic: string }) => (
const RewardColumn = ({ title, statistic, loading }: RewardsColumnProps) => (
<Col span={24} xl={12}>
<RewardsStatistic title={title} value={statistic} />
<RewardsStatistic title={title} value={statistic || '--'} loading={!!loading} />
</Col>
);

type RewardsSectionProps = { reward: bigint; topUp: bigint };
export const RewardsSection: FC<RewardsSectionProps> = memo(function RewardsSection({
reward,
topUp,
}) {
type RewardsSectionProps = {
ownerAddress: Address;
id: string;
type: string;
dataTestId: string;
};

export const RewardsSection: FC<RewardsSectionProps> = ({ ownerAddress, id, type, dataTestId }) => {
const [isPendingIncentivesLoading, setIsPendingIncentivesLoading] = useState<boolean>(true);
const [pendingIncentives, setPendingIncentives] = useState<{
reward: string;
topUp: string;
} | null>(null);

const tokenomicsUnitType = useTokenomicsUnitType(type);
const {
isFetching,
reward: claimableReward,
topUp: claimableTopup,
} = useClaimableIncentives(
TOKENOMICS.addresses[1],
TOKENOMICS.abi,
ownerAddress,
id,
tokenomicsUnitType,
);

useEffect(() => {
const provider = getEthersProviderForEthereum();
const contract = getTokenomicsEthersContract(TOKENOMICS.addresses[1]);

getPendingIncentives(provider, contract, `${tokenomicsUnitType}`, id)
.then((data) => setPendingIncentives(data))
.catch((error) => console.error(error))
.finally(() => setIsPendingIncentivesLoading(false));
}, [ownerAddress, id, tokenomicsUnitType]);

return (
<Flex vertical gap={4}>
<Row>
<RewardColumn title={'Claimable Reward'} statistic={`${rewardsFormatter(reward, 4)} ETH`} />
<RewardColumn title={'Claimable Top-Up'} statistic={`${rewardsFormatter(topUp, 2)} OLAS`} />
</Row>
<Row>
<a href="https://tokenomics.olas.network/donate">
Make donation {UNICODE_SYMBOLS.EXTERNAL_LINK}
</a>
</Row>
<Flex gap={16} vertical className="mt-12" data-testid={dataTestId}>
<Flex vertical gap={4}>
<Row>
<RewardColumn
title="Claimable Reward"
statistic={claimableReward ? `${claimableReward} ETH` : null}
loading={isFetching}
/>
<RewardColumn
title="Claimable Top Up"
statistic={claimableTopup ? `${claimableTopup} OLAS` : null}
/>
</Row>
</Flex>

<Flex vertical gap={12}>
<Row>
<RewardColumn
title="Pending Reward"
statistic={pendingIncentives ? `${pendingIncentives?.reward} ETH` : null}
loading={isPendingIncentivesLoading}
/>
<RewardColumn
title="Pending Top Up"
statistic={pendingIncentives ? `${pendingIncentives?.topUp} OLAS` : null}
loading={isPendingIncentivesLoading}
/>
</Row>

<Row>
<a href="https://tokenomics.olas.network/donate">
Make donation {UNICODE_SYMBOLS.EXTERNAL_LINK}
</a>
</Row>
</Flex>
</Flex>
);
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { NAV_TYPES, TOKENOMICS_UNIT_TYPES } from '../../../util/constants';

export const useTokenomicsUnitType = (type?: string) => {
if (type === NAV_TYPES.COMPONENT) return TOKENOMICS_UNIT_TYPES.COMPONENT;
if (type === NAV_TYPES.AGENT) return TOKENOMICS_UNIT_TYPES.AGENT;
return;
};
Loading

0 comments on commit 9373860

Please sign in to comment.