fix(governance): rewards decimals set per asset (#3941)

This commit is contained in:
Sam Keen 2023-05-25 10:37:40 +01:00 committed by GitHub
parent e4d4646878
commit f5e67a0c2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 99 additions and 32 deletions

View File

@ -8,6 +8,7 @@ const mockData = {
{
asset: 'tDAI',
totalAmount: '5',
decimals: 6,
rewardTypes: {
ACCOUNT_TYPE_GLOBAL_REWARD: {
amount: '0',

View File

@ -1,6 +1,5 @@
import { formatNumber, toBigNum } from '@vegaprotocol/utils';
import { Tooltip } from '@vegaprotocol/ui-toolkit';
import { useAppState } from '../../../contexts/app-state/app-state-context';
import {
rowGridItemStyles,
RewardsTable,
@ -13,7 +12,8 @@ interface EpochIndividualRewardsGridProps {
}
interface RewardItemProps {
value: string;
amount: string;
decimals: number;
percentageOfTotal?: string;
dataTestId: string;
last?: boolean;
@ -21,15 +21,14 @@ interface RewardItemProps {
const DisplayReward = ({
reward,
decimals,
percentageOfTotal,
}: {
reward: string;
decimals: number;
percentageOfTotal?: string;
}) => {
const { t } = useTranslation();
const {
appState: { decimals },
} = useAppState();
if (Number(reward) === 0) {
return <span className="text-vega-dark-300">-</span>;
@ -64,7 +63,8 @@ const DisplayReward = ({
};
const RewardItem = ({
value,
amount,
decimals,
percentageOfTotal,
dataTestId,
last,
@ -72,7 +72,11 @@ const RewardItem = ({
<div data-testid={dataTestId} className={rowGridItemStyles(last)}>
<div className="h-full w-5 absolute right-0 top-0 bg-gradient-to-r from-transparent to-black pointer-events-none" />
<div className="overflow-auto p-5">
<DisplayReward reward={value} percentageOfTotal={percentageOfTotal} />
<DisplayReward
reward={amount}
decimals={decimals}
percentageOfTotal={percentageOfTotal}
/>
</div>
<div className="h-full w-5 absolute left-0 top-0 bg-gradient-to-l from-transparent to-black pointer-events-none" />
</div>
@ -86,7 +90,7 @@ export const EpochIndividualRewardsTable = ({
dataTestId="epoch-individual-rewards-table"
epoch={Number(data.epoch)}
>
{data.rewards.map(({ asset, rewardTypes, totalAmount }, i) => (
{data.rewards.map(({ asset, rewardTypes, totalAmount, decimals }, i) => (
<div className="contents" key={i}>
<div
data-testid="individual-rewards-asset"
@ -98,13 +102,19 @@ export const EpochIndividualRewardsTable = ({
([key, { amount, percentageOfTotal }]) => (
<RewardItem
key={key}
value={amount}
amount={amount}
decimals={decimals}
percentageOfTotal={percentageOfTotal}
dataTestId={key}
/>
)
)}
<RewardItem dataTestId="total" value={totalAmount} last={true} />
<RewardItem
dataTestId="total"
amount={totalAmount}
decimals={decimals}
last={true}
/>
</div>
))}
</RewardsTable>

View File

@ -8,7 +8,7 @@ describe('generateEpochIndividualRewardsList', () => {
amount: '100',
percentageOfTotal: '0.1',
receivedAt: new Date(),
asset: { id: 'usd', symbol: 'USD', name: 'USD' },
asset: { id: 'usd', symbol: 'USD', name: 'USD', decimals: 6 },
party: { id: 'blah' },
epoch: { id: '1' },
};
@ -18,7 +18,7 @@ describe('generateEpochIndividualRewardsList', () => {
amount: '50',
percentageOfTotal: '0.05',
receivedAt: new Date(),
asset: { id: 'eur', symbol: 'EUR', name: 'EUR' },
asset: { id: 'eur', symbol: 'EUR', name: 'EUR', decimals: 5 },
party: { id: 'blah' },
epoch: { id: '2' },
};
@ -28,7 +28,7 @@ describe('generateEpochIndividualRewardsList', () => {
amount: '200',
percentageOfTotal: '0.2',
receivedAt: new Date(),
asset: { id: 'gbp', symbol: 'GBP', name: 'GBP' },
asset: { id: 'gbp', symbol: 'GBP', name: 'GBP', decimals: 7 },
party: { id: 'blah' },
epoch: { id: '2' },
};
@ -38,7 +38,7 @@ describe('generateEpochIndividualRewardsList', () => {
amount: '100',
percentageOfTotal: '0.1',
receivedAt: new Date(),
asset: { id: 'usd', symbol: 'USD', name: 'USD' },
asset: { id: 'usd', symbol: 'USD', name: 'USD', decimals: 6 },
party: { id: 'blah' },
epoch: { id: '1' },
};
@ -48,7 +48,7 @@ describe('generateEpochIndividualRewardsList', () => {
amount: '150',
percentageOfTotal: '0.15',
receivedAt: new Date(),
asset: { id: 'usd', symbol: 'USD', name: 'USD' },
asset: { id: 'usd', symbol: 'USD', name: 'USD', decimals: 6 },
party: { id: 'blah' },
epoch: { id: '3' },
};
@ -58,7 +58,7 @@ describe('generateEpochIndividualRewardsList', () => {
amount: '50',
percentageOfTotal: '0.05',
receivedAt: new Date(),
asset: { id: 'eur', symbol: 'EUR', name: 'EUR' },
asset: { id: 'eur', symbol: 'EUR', name: 'EUR', decimals: 5 },
party: { id: 'blah' },
epoch: { id: '2' },
};
@ -99,6 +99,7 @@ describe('generateEpochIndividualRewardsList', () => {
rewards: [
{
asset: 'USD',
decimals: 6,
totalAmount: '100',
rewardTypes: {
[AccountType.ACCOUNT_TYPE_FEES_INFRASTRUCTURE]: {
@ -167,6 +168,7 @@ describe('generateEpochIndividualRewardsList', () => {
{
asset: 'GBP',
totalAmount: '200',
decimals: 7,
rewardTypes: {
[AccountType.ACCOUNT_TYPE_FEES_INFRASTRUCTURE]: {
amount: '0',
@ -197,6 +199,7 @@ describe('generateEpochIndividualRewardsList', () => {
{
asset: 'EUR',
totalAmount: '50',
decimals: 5,
rewardTypes: {
[AccountType.ACCOUNT_TYPE_FEES_INFRASTRUCTURE]: {
amount: '0',
@ -232,6 +235,7 @@ describe('generateEpochIndividualRewardsList', () => {
{
asset: 'USD',
totalAmount: '200',
decimals: 6,
rewardTypes: {
[AccountType.ACCOUNT_TYPE_FEES_INFRASTRUCTURE]: {
amount: '0',
@ -279,6 +283,7 @@ describe('generateEpochIndividualRewardsList', () => {
rewards: [
{
asset: 'USD',
decimals: 6,
totalAmount: '150',
rewardTypes: {
[AccountType.ACCOUNT_TYPE_FEES_INFRASTRUCTURE]: {
@ -315,6 +320,7 @@ describe('generateEpochIndividualRewardsList', () => {
{
asset: 'GBP',
totalAmount: '200',
decimals: 7,
rewardTypes: {
[AccountType.ACCOUNT_TYPE_FEES_INFRASTRUCTURE]: {
amount: '0',
@ -345,6 +351,7 @@ describe('generateEpochIndividualRewardsList', () => {
{
asset: 'EUR',
totalAmount: '50',
decimals: 5,
rewardTypes: {
[AccountType.ACCOUNT_TYPE_FEES_INFRASTRUCTURE]: {
amount: '0',
@ -390,6 +397,7 @@ describe('generateEpochIndividualRewardsList', () => {
{
asset: 'USD',
totalAmount: '200',
decimals: 6,
rewardTypes: {
[AccountType.ACCOUNT_TYPE_FEES_INFRASTRUCTURE]: {
amount: '0',

View File

@ -9,6 +9,7 @@ export interface EpochIndividualReward {
rewards: {
asset: string;
totalAmount: string;
decimals: number;
rewardTypes: {
[key in AccountType]?: {
amount: string;
@ -53,6 +54,7 @@ export const generateEpochIndividualRewardsList = ({
const epochIndividualRewards = rewards.reduce((acc, reward) => {
const epochId = reward.epoch.id;
const assetName = reward.asset.name;
const assetDecimals = reward.asset.decimals;
const rewardType = reward.rewardType;
const amount = reward.amount;
const percentageOfTotal = reward.percentageOfTotal;
@ -73,6 +75,7 @@ export const generateEpochIndividualRewardsList = ({
if (!asset) {
asset = {
asset: assetName,
decimals: assetDecimals,
totalAmount: '0',
rewardTypes: Object.fromEntries(emptyRowAccountTypes),
};

View File

@ -52,6 +52,7 @@ const assetRewards: Map<
assetRewards.set(assetId, {
assetId,
name: 'tDAI TEST',
decimals: 6,
rewards,
totalAmount: '295',
});

View File

@ -1,6 +1,5 @@
import { formatNumber, toBigNum } from '@vegaprotocol/utils';
import { Tooltip } from '@vegaprotocol/ui-toolkit';
import { useAppState } from '../../../contexts/app-state/app-state-context';
import {
rowGridItemStyles,
RewardsTable,
@ -12,16 +11,19 @@ interface EpochTotalRewardsGridProps {
}
interface RewardItemProps {
value: string;
amount: string;
decimals: number;
dataTestId: string;
last?: boolean;
}
const DisplayReward = ({ reward }: { reward: string }) => {
const {
appState: { decimals },
} = useAppState();
const DisplayReward = ({
reward,
decimals,
}: {
reward: string;
decimals: number;
}) => {
if (Number(reward) === 0) {
return <span className="text-vega-dark-300">-</span>;
}
@ -33,11 +35,16 @@ const DisplayReward = ({ reward }: { reward: string }) => {
);
};
const RewardItem = ({ value, dataTestId, last }: RewardItemProps) => (
const RewardItem = ({
amount,
decimals,
dataTestId,
last,
}: RewardItemProps) => (
<div data-testid={dataTestId} className={rowGridItemStyles(last)}>
<div className="h-full w-5 absolute right-0 top-0 bg-gradient-to-r from-transparent to-black pointer-events-none" />
<div className="overflow-auto p-5">
<DisplayReward reward={value} />
<DisplayReward reward={amount} decimals={decimals} />
</div>
<div className="h-full w-5 absolute left-0 top-0 bg-gradient-to-l from-transparent to-black pointer-events-none" />
</div>
@ -49,15 +56,25 @@ export const EpochTotalRewardsTable = ({
return (
<RewardsTable dataTestId="epoch-total-rewards-table" epoch={data.epoch}>
{Array.from(data.assetRewards.values()).map(
({ name, rewards, totalAmount }, i) => (
({ name, rewards, totalAmount, decimals }, i) => (
<div className="contents" key={i}>
<div data-testid="asset" className={`${rowGridItemStyles()} p-5`}>
{name}
</div>
{Array.from(rewards.values()).map(({ rewardType, amount }, i) => (
<RewardItem key={i} dataTestId={rewardType} value={amount} />
<RewardItem
key={i}
dataTestId={rewardType}
amount={amount}
decimals={decimals}
/>
))}
<RewardItem dataTestId="total" value={totalAmount} last={true} />
<RewardItem
dataTestId="total"
amount={totalAmount}
decimals={decimals}
last={true}
/>
</div>
)
)}

View File

@ -56,12 +56,14 @@ describe('generateEpochAssetRewardsList', () => {
node: {
id: '1',
name: 'Asset 1',
decimals: 18,
},
},
{
node: {
id: '2',
name: 'Asset 2',
decimals: 6,
},
},
],
@ -101,6 +103,7 @@ describe('generateEpochAssetRewardsList', () => {
{
node: {
epoch: 1,
decimals: 18,
assetId: '1',
rewardType: AccountType.ACCOUNT_TYPE_GLOBAL_REWARD,
amount: '123',
@ -128,6 +131,7 @@ describe('generateEpochAssetRewardsList', () => {
'1',
{
assetId: '1',
decimals: 0,
name: '',
rewards: new Map([
[
@ -196,12 +200,14 @@ describe('generateEpochAssetRewardsList', () => {
node: {
id: '1',
name: 'Asset 1',
decimals: 18,
},
},
{
node: {
id: '2',
name: 'Asset 2',
decimals: 6,
},
},
],
@ -212,6 +218,7 @@ describe('generateEpochAssetRewardsList', () => {
node: {
epoch: 1,
assetId: '1',
decimals: 18,
rewardType: AccountType.ACCOUNT_TYPE_REWARD_MAKER_PAID_FEES,
amount: '123',
},
@ -220,6 +227,7 @@ describe('generateEpochAssetRewardsList', () => {
node: {
epoch: 1,
assetId: '1',
decimals: 18,
rewardType: AccountType.ACCOUNT_TYPE_FEES_INFRASTRUCTURE,
amount: '100',
},
@ -228,6 +236,7 @@ describe('generateEpochAssetRewardsList', () => {
node: {
epoch: 2,
assetId: '1',
decimals: 18,
rewardType: AccountType.ACCOUNT_TYPE_REWARD_LP_RECEIVED_FEES,
amount: '5',
},
@ -254,6 +263,7 @@ describe('generateEpochAssetRewardsList', () => {
'1',
{
assetId: '1',
decimals: 18,
name: 'Asset 1',
rewards: new Map([
[
@ -319,6 +329,7 @@ describe('generateEpochAssetRewardsList', () => {
'1',
{
assetId: '1',
decimals: 18,
name: 'Asset 1',
rewards: new Map([
[
@ -387,12 +398,14 @@ describe('generateEpochAssetRewardsList', () => {
node: {
id: '1',
name: 'Asset 1',
decimals: 18,
},
},
{
node: {
id: '2',
name: 'Asset 2',
decimals: 6,
},
},
],
@ -403,6 +416,7 @@ describe('generateEpochAssetRewardsList', () => {
node: {
epoch: 1,
assetId: '1',
decimals: 18,
rewardType: AccountType.ACCOUNT_TYPE_REWARD_MAKER_PAID_FEES,
amount: '123',
},
@ -411,6 +425,7 @@ describe('generateEpochAssetRewardsList', () => {
node: {
epoch: 1,
assetId: '1',
decimals: 18,
rewardType: AccountType.ACCOUNT_TYPE_FEES_INFRASTRUCTURE,
amount: '100',
},
@ -419,6 +434,7 @@ describe('generateEpochAssetRewardsList', () => {
node: {
epoch: 2,
assetId: '1',
decimals: 18,
rewardType: AccountType.ACCOUNT_TYPE_REWARD_LP_RECEIVED_FEES,
amount: '6',
},
@ -427,6 +443,7 @@ describe('generateEpochAssetRewardsList', () => {
node: {
epoch: 2,
assetId: '1',
decimals: 18,
rewardType: AccountType.ACCOUNT_TYPE_REWARD_LP_RECEIVED_FEES,
amount: '27',
},
@ -435,6 +452,7 @@ describe('generateEpochAssetRewardsList', () => {
node: {
epoch: 3,
assetId: '1',
decimals: 18,
rewardType: AccountType.ACCOUNT_TYPE_FEES_INFRASTRUCTURE,
amount: '15',
},
@ -467,6 +485,7 @@ describe('generateEpochAssetRewardsList', () => {
{
assetId: '1',
name: 'Asset 1',
decimals: 18,
rewards: new Map([
[
AccountType.ACCOUNT_TYPE_GLOBAL_REWARD,
@ -531,6 +550,7 @@ describe('generateEpochAssetRewardsList', () => {
'1',
{
assetId: '1',
decimals: 18,
name: 'Asset 1',
rewards: new Map([
[
@ -608,6 +628,7 @@ describe('generateEpochAssetRewardsList', () => {
'1',
{
assetId: '1',
decimals: 18,
name: 'Asset 1',
rewards: new Map([
[

View File

@ -22,6 +22,7 @@ export type AggregatedEpochRewardSummary = {
name: EpochSummaryWithNamedReward['name'];
rewards: Map<RewardType, RewardItem>;
totalAmount: string;
decimals: number;
};
export type EpochTotalSummary = {
@ -91,6 +92,7 @@ export const generateEpochTotalRewardsList = ({
assetId: reward.assetId,
name: matchingAsset?.name || '',
rewards: rewards || new Map(emptyRowAccountTypes),
decimals: matchingAsset?.decimals || 0,
totalAmount: (
Number(reward.amount) + Number(assetWithRewards?.totalAmount || 0)
).toString(),

View File

@ -4,6 +4,7 @@ fragment RewardFields on Reward {
id
symbol
name
decimals
}
party {
id
@ -67,6 +68,7 @@ query EpochAssetsRewards(
node {
id
name
decimals
}
}
}

View File

@ -3,7 +3,7 @@ import * as Types from '@vegaprotocol/types';
import { gql } from '@apollo/client';
import * as Apollo from '@apollo/client';
const defaultOptions = {} as const;
export type RewardFieldsFragment = { __typename?: 'Reward', rewardType: Types.AccountType, amount: string, percentageOfTotal: string, receivedAt: any, asset: { __typename?: 'Asset', id: string, symbol: string, name: string }, party: { __typename?: 'Party', id: string }, epoch: { __typename?: 'Epoch', id: string } };
export type RewardFieldsFragment = { __typename?: 'Reward', rewardType: Types.AccountType, amount: string, percentageOfTotal: string, receivedAt: any, asset: { __typename?: 'Asset', id: string, symbol: string, name: string, decimals: number }, party: { __typename?: 'Party', id: string }, epoch: { __typename?: 'Epoch', id: string } };
export type DelegationFieldsFragment = { __typename?: 'Delegation', amount: string, epoch: number };
@ -16,7 +16,7 @@ export type RewardsQueryVariables = Types.Exact<{
}>;
export type RewardsQuery = { __typename?: 'Query', party?: { __typename?: 'Party', id: string, rewardsConnection?: { __typename?: 'RewardsConnection', edges?: Array<{ __typename?: 'RewardEdge', node: { __typename?: 'Reward', rewardType: Types.AccountType, amount: string, percentageOfTotal: string, receivedAt: any, asset: { __typename?: 'Asset', id: string, symbol: string, name: string }, party: { __typename?: 'Party', id: string }, epoch: { __typename?: 'Epoch', id: string } } } | null> | null } | null, delegationsConnection?: { __typename?: 'DelegationsConnection', edges?: Array<{ __typename?: 'DelegationEdge', node: { __typename?: 'Delegation', amount: string, epoch: number } } | null> | null } | null } | null };
export type RewardsQuery = { __typename?: 'Query', party?: { __typename?: 'Party', id: string, rewardsConnection?: { __typename?: 'RewardsConnection', edges?: Array<{ __typename?: 'RewardEdge', node: { __typename?: 'Reward', rewardType: Types.AccountType, amount: string, percentageOfTotal: string, receivedAt: any, asset: { __typename?: 'Asset', id: string, symbol: string, name: string, decimals: number }, party: { __typename?: 'Party', id: string }, epoch: { __typename?: 'Epoch', id: string } } } | null> | null } | null, delegationsConnection?: { __typename?: 'DelegationsConnection', edges?: Array<{ __typename?: 'DelegationEdge', node: { __typename?: 'Delegation', amount: string, epoch: number } } | null> | null } | null } | null };
export type EpochRewardSummaryFieldsFragment = { __typename?: 'EpochRewardSummary', epoch: number, assetId: string, amount: string, rewardType: Types.AccountType };
@ -26,7 +26,7 @@ export type EpochAssetsRewardsQueryVariables = Types.Exact<{
}>;
export type EpochAssetsRewardsQuery = { __typename?: 'Query', assetsConnection?: { __typename?: 'AssetsConnection', edges?: Array<{ __typename?: 'AssetEdge', node: { __typename?: 'Asset', id: string, name: string } } | null> | null } | null, epochRewardSummaries?: { __typename?: 'EpochRewardSummaryConnection', edges?: Array<{ __typename?: 'EpochRewardSummaryEdge', node: { __typename?: 'EpochRewardSummary', epoch: number, assetId: string, amount: string, rewardType: Types.AccountType } } | null> | null } | null };
export type EpochAssetsRewardsQuery = { __typename?: 'Query', assetsConnection?: { __typename?: 'AssetsConnection', edges?: Array<{ __typename?: 'AssetEdge', node: { __typename?: 'Asset', id: string, name: string, decimals: number } } | null> | null } | null, epochRewardSummaries?: { __typename?: 'EpochRewardSummaryConnection', edges?: Array<{ __typename?: 'EpochRewardSummaryEdge', node: { __typename?: 'EpochRewardSummary', epoch: number, assetId: string, amount: string, rewardType: Types.AccountType } } | null> | null } | null };
export type EpochFieldsFragment = { __typename?: 'Epoch', id: string, timestamps: { __typename?: 'EpochTimestamps', start?: any | null, end?: any | null, expiry?: any | null } };
@ -42,6 +42,7 @@ export const RewardFieldsFragmentDoc = gql`
id
symbol
name
decimals
}
party {
id
@ -143,6 +144,7 @@ export const EpochAssetsRewardsDocument = gql`
node {
id
name
decimals
}
}
}