From afb0235bfbc919bd5d231def71346e44076cdaad Mon Sep 17 00:00:00 2001 From: ianrowan Date: Mon, 21 Oct 2024 16:04:32 -0500 Subject: [PATCH 1/7] add erc20 helper --- .../helpers/ContractHelpers/Abi/ERC20Abi.ts | 7 +++++++ .../helpers/ContractHelpers/ERC20Helper.ts | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 packages/commonwealth/client/scripts/helpers/ContractHelpers/ERC20Helper.ts diff --git a/packages/commonwealth/client/scripts/helpers/ContractHelpers/Abi/ERC20Abi.ts b/packages/commonwealth/client/scripts/helpers/ContractHelpers/Abi/ERC20Abi.ts index 48ad636212f..2e32fc0dc69 100644 --- a/packages/commonwealth/client/scripts/helpers/ContractHelpers/Abi/ERC20Abi.ts +++ b/packages/commonwealth/client/scripts/helpers/ContractHelpers/Abi/ERC20Abi.ts @@ -9,4 +9,11 @@ export const Erc20Abi = [ stateMutability: 'nonpayable', type: 'function', }, + { + inputs: [{ internalType: 'address', name: 'account', type: 'address' }], + stateMutability: 'view', + type: 'function', + name: 'balanceOf', + outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], + }, ]; diff --git a/packages/commonwealth/client/scripts/helpers/ContractHelpers/ERC20Helper.ts b/packages/commonwealth/client/scripts/helpers/ContractHelpers/ERC20Helper.ts new file mode 100644 index 00000000000..d4517b79212 --- /dev/null +++ b/packages/commonwealth/client/scripts/helpers/ContractHelpers/ERC20Helper.ts @@ -0,0 +1,16 @@ +import { Erc20Abi } from './Abi/ERC20Abi'; +import ContractBase from './ContractBase'; +class ERC20Helper extends ContractBase { + constructor(contractAddress: string, rpc: string) { + super(contractAddress, Erc20Abi, rpc); + } + async getBalance(userAddress: string): Promise { + if (!this.initialized) { + await this.initialize(); + } + const balance = this.contract.methods.balanceOf(userAddress); + return Number(balance); + } +} + +export default ERC20Helper; From a49afecfe50ea19d05696de0cb663e725c6a4457 Mon Sep 17 00:00:00 2001 From: Marcin Date: Tue, 22 Oct 2024 13:58:10 +0200 Subject: [PATCH 2/7] add token_address to query --- libs/model/src/community/GetTopics.query.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/libs/model/src/community/GetTopics.query.ts b/libs/model/src/community/GetTopics.query.ts index 324e2302c16..9e2d7ef203d 100644 --- a/libs/model/src/community/GetTopics.query.ts +++ b/libs/model/src/community/GetTopics.query.ts @@ -73,6 +73,7 @@ WITH topic_data AS ( weighted_voting, token_symbol, vote_weight_multiplier, + token_address, created_at::text AS created_at, updated_at::text AS updated_at, deleted_at::text AS deleted_at, From f8509b81fa95cd44821aead4c24368466cb60ca3 Mon Sep 17 00:00:00 2001 From: Marcin Date: Tue, 22 Oct 2024 13:59:05 +0200 Subject: [PATCH 3/7] change implementation of getBalance --- .../client/scripts/helpers/ContractHelpers/ERC20Helper.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/commonwealth/client/scripts/helpers/ContractHelpers/ERC20Helper.ts b/packages/commonwealth/client/scripts/helpers/ContractHelpers/ERC20Helper.ts index d4517b79212..04aaa4f250a 100644 --- a/packages/commonwealth/client/scripts/helpers/ContractHelpers/ERC20Helper.ts +++ b/packages/commonwealth/client/scripts/helpers/ContractHelpers/ERC20Helper.ts @@ -1,15 +1,17 @@ +import Web3 from 'web3'; import { Erc20Abi } from './Abi/ERC20Abi'; import ContractBase from './ContractBase'; class ERC20Helper extends ContractBase { constructor(contractAddress: string, rpc: string) { super(contractAddress, Erc20Abi, rpc); } - async getBalance(userAddress: string): Promise { + async getBalance(userAddress: string): Promise { if (!this.initialized) { await this.initialize(); } - const balance = this.contract.methods.balanceOf(userAddress); - return Number(balance); + const balance = await this.contract.methods.balanceOf(userAddress).call(); + + return Web3.utils.fromWei(balance, 'ether'); } } From 08b9658e036f9b0b8672f5b57885c9e16bc78077 Mon Sep 17 00:00:00 2001 From: Marcin Date: Tue, 22 Oct 2024 14:11:55 +0200 Subject: [PATCH 4/7] refactor --- .../client/scripts/state/api/token/index.ts | 4 ---- .../scripts/state/api/{token => tokens}/createToken.ts | 0 .../scripts/state/api/{token => tokens}/fetchTokens.ts | 0 .../client/scripts/state/api/tokens/index.ts | 10 +++++++++- .../QuickTokenLaunchForm/QuickTokenLaunchForm.tsx | 2 +- .../Contests/FundContestDrawer/useFundContestForm.ts | 3 +-- .../SignTokenTransactions/SignTokenTransactions.tsx | 2 +- .../TokenInformationForm/TokenInformationForm.tsx | 2 +- 8 files changed, 13 insertions(+), 10 deletions(-) delete mode 100644 packages/commonwealth/client/scripts/state/api/token/index.ts rename packages/commonwealth/client/scripts/state/api/{token => tokens}/createToken.ts (100%) rename packages/commonwealth/client/scripts/state/api/{token => tokens}/fetchTokens.ts (100%) diff --git a/packages/commonwealth/client/scripts/state/api/token/index.ts b/packages/commonwealth/client/scripts/state/api/token/index.ts deleted file mode 100644 index bcd683a4e8a..00000000000 --- a/packages/commonwealth/client/scripts/state/api/token/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -import useCreateTokenMutation from './createToken'; -import useFetchTokensQuery from './fetchTokens'; - -export { useCreateTokenMutation, useFetchTokensQuery }; diff --git a/packages/commonwealth/client/scripts/state/api/token/createToken.ts b/packages/commonwealth/client/scripts/state/api/tokens/createToken.ts similarity index 100% rename from packages/commonwealth/client/scripts/state/api/token/createToken.ts rename to packages/commonwealth/client/scripts/state/api/tokens/createToken.ts diff --git a/packages/commonwealth/client/scripts/state/api/token/fetchTokens.ts b/packages/commonwealth/client/scripts/state/api/tokens/fetchTokens.ts similarity index 100% rename from packages/commonwealth/client/scripts/state/api/token/fetchTokens.ts rename to packages/commonwealth/client/scripts/state/api/tokens/fetchTokens.ts diff --git a/packages/commonwealth/client/scripts/state/api/tokens/index.ts b/packages/commonwealth/client/scripts/state/api/tokens/index.ts index b7665891a87..fc169e154cf 100644 --- a/packages/commonwealth/client/scripts/state/api/tokens/index.ts +++ b/packages/commonwealth/client/scripts/state/api/tokens/index.ts @@ -1,3 +1,11 @@ +import useCreateTokenMutation from './createToken'; +import useFetchTokensQuery from './fetchTokens'; +import useTokenBalanceQuery from './getTokenBalance'; import useTokenMetadataQuery from './getTokenMetadata'; -export { useTokenMetadataQuery }; +export { + useCreateTokenMutation, + useFetchTokensQuery, + useTokenBalanceQuery, + useTokenMetadataQuery, +}; diff --git a/packages/commonwealth/client/scripts/views/pages/Communities/TokenLaunchDrawer/QuickTokenLaunchForm/QuickTokenLaunchForm.tsx b/packages/commonwealth/client/scripts/views/pages/Communities/TokenLaunchDrawer/QuickTokenLaunchForm/QuickTokenLaunchForm.tsx index 22ad5a2196c..9043d61ba97 100644 --- a/packages/commonwealth/client/scripts/views/pages/Communities/TokenLaunchDrawer/QuickTokenLaunchForm/QuickTokenLaunchForm.tsx +++ b/packages/commonwealth/client/scripts/views/pages/Communities/TokenLaunchDrawer/QuickTokenLaunchForm/QuickTokenLaunchForm.tsx @@ -10,7 +10,7 @@ import useCreateCommunityMutation, { } from 'state/api/communities/createCommunity'; import { generateImage } from 'state/api/general/generateImage'; import { useLaunchTokenMutation } from 'state/api/launchPad'; -import { useCreateTokenMutation } from 'state/api/token'; +import { useCreateTokenMutation } from 'state/api/tokens'; import useUserStore from 'state/ui/user'; import PageCounter from 'views/components/PageCounter'; import { CWText } from 'views/components/component_kit/cw_text'; diff --git a/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Contests/FundContestDrawer/useFundContestForm.ts b/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Contests/FundContestDrawer/useFundContestForm.ts index 4ccce6fe1b4..744d540dae7 100644 --- a/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Contests/FundContestDrawer/useFundContestForm.ts +++ b/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Contests/FundContestDrawer/useFundContestForm.ts @@ -5,8 +5,7 @@ import { useGetUserEthBalanceQuery, } from 'state/api/communityStake'; import { useGetContestBalanceQuery } from 'state/api/contests'; -import { useTokenMetadataQuery } from 'state/api/tokens'; -import useTokenBalanceQuery from 'state/api/tokens/getTokenBalance'; +import { useTokenBalanceQuery, useTokenMetadataQuery } from 'state/api/tokens'; import { convertTokenAmountToUsd } from 'views/modals/ManageCommunityStakeModal/utils'; import { calculateNewContractBalance, getAmountError } from './utils'; diff --git a/packages/commonwealth/client/scripts/views/pages/LaunchToken/steps/SignatureStep/SignTokenTransactions/SignTokenTransactions.tsx b/packages/commonwealth/client/scripts/views/pages/LaunchToken/steps/SignatureStep/SignTokenTransactions/SignTokenTransactions.tsx index 97839b4d42c..29a24eb90ea 100644 --- a/packages/commonwealth/client/scripts/views/pages/LaunchToken/steps/SignatureStep/SignTokenTransactions/SignTokenTransactions.tsx +++ b/packages/commonwealth/client/scripts/views/pages/LaunchToken/steps/SignatureStep/SignTokenTransactions/SignTokenTransactions.tsx @@ -2,7 +2,7 @@ import { ChainBase, commonProtocol } from '@hicommonwealth/shared'; import React from 'react'; import { useUpdateCommunityMutation } from 'state/api/communities'; import { useLaunchTokenMutation } from 'state/api/launchPad'; -import { useCreateTokenMutation } from 'state/api/token'; +import { useCreateTokenMutation } from 'state/api/tokens'; import useUserStore from 'state/ui/user'; import { CWDivider } from 'views/components/component_kit/cw_divider'; import { CWText } from 'views/components/component_kit/cw_text'; diff --git a/packages/commonwealth/client/scripts/views/pages/LaunchToken/steps/TokenInformationStep/TokenInformationForm/TokenInformationForm.tsx b/packages/commonwealth/client/scripts/views/pages/LaunchToken/steps/TokenInformationStep/TokenInformationForm/TokenInformationForm.tsx index 839f642b7f0..5da3e160038 100644 --- a/packages/commonwealth/client/scripts/views/pages/LaunchToken/steps/TokenInformationStep/TokenInformationForm/TokenInformationForm.tsx +++ b/packages/commonwealth/client/scripts/views/pages/LaunchToken/steps/TokenInformationStep/TokenInformationForm/TokenInformationForm.tsx @@ -9,7 +9,7 @@ import { MixpanelCommunityCreationEvent, MixpanelLoginPayload, } from 'shared/analytics/types'; -import { useFetchTokensQuery } from 'state/api/token'; +import { useFetchTokensQuery } from 'state/api/tokens'; import useUserStore from 'state/ui/user'; import { useDebounce } from 'usehooks-ts'; import { From 054d26095a126e7511d2a695e82f67bd308b6552 Mon Sep 17 00:00:00 2001 From: Marcin Date: Tue, 22 Oct 2024 15:39:58 +0200 Subject: [PATCH 5/7] display vote weight --- .../state/api/tokens/getERC20Balance.ts | 36 +++++++++++++++++++ .../client/scripts/state/api/tokens/index.ts | 2 ++ .../components/TokenBanner/TokenBanner.scss | 8 +++-- .../components/TokenBanner/TokenBanner.tsx | 4 +-- .../pages/discussions/DiscussionsPage.tsx | 13 +++++++ 5 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 packages/commonwealth/client/scripts/state/api/tokens/getERC20Balance.ts diff --git a/packages/commonwealth/client/scripts/state/api/tokens/getERC20Balance.ts b/packages/commonwealth/client/scripts/state/api/tokens/getERC20Balance.ts new file mode 100644 index 00000000000..195e4bb7661 --- /dev/null +++ b/packages/commonwealth/client/scripts/state/api/tokens/getERC20Balance.ts @@ -0,0 +1,36 @@ +import { useQuery } from '@tanstack/react-query'; +import ERC20Helper from 'helpers/ContractHelpers/ERC20Helper'; + +const GET_ERC20_BALANCE_STALE_TIME = 60 * 1_000; // 1 min + +interface UseGetERC20BalanceQueryProps { + userAddress: string; + tokenAddress: string; + nodeRpc: string; +} + +const getERC20Balance = async ({ + userAddress, + tokenAddress, + nodeRpc, +}: UseGetERC20BalanceQueryProps) => { + const helper = new ERC20Helper(tokenAddress, nodeRpc); + + return helper.getBalance(userAddress); +}; + +const useGetERC20BalanceQuery = ({ + userAddress, + tokenAddress, + nodeRpc, +}: UseGetERC20BalanceQueryProps) => { + return useQuery({ + queryKey: [userAddress, tokenAddress, nodeRpc], + queryFn: () => getERC20Balance({ userAddress, tokenAddress, nodeRpc }), + enabled: !!tokenAddress && userAddress && !!nodeRpc, + staleTime: GET_ERC20_BALANCE_STALE_TIME, + retry: false, + }); +}; + +export default useGetERC20BalanceQuery; diff --git a/packages/commonwealth/client/scripts/state/api/tokens/index.ts b/packages/commonwealth/client/scripts/state/api/tokens/index.ts index fc169e154cf..2a7ab465767 100644 --- a/packages/commonwealth/client/scripts/state/api/tokens/index.ts +++ b/packages/commonwealth/client/scripts/state/api/tokens/index.ts @@ -1,11 +1,13 @@ import useCreateTokenMutation from './createToken'; import useFetchTokensQuery from './fetchTokens'; +import useGetERC20BalanceQuery from './getERC20Balance'; import useTokenBalanceQuery from './getTokenBalance'; import useTokenMetadataQuery from './getTokenMetadata'; export { useCreateTokenMutation, useFetchTokensQuery, + useGetERC20BalanceQuery, useTokenBalanceQuery, useTokenMetadataQuery, }; diff --git a/packages/commonwealth/client/scripts/views/components/TokenBanner/TokenBanner.scss b/packages/commonwealth/client/scripts/views/components/TokenBanner/TokenBanner.scss index 0f3f64481b2..08744f872fc 100644 --- a/packages/commonwealth/client/scripts/views/components/TokenBanner/TokenBanner.scss +++ b/packages/commonwealth/client/scripts/views/components/TokenBanner/TokenBanner.scss @@ -52,8 +52,10 @@ } } } - - .vote-weight-label { - color: $neutral-500; + .vote-weight { + margin-left: 8px; + .vote-weight-label { + color: $neutral-500; + } } } diff --git a/packages/commonwealth/client/scripts/views/components/TokenBanner/TokenBanner.tsx b/packages/commonwealth/client/scripts/views/components/TokenBanner/TokenBanner.tsx index 2da2543e20f..e0558252c45 100644 --- a/packages/commonwealth/client/scripts/views/components/TokenBanner/TokenBanner.tsx +++ b/packages/commonwealth/client/scripts/views/components/TokenBanner/TokenBanner.tsx @@ -21,7 +21,7 @@ interface TokenBannerProps { change?: number; isLoading?: boolean; popover?: Pick; - voteWeight?: number; + voteWeight?: string; } const TokenBanner = ({ @@ -76,7 +76,7 @@ const TokenBanner = ({ )} {voteWeight && ( -
+
Your vote weight diff --git a/packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx b/packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx index e9d10cb3871..22894df489f 100644 --- a/packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx +++ b/packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx @@ -19,6 +19,7 @@ import { ThreadCard } from './ThreadCard'; import { sortByFeaturedFilter, sortPinned } from './helpers'; import { slugify, splitAndDecodeURL } from '@hicommonwealth/shared'; +import { useGetERC20BalanceQuery } from 'client/scripts/state/api/tokens'; import { formatAddressShort } from 'helpers'; import { getThreadActionTooltipText } from 'helpers/threads'; import useBrowserWindow from 'hooks/useBrowserWindow'; @@ -99,6 +100,12 @@ const DiscussionsPage = ({ topicName }: DiscussionsPageProps) => { const { contestsData } = useCommunityContests(); + const { data: erc20Balance } = useGetERC20BalanceQuery({ + tokenAddress: topicObj?.token_address || '', + userAddress: user.activeAccount?.address || '', + nodeRpc: app?.chain.meta?.ChainNode?.url || '', + }); + const { dateCursor } = useDateCursor({ dateRange: searchParams.get('dateRange') as ThreadTimelineFilterTypes, }); @@ -185,6 +192,11 @@ const DiscussionsPage = ({ topicName }: DiscussionsPageProps) => { return isContestInTopic && isActive; }); + const voteWeight = + isTopicWeighted && erc20Balance + ? String((topicObj?.vote_weight_multiplier * erc20Balance).toFixed(0)) + : ''; + return ( // @ts-expect-error @@ -323,6 +335,7 @@ const DiscussionsPage = ({ topicName }: DiscussionsPageProps) => { name={tokenMetadata?.name} ticker={topicObj?.token_symbol} avatarUrl={tokenMetadata?.logo} + voteWeight={voteWeight} popover={{ title: tokenMetadata?.name, body: formatAddressShort(topicObj.token_address!, 6, 6), From 251a2ec7cfdc77a21d0266e79149de7998842e71 Mon Sep 17 00:00:00 2001 From: Marcin Date: Tue, 22 Oct 2024 16:14:10 +0200 Subject: [PATCH 6/7] copy changes --- .../steps/SignTransactionsStep/SignTransactionsStep.tsx | 7 ++++--- .../scripts/views/pages/discussions/DiscussionsPage.tsx | 8 +++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Contests/ManageContest/steps/SignTransactionsStep/SignTransactionsStep.tsx b/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Contests/ManageContest/steps/SignTransactionsStep/SignTransactionsStep.tsx index c5a435f4951..631499aeb07 100644 --- a/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Contests/ManageContest/steps/SignTransactionsStep/SignTransactionsStep.tsx +++ b/packages/commonwealth/client/scripts/views/pages/CommunityManagement/Contests/ManageContest/steps/SignTransactionsStep/SignTransactionsStep.tsx @@ -232,9 +232,10 @@ const SignTransactionsStep = ({
Sign transactions to launch contest - You must sign two (2) transactions to launch your community contest. - The first is to route the fees generated from stake to the contest - address. The second is to launch the contest contract onchain. + You must sign this transaction to deploy the contest.{' '} + {isContestRecurring + ? 'It routes the fees generated from stake to the contest address and launchs the contest contract onchain.' + : 'It launchs the contest contract onchain.'} diff --git a/packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx b/packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx index 22894df489f..d0eb31bd442 100644 --- a/packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx +++ b/packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx @@ -39,6 +39,7 @@ import { isContestActive } from 'views/pages/CommunityManagement/Contests/utils' import useTokenMetadataQuery from '../../../state/api/tokens/getTokenMetadata'; import { AdminOnboardingSlider } from '../../components/AdminOnboardingSlider'; import { UserTrainingSlider } from '../../components/UserTrainingSlider'; +import { CWText } from '../../components/component_kit/cw_text'; import { DiscussionsFeedDiscovery } from './DiscussionsFeedDiscovery'; import { EmptyThreadsPlaceholder } from './EmptyThreadsPlaceholder'; @@ -338,7 +339,12 @@ const DiscussionsPage = ({ topicName }: DiscussionsPageProps) => { voteWeight={voteWeight} popover={{ title: tokenMetadata?.name, - body: formatAddressShort(topicObj.token_address!, 6, 6), + body: ( + + This topic has weighted voting enabled using{' '} + {formatAddressShort(topicObj.token_address!, 6, 6)} + + ), }} /> )} From 7115910a3c8bd420b6c378dc608bd8039f374f96 Mon Sep 17 00:00:00 2001 From: Marcin Date: Tue, 22 Oct 2024 16:41:12 +0200 Subject: [PATCH 7/7] ci --- .../client/scripts/state/api/tokens/getERC20Balance.ts | 4 ++-- .../scripts/views/pages/discussions/DiscussionsPage.tsx | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/commonwealth/client/scripts/state/api/tokens/getERC20Balance.ts b/packages/commonwealth/client/scripts/state/api/tokens/getERC20Balance.ts index 195e4bb7661..f135e8f47e8 100644 --- a/packages/commonwealth/client/scripts/state/api/tokens/getERC20Balance.ts +++ b/packages/commonwealth/client/scripts/state/api/tokens/getERC20Balance.ts @@ -16,7 +16,7 @@ const getERC20Balance = async ({ }: UseGetERC20BalanceQueryProps) => { const helper = new ERC20Helper(tokenAddress, nodeRpc); - return helper.getBalance(userAddress); + return await helper.getBalance(userAddress); }; const useGetERC20BalanceQuery = ({ @@ -27,7 +27,7 @@ const useGetERC20BalanceQuery = ({ return useQuery({ queryKey: [userAddress, tokenAddress, nodeRpc], queryFn: () => getERC20Balance({ userAddress, tokenAddress, nodeRpc }), - enabled: !!tokenAddress && userAddress && !!nodeRpc, + enabled: !!tokenAddress && !!userAddress && !!nodeRpc, staleTime: GET_ERC20_BALANCE_STALE_TIME, retry: false, }); diff --git a/packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx b/packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx index d0eb31bd442..7979c479939 100644 --- a/packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx +++ b/packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx @@ -195,7 +195,11 @@ const DiscussionsPage = ({ topicName }: DiscussionsPageProps) => { const voteWeight = isTopicWeighted && erc20Balance - ? String((topicObj?.vote_weight_multiplier * erc20Balance).toFixed(0)) + ? String( + ( + (topicObj?.vote_weight_multiplier || 1) * Number(erc20Balance) + ).toFixed(0), + ) : ''; return (