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

Fix Unit tests in Wallet migration branch #988

Open
wants to merge 50 commits into
base: new-delegate-factory
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
29c4c2a
Add wagmi config and start replacing web3-react
hernandoagf Mar 4, 2025
ead5e44
Replace network param from web3-react hooks
hernandoagf Mar 5, 2025
b9bb625
Remove chain id and address imports from web3-react
hernandoagf Mar 5, 2025
629ddb0
Add wagmi cli and generate abis
hernandoagf Mar 5, 2025
3aa106f
Add wagmi CLI and and start implementing read hooks
hernandoagf Mar 5, 2025
ed060be
Replace DSR logic to use bigint
hernandoagf Mar 6, 2025
428b258
Replace remaining read contract calls for Viem read contracts
hernandoagf Mar 6, 2025
11f0211
Update some BigNumber references to big int
hernandoagf Mar 6, 2025
b81512b
Update first write hook to wagmi flow
hernandoagf Mar 6, 2025
61bbfd0
Update approve write hook to new flow
hernandoagf Mar 7, 2025
1048323
Update MKR deposit hook to new write flow
hernandoagf Mar 7, 2025
4648e8d
Update MKR withdraw hooks to new flow
hernandoagf Mar 7, 2025
90af873
Update ESM burn write hook to new flow
hernandoagf Mar 7, 2025
03465be
Migrate the ESM shutdown write hook to new flow
hernandoagf Mar 7, 2025
b4b44fd
Migrate delegate create write hook to new flow
hernandoagf Mar 7, 2025
09a28d4
Migrate executive vote hooks to new flow
hernandoagf Mar 7, 2025
9be0b77
Migrate poll creation hook to new flow
hernandoagf Mar 7, 2025
6b2cafe
Migrate delegate lock hook to new flow
hernandoagf Mar 10, 2025
1f09bd6
Migrate delegate free hook to new flow
hernandoagf Mar 10, 2025
4a00ce4
Update poll vote and gasless vote flows to use Viem
hernandoagf Mar 11, 2025
24fb0e6
Remove dethcrypto/eth-sdk packages and related code
hernandoagf Mar 11, 2025
f537ecc
Replace ethers references for viem
hernandoagf Mar 11, 2025
610b388
Update lockfile
hernandoagf Mar 11, 2025
26e8549
Remove remaining Ethers references
hernandoagf Mar 12, 2025
e24c0cf
Replace all bignumber references for bigint
hernandoagf Mar 12, 2025
5f916a1
Remove unused dependencies and code
hernandoagf Mar 13, 2025
9d24302
Fix issue when fetching logs
hernandoagf Mar 13, 2025
e7f5331
Update typescript to v5 and theme-ui to v0.16
hernandoagf Mar 13, 2025
fa2a2c8
Merge branch 'new-delegate-factory' into wallet-refactor
hernandoagf Mar 14, 2025
1215752
Fix big int formatting issues
hernandoagf Mar 14, 2025
0ffd12b
Remove unnecessary step in CI
hernandoagf Mar 14, 2025
61a3653
Fix wallet connection in e2e tests
hernandoagf Mar 14, 2025
da6192c
Fix wallet connection in e2e tests
hernandoagf Mar 14, 2025
36e576c
Fixes for e2e tests
hernandoagf Mar 14, 2025
d59b4e5
Merge branch 'new-delegate-factory' into wallet-refactor
hernandoagf Mar 14, 2025
c747d26
Handle 0n balance
adamgoth Mar 17, 2025
30a6fdc
Remove ethers peer dependency from defender relay client
hernandoagf Mar 17, 2025
85af6a0
Update file extensions
hernandoagf Mar 17, 2025
74e4745
Install Vitest packages
hernandoagf Mar 17, 2025
aa34188
Update Jest references to Vitest
hernandoagf Mar 17, 2025
ca85138
Fix tests
hernandoagf Mar 18, 2025
74e4e86
Fix remaining unit tests
hernandoagf Mar 18, 2025
e1f04d8
Add test:ci command
hernandoagf Mar 18, 2025
4c26f33
Add missing coverage package
hernandoagf Mar 18, 2025
ad1b8d2
Merge branch 'new-delegate-factory' into wallet-refactor
hernandoagf Mar 18, 2025
cee83c2
Remove new BigNumber references
hernandoagf Mar 18, 2025
d321973
Merge branch 'wallet-refactor' into replace-jest-for-vitest
hernandoagf Mar 18, 2025
fda4d90
Merge branch 'new-delegate-factory' into replace-jest-for-vitest
hernandoagf Mar 18, 2025
29241f5
Update mock function
hernandoagf Mar 18, 2025
1379b00
Merge branch 'new-delegate-factory' into replace-jest-for-vitest
hernandoagf Mar 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Migrate poll creation hook to new flow
hernandoagf committed Mar 7, 2025
commit 9be0b771d64005d638fd038254908c1af6832bb5
62 changes: 34 additions & 28 deletions modules/polling/components/PollCreateModal.tsx
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ import { Button, Flex, Close, Text, Box, Spinner } from 'theme-ui';
import { Icon } from '@makerdao/dai-ui-icons';
import { DialogOverlay, DialogContent } from 'modules/app/components/Dialog';

import { TXMined } from 'modules/web3/types/transaction';
import { Poll } from 'modules/polling/types';
import { useNetwork } from 'modules/app/hooks/useNetwork';
import { usePollCreate } from '../hooks/usePollCreate';
@@ -20,32 +19,31 @@ import EtherscanLink from 'modules/web3/components/EtherscanLink';
type Props = {
close: () => void;
poll: Poll | undefined;
setPoll: (any) => void;
setPoll: (poll: Poll | undefined) => void;
};

const PollCreateModal = ({ close, poll, setPoll }: Props): JSX.Element => {
const { createPoll, tx } = usePollCreate();

const [step, setStep] = useState('confirm');
const onPollCreate = () => {
if (!poll) return;

createPoll(
poll.startDate.getTime() / 1000,
poll.endDate.getTime() / 1000,
poll.multiHash,
poll.url || '',
{
initialized: () => setStep('signing'),
pending: () => setStep('pending'),
mined: () => {
setPoll(undefined);
close();
},
error: () => setStep('failed')
}
);
};
const [txHash, setTxHash] = useState<`0x${string}` | undefined>();

const createPoll = usePollCreate({
startDate: poll && BigInt(poll.startDate.getTime() / 1000),
endDate: poll && BigInt(poll.endDate.getTime() / 1000),
multiHash: poll && poll.multiHash,
url: poll && poll.url,
onStart: (hash: `0x${string}`) => {
setTxHash(hash);
setStep('pending');
},
onSuccess: (hash: `0x${string}`) => {
setTxHash(hash);
setPoll(undefined);
close();
},
onError: () => {
setStep('failed');
}
});

const Default = () => {
return (
@@ -76,7 +74,15 @@ const PollCreateModal = ({ close, poll, setPoll }: Props): JSX.Element => {
</Box>
</Box>
<Box sx={{ width: '100%', mt: 3 }}>
<Button variant="primaryLarge" sx={{ width: '100%' }} onClick={onPollCreate}>
<Button
variant="primaryLarge"
sx={{ width: '100%' }}
disabled={!poll || createPoll.isLoading || !createPoll.prepared}
onClick={() => {
setStep('signing');
createPoll.execute();
}}
>
Create Poll
</Button>
</Box>
@@ -91,11 +97,11 @@ const PollCreateModal = ({ close, poll, setPoll }: Props): JSX.Element => {
case 'signing':
return <Signing close={close} />;
case 'pending':
return <Pending tx={tx} close={close} />;
return <Pending txHash={txHash} close={close} />;
case 'failed':
return <Error close={close} />;
}
}, [step, tx]);
}, [step, txHash, createPoll.isLoading, createPoll.prepared, createPoll.execute]);

return (
<DialogOverlay isOpen onDismiss={close}>
@@ -124,7 +130,7 @@ const Signing = ({ close }) => (
</Flex>
);

const Pending = ({ tx, close }) => {
const Pending = ({ txHash, close }: { txHash: `0x${string}` | undefined; close: () => void }) => {
const network = useNetwork();

return (
@@ -144,7 +150,7 @@ const Pending = ({ tx, close }) => {
Poll will be created once the transaction has been confirmed.
</Text>

<EtherscanLink type="transaction" hash={(tx as TXMined).hash} network={network} />
{txHash && <EtherscanLink type="transaction" hash={txHash} network={network} />}

<Button
onClick={close}
111 changes: 37 additions & 74 deletions modules/polling/hooks/usePollCreate.ts
Original file line number Diff line number Diff line change
@@ -6,78 +6,41 @@ SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { Dispatch, SetStateAction, useState } from 'react';
import useTransactionStore, {
transactionsSelectors,
transactionsApi
} from 'modules/web3/stores/transactions';
import { shallow } from 'zustand/shallow';
import { Transaction } from 'modules/web3/types/transaction';
import { useContracts } from 'modules/web3/hooks/useContracts';
import { useAccount } from 'modules/app/hooks/useAccount';
import { MainnetSdk } from '@dethcrypto/eth-sdk-client';
import { useWeb3 } from 'modules/web3/hooks/useWeb3';
import { sendTransaction } from 'modules/web3/helpers/sendTransaction';

type CreateResponse = {
txId: string | null;
setTxId: Dispatch<SetStateAction<string | null>>;
createPoll: (
startDate: number,
endDate: number,
multiHash: string,
url: string,
callbacks?: Record<string, (id?: string) => void>
) => void;
tx: Transaction | null;
};

export const usePollCreate = (): CreateResponse => {
const [txId, setTxId] = useState<string | null>(null);

const { account } = useAccount();
const { provider } = useWeb3();

// We want to use the original polling contract deployment for creating polls to avoid pollId collisions
const { pollingOld } = useContracts() as MainnetSdk;

const [track, tx] = useTransactionStore(
state => [state.track, txId ? transactionsSelectors.getTransaction(state, txId) : null],
shallow
);

const createPoll = (startDate, endDate, multiHash, url, callbacks) => {
if (!account || !provider) {
return;
}

const createTxCreator = async () => {
const populatedTransaction = await pollingOld.populateTransaction.createPoll(
startDate,
endDate,
multiHash,
url
);

return sendTransaction(populatedTransaction, provider, account);
};
const txId = track(createTxCreator, account, 'Creating poll', {
initialized: () => {
if (typeof callbacks?.initialized === 'function') callbacks.initialized();
},
pending: () => {
if (typeof callbacks?.pending === 'function') callbacks.pending();
},
mined: txId => {
transactionsApi.getState().setMessage(txId, 'Created poll');
if (typeof callbacks?.mined === 'function') callbacks.mined();
},
error: () => {
if (typeof callbacks?.error === 'function') callbacks.error();
}
});
setTxId(txId);
};

return { txId, setTxId, createPoll, tx };
import { useChainId } from 'wagmi';
import { useWriteContractFlow } from 'modules/web3/hooks/useWriteContractFlow';
import { pollingOldAbi, pollingOldAddress } from 'modules/contracts/generated';
import { WriteHook, WriteHookParams } from 'modules/web3/types/hooks';

export const usePollCreate = ({
startDate,
endDate,
multiHash,
url,
gas,
enabled: paramEnabled = true,
onSuccess,
onError,
onStart
}: WriteHookParams & {
startDate: bigint | undefined;
endDate: bigint | undefined;
multiHash: string | undefined;
url: string | undefined;
}): WriteHook => {
const chainId = useChainId();

const enabled = paramEnabled && !!startDate && !!endDate && !!multiHash && !!url;

return useWriteContractFlow({
address: pollingOldAddress[chainId],
abi: pollingOldAbi,
functionName: 'createPoll',
args: [startDate as bigint, endDate as bigint, multiHash as string, url as string],
chainId,
enabled,
gas,
onSuccess,
onError,
onStart
});
};