Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable delegation for validators, groups and their signers #135

Merged
4 changes: 3 additions & 1 deletion src/config/wagmi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ export const wagmiConfig = createConfig({
chains: [config.chain],
connectors,
transports: {
[celo.id]: fallback([http(config.chain.rpcUrls.default.http[0]), http(infuraRpcUrl)]),
[celo.id]: fallback([http(config.chain.rpcUrls.default.http[0]), http(infuraRpcUrl)], {
rank: true,
nicolasbrugneaux marked this conversation as resolved.
Show resolved Hide resolved
}),
[celoAlfajores.id]: http(config.chain.rpcUrls.default.http[0]),
},
});
Expand Down
72 changes: 63 additions & 9 deletions src/features/account/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { accountsABI, lockedGoldABI } from '@celo/abis';
import { validatorsABI } from '@celo/abis-12';
import { useEffect, useState } from 'react';
import { useToastError } from 'src/components/notifications/useToastError';
import { BALANCE_REFRESH_INTERVAL, ZERO_ADDRESS } from 'src/config/consts';
Expand Down Expand Up @@ -45,33 +46,86 @@ export function useLockedBalance(address?: Address) {
};
}

// Note, this retrieves the address' info from the Accounts contract
// It has nothing to do with wallets or backend services
export function useAccountDetails(address?: Address) {
export function useVoteSigner(address?: Address) {
const {
data: isRegistered,
data: voteSigner,
isError,
isLoading,
error,
refetch,
} = useReadContract({
address: Addresses.Accounts,
abi: accountsABI,
functionName: 'voteSignerToAccount',
args: [address || ZERO_ADDRESS],
query: { enabled: !!address },
});

useToastError(error, 'Error fetching vote signer');

return {
voteSigner,
isError,
isLoading,
refetch,
};
}

// Note, this retrieves the address' info from the Accounts contract
// It has nothing to do with wallets or backend services
export function useAccountDetails(address?: Address, addressOrVoteSigner?: Address) {
const isAccountResult = useReadContract({
address: Addresses.Accounts,
abi: accountsABI,
functionName: 'isAccount',
args: [address || ZERO_ADDRESS],
query: { enabled: !!address },
});

const isValidatorOrVoteSignerResult = useReadContract({
address: Addresses.Validators,
abi: validatorsABI,
functionName: 'isValidator',
args: [addressOrVoteSigner || ZERO_ADDRESS],
query: { enabled: !!addressOrVoteSigner },
});

const isValidatorGroupOrVoteSignerResult = useReadContract({
aaronmgdr marked this conversation as resolved.
Show resolved Hide resolved
address: Addresses.Validators,
abi: validatorsABI,
functionName: 'isValidatorGroup',
args: [addressOrVoteSigner || ZERO_ADDRESS],
query: { enabled: !!addressOrVoteSigner },
});

// Note, more reads can be added here if more info is needed, such
// as name, metadataUrl, walletAddress, voteSignerToAccount, etc.

useToastError(error, 'Error fetching account registration status');
useToastError(
isAccountResult.error ||
isValidatorOrVoteSignerResult.error ||
isValidatorGroupOrVoteSignerResult.error,
'Error fetching account details',
);

return {
isRegistered,
isError,
isLoading,
refetch,
isRegistered: isAccountResult.data,
isValidatorOrVoteSigner: isValidatorOrVoteSignerResult.data,
isValidatorGroupOrVoteSigner: isValidatorGroupOrVoteSignerResult.data,
isError:
isAccountResult.isError ||
isValidatorOrVoteSignerResult.isError ||
isValidatorGroupOrVoteSignerResult.isError,
isLoading:
isAccountResult.isLoading ||
isValidatorOrVoteSignerResult.isLoading ||
isValidatorGroupOrVoteSignerResult.isLoading,
refetch: () =>
Promise.all([
isAccountResult.refetch(),
isValidatorOrVoteSignerResult.refetch(),
isValidatorGroupOrVoteSignerResult.refetch(),
]),
};
}

Expand Down
35 changes: 25 additions & 10 deletions src/features/delegation/DelegationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { RadioField } from 'src/components/input/RadioField';
import { RangeField } from 'src/components/input/RangeField';
import { TextField } from 'src/components/input/TextField';
import { MAX_NUM_DELEGATEES, ZERO_ADDRESS } from 'src/config/consts';
import { useAccountDetails, useVoteSigner } from 'src/features/account/hooks';
import { getDelegateTxPlan } from 'src/features/delegation/delegatePlan';
import { useDelegatees } from 'src/features/delegation/hooks/useDelegatees';
import { useDelegationBalances } from 'src/features/delegation/hooks/useDelegationBalances';
Expand Down Expand Up @@ -43,6 +44,8 @@ export function DelegationForm({
const { address } = useAccount();
const { addressToDelegatee } = useDelegatees();
const { delegations, refetch } = useDelegationBalances(address);
const { voteSigner } = useVoteSigner(address);
const { isValidatorOrVoteSigner, isValidatorGroupOrVoteSigner } = useAccountDetails(address, voteSigner);

const { getNextTx, txPlanIndex, numTxs, isPlanStarted, onTxSuccess } =
useTransactionPlan<DelegateFormValues>({
Expand All @@ -65,6 +68,7 @@ export function DelegationForm({

const { writeContract, isLoading } = useWriteContractWithReceipt('delegation', onTxSuccess);
const isInputDisabled = isLoading || isPlanStarted;
const canDelegate = !isValidatorOrVoteSigner && !isValidatorGroupOrVoteSigner;

const onSubmit = (values: DelegateFormValues) => writeContract(getNextTx(values));

Expand All @@ -86,7 +90,7 @@ export function DelegationForm({
validateOnBlur={false}
>
{({ values }) => (
<Form className="mt-4 flex flex-1 flex-col justify-between">
<Form className="mt-4 flex flex-1 flex-col justify-between space-y-3">
<div
className={values.action === DelegateActionType.Transfer ? 'space-y-3' : 'space-y-5'}
>
Expand All @@ -113,15 +117,26 @@ export function DelegationForm({
)}
<PercentField delegations={delegations} disabled={isInputDisabled} />
</div>
<MultiTxFormSubmitButton
txIndex={txPlanIndex}
numTxs={numTxs}
isLoading={isLoading}
loadingText={ActionToVerb[values.action]}
tipText={ActionToTipText[values.action]}
>
{`${toTitleCase(values.action)}`}
</MultiTxFormSubmitButton>

{
<MultiTxFormSubmitButton
txIndex={txPlanIndex}
numTxs={numTxs}
isLoading={isLoading}
loadingText={ActionToVerb[values.action]}
tipText={ActionToTipText[values.action]}
disabled={!canDelegate}
>
{`${toTitleCase(values.action)}`}
</MultiTxFormSubmitButton>
}

{!canDelegate && (
<p className={'min-w-[18rem] max-w-sm text-xs text-red-600'}>
Validators and validator groups (as well as their signers) cannot delegate their
voting power.
</p>
)}
</Form>
)}
</Formik>
Expand Down
1 change: 0 additions & 1 deletion src/features/delegation/components/DelegateButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { objLength } from 'src/utils/objects';

export function DelegateButton({ delegatee }: { delegatee: Delegatee }) {
const { proposalToVotes } = useDelegateeHistory(delegatee.address);

const showTxModal = useTransactionModal(TransactionFlowType.Delegate, {
delegatee: delegatee.address,
});
Expand Down
Loading