From f5e67a0c2d0c5e4eeedde1912ddbc683efcbcadb Mon Sep 17 00:00:00 2001 From: Sam Keen Date: Thu, 25 May 2023 10:37:40 +0100 Subject: [PATCH] fix(governance): rewards decimals set per asset (#3941) --- .../epoch-individual-rewards-table.spec.tsx | 1 + .../epoch-individual-rewards-table.tsx | 30 +++++++++----- ...rate-epoch-individual-rewards-list.spec.ts | 20 ++++++--- .../generate-epoch-individual-rewards-list.ts | 3 ++ .../epoch-total-rewards-table.spec.tsx | 1 + .../epoch-total-rewards-table.tsx | 41 +++++++++++++------ .../generate-epoch-total-rewards-list.spec.ts | 21 ++++++++++ .../generate-epoch-total-rewards-list.ts | 2 + .../src/routes/rewards/home/Rewards.graphql | 2 + .../rewards/home/__generated__/Rewards.ts | 8 ++-- .../src/lib/__generated__/market-data.ts | 2 +- 11 files changed, 99 insertions(+), 32 deletions(-) diff --git a/apps/governance/src/routes/rewards/epoch-individual-rewards/epoch-individual-rewards-table.spec.tsx b/apps/governance/src/routes/rewards/epoch-individual-rewards/epoch-individual-rewards-table.spec.tsx index f3bcc17ad..1977563a6 100644 --- a/apps/governance/src/routes/rewards/epoch-individual-rewards/epoch-individual-rewards-table.spec.tsx +++ b/apps/governance/src/routes/rewards/epoch-individual-rewards/epoch-individual-rewards-table.spec.tsx @@ -8,6 +8,7 @@ const mockData = { { asset: 'tDAI', totalAmount: '5', + decimals: 6, rewardTypes: { ACCOUNT_TYPE_GLOBAL_REWARD: { amount: '0', diff --git a/apps/governance/src/routes/rewards/epoch-individual-rewards/epoch-individual-rewards-table.tsx b/apps/governance/src/routes/rewards/epoch-individual-rewards/epoch-individual-rewards-table.tsx index eed2a8f24..76dde9fa7 100644 --- a/apps/governance/src/routes/rewards/epoch-individual-rewards/epoch-individual-rewards-table.tsx +++ b/apps/governance/src/routes/rewards/epoch-individual-rewards/epoch-individual-rewards-table.tsx @@ -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 -; @@ -64,7 +63,8 @@ const DisplayReward = ({ }; const RewardItem = ({ - value, + amount, + decimals, percentageOfTotal, dataTestId, last, @@ -72,7 +72,11 @@ const RewardItem = ({
- +
@@ -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) => (
( ) )} - +
))} diff --git a/apps/governance/src/routes/rewards/epoch-individual-rewards/generate-epoch-individual-rewards-list.spec.ts b/apps/governance/src/routes/rewards/epoch-individual-rewards/generate-epoch-individual-rewards-list.spec.ts index d1afb485c..761f69bd5 100644 --- a/apps/governance/src/routes/rewards/epoch-individual-rewards/generate-epoch-individual-rewards-list.spec.ts +++ b/apps/governance/src/routes/rewards/epoch-individual-rewards/generate-epoch-individual-rewards-list.spec.ts @@ -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', diff --git a/apps/governance/src/routes/rewards/epoch-individual-rewards/generate-epoch-individual-rewards-list.ts b/apps/governance/src/routes/rewards/epoch-individual-rewards/generate-epoch-individual-rewards-list.ts index 5f993d478..953595c5e 100644 --- a/apps/governance/src/routes/rewards/epoch-individual-rewards/generate-epoch-individual-rewards-list.ts +++ b/apps/governance/src/routes/rewards/epoch-individual-rewards/generate-epoch-individual-rewards-list.ts @@ -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), }; diff --git a/apps/governance/src/routes/rewards/epoch-total-rewards/epoch-total-rewards-table.spec.tsx b/apps/governance/src/routes/rewards/epoch-total-rewards/epoch-total-rewards-table.spec.tsx index 030bca178..cf581e3b9 100644 --- a/apps/governance/src/routes/rewards/epoch-total-rewards/epoch-total-rewards-table.spec.tsx +++ b/apps/governance/src/routes/rewards/epoch-total-rewards/epoch-total-rewards-table.spec.tsx @@ -52,6 +52,7 @@ const assetRewards: Map< assetRewards.set(assetId, { assetId, name: 'tDAI TEST', + decimals: 6, rewards, totalAmount: '295', }); diff --git a/apps/governance/src/routes/rewards/epoch-total-rewards/epoch-total-rewards-table.tsx b/apps/governance/src/routes/rewards/epoch-total-rewards/epoch-total-rewards-table.tsx index 365ab0d03..d70ce0333 100644 --- a/apps/governance/src/routes/rewards/epoch-total-rewards/epoch-total-rewards-table.tsx +++ b/apps/governance/src/routes/rewards/epoch-total-rewards/epoch-total-rewards-table.tsx @@ -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 -; } @@ -33,11 +35,16 @@ const DisplayReward = ({ reward }: { reward: string }) => { ); }; -const RewardItem = ({ value, dataTestId, last }: RewardItemProps) => ( +const RewardItem = ({ + amount, + decimals, + dataTestId, + last, +}: RewardItemProps) => (
- +
@@ -49,15 +56,25 @@ export const EpochTotalRewardsTable = ({ return ( {Array.from(data.assetRewards.values()).map( - ({ name, rewards, totalAmount }, i) => ( + ({ name, rewards, totalAmount, decimals }, i) => (
{name}
{Array.from(rewards.values()).map(({ rewardType, amount }, i) => ( - + ))} - +
) )} diff --git a/apps/governance/src/routes/rewards/epoch-total-rewards/generate-epoch-total-rewards-list.spec.ts b/apps/governance/src/routes/rewards/epoch-total-rewards/generate-epoch-total-rewards-list.spec.ts index 8b7375cec..aeae34acb 100644 --- a/apps/governance/src/routes/rewards/epoch-total-rewards/generate-epoch-total-rewards-list.spec.ts +++ b/apps/governance/src/routes/rewards/epoch-total-rewards/generate-epoch-total-rewards-list.spec.ts @@ -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([ [ diff --git a/apps/governance/src/routes/rewards/epoch-total-rewards/generate-epoch-total-rewards-list.ts b/apps/governance/src/routes/rewards/epoch-total-rewards/generate-epoch-total-rewards-list.ts index ddf56cac2..4c20dab59 100644 --- a/apps/governance/src/routes/rewards/epoch-total-rewards/generate-epoch-total-rewards-list.ts +++ b/apps/governance/src/routes/rewards/epoch-total-rewards/generate-epoch-total-rewards-list.ts @@ -22,6 +22,7 @@ export type AggregatedEpochRewardSummary = { name: EpochSummaryWithNamedReward['name']; rewards: Map; 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(), diff --git a/apps/governance/src/routes/rewards/home/Rewards.graphql b/apps/governance/src/routes/rewards/home/Rewards.graphql index cb0b3b5cb..59a26c212 100644 --- a/apps/governance/src/routes/rewards/home/Rewards.graphql +++ b/apps/governance/src/routes/rewards/home/Rewards.graphql @@ -4,6 +4,7 @@ fragment RewardFields on Reward { id symbol name + decimals } party { id @@ -67,6 +68,7 @@ query EpochAssetsRewards( node { id name + decimals } } } diff --git a/apps/governance/src/routes/rewards/home/__generated__/Rewards.ts b/apps/governance/src/routes/rewards/home/__generated__/Rewards.ts index d3caf2905..8681a3200 100644 --- a/apps/governance/src/routes/rewards/home/__generated__/Rewards.ts +++ b/apps/governance/src/routes/rewards/home/__generated__/Rewards.ts @@ -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 } } } diff --git a/libs/markets/src/lib/__generated__/market-data.ts b/libs/markets/src/lib/__generated__/market-data.ts index a72953a0a..2ce7201b7 100644 --- a/libs/markets/src/lib/__generated__/market-data.ts +++ b/libs/markets/src/lib/__generated__/market-data.ts @@ -167,4 +167,4 @@ export function useMarketDataLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions } export type MarketDataQueryHookResult = ReturnType; export type MarketDataLazyQueryHookResult = ReturnType; -export type MarketDataQueryResult = Apollo.QueryResult; +export type MarketDataQueryResult = Apollo.QueryResult; \ No newline at end of file