From 3ae85f8626cf324a627fbb860eaef96706350af6 Mon Sep 17 00:00:00 2001 From: Karelian Pie Date: Fri, 23 Jun 2023 21:24:43 +0300 Subject: [PATCH 1/2] feat: Migrate VoteTab to wagmi --- apps/veyfi/components/VoteTab.tsx | 30 ++++++++++++++---------- apps/veyfi/utils/actions/votingEscrow.ts | 22 ++++++++++------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/apps/veyfi/components/VoteTab.tsx b/apps/veyfi/components/VoteTab.tsx index cd94bdbee..08c29b94e 100644 --- a/apps/veyfi/components/VoteTab.tsx +++ b/apps/veyfi/components/VoteTab.tsx @@ -1,34 +1,40 @@ -import {useState} from 'react'; +import {useCallback, useState} from 'react'; import Link from 'next/link'; -import {useTransaction} from '@veYFI/hooks/useTransaction'; -import * as VotingEscrowActions from '@veYFI/utils/actions/votingEscrow'; +import {delegateVote} from '@veYFI/utils/actions/votingEscrow'; +import {SNAPSHOT_DELEGATE_REGISTRY_ADDRESS} from '@veYFI/utils/constants'; import {validateAddress, validateNetwork} from '@veYFI/utils/validations'; import {Button} from '@yearn-finance/web-lib/components/Button'; import {useWeb3} from '@yearn-finance/web-lib/contexts/useWeb3'; import {toAddress} from '@yearn-finance/web-lib/utils/address'; +import {defaultTxStatus} from '@yearn-finance/web-lib/utils/web3/transaction'; import {Input} from '@common/components/Input'; -import type {ethers} from 'ethers'; import type {ReactElement} from 'react'; import type {TAddress} from '@yearn-finance/web-lib/types'; function VoteTab(): ReactElement { const [delegateAddress, set_delegateAddress] = useState(''); const {provider, address, isActive, chainID} = useWeb3(); - const [delegateVote, delegateVoteStatus] = useTransaction(VotingEscrowActions.delegateVote); - const web3Provider = provider as ethers.providers.Web3Provider; + const [delegateVoteStatus, set_delegateVoteStatus] = useState(defaultTxStatus); + const userAddress = address as TAddress; const {isValid: isValidNetwork} = validateNetwork({supportedNetwork: 1, walletNetwork: chainID}); const {isValid: isValidDelegateAddress, error: delegateAddressError} = validateAddress({address: delegateAddress}); - const executeDelegateVote = (): void => { + const handleExecuteDelegateVote = useCallback(async (): Promise => { if (!userAddress || !isValidDelegateAddress) { return; } - delegateVote(web3Provider, userAddress, toAddress(delegateAddress)); - }; + + await delegateVote({ + connector: provider, + contractAddress: SNAPSHOT_DELEGATE_REGISTRY_ADDRESS, + statusHandler: set_delegateVoteStatus, + delegateAddress: toAddress(delegateAddress) + }); + }, [delegateAddress, isValidDelegateAddress, provider, userAddress]); return (
@@ -66,9 +72,9 @@ function VoteTab(): ReactElement { /> diff --git a/apps/veyfi/utils/actions/votingEscrow.ts b/apps/veyfi/utils/actions/votingEscrow.ts index 3fa008c60..9d33ae08d 100644 --- a/apps/veyfi/utils/actions/votingEscrow.ts +++ b/apps/veyfi/utils/actions/votingEscrow.ts @@ -1,13 +1,15 @@ import {ethers} from 'ethers'; import {handleTx} from '@yearn-finance/web-lib/utils/web3/transaction'; +import {assertAddress, handleTx as handleTxWagmi} from '@common/utils/toWagmiProvider'; import SNAPSHOT_DELEGATE_REGISTRY_ABI from '../abi/SnapshotDelegateRegistry.abi'; import VEYFI_ABI from '../abi/veYFI.abi'; -import {SNAPSHOT_DELEGATE_REGISTRY_ADDRESS, YEARN_SNAPSHOT_SPACE} from '../constants'; +import {YEARN_SNAPSHOT_SPACE} from '../constants'; import type {TAddress} from '@yearn-finance/web-lib/types'; import type {TSeconds} from '@yearn-finance/web-lib/utils/time'; import type {TTxResponse} from '@yearn-finance/web-lib/utils/web3/transaction'; +import type {TWriteTransaction} from '@common/utils/toWagmiProvider'; export async function approveLock( provider: ethers.providers.JsonRpcProvider, @@ -79,12 +81,14 @@ export async function withdrawLocked( return await handleTx(votingEscrowContract.withdraw()); } -export async function delegateVote( - provider: ethers.providers.Web3Provider, - accountAddress: TAddress, - delegateAddress: TAddress -): Promise { - const signer = provider.getSigner(accountAddress); - const delegateRegistryContract = new ethers.Contract(SNAPSHOT_DELEGATE_REGISTRY_ADDRESS, SNAPSHOT_DELEGATE_REGISTRY_ABI, signer); - return await handleTx(delegateRegistryContract.setDelegate(ethers.utils.formatBytes32String(YEARN_SNAPSHOT_SPACE), delegateAddress)); +type TDelegateVote = TWriteTransaction & {delegateAddress: TAddress}; +export async function delegateVote(props: TDelegateVote): Promise { + assertAddress(props.contractAddress); + + return await handleTxWagmi(props, { + address: props.contractAddress, + abi: SNAPSHOT_DELEGATE_REGISTRY_ABI, + functionName: 'setDelegate', + args: [ethers.utils.formatBytes32String(YEARN_SNAPSHOT_SPACE), props.delegateAddress] + }); } From bc1da6e38570ab82627432f6b7fd1019d2c15dc6 Mon Sep 17 00:00:00 2001 From: Karelian Pie Date: Mon, 26 Jun 2023 12:56:51 +0300 Subject: [PATCH 2/2] refactor: Assert all addresses --- apps/common/utils/toWagmiProvider.tsx | 2 +- apps/veyfi/utils/actions/votingEscrow.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/common/utils/toWagmiProvider.tsx b/apps/common/utils/toWagmiProvider.tsx index 3dcbb88b9..41349b76e 100644 --- a/apps/common/utils/toWagmiProvider.tsx +++ b/apps/common/utils/toWagmiProvider.tsx @@ -48,7 +48,7 @@ export function assertAddress(addr: string | TAddress | undefined, name?: string assert(!isEth(addr), `${name || 'Address'} is 0xE`); } -export function assertAddresses(addresses: string[] | TAddress[] | undefined[], name?: string): asserts addresses is TAddress[] { +export function assertAddresses(addresses: (string | TAddress | undefined)[], name?: string): asserts addresses is TAddress[] { addresses.forEach((addr): void => assertAddress(addr, name)); } diff --git a/apps/veyfi/utils/actions/votingEscrow.ts b/apps/veyfi/utils/actions/votingEscrow.ts index 9d33ae08d..0e27c161f 100644 --- a/apps/veyfi/utils/actions/votingEscrow.ts +++ b/apps/veyfi/utils/actions/votingEscrow.ts @@ -1,6 +1,6 @@ import {ethers} from 'ethers'; import {handleTx} from '@yearn-finance/web-lib/utils/web3/transaction'; -import {assertAddress, handleTx as handleTxWagmi} from '@common/utils/toWagmiProvider'; +import {assertAddresses, handleTx as handleTxWagmi} from '@common/utils/toWagmiProvider'; import SNAPSHOT_DELEGATE_REGISTRY_ABI from '../abi/SnapshotDelegateRegistry.abi'; import VEYFI_ABI from '../abi/veYFI.abi'; @@ -83,7 +83,7 @@ export async function withdrawLocked( type TDelegateVote = TWriteTransaction & {delegateAddress: TAddress}; export async function delegateVote(props: TDelegateVote): Promise { - assertAddress(props.contractAddress); + assertAddresses([props.delegateAddress, props.contractAddress]); return await handleTxWagmi(props, { address: props.contractAddress,