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

Remove code related to the gas based staking pools #144

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
// PARTICULAR PURPOSE. See the GNU General Public License for more details.

import { render, screen } from '@testing-library/react';
import { FaNetworkWired } from 'react-icons/fa';
import PoolCommission from '../../../src/components/pools/PoolCommission';
import { withChakraTheme } from '../../test-utilities';
import { StakingPool } from '../../../src/graphql/models';
import stakingPoolsData from '../../../src/stories/stake/tables/stakingPoolsData';
import { FaNetworkWired } from 'react-icons/fa';
import { withChakraTheme } from '../../test-utilities';

const [pool] = stakingPoolsData as unknown as StakingPool[];

Expand Down Expand Up @@ -43,9 +43,7 @@ describe('PoolCommission component', () => {
pool.commissionPercentage !== null
? numberFormat.format(pool.commissionPercentage)
: '-';
const commissionLabel = pool.fee.commission
? `${(pool.fee.commission / 100).toFixed(2)} %`
: `${pool.fee.gas} Gas`;
const commissionLabel = `${(pool.fee.commission / 100).toFixed(2)} %`;

const valueLabel = `${accruedCommissionLabel} (${commissionLabel})`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { BigNumber } from 'ethers';
import { PoolSetting } from '../../../src/components/stake/PoolSetting';
import useStakingPoolQuery from '../../../src/graphql/hooks/useStakingPool';
import useTotalPoolBalance from '../../../src/graphql/hooks/useTotalPoolBalance';
import { StakingPool } from '../../../src/graphql/models';
import { useStakingPool } from '../../../src/services/pool';
import { useStakingPoolFactory } from '../../../src/services/poolFactory';
import { withChakraTheme } from '../../test-utilities';
Expand Down
93 changes: 12 additions & 81 deletions apps/staking/__tests__/services/pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,25 @@
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more details.

import { renderHook, waitFor } from '@testing-library/react';
import {
useFlatRateCommission,
useGasTaxCommission,
useStakingPool,
useStakingPoolCommission,
} from '../../src/services/pool';
Fee,
FlatRateCommission,
StakingPoolImpl,
} from '@cartesi/staking-pool';
import { renderHook, waitFor } from '@testing-library/react';
import { BigNumber, FixedNumber } from 'ethers';
import { act } from 'react-dom/test-utils';
import {
useFeeContract,
useFlatRateCommissionContract,
useGasTaxCommissionContract,
useStakingPoolContract,
} from '../../src/services/contracts';
import { useTransaction, Transaction } from '../../src/services/transaction';
import {
Fee,
FlatRateCommission,
GasTaxCommission,
StakingPoolImpl,
} from '@cartesi/staking-pool';
import { act } from 'react-dom/test-utils';
import { BigNumber, FixedNumber } from 'ethers';
useFlatRateCommission,
useStakingPool,
useStakingPoolCommission,
} from '../../src/services/pool';
import { Transaction, useTransaction } from '../../src/services/transaction';

jest.mock('@explorer/wallet/src/useWallet');

Expand All @@ -43,7 +40,6 @@ jest.mock('../../src/services/eth', () => ({
}));

jest.mock('../../src/services/contracts', () => ({
useGasTaxCommissionContract: jest.fn(),
useFlatRateCommissionContract: jest.fn(),
useFeeContract: jest.fn(),
useStakingPoolContract: jest.fn(),
Expand All @@ -55,10 +51,6 @@ const mockedUseTransaction = useTransaction as jest.MockedFunction<
typeof useTransaction
>;

const mockedUseGasTaxCommissionContract =
useGasTaxCommissionContract as jest.MockedFunction<
typeof useGasTaxCommissionContract
>;
const mockedUseFlatRateCommissionContract =
useFlatRateCommissionContract as jest.MockedFunction<
typeof useFlatRateCommissionContract
Expand All @@ -72,14 +64,6 @@ const mockUseFeeContract = useFeeContract as jest.MockedFunction<
typeof useFeeContract
>;

const gasTaxCommissionContract = {
setGas: jest.fn(),
gas: () => Promise.resolve(),
feeRaiseTimeout: () => Promise.resolve(),
maxRaise: () => Promise.resolve(),
timeoutTimestamp: () => Promise.resolve(BigNumber.from('10000')),
};

const flatRateCommissionContract = {
setRate: jest.fn(),
rate: () => Promise.resolve(),
Expand Down Expand Up @@ -117,59 +101,6 @@ const stakingPoolContract = {
};

describe('pool service', () => {
describe('useGasTaxCommission hook', () => {
it('should return default initial values when fee is undefined', () => {
mockedUseGasTaxCommissionContract.mockReturnValue(undefined);

const { result } = renderHook(() => useGasTaxCommission(address));

expect(result.current.gas).toBe(undefined);
expect(result.current.maxRaise).toBe(undefined);
expect(result.current.timeoutTimestamp).toBe(undefined);
expect(result.current.raiseTimeout).toBe(undefined);
});

it('should set a transaction when changing gas', async () => {
mockedUseGasTaxCommissionContract.mockReturnValue(
gasTaxCommissionContract as unknown as GasTaxCommission
);
const mockedSet = jest.fn();
mockedUseTransaction.mockReturnValue({
set: mockedSet,
} as unknown as Transaction<any>);
const { result } = renderHook(() => useGasTaxCommission(address));
await act(async () => {
result.current.changeGas(1000);
});
expect(mockedSet).toHaveBeenCalled();
});

it('should set correct values when fee is defined', async () => {
const gas = BigNumber.from('1000');
const raiseTimeout = BigNumber.from('10000000');
const maxRaise = BigNumber.from('1000000000');
const timeoutTimestamp = BigNumber.from('10000');
mockedUseGasTaxCommissionContract.mockReturnValue({
setGas: jest.fn(),
gas: () => Promise.resolve(gas),
feeRaiseTimeout: () => Promise.resolve(raiseTimeout),
maxRaise: () => Promise.resolve(maxRaise),
timeoutTimestamp: () => Promise.resolve(timeoutTimestamp),
} as unknown as GasTaxCommission);

const { result } = renderHook(() => useGasTaxCommission(address));

await waitFor(() => {
expect(result.current.gas).toStrictEqual(gas);
expect(result.current.raiseTimeout).toStrictEqual(raiseTimeout);
expect(result.current.maxRaise).toStrictEqual(maxRaise);
expect(result.current.timeoutTimestamp).toStrictEqual(
new Date(timeoutTimestamp.toNumber() * 1000)
);
});
});
});

describe('useFlatRateCommission hook', () => {
it('should return default initial values when fee is undefined', () => {
mockedUseFlatRateCommissionContract.mockReturnValue(undefined);
Expand Down
9 changes: 0 additions & 9 deletions apps/staking/__tests__/utils/messages.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,6 @@ describe('useMessages hook', () => {
'This model calculates the commission as a fixed percentage of the block CTSI reward before distributing the remaining amount to the pool users.'
);
});

it('should return a message of how gas-based works', () => {
const { result } = renderHook(() =>
useMessages('commission.model.gasBased.howItWorks')
);
expect(result.current).toEqual(
'This model calculates the commission considering the current network gas price, Ethereum price and CTSI price. The configured amount of gas above is multiplied by the gas price provided by a ChainLink oracle, then converted from ETH to CTSI using an Uniswap V2 price oracle.'
);
});
});
});

Expand Down
4 changes: 2 additions & 2 deletions apps/staking/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"lint:fix": "yarn lint --fix",
"start": "next start",
"storybook": "start-storybook -p 6006",
"test": "jest",
"test:ci": "jest --runInBand --coverage"
"test": "dotenv -c test -- jest",
"test:ci": "dotenv -c test jest --runInBand --coverage"
},
"dependencies": {
"@apollo/client": "3.4.13",
Expand Down
8 changes: 3 additions & 5 deletions apps/staking/src/components/pools/PoolCommission.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more details.

import React, { FC } from 'react';
import { Flex, HStack, SystemProps, Text, TextProps } from '@chakra-ui/react';
import { Icon } from '@chakra-ui/icons';
import { Flex, HStack, SystemProps, Text, TextProps } from '@chakra-ui/react';
import { FC } from 'react';
import { IconType } from 'react-icons';
import { StakingPool } from '../../graphql/models';

Expand All @@ -33,9 +33,7 @@ const PoolCommission: FC<PoolCommissionProps> = (props) => {
pool.commissionPercentage !== null
? numberFormat.format(pool.commissionPercentage)
: '-';
const commissionLabel = pool.fee.commission
? `${(pool.fee.commission / 100).toFixed(2)} %`
: `${pool.fee.gas} Gas`;
const commissionLabel = `${(pool.fee.commission / 100).toFixed(2)} %`;

const valueLabel = `${accruedCommissionLabel} (${commissionLabel})`;
return (
Expand Down
10 changes: 5 additions & 5 deletions apps/staking/src/components/pools/fee/CommissionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more details.

import { FC, useEffect, useMemo } from 'react';
import {
Alert,
Button,
Expand All @@ -18,18 +17,19 @@ import {
FormHelperText,
FormLabel,
HStack,
Text,
VStack,
Input,
InputGroup,
InputRightAddon,
Text,
VStack,
} from '@chakra-ui/react';
import { useForm } from 'react-hook-form';
import humanizeDuration from 'humanize-duration';
import { FC, useEffect, useMemo } from 'react';
import { useForm } from 'react-hook-form';

export interface CommissionFormProps {
currentValue: number; // current value
unit: string; // unit: '%' | 'gas'
unit: string; // unit: '%'
min?: number; // minimum value
max?: number; // maximum value
maxRaise: number; // max raise of the value
Expand Down
2 changes: 1 addition & 1 deletion apps/staking/src/components/stake/CommissionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { useForm } from 'react-hook-form';

export interface CommissionFormProps {
currentValue: number;
unit: '%' | 'gas';
unit: '%';
min?: number;
max?: number;
maxRaise: number;
Expand Down
24 changes: 4 additions & 20 deletions apps/staking/src/components/stake/PoolSetting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import { FC } from 'react';
import { useForm } from 'react-hook-form';
import { FaBalanceScaleLeft } from 'react-icons/fa';
import FlatRateContainer from '../../containers/stake/FlatRateContainer';
import GasTaxContainer from '../../containers/stake/GasTaxContainer';
import useStakingPoolQuery from '../../graphql/hooks/useStakingPool';
import { useStakingPool } from '../../services/pool';
import { useStakingPoolFactory } from '../../services/poolFactory';
Expand Down Expand Up @@ -129,12 +128,6 @@ export const PoolSetting: FC<PoolSettingsProps> = ({ address }) => {
pool.amounts?.stake > BigNumber.from(0) ||
pool.amounts?.unstake > BigNumber.from(0) ||
pool.amounts?.withdraw > BigNumber.from(0);
const feeType =
stakingPool?.fee?.commission !== null
? 'flatRate'
: stakingPool?.fee?.gas !== null
? 'gasTax'
: undefined;
const isRebalancing = pool.rebalanceTransaction?.isOngoing;
const isRebalanceButtonDisabled = !isRebalanceEnabled || isRebalancing;
const hasSamePoS = isSamePoS(pool.pos, poolFactory.pos);
Expand Down Expand Up @@ -297,19 +290,10 @@ export const PoolSetting: FC<PoolSettingsProps> = ({ address }) => {
</Notification>
)}

{feeType == 'flatRate' && (
<FlatRateContainer
pool={address}
alertMessage={wordingFor.commission}
/>
)}

{feeType == 'gasTax' && (
<GasTaxContainer
pool={address}
alertMessage={wordingFor.commission}
/>
)}
<FlatRateContainer
pool={address}
alertMessage={wordingFor.commission}
/>

<Stack
justifySelf="flex-end"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export const WalletBalanceSection: FC<IWalletBalanceSectionProps> = ({
const { isGnosisSafe } = useWallet();
const ethInfoMessage = useMessages('balance.eth.available.forGasCosts');
const iconColor = useColorModeValue('light.primary', 'dark.primary');
const iconBg = useColorModeValue('dark.gray.senary', 'dark.gray.primary');
const warningIconColor = useColorModeValue(
'light.support.warning',
'white'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more details.

import { FC, useState } from 'react';
import { LockIcon } from '@chakra-ui/icons';
import {
Box,
Expand All @@ -25,6 +24,7 @@ import {
import { Address, StakeIcon } from '@explorer/ui';
import { first, last } from 'lodash/fp';
import NextLink from 'next/link';
import { FC, useState } from 'react';
import { StakingPool } from '../../../graphql/models';
import labels from '../../../utils/labels';
import { formatCTSI } from '../../../utils/token';
Expand Down Expand Up @@ -92,29 +92,11 @@ const PoolPerformanceTableRow: FC<PoolPerformanceTableRowProps> = ({
? numberFormat.format(pool.commissionPercentage)
: '-';

let flatRate = pool.fee.commission > 0;
const gasTax = pool.fee.gas > 0;

// XXX: if both are zero, currently we don't which is it, for now let's assume it's flat rate
if (!flatRate && !gasTax) {
flatRate = true;
}

// commission label
let commissionLabel = '';
if (flatRate) {
commissionLabel = `${(pool.fee.commission / 100).toFixed(2)} %`;
} else if (gasTax) {
commissionLabel = `${pool.fee.gas} Gas`;
}
const commissionLabel = `${(pool.fee.commission / 100).toFixed(2)} %`;

// commission help tooltip
let commissionTooltip: string = undefined;
if (flatRate) {
commissionTooltip = labels.flatRateCommission;
} else if (gasTax) {
commissionTooltip = labels.gasTaxCommission;
}
const commissionTooltip: string = labels.flatRateCommission;

const [isHovered, setHovered] = useState(false);
const backgroundColor = useColorModeValue('white', 'dark.gray.primary');
Expand Down
Loading