forked from safe-global/safe-wallet-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Counterfactual] Safe creation (safe-global#3180)
* feat: Create counterfactual 1/1 safes * fix: Add feature flag * fix: Lint issues * fix: Use incremental saltNonce for all safe creations * fix: Replace useCounterfactualBalance hook with get function and write tests * refactor: Move creation logic out of Review component * fix: useLoadBalance check for undefined value * fix: Extract saltNonce, safeAddress calculation into a hook * refactor: Rename redux slice * fix: Show error message in case saltNonce can't be retrieved * fix: Disable create button if deploy props are loading * fix: Revert hook change and update comment
- Loading branch information
1 parent
e881ab1
commit 74ed3ce
Showing
32 changed files
with
637 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import * as creationUtils from '@/components/new-safe/create/logic/index' | ||
import { getAvailableSaltNonce } from '@/components/new-safe/create/logic/utils' | ||
import * as web3Utils from '@/hooks/wallets/web3' | ||
import { faker } from '@faker-js/faker' | ||
import type { DeploySafeProps } from '@safe-global/protocol-kit' | ||
import { BrowserProvider, type Eip1193Provider } from 'ethers' | ||
|
||
describe('getAvailableSaltNonce', () => { | ||
jest.spyOn(creationUtils, 'computeNewSafeAddress').mockReturnValue(Promise.resolve(faker.finance.ethereumAddress())) | ||
|
||
let mockProvider: BrowserProvider | ||
let mockDeployProps: DeploySafeProps | ||
|
||
beforeAll(() => { | ||
mockProvider = new BrowserProvider(jest.fn() as unknown as Eip1193Provider) | ||
mockDeployProps = { | ||
safeAccountConfig: { | ||
threshold: 1, | ||
owners: [faker.finance.ethereumAddress()], | ||
fallbackHandler: faker.finance.ethereumAddress(), | ||
}, | ||
} | ||
}) | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it('should return initial nonce if no contract is deployed to the computed address', async () => { | ||
jest.spyOn(web3Utils, 'isSmartContract').mockReturnValue(Promise.resolve(false)) | ||
const initialNonce = faker.string.numeric() | ||
|
||
const result = await getAvailableSaltNonce(mockProvider, { ...mockDeployProps, saltNonce: initialNonce }) | ||
|
||
expect(result).toEqual(initialNonce) | ||
}) | ||
|
||
it('should return an increased nonce if a contract is deployed to the computed address', async () => { | ||
jest.spyOn(web3Utils, 'isSmartContract').mockReturnValueOnce(Promise.resolve(true)) | ||
const initialNonce = faker.string.numeric() | ||
|
||
const result = await getAvailableSaltNonce(mockProvider, { ...mockDeployProps, saltNonce: initialNonce }) | ||
|
||
jest.spyOn(web3Utils, 'isSmartContract').mockReturnValueOnce(Promise.resolve(false)) | ||
|
||
const increasedNonce = (Number(initialNonce) + 1).toString() | ||
|
||
expect(result).toEqual(increasedNonce) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { computeNewSafeAddress } from '@/components/new-safe/create/logic/index' | ||
import { isSmartContract } from '@/hooks/wallets/web3' | ||
import type { DeploySafeProps } from '@safe-global/protocol-kit' | ||
import type { BrowserProvider } from 'ethers' | ||
|
||
export const getAvailableSaltNonce = async (provider: BrowserProvider, props: DeploySafeProps): Promise<string> => { | ||
const safeAddress = await computeNewSafeAddress(provider, props) | ||
const isContractDeployed = await isSmartContract(provider, safeAddress) | ||
|
||
// Safe is already deployed so we try the next saltNonce | ||
if (isContractDeployed) { | ||
return getAvailableSaltNonce(provider, { ...props, saltNonce: (Number(props.saltNonce) + 1).toString() }) | ||
} | ||
|
||
// We know that there will be a saltNonce but the type has it as optional | ||
return props.saltNonce! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,7 @@ | |
text-decoration: line-through; | ||
color: var(--color-text-secondary); | ||
} | ||
|
||
.errorMessage { | ||
margin-top: 0; | ||
} |
Oops, something went wrong.