From 0758e68c7f5d505269957005305f8d2a42398702 Mon Sep 17 00:00:00 2001 From: Kamil Pyszkowski Date: Tue, 3 Oct 2023 16:49:24 +0200 Subject: [PATCH 01/10] Update contract for AuthorizationIncreased event Replaced contract-providing hook (useStakingAppContract to useTStakingContract). --- .../useSubscribeToAuthorizationIncreasedEvent.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/hooks/staking-applications/useSubscribeToAuthorizationIncreasedEvent.ts b/src/hooks/staking-applications/useSubscribeToAuthorizationIncreasedEvent.ts index 5eca9e0a8..9d04b9778 100644 --- a/src/hooks/staking-applications/useSubscribeToAuthorizationIncreasedEvent.ts +++ b/src/hooks/staking-applications/useSubscribeToAuthorizationIncreasedEvent.ts @@ -2,14 +2,16 @@ import { stakingApplicationsSlice, StakingAppName, } from "../../store/staking-applications" -import { useSubscribeToContractEvent } from "../../web3/hooks" +import { + useSubscribeToContractEvent, + useTStakingContract, +} from "../../web3/hooks" import { useAppDispatch } from "../store" -import { useStakingAppContract } from "./useStakingAppContract" export const useSubscribeToAuthorizationIncreasedEvent = ( appName: StakingAppName ) => { - const contract = useStakingAppContract(appName) + const contract = useTStakingContract() const dispatch = useAppDispatch() useSubscribeToContractEvent( From 475964925bbab027f9d3030a90948cb409018d63 Mon Sep 17 00:00:00 2001 From: Kamil Pyszkowski Date: Thu, 5 Oct 2023 17:51:05 +0200 Subject: [PATCH 02/10] Extract name from application address Extracted application name, removed hooks `appName` argument --- src/App.tsx | 3 +-- ...eSubscribeToAuthorizationIncreasedEvent.ts | 20 +++++++++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 6806aef6e..06cd66939 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -68,8 +68,7 @@ const Web3EventHandlerComponent = () => { useSubscribeToStakedEvent() useSubscribeToUnstakedEvent() useSubscribeToToppedUpEvent() - useSubscribeToAuthorizationIncreasedEvent("tbtc") - useSubscribeToAuthorizationIncreasedEvent("randomBeacon") + useSubscribeToAuthorizationIncreasedEvent() useSubscribeToAuthorizationDecreaseApprovedEvent("tbtc") useSubscribeToAuthorizationDecreaseApprovedEvent("randomBeacon") useSubscribeToAuthorizationDecreaseRequestedEvent("tbtc") diff --git a/src/hooks/staking-applications/useSubscribeToAuthorizationIncreasedEvent.ts b/src/hooks/staking-applications/useSubscribeToAuthorizationIncreasedEvent.ts index 9d04b9778..a3bb4c7b5 100644 --- a/src/hooks/staking-applications/useSubscribeToAuthorizationIncreasedEvent.ts +++ b/src/hooks/staking-applications/useSubscribeToAuthorizationIncreasedEvent.ts @@ -2,15 +2,25 @@ import { stakingApplicationsSlice, StakingAppName, } from "../../store/staking-applications" +import { threshold } from "../../utils/getThresholdLib" import { useSubscribeToContractEvent, useTStakingContract, } from "../../web3/hooks" import { useAppDispatch } from "../store" -export const useSubscribeToAuthorizationIncreasedEvent = ( - appName: StakingAppName -) => { +const getApplicationName = (address: string) => { + const { multiAppStaking } = threshold + const namesDictionary = Object.fromEntries( + Object.entries(multiAppStaking).map(([name, { address }]) => [ + address, + name === "ecdsa" ? "tbtc" : name, + ]) + ) + return namesDictionary[address] +} + +export const useSubscribeToAuthorizationIncreasedEvent = () => { const contract = useTStakingContract() const dispatch = useAppDispatch() @@ -18,7 +28,9 @@ export const useSubscribeToAuthorizationIncreasedEvent = ( contract, "AuthorizationIncreased", // @ts-ignore - async (stakingProvider, operator, fromAmount, toAmount) => { + async (stakingProvider, applicationAddress, fromAmount, toAmount) => { + const appName = getApplicationName(applicationAddress) + dispatch( stakingApplicationsSlice.actions.authorizationIncreased({ stakingProvider, From 9d1fbf2f08fefbf50b469abec994cd2c866ad27d Mon Sep 17 00:00:00 2001 From: Kamil Pyszkowski Date: Fri, 6 Oct 2023 11:05:01 +0200 Subject: [PATCH 03/10] Rename callback argument: applicationAddress -> application --- .../useSubscribeToAuthorizationIncreasedEvent.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hooks/staking-applications/useSubscribeToAuthorizationIncreasedEvent.ts b/src/hooks/staking-applications/useSubscribeToAuthorizationIncreasedEvent.ts index a3bb4c7b5..bf54693a9 100644 --- a/src/hooks/staking-applications/useSubscribeToAuthorizationIncreasedEvent.ts +++ b/src/hooks/staking-applications/useSubscribeToAuthorizationIncreasedEvent.ts @@ -28,8 +28,8 @@ export const useSubscribeToAuthorizationIncreasedEvent = () => { contract, "AuthorizationIncreased", // @ts-ignore - async (stakingProvider, applicationAddress, fromAmount, toAmount) => { - const appName = getApplicationName(applicationAddress) + async (stakingProvider, application, fromAmount, toAmount) => { + const appName = getApplicationName(application) dispatch( stakingApplicationsSlice.actions.authorizationIncreased({ From 36d20c74289d911ebc0964c4df8b95aae4db4c5f Mon Sep 17 00:00:00 2001 From: Kamil Pyszkowski Date: Mon, 9 Oct 2023 14:45:24 +0200 Subject: [PATCH 04/10] Refactor app name/label utils https://github.com/threshold-network/token-dashboard/pull/636\#discussion_r1350098230 --- .../InititateDeauthorization.tsx | 4 ++-- .../AuthorizeStakingApps.tsx | 4 ++-- ...eSubscribeToAuthorizationIncreasedEvent.ts | 20 +++-------------- .../Staking/OperatorAddressMappingCard.tsx | 4 ++-- src/utils/getStakingAppLabel.ts | 22 ++++++++++++------- 5 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/components/Modal/DeauthorizeApplicationModal/InititateDeauthorization.tsx b/src/components/Modal/DeauthorizeApplicationModal/InititateDeauthorization.tsx index a9e3dd103..195e7a3f1 100644 --- a/src/components/Modal/DeauthorizeApplicationModal/InititateDeauthorization.tsx +++ b/src/components/Modal/DeauthorizeApplicationModal/InititateDeauthorization.tsx @@ -24,7 +24,7 @@ import shortenAddress from "../../../utils/shortenAddress" import TokenBalance from "../../TokenBalance" import { StakingAppName } from "../../../store/staking-applications" import { useInitiateDeauthorization } from "../../../hooks/staking-applications" -import { getSakingAppLabel } from "../../../utils/getStakingAppLabel" +import { getStakingAppLabelFromAppName } from "../../../utils/getStakingAppLabel" import ModalCloseButton from "../ModalCloseButton" import { StakingProviderAppInfo } from "../../../threshold-ts/applications" @@ -62,7 +62,7 @@ const InitiateDeauthorization: FC<
You're about to initiate the decrease of your{" "} - {getSakingAppLabel(stakingAppName)} authorization. + {getStakingAppLabelFromAppName(stakingAppName)} authorization.
Initiation and confirmation of deauthorization is a two step action. diff --git a/src/components/Modal/StakingApplications/AuthorizeStakingApps.tsx b/src/components/Modal/StakingApplications/AuthorizeStakingApps.tsx index e1b61a61c..b4917c212 100644 --- a/src/components/Modal/StakingApplications/AuthorizeStakingApps.tsx +++ b/src/components/Modal/StakingApplications/AuthorizeStakingApps.tsx @@ -29,7 +29,7 @@ import { selectStakingAppByStakingProvider, StakingAppName, } from "../../../store/staking-applications" -import { getSakingAppLabel } from "../../../utils/getStakingAppLabel" +import { getStakingAppLabelFromAppName } from "../../../utils/getStakingAppLabel" import ModalCloseButton from "../ModalCloseButton" export type AuthorizeAppsProps = BaseModalProps & { @@ -125,7 +125,7 @@ const StakingApplicationToAuth: FC<{ - {getSakingAppLabel(appName)} app - {percentage} + {getStakingAppLabelFromAppName(appName)} app - {percentage} Authorization Amount { - const { multiAppStaking } = threshold - const namesDictionary = Object.fromEntries( - Object.entries(multiAppStaking).map(([name, { address }]) => [ - address, - name === "ecdsa" ? "tbtc" : name, - ]) - ) - return namesDictionary[address] -} - export const useSubscribeToAuthorizationIncreasedEvent = () => { const contract = useTStakingContract() const dispatch = useAppDispatch() @@ -29,7 +15,7 @@ export const useSubscribeToAuthorizationIncreasedEvent = () => { "AuthorizationIncreased", // @ts-ignore async (stakingProvider, application, fromAmount, toAmount) => { - const appName = getApplicationName(application) + const appName = getStakingAppNameFromAppAddress(application) dispatch( stakingApplicationsSlice.actions.authorizationIncreased({ diff --git a/src/pages/Staking/OperatorAddressMappingCard.tsx b/src/pages/Staking/OperatorAddressMappingCard.tsx index 3c4c303df..47e738ab3 100644 --- a/src/pages/Staking/OperatorAddressMappingCard.tsx +++ b/src/pages/Staking/OperatorAddressMappingCard.tsx @@ -23,7 +23,7 @@ import { selectMappedOperators } from "../../store/account/selectors" import shortenAddress from "../../utils/shortenAddress" import { isAddressZero } from "../../web3/utils" import { FcCheckmark, FiLink2 } from "react-icons/all" -import { getSakingAppLabel } from "../../utils/getStakingAppLabel" +import { getStakingAppLabelFromAppName } from "../../utils/getStakingAppLabel" import { StakingAppName } from "../../store/staking-applications" const OperatorAddressMappingCard: FC<{ stakingProvider: string }> = ({ @@ -63,7 +63,7 @@ const OperatorAddressMappingCard: FC<{ stakingProvider: string }> = ({ - {getSakingAppLabel(appName as StakingAppName)} App + {getStakingAppLabelFromAppName(appName as StakingAppName)} App diff --git a/src/utils/getStakingAppLabel.ts b/src/utils/getStakingAppLabel.ts index f8ce7ec95..5a9b475a2 100644 --- a/src/utils/getStakingAppLabel.ts +++ b/src/utils/getStakingAppLabel.ts @@ -6,16 +6,22 @@ const stakingAppNameToAppLabel: Record = { randomBeacon: "Random Beacon", } -const stakingAppAddressToName: { [key: string]: string } = { - [threshold.multiAppStaking.ecdsa.address]: stakingAppNameToAppLabel.tbtc, - [threshold.multiAppStaking.randomBeacon.address]: - stakingAppNameToAppLabel.randomBeacon, +const stakingAppAddressToAppName: Record = { + [threshold.multiAppStaking.ecdsa.address]: "tbtc", + [threshold.multiAppStaking.randomBeacon.address]: "randomBeacon", } -export const getStakingAppNameFromAddress = (stakingAppAddress: string) => { - return stakingAppAddressToName[stakingAppAddress] ?? "App" +export const getStakingAppNameFromAppAddress = (stakingAppAddress: string) => { + return stakingAppAddressToAppName[stakingAppAddress] } -export const getSakingAppLabel = (stakingAppName: StakingAppName) => { - return stakingAppNameToAppLabel[stakingAppName] +export const getStakingAppLabelFromAppName = ( + stakingAppName: StakingAppName +) => { + return stakingAppNameToAppLabel[stakingAppName] || "App" +} + +export const getStakingAppLabelFromAppAddress = (address: string) => { + const appName = getStakingAppNameFromAppAddress(address) + return getStakingAppLabelFromAppName(appName) || "App" } From 95b52b769bcb0242136d4e7ad70aeed326f94454 Mon Sep 17 00:00:00 2001 From: Kamil Pyszkowski Date: Mon, 9 Oct 2023 15:17:10 +0200 Subject: [PATCH 05/10] Add tests for app label/name utils --- .../__tests__/getStakingAppLabel.test.ts | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/utils/__tests__/getStakingAppLabel.test.ts diff --git a/src/utils/__tests__/getStakingAppLabel.test.ts b/src/utils/__tests__/getStakingAppLabel.test.ts new file mode 100644 index 000000000..d289ff355 --- /dev/null +++ b/src/utils/__tests__/getStakingAppLabel.test.ts @@ -0,0 +1,47 @@ +import { + getStakingAppLabelFromAppAddress, + getStakingAppLabelFromAppName, + getStakingAppNameFromAppAddress, +} from "../getStakingAppLabel" +import { StakingAppName } from "../../store/staking-applications" +import { getThresholdLib } from "../getThresholdLib" + +const MOCK_ADDRESSES: Record = { + tbtc: getThresholdLib().multiAppStaking.ecdsa.address, + randomBeacon: getThresholdLib().multiAppStaking.randomBeacon.address, +} +const MOCK_LABELS: Record = { + tbtc: "tBTC", + randomBeacon: "Random Beacon", +} +const MOCK_APP_NAMES: StakingAppName[] = ["tbtc", "randomBeacon"] + +describe("Staking app label utils tests", () => { + const [tbtcName, rbName] = MOCK_APP_NAMES + const tbtcAddress = MOCK_ADDRESSES[tbtcName] + const rbAddress = MOCK_ADDRESSES[rbName] + + it("returns correct app label if app address is given", () => { + const resultTbtcLabel = getStakingAppLabelFromAppAddress(tbtcAddress) + const resultRbLabel = getStakingAppLabelFromAppAddress(rbAddress) + + expect(resultTbtcLabel).toBe(MOCK_LABELS[tbtcName]) + expect(resultRbLabel).toBe(MOCK_LABELS[rbName]) + }) + + it("returns correct app label if app name is given", () => { + const resultTbtcLabel = getStakingAppLabelFromAppName(tbtcName) + const resultRbLabel = getStakingAppLabelFromAppName(rbName) + + expect(resultTbtcLabel).toBe(MOCK_LABELS[tbtcName]) + expect(resultRbLabel).toBe(MOCK_LABELS[rbName]) + }) + + it("returns correct app name if address is given", () => { + const resultTbtcName = getStakingAppNameFromAppAddress(tbtcAddress) + const resultRbName = getStakingAppNameFromAppAddress(rbAddress) + + expect(resultTbtcName).toBe(tbtcName) + expect(resultRbName).toBe(rbName) + }) +}) From e79771c28e93912b0da92cfed73b30d7b511ee1b Mon Sep 17 00:00:00 2001 From: Kamil Pyszkowski Date: Mon, 9 Oct 2023 15:31:45 +0200 Subject: [PATCH 06/10] Update import --- .../StakingApplications/StakingApplicationsAuthorized.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/Modal/StakingApplications/StakingApplicationsAuthorized.tsx b/src/components/Modal/StakingApplications/StakingApplicationsAuthorized.tsx index 5410c74fd..a79c88104 100644 --- a/src/components/Modal/StakingApplications/StakingApplicationsAuthorized.tsx +++ b/src/components/Modal/StakingApplications/StakingApplicationsAuthorized.tsx @@ -32,7 +32,7 @@ import { formatTokenAmount } from "../../../utils/formatAmount" import { ExplorerDataType } from "../../../utils/createEtherscanLink" import { ExternalHref } from "../../../enums" import { BaseModalProps } from "../../../types" -import { getStakingAppNameFromAddress } from "../../../utils/getStakingAppLabel" +import { getStakingAppNameFromAppAddress } from "../../../utils/getStakingAppLabel" import StakingTimeline from "../../StakingTimeline" import ButtonLink from "../../ButtonLink" import ModalCloseButton from "../ModalCloseButton" @@ -79,7 +79,7 @@ const StakingApplicationsAuthorizedBase: FC< {authorizedStakingApplications.map((_) => ( - {`${getStakingAppNameFromAddress( + {`${getStakingAppNameFromAppAddress( _.address )} Authorization Amount`} {`${formatTokenAmount(_.amount)} T (${formatPercentage( From b68d3b5aeb42ea33028f0fa850df723199844aae Mon Sep 17 00:00:00 2001 From: Kamil Pyszkowski Date: Thu, 12 Oct 2023 11:26:23 +0200 Subject: [PATCH 07/10] Remove fallback return value --- src/utils/getStakingAppLabel.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/getStakingAppLabel.ts b/src/utils/getStakingAppLabel.ts index 5a9b475a2..90587750e 100644 --- a/src/utils/getStakingAppLabel.ts +++ b/src/utils/getStakingAppLabel.ts @@ -18,7 +18,7 @@ export const getStakingAppNameFromAppAddress = (stakingAppAddress: string) => { export const getStakingAppLabelFromAppName = ( stakingAppName: StakingAppName ) => { - return stakingAppNameToAppLabel[stakingAppName] || "App" + return stakingAppNameToAppLabel[stakingAppName] } export const getStakingAppLabelFromAppAddress = (address: string) => { From 112e081f8296f7a7a3c1a150331c8785721edfee Mon Sep 17 00:00:00 2001 From: Kamil Pyszkowski Date: Thu, 12 Oct 2023 11:33:07 +0200 Subject: [PATCH 08/10] Rename variables rb -> randomBeacon mock variables: CAPS -> camelCase --- .../__tests__/getStakingAppLabel.test.ts | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/utils/__tests__/getStakingAppLabel.test.ts b/src/utils/__tests__/getStakingAppLabel.test.ts index d289ff355..dabbd038a 100644 --- a/src/utils/__tests__/getStakingAppLabel.test.ts +++ b/src/utils/__tests__/getStakingAppLabel.test.ts @@ -6,42 +6,45 @@ import { import { StakingAppName } from "../../store/staking-applications" import { getThresholdLib } from "../getThresholdLib" -const MOCK_ADDRESSES: Record = { +const mockAddresses: Record = { tbtc: getThresholdLib().multiAppStaking.ecdsa.address, randomBeacon: getThresholdLib().multiAppStaking.randomBeacon.address, } -const MOCK_LABELS: Record = { +const mockLabels: Record = { tbtc: "tBTC", randomBeacon: "Random Beacon", } -const MOCK_APP_NAMES: StakingAppName[] = ["tbtc", "randomBeacon"] +const mockAppNames: StakingAppName[] = ["tbtc", "randomBeacon"] describe("Staking app label utils tests", () => { - const [tbtcName, rbName] = MOCK_APP_NAMES - const tbtcAddress = MOCK_ADDRESSES[tbtcName] - const rbAddress = MOCK_ADDRESSES[rbName] + const [tbtcName, randomBeaconName] = mockAppNames + const tbtcAddress = mockAddresses[tbtcName] + const randomBeaconAddress = mockAddresses[randomBeaconName] it("returns correct app label if app address is given", () => { const resultTbtcLabel = getStakingAppLabelFromAppAddress(tbtcAddress) - const resultRbLabel = getStakingAppLabelFromAppAddress(rbAddress) + const resultRandomBeaconLabel = + getStakingAppLabelFromAppAddress(randomBeaconAddress) - expect(resultTbtcLabel).toBe(MOCK_LABELS[tbtcName]) - expect(resultRbLabel).toBe(MOCK_LABELS[rbName]) + expect(resultTbtcLabel).toBe(mockLabels[tbtcName]) + expect(resultRandomBeaconLabel).toBe(mockLabels[randomBeaconName]) }) it("returns correct app label if app name is given", () => { const resultTbtcLabel = getStakingAppLabelFromAppName(tbtcName) - const resultRbLabel = getStakingAppLabelFromAppName(rbName) + const resultRandomBeaconLabel = + getStakingAppLabelFromAppName(randomBeaconName) - expect(resultTbtcLabel).toBe(MOCK_LABELS[tbtcName]) - expect(resultRbLabel).toBe(MOCK_LABELS[rbName]) + expect(resultTbtcLabel).toBe(mockLabels[tbtcName]) + expect(resultRandomBeaconLabel).toBe(mockLabels[randomBeaconName]) }) it("returns correct app name if address is given", () => { const resultTbtcName = getStakingAppNameFromAppAddress(tbtcAddress) - const resultRbName = getStakingAppNameFromAppAddress(rbAddress) + const resultRandomBeaconName = + getStakingAppNameFromAppAddress(randomBeaconAddress) expect(resultTbtcName).toBe(tbtcName) - expect(resultRbName).toBe(rbName) + expect(resultRandomBeaconName).toBe(randomBeaconName) }) }) From 6feb8e13ad59d7fde4f91bfbae1440d912f92db0 Mon Sep 17 00:00:00 2001 From: Kamil Pyszkowski Date: Thu, 12 Oct 2023 12:03:47 +0200 Subject: [PATCH 09/10] Add test for fallback app label if address is unexpected --- src/utils/__tests__/getStakingAppLabel.test.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/utils/__tests__/getStakingAppLabel.test.ts b/src/utils/__tests__/getStakingAppLabel.test.ts index dabbd038a..8629a2bdc 100644 --- a/src/utils/__tests__/getStakingAppLabel.test.ts +++ b/src/utils/__tests__/getStakingAppLabel.test.ts @@ -47,4 +47,10 @@ describe("Staking app label utils tests", () => { expect(resultTbtcName).toBe(tbtcName) expect(resultRandomBeaconName).toBe(randomBeaconName) }) + + it("returns fallback value if address is unexpected", () => { + const resultName = getStakingAppLabelFromAppAddress("0xun3xp3c73d400r3s5") + + expect(resultName).toBe("App") + }) }) From d8378ce48eaa77c6e1352698e6c1117417879d17 Mon Sep 17 00:00:00 2001 From: michalsmiarowski Date: Wed, 18 Oct 2023 13:17:35 +0200 Subject: [PATCH 10/10] Update T Token Dashboard version to v1.12.0-pre `v1.11.0` is live! --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 02854bdc0..fa263507d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "token-dashboard", - "version": "1.11.0-pre", + "version": "1.12.0-pre", "private": true, "dependencies": { "@chakra-ui/icons": "^1.0.15",