Skip to content

Commit

Permalink
update vite
Browse files Browse the repository at this point in the history
  • Loading branch information
jennyg0 committed Aug 29, 2024
1 parent 3d9b487 commit 8c22b8c
Show file tree
Hide file tree
Showing 60 changed files with 253 additions and 305 deletions.
2 changes: 0 additions & 2 deletions templates/react/vite-ethers/src/components/Account.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use client'

import { useEthereum } from './Context';

export function Account() {
Expand Down
3 changes: 0 additions & 3 deletions templates/react/vite-ethers/src/components/Balance.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use client'

import { useState, useEffect } from 'react';
import { ethers } from 'ethers';

import { useAsync } from '../hooks/useAsync';
import { useEthereum } from './Context';

Expand Down
3 changes: 0 additions & 3 deletions templates/react/vite-ethers/src/components/BlockNumber.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
'use client'

import { useState, useEffect } from 'react';

import { useEthereum } from './Context';

export function BlockNumber() {
Expand Down
2 changes: 0 additions & 2 deletions templates/react/vite-ethers/src/components/Connect.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use client'

import { useEthereum } from './Context';

export function Connect() {
Expand Down
4 changes: 1 addition & 3 deletions templates/react/vite-ethers/src/components/Context.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use client';

import { JsonRpcSigner } from 'ethers';
import React, { useState, useEffect, createContext, useContext } from 'react';
import { useState, useEffect, createContext, useContext } from 'react';
import { BrowserProvider } from 'zksync-ethers';

type Chain = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use client'

import { useAsync } from '../hooks/useAsync';
import { chains, useEthereum } from './Context';

Expand Down
3 changes: 0 additions & 3 deletions templates/react/vite-ethers/src/components/ReadContract.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use client'

import { useState, useEffect } from 'react';
import { Contract } from 'zksync-ethers';

import { useAsync } from '../hooks/useAsync';
import { useEthereum } from './Context';
import { daiContractConfig } from './contracts'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use client'

import { useState } from 'react';
import { ethers } from 'ethers';

import { useAsync } from '../hooks/useAsync';
import { useEthereum } from './Context';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use client'

import { useState, useEffect } from 'react';
import { ethers } from 'ethers';

import { useAsync } from '../hooks/useAsync';
import { useEthereum } from './Context';

Expand Down
3 changes: 0 additions & 3 deletions templates/react/vite-ethers/src/components/SignMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use client'

import { useState, FormEvent } from 'react';
import { ethers } from 'ethers';

import { useAsync } from '../hooks/useAsync';
import { useEthereum } from './Context';

Expand Down
3 changes: 0 additions & 3 deletions templates/react/vite-ethers/src/components/SignTypedData.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
'use client'

import { ethers, type TypedDataField } from 'ethers';

import { useAsync } from '../hooks/useAsync';
import { defaultChain, useEthereum } from './Context';

Expand Down
3 changes: 0 additions & 3 deletions templates/react/vite-ethers/src/components/Token.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use client'

import { useState } from 'react';
import { Contract } from 'zksync-ethers';

import { useAsync } from '../hooks/useAsync';
import { erc20ABI, daiContractConfig } from './contracts'
import { useEthereum } from './Context';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use client'

import { useState, useEffect } from 'react';
import { Contract } from 'zksync-ethers';

import { daiContractConfig } from './contracts'
import { useEthereum } from './Context';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
'use client'

import { useEffect, useState } from 'react';

import { useEthereum } from './Context';

export function WatchPendingTransactions() {
Expand Down
3 changes: 0 additions & 3 deletions templates/react/vite-ethers/src/components/WriteContract.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use client'

import { useState } from 'react';
import { Contract } from 'zksync-ethers';

import { useAsync } from '../hooks/useAsync';
import { daiContractConfig } from './contracts'
import { useEthereum } from './Context';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use client'

import { useState, useEffect } from 'react';
import { useState, useCallback } from 'react';
import { Contract } from 'zksync-ethers';

import { useAsync } from '../hooks/useAsync';
import { daiContractConfig } from './contracts';
import { useEthereum } from './Context';
Expand All @@ -11,11 +10,13 @@ export function WriteContractPrepared() {
const [amount, setAmount] = useState<string | null>(null);
const { getSigner, getProvider } = useEthereum();

const getContractInstance = async () => {
const getContractInstance = useCallback(async () => {
return new Contract(daiContractConfig.address, daiContractConfig.abi, await getSigner()!);
}
}, [getSigner]);

const prepareAndSendTransaction = useCallback(async () => {
if (!amount) return null;

const { result: preparedTransaction, execute: prepareTransaction, inProgress: prepareInProgress, error: prepareError } = useAsync(async () => {
const contract = await getContractInstance();

// random address for testing, replace with contract address that you want to allow to spend your tokens
Expand All @@ -24,41 +25,42 @@ export function WriteContractPrepared() {
const gasPrice = await getProvider()!.getGasPrice();
const gasLimit = await contract.getFunction("approve").estimateGas(spender, amount);

return {
const preparedTransaction = {
args: [spender, amount],
overrides: {
gasPrice,
gasLimit
}
};
});

const { result: transaction, execute: sendTransaction, inProgress, error } = useAsync(async () => {
const contract = await getContractInstance();
const result = await contract.approve(...preparedTransaction!.args, preparedTransaction!.overrides);
waitForReceipt(result.hash);
const result = await contract.approve(...preparedTransaction.args, preparedTransaction.overrides);
return result;
});
}, [amount, getContractInstance, getProvider]);

const { result: transaction, execute: executePrepareAndSendTransaction, inProgress, error } = useAsync(prepareAndSendTransaction);

const { result: receipt, execute: waitForReceipt, inProgress: receiptInProgress, error: receiptError } = useAsync(async (transactionHash) => {
const { result: receipt, execute: waitForReceipt, inProgress: receiptInProgress, error: receiptError } = useAsync(async (transactionHash: string) => {
return await getProvider()!.waitForTransaction(transactionHash);
});

useEffect(() => {
if (!amount) return;
prepareTransaction();
}, [amount]);

const handleSubmit = (e: React.FormEvent) => {
const handleSubmit = useCallback(async (e: React.FormEvent) => {
e.preventDefault();
sendTransaction();
};
const result = await executePrepareAndSendTransaction();
if (result) {
waitForReceipt(result.hash);
}
}, [executePrepareAndSendTransaction, waitForReceipt]);

return (
<div>
<form onSubmit={handleSubmit}>
<input value={amount ?? ""} onChange={e => setAmount(e.target.value)} type="number" placeholder="allowance amount" />
<button disabled={prepareInProgress || !preparedTransaction} type="submit">Approve</button>
<input
value={amount ?? ""}
onChange={e => setAmount(e.target.value)}
type="number"
placeholder="allowance amount"
/>
<button disabled={inProgress || !amount} type="submit">Approve</button>
</form>

{inProgress ? (
Expand All @@ -79,7 +81,6 @@ export function WriteContractPrepared() {
)
)}

{prepareError && <div>Preparing Transaction Error: {prepareError.message}</div>}
{error && <div>Error: {error.message}</div>}
{receiptError && <div>Receipt Error: {receiptError.message}</div>}
</div>
Expand Down
2 changes: 0 additions & 2 deletions templates/react/vite-ethers5/src/components/Account.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use client'

import { useEthereum } from './Context';

export function Account() {
Expand Down
3 changes: 0 additions & 3 deletions templates/react/vite-ethers5/src/components/Balance.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use client'

import { useState, useEffect } from 'react';
import { formatEther } from 'ethers/lib/utils';

import { useAsync } from '../hooks/useAsync';
import { useEthereum } from './Context';

Expand Down
3 changes: 0 additions & 3 deletions templates/react/vite-ethers5/src/components/BlockNumber.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
'use client'

import { useState, useEffect } from 'react';

import { useEthereum } from './Context';

export function BlockNumber() {
Expand Down
2 changes: 0 additions & 2 deletions templates/react/vite-ethers5/src/components/Connect.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use client'

import { useEthereum } from './Context';

export function Connect() {
Expand Down
2 changes: 1 addition & 1 deletion templates/react/vite-ethers5/src/components/Context.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect, createContext, useContext } from 'react';
import { useState, useEffect, createContext, useContext } from 'react';
import { Signer, Web3Provider } from 'zksync-ethers';

type Chain = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use client'

import { useAsync } from '../hooks/useAsync';
import { chains, useEthereum } from './Context';

Expand Down
3 changes: 0 additions & 3 deletions templates/react/vite-ethers5/src/components/ReadContract.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use client'

import { useState, useEffect } from 'react';
import { Contract } from 'zksync-ethers';

import { useAsync } from '../hooks/useAsync';
import { useEthereum } from './Context';
import { daiContractConfig } from './contracts'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use client'

import { useState } from 'react';
import { parseEther } from 'ethers/lib/utils';

import { useAsync } from '../hooks/useAsync';
import { useEthereum } from './Context';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use client'

import { useState, useEffect } from 'react';
import { parseEther } from 'ethers/lib/utils';

import { useAsync } from '../hooks/useAsync';
import { useEthereum } from './Context';

Expand Down
3 changes: 0 additions & 3 deletions templates/react/vite-ethers5/src/components/SignMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use client'

import { useState, FormEvent } from 'react';
import { verifyMessage } from 'ethers/lib/utils';

import { useAsync } from '../hooks/useAsync';
import { useEthereum } from './Context';

Expand Down
3 changes: 0 additions & 3 deletions templates/react/vite-ethers5/src/components/SignTypedData.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
'use client'

import { verifyTypedData } from 'ethers/lib/utils';

import { useAsync } from '../hooks/useAsync';
import { defaultChain, useEthereum } from './Context';

Expand Down
3 changes: 0 additions & 3 deletions templates/react/vite-ethers5/src/components/Token.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use client'

import { useState } from 'react';
import { Contract } from 'zksync-ethers';

import { useAsync } from '../hooks/useAsync';
import { erc20ABI, daiContractConfig } from './contracts'
import { useEthereum } from './Context';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
'use client'

import { useState, useEffect } from 'react';
import { Contract } from 'zksync-ethers';
import type { BigNumber } from 'ethers';

import { daiContractConfig } from './contracts'
import { useEthereum } from './Context';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
'use client'

import { useEffect, useState } from 'react';

import { useEthereum } from './Context';

export function WatchPendingTransactions() {
Expand Down
3 changes: 0 additions & 3 deletions templates/react/vite-ethers5/src/components/WriteContract.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use client'

import { useState } from 'react';
import { Contract } from 'zksync-ethers';

import { useAsync } from '../hooks/useAsync';
import { daiContractConfig } from './contracts'
import { useEthereum } from './Context';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use client'

import { useState, useEffect } from 'react';
import { Contract } from 'zksync-ethers';

import { useAsync } from '../hooks/useAsync';
import { daiContractConfig } from './contracts';
import { useEthereum } from './Context';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from 'react'
import { useAccount, useBalance, type UseBalanceParameters } from 'wagmi'
import { formatUnits } from 'viem'
import { useAccount, useBalance } from 'wagmi'
import { Address, formatUnits } from 'viem'

export function Balance() {
return (
Expand Down Expand Up @@ -37,7 +37,7 @@ export function AccountBalance() {
export function FindBalance() {
const [address, setAddress] = useState('')
const { data, isLoading, refetch } = useBalance({
address: address as UseBalanceParameters['address'],
address: address as Address,
})

const [value, setValue] = useState('')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FormEvent, useState } from 'react';
import { parseEther } from 'viem';
import { Address, parseEther } from 'viem';
import { useSendTransaction, useWaitForTransactionReceipt } from 'wagmi';
import { estimateGas } from '@wagmi/core'
import { config } from '../wagmi';
Expand All @@ -16,13 +16,13 @@ export function EstimateGas() {
if (to && value) {
try {
const gasEstimate = await estimateGas(config, {
to: to as `0x${string}`,
to: to as Address,
value: parseEther(value),
});
setEstimatedGas(gasEstimate);

const hash = await sendTransactionAsync({
to: to as `0x${string}`,
to: to as Address,
value: parseEther(value),
gas: gasEstimate,
});
Expand All @@ -37,7 +37,7 @@ export function EstimateGas() {

const [transactionHash, setTransactionHash] = useState<string | undefined>(undefined);
const { isFetching: isConfirming, isSuccess: isConfirmed, error: receiptError } = useWaitForTransactionReceipt({
hash: transactionHash as `0x${string}` | undefined,
hash: transactionHash as Address | undefined,
});

return (
Expand Down
Loading

0 comments on commit 8c22b8c

Please sign in to comment.