Skip to content

Commit 7005a68

Browse files
Merge pull request #156 from valory-xyz/tanya/revoke
feat: implement revoking power
2 parents 7b5acd6 + 0f4cbf7 commit 7005a68

29 files changed

+447
-79
lines changed

apps/govern/common-util/constants/scopeKeys.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const INVALIDATE_AFTER_UPDATE_KEYS = [
1414
TIME_SUM_KEY,
1515
];
1616

17-
export const INVALIDATE_AFTER_ACCOUNT_CHANGE = [
17+
export const INVALIDATE_AFTER_USER_DATA_CHANGE = [
1818
LAST_USER_VOTE_KEY,
1919
VOTE_USER_POWER_KEY,
2020
NEXT_USERS_SLOPES_KEY,

apps/govern/common-util/functions/addresses.ts

-13
This file was deleted.

apps/govern/common-util/functions/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from './addresses';
21
export * from './frontend-library';
32
export * from './requests';
43
export * from './web3';

apps/govern/common-util/functions/nominee-hash.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AbiCoder, ethers } from 'ethers';
22
import { Address } from 'viem';
33

4-
export const getNomineeHash = (account: Address, chainId: number) => {
4+
export const getNomineeHash = (account: Address | string, chainId: number) => {
55
const nomineeEncoded = AbiCoder.defaultAbiCoder().encode(
66
['bytes32', 'uint256'],
77
[account, chainId],

apps/govern/common-util/functions/requests.ts

+31-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import {
99
STAKING_FACTORY,
1010
VE_OLAS,
1111
} from 'libs/util-contracts/src/lib/abiAndAddresses';
12-
import { getEstimatedGasLimit, sendTransaction } from 'libs/util-functions/src';
12+
import {
13+
getAddressFromBytes32,
14+
getEstimatedGasLimit,
15+
sendTransaction,
16+
} from 'libs/util-functions/src';
1317

1418
import { SUPPORTED_CHAINS, wagmiConfig } from 'common-util/config/wagmi';
1519

16-
import { getAddressFromBytes32 } from './addresses';
1720
import { getUnixNextWeekStartTimestamp } from './time';
1821
import {
1922
getGovernorContract,
@@ -24,6 +27,7 @@ import {
2427
getVoteWeightingContract,
2528
} from './web3';
2629
import { RPC_URLS } from 'libs/util-constants/src';
30+
import { Nominee } from 'types';
2731

2832
type VoteForNomineeWeightsParams = {
2933
account: Address | undefined;
@@ -54,9 +58,7 @@ export const voteForNomineeWeights = async ({
5458

5559
export const checkIfNomineeRemoved = async (allocations: { address: Address }[]) => {
5660
const contract = getVoteWeightingContract();
57-
const result: { account: Address; chainId: number }[] = await contract.methods
58-
.getAllRemovedNominees()
59-
.call();
61+
const result: Nominee[] = await contract.methods.getAllRemovedNominees().call();
6062

6163
if (!result) return [];
6264

@@ -415,3 +417,27 @@ export const voteForProposal = async ({
415417

416418
return result;
417419
};
420+
421+
/**
422+
* Revoke voting power from a removed nominee
423+
*/
424+
type RevokePowerParams = {
425+
account: Address | undefined;
426+
nominee: string;
427+
chainId: number;
428+
};
429+
430+
export const revokePower = async ({ account, nominee, chainId }: RevokePowerParams) => {
431+
const contract = getVoteWeightingContract();
432+
const voteFn = contract.methods.revokeRemovedNomineeVotingPower(nominee, chainId);
433+
434+
const estimatedGas = await getEstimatedGasLimit(voteFn, account);
435+
const fn = voteFn.send({ from: account, estimatedGas });
436+
437+
const result = await sendTransaction(fn, account, {
438+
supportedChains: SUPPORTED_CHAINS,
439+
rpcUrls: RPC_URLS,
440+
});
441+
442+
return result;
443+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ActionCreatorWithoutPayload } from '@reduxjs/toolkit';
2+
import { queryClient } from 'context/Web3ModalProvider';
3+
import { clearState } from 'store/govern';
4+
import { AppDispatch } from 'store/index';
5+
6+
// Reset previously saved data so it's re-fetched automatically
7+
export const resetState = (
8+
keys: string[],
9+
dispatch: AppDispatch,
10+
clearAction: ActionCreatorWithoutPayload<
11+
'govern/clearState' | 'govern/clearUserState'
12+
> = clearState,
13+
) => {
14+
queryClient.removeQueries({
15+
predicate: (query) =>
16+
keys.includes(
17+
// `query.queryKey[0]` is just the name of the function that initiate the query cache, e.g "readContract"
18+
// while `query.queryKey[1]` has query arguments, including scopeKey if provided
19+
(query.queryKey[1] as Record<string, string>)?.scopeKey,
20+
),
21+
});
22+
dispatch(clearAction());
23+
};

apps/govern/components/Contract/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import { useEnsName } from 'wagmi';
88

99
import { CHAIN_NAMES, EXPLORER_URLS, UNICODE_SYMBOLS } from 'libs/util-constants/src';
1010

11-
import { getAddressFromBytes32, truncateAddress } from 'common-util/functions/addresses';
1211
import { useAppSelector } from 'store/index';
1312

1413
import { ContractConfiguration } from './ContractConfiguration';
1514
import { useContractParams } from './hooks';
15+
import { getAddressFromBytes32, truncateAddress } from 'libs/util-functions/src';
1616

1717
const StyledMain = styled.main`
1818
display: flex;

apps/govern/components/Contracts/EditVotes/index.tsx

+4-11
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ import { useAccount } from 'wagmi';
1010
import { CHAIN_NAMES, RETAINER_ADDRESS } from 'libs/util-constants/src';
1111

1212
import { INVALIDATE_AFTER_UPDATE_KEYS } from 'common-util/constants/scopeKeys';
13-
import { getBytes32FromAddress } from 'common-util/functions/addresses';
1413
import { voteForNomineeWeights } from 'common-util/functions/requests';
15-
import { queryClient } from 'context/Web3ModalProvider';
16-
import { clearState } from 'store/govern';
1714
import { useAppDispatch, useAppSelector } from 'store/index';
1815

1916
import { ConfirmModal } from './ConfirmModal';
@@ -24,6 +21,8 @@ import {
2421
checkNoRemovedNominees,
2522
checkNotNegativeSlope,
2623
} from './validations';
24+
import { resetState } from 'common-util/functions/resetState';
25+
import { getBytes32FromAddress } from 'libs/util-functions/src';
2726

2827
const { Paragraph, Text } = Typography;
2928

@@ -171,14 +170,8 @@ export const EditVotes = ({ allocations, setAllocations, setIsUpdating }: EditVo
171170
});
172171

173172
setIsUpdating(false);
174-
// Reset previously saved data so it's re-fetched automatically
175-
queryClient.removeQueries({
176-
predicate: (query) =>
177-
INVALIDATE_AFTER_UPDATE_KEYS.includes(
178-
(query.queryKey[1] as Record<string, string>)?.scopeKey,
179-
),
180-
});
181-
dispatch(clearState());
173+
174+
resetState(INVALIDATE_AFTER_UPDATE_KEYS, dispatch);
182175
})
183176
.catch((error) => {
184177
notification.error({

apps/govern/components/Contracts/EditVotes/utils.spec.ts

+3
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ const userVotes: Record<Address, UserVotes> = {
3030
'0x2': {
3131
current: { slope: 1, power: 8000, end: 1000 },
3232
next: { slope: 1, power: 8000, end: 1000 },
33+
chainId: 1,
3334
},
3435
'0x3': {
3536
current: { slope: 1, power: 500, end: 500 },
3637
next: { slope: 1, power: 500, end: 500 },
38+
chainId: 1,
3739
},
3840
'0x5': {
3941
current: { slope: 1, power: 1000, end: 1000 },
4042
next: { slope: 1, power: 1000, end: 1000 },
43+
chainId: 1,
4144
},
4245
};
4346

0 commit comments

Comments
 (0)