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

refactor: Remove unused code that was duplicated #1230

Merged
merged 5 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
12 changes: 6 additions & 6 deletions src/components/create-safe/logic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ import { Errors, logError } from '@/services/exceptions'
import { ErrorCode } from '@ethersproject/logger'
import { isWalletRejection } from '@/utils/wallets'

export type SafeCreationProps = {
owners: string[]
threshold: number
saltNonce: number
}

/**
* Prepare data for creating a Safe for the Core SDK
*/
Expand Down Expand Up @@ -122,12 +128,6 @@ export const getSafeCreationTxInfo = async (
}
}

export type SafeCreationProps = {
owners: string[]
threshold: number
saltNonce: number
}

export const estimateSafeCreationGas = async (
chain: ChainInfo,
provider: JsonRpcProvider,
Expand Down
2 changes: 1 addition & 1 deletion src/components/new-safe/CardStepper/useCardStepper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type StepRenderProps<TData> = {
setProgressColor?: Dispatch<SetStateAction<string>>
}

export type Step<TData> = {
type Step<TData> = {
title: string
subtitle: string
render: (
Expand Down
2 changes: 0 additions & 2 deletions src/components/new-safe/CreateSafe/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export type NewSafeFormData = {
name: string
threshold: number
owners: NamedAddress[]
mobileOwners: NamedAddress[]
saltNonce: number
safeAddress?: string
}
Expand Down Expand Up @@ -173,7 +172,6 @@ const CreateSafe = () => {

const initialData: NewSafeFormData = {
name: '',
mobileOwners: [] as NamedAddress[],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover from early implementation

owners: [defaultOwner],
threshold: 1,
saltNonce: Date.now(),
Expand Down
39 changes: 0 additions & 39 deletions src/components/new-safe/StepCard/index.tsx

This file was deleted.

31 changes: 0 additions & 31 deletions src/components/new-safe/StepCard/styles.module.css

This file was deleted.

4 changes: 2 additions & 2 deletions src/components/new-safe/steps/Step1/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ function CreateSafeStep1({
</Grid>
<Typography variant="body2" mt={2}>
By continuing, you agree to our{' '}
<Link href="https://gnosis-safe.io/terms" target="_blank" rel="noopener noreferrer" fontWeight={700}>
<Link href="https://safe.global/terms" target="_blank" rel="noopener noreferrer" fontWeight={700}>
terms of use
</Link>{' '}
and{' '}
<Link href="https://gnosis-safe.io/privacy" target="_blank" rel="noopener noreferrer" fontWeight={700}>
<Link href="https://safe.global/privacy" target="_blank" rel="noopener noreferrer" fontWeight={700}>
privacy policy
</Link>
.
Expand Down
2 changes: 0 additions & 2 deletions src/components/new-safe/steps/Step4/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ export const SAFE_PENDING_CREATION_STORAGE_KEY = 'pendingSafe'
export type PendingSafeData = NewSafeFormData & {
txHash?: string
tx?: PendingSafeTx
safeAddress?: string
saltNonce: number
Comment on lines -29 to -30
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are already part of NewSafeFormData so I removed them here

}

export const CreateSafeStatus = ({ setProgressColor }: StepRenderProps<NewSafeFormData>) => {
Expand Down
126 changes: 4 additions & 122 deletions src/components/new-safe/steps/Step4/logic/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
import type { Web3Provider, JsonRpcProvider } from '@ethersproject/providers'
import type Safe from '@gnosis.pm/safe-core-sdk'
import { SafeFactory, type DeploySafeProps } from '@gnosis.pm/safe-core-sdk'
import { createEthersAdapter } from '@/hooks/coreSDK/safeCoreSDK'
import type { ChainInfo, SafeInfo } from '@gnosis.pm/safe-react-gateway-sdk'
import { EMPTY_DATA, ZERO_ADDRESS } from '@gnosis.pm/safe-core-sdk/dist/src/utils/constants'
import {
getFallbackHandlerContractInstance,
getGnosisSafeContractInstance,
getProxyFactoryContractInstance,
} from '@/services/contracts/safeContracts'
import { LATEST_SAFE_VERSION } from '@/config/constants'
import type { PredictSafeProps } from '@gnosis.pm/safe-core-sdk/dist/src/safeFactory'
import type { ChainInfo } from '@gnosis.pm/safe-react-gateway-sdk'
import { getProxyFactoryContractInstance } from '@/services/contracts/safeContracts'
import type { ConnectedWallet } from '@/services/onboard'
import { BigNumber } from '@ethersproject/bignumber'
import { getSafeInfo } from '@gnosis.pm/safe-react-gateway-sdk'
import { backOff } from 'exponential-backoff'
import { SafeCreationStatus } from '@/components/new-safe/steps/Step4/useSafeCreation'
import { didRevert, type EthersError } from '@/utils/ethers-utils'
import { Errors, logError } from '@/services/exceptions'
Expand All @@ -29,77 +17,7 @@ import { SAFE_APPS_EVENTS, trackEvent } from '@/services/analytics'
import type { AppDispatch, AppThunk } from '@/store'
import { showNotification } from '@/store/notificationsSlice'
import { formatError } from '@/hooks/useTxNotifications'

/**
* Prepare data for creating a Safe for the Core SDK
*/
export const getSafeDeployProps = (
safeParams: SafeCreationProps,
callback: (txHash: string) => void,
chainId: string,
): PredictSafeProps & { callback: DeploySafeProps['callback'] } => {
const fallbackHandler = getFallbackHandlerContractInstance(chainId)

return {
safeAccountConfig: {
threshold: safeParams.threshold,
owners: safeParams.owners,
fallbackHandler: fallbackHandler.address,
},
safeDeploymentConfig: {
saltNonce: safeParams.saltNonce.toString(),
},
callback,
}
}

/**
* Create a Safe creation transaction via Core SDK and submits it to the wallet
*/
export const createNewSafe = async (ethersProvider: Web3Provider, props: DeploySafeProps): Promise<Safe> => {
const ethAdapter = createEthersAdapter(ethersProvider)

const safeFactory = await SafeFactory.create({ ethAdapter })
return safeFactory.deploySafe(props)
}

/**
* Compute the new counterfactual Safe address before it is actually created
*/
export const computeNewSafeAddress = async (ethersProvider: Web3Provider, props: PredictSafeProps): Promise<string> => {
const ethAdapter = createEthersAdapter(ethersProvider)

const safeFactory = await SafeFactory.create({ ethAdapter })
return safeFactory.predictSafeAddress(props)
}

/**
* Encode a Safe creation transaction NOT using the Core SDK because it doesn't support that
* This is used for gas estimation.
*/
export const encodeSafeCreationTx = ({
owners,
threshold,
saltNonce,
chain,
}: SafeCreationProps & { chain: ChainInfo }) => {
const safeContract = getGnosisSafeContractInstance(chain, LATEST_SAFE_VERSION)
const proxyContract = getProxyFactoryContractInstance(chain.chainId)
const fallbackHandlerContract = getFallbackHandlerContractInstance(chain.chainId)

const setupData = safeContract.encode('setup', [
owners,
threshold,
ZERO_ADDRESS,
EMPTY_DATA,
fallbackHandlerContract.address,
ZERO_ADDRESS,
'0',
ZERO_ADDRESS,
])

return proxyContract.encode('createProxyWithNonce', [safeContract.getAddress(), setupData, saltNonce])
}
import { encodeSafeCreationTx } from '@/components/create-safe/logic'

/**
* Encode a Safe creation tx in a way that we can store locally and monitor using _waitForTransaction
Expand All @@ -108,15 +26,14 @@ export const getSafeCreationTxInfo = async (
provider: Web3Provider,
params: NewSafeFormData,
chain: ChainInfo,
saltNonce: number,
wallet: ConnectedWallet,
): Promise<PendingSafeTx> => {
const proxyContract = getProxyFactoryContractInstance(chain.chainId)

const data = encodeSafeCreationTx({
owners: params.owners.map((owner) => owner.address),
threshold: params.threshold,
saltNonce,
saltNonce: params.saltNonce,
chain,
})

Expand All @@ -130,41 +47,6 @@ export const getSafeCreationTxInfo = async (
}
}

export type SafeCreationProps = {
owners: string[]
threshold: number
saltNonce: number
}

export const estimateSafeCreationGas = async (
chain: ChainInfo,
provider: JsonRpcProvider,
from: string,
safeParams: SafeCreationProps,
): Promise<BigNumber> => {
const proxyFactoryContract = getProxyFactoryContractInstance(chain.chainId)
const encodedSafeCreationTx = encodeSafeCreationTx({ ...safeParams, chain })

return provider.estimateGas({
from: from,
to: proxyFactoryContract.getAddress(),
data: encodedSafeCreationTx,
})
}

export const pollSafeInfo = async (chainId: string, safeAddress: string): Promise<SafeInfo> => {
// exponential delay between attempts for around 4 min
return backOff(() => getSafeInfo(chainId, safeAddress), {
startingDelay: 750,
maxDelay: 20000,
numOfAttempts: 19,
retry: (e) => {
console.info('waiting for client-gateway to provide safe information', e)
return true
},
})
}

export const handleSafeCreationError = (error: EthersError) => {
logError(Errors._800, error.message)

Expand Down
5 changes: 2 additions & 3 deletions src/components/new-safe/steps/Step4/useSafeCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ import type { PendingSafeData } from '@/components/new-safe/steps/Step4/index'
import type { PendingSafeTx } from '@/components/create-safe/types'
import {
checkSafeCreationTx,
createNewSafe,
getSafeCreationTxInfo,
getSafeDeployProps,
handleSafeCreationError,
SAFE_CREATION_ERROR_KEY,
showSafeCreationError,
} from '@/components/new-safe/steps/Step4/logic'
import { useAppDispatch } from '@/store'
import { closeByGroupKey } from '@/store/notificationsSlice'
import { CREATE_SAFE_EVENTS, trackEvent } from '@/services/analytics'
import { createNewSafe, getSafeDeployProps } from '@/components/create-safe/logic'

export enum SafeCreationStatus {
AWAITING,
Expand Down Expand Up @@ -62,7 +61,7 @@ export const useSafeCreation = (
dispatch(closeByGroupKey({ groupKey: SAFE_CREATION_ERROR_KEY }))

try {
const tx = await getSafeCreationTxInfo(provider, pendingSafe, chain, pendingSafe.saltNonce, wallet)
const tx = await getSafeCreationTxInfo(provider, pendingSafe, chain, wallet)

const safeParams = getSafeDeployProps(
{
Expand Down
4 changes: 2 additions & 2 deletions src/components/welcome/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Button, Divider, Grid, Paper, Typography } from '@mui/material'
import { useRouter } from 'next/router'
import { CREATE_SAFE_EVENTS, LOAD_SAFE_EVENTS } from '@/services/analytics/events/createLoadSafe'
import Track from '../common/Track'
import { AppRoutes } from '@/config/routes'

const NewSafe = () => {
const router = useRouter()
Expand All @@ -28,8 +29,7 @@ const NewSafe = () => {
for creating your new Safe.
</Typography>
<Track {...CREATE_SAFE_EVENTS.CREATE_BUTTON}>
{/* TODO: Revert this to /open before merging into dev */}
<Button variant="contained" onClick={() => router.push('/new-safe/create')}>
<Button variant="contained" onClick={() => router.push(AppRoutes.open)}>
+ Create new Safe
</Button>
</Track>
Expand Down
4 changes: 0 additions & 4 deletions src/pages/open.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ const Open: NextPage = () => {
}
}, [router, shouldUseNewCreation])

if (shouldUseNewCreation) {
return <></>
}
Comment on lines -20 to -22
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was causing hydration errors on my end but now there is a small loading flash which is also not ideal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we not render different links on the welcome page instead of replacing the route?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! I will try that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to work good. I also had to update it for the safe app landing pages since they also link to the safe creation but I think its fine cc @iamacook


return (
<main>
<Head>
Expand Down