-
Notifications
You must be signed in to change notification settings - Fork 20
Add contract address calculator based on from and nonce #95
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
Open
olehmisar
wants to merge
4
commits into
dethcrypto:main
Choose a base branch
from
olehmisar:contract-address-calculator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,106 @@ | ||
| import React, { ReactElement, useState } from 'react'; | ||
|
|
||
| import { CopyableConversionInput } from '../../src/components/CopyableConversionInput'; | ||
| import { CalculatorIcon } from '../../src/components/icons/CalculatorIcon'; | ||
| import { Button } from '../../src/components/lib/Button'; | ||
| import { Header } from '../../src/components/lib/Header'; | ||
| import { NodeBlock } from '../../src/components/lib/NodeBlock'; | ||
| import { ToolContainer } from '../../src/components/ToolContainer'; | ||
| import { getContractAddress } from '../../src/lib/contractAddress'; | ||
| import { handleChangeValidated } from '../../src/misc/handleChangeValidated'; | ||
| import { WithError } from '../../src/misc/types'; | ||
| import { addressValidator } from '../../src/misc/validation/validators/addressValidator'; | ||
| import { numberValidator } from '../../src/misc/validation/validators/numberValidator'; | ||
|
|
||
| export default function ContractAddressCalculator(): ReactElement { | ||
| const [fromAddress, setFromAddress] = useState<WithError<string>>({ | ||
| value: '', | ||
| }); | ||
| const [nonce, setNonce] = useState<WithError<string>>({ | ||
| value: '', | ||
| }); | ||
|
|
||
| const [calculatedContractAddress, setCalculatedContractAddress] = useState< | ||
| string | undefined | ||
| >(); | ||
|
|
||
| function flushResults(): void { | ||
| setCalculatedContractAddress(undefined); | ||
| } | ||
|
|
||
| function handleChangeFromAddress(newValue: string): void { | ||
| handleChangeValidated({ | ||
| newValue, | ||
| validateFn: (newValue) => addressValidator(newValue), | ||
| setState: setFromAddress, | ||
| flushFn: flushResults, | ||
| }); | ||
| } | ||
|
|
||
| function handleChangeNonce(newValue: string): void { | ||
| handleChangeValidated({ | ||
| newValue, | ||
| validateFn: (newValue) => numberValidator(newValue), | ||
| setState: setNonce, | ||
| flushFn: flushResults, | ||
| }); | ||
| } | ||
|
|
||
| function handleCalculateContractAddress(): void { | ||
| try { | ||
| setCalculatedContractAddress( | ||
| getContractAddress({ | ||
| from: fromAddress.value, | ||
| nonce: nonce.value, | ||
| }), | ||
| ); | ||
| } catch { | ||
| flushResults(); | ||
| } | ||
| } | ||
|
|
||
| const calculateButtonIsDisabled = Boolean( | ||
| !fromAddress.value || fromAddress.error || !nonce.value || nonce.error, | ||
| ); | ||
|
|
||
| return ( | ||
| <ToolContainer> | ||
| <form className="flex w-full flex-col items-start sm:mr-auto sm:items-center md:items-start"> | ||
| <Header | ||
| icon={<CalculatorIcon height={24} width={24} />} | ||
| text={['Calculators', 'Contract Address Calculator']} | ||
| /> | ||
| <section className="flex w-full flex-col gap-5"> | ||
| <CopyableConversionInput | ||
| name="From" | ||
| value={fromAddress.value} | ||
| error={fromAddress.error} | ||
| placeholder="0x0.." | ||
| onChange={(event) => handleChangeFromAddress(event.target.value)} | ||
| /> | ||
| <CopyableConversionInput | ||
| name="Nonce" | ||
| value={nonce.value} | ||
| error={nonce.error} | ||
| onChange={(event) => handleChangeNonce(event.target.value)} | ||
| /> | ||
| </section> | ||
| </form> | ||
| <Button | ||
| title="Calculate" | ||
| className="mt-6" | ||
| disabled={calculateButtonIsDisabled} | ||
| onClick={handleCalculateContractAddress} | ||
| > | ||
| Calculate | ||
| </Button> | ||
| {calculatedContractAddress && ( | ||
| <section className="mt-10 rounded-md border border-gray-600 bg-gray-900 p-8"> | ||
| <NodeBlock toggle={false} str={calculatedContractAddress}> | ||
| Contract address: | ||
| </NodeBlock> | ||
| </section> | ||
| )} | ||
| </ToolContainer> | ||
| ); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,14 @@ | ||
| import { expect } from 'earljs'; | ||
|
|
||
| import { getContractAddress } from './contractAddress'; | ||
|
|
||
| describe(getContractAddress.name, () => { | ||
| it('calculates a contract address based on a from address and a nonce', () => { | ||
| expect( | ||
| getContractAddress({ | ||
| from: '0x9C33eaCc2F50E39940D3AfaF2c7B8246B681A374', | ||
| nonce: '0', | ||
| }), | ||
| ).toEqual('0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f'); | ||
| }); | ||
| }); |
This file contains hidden or 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,8 @@ | ||
| import { getContractAddress as getContractAddressImpl } from '@ethersproject/address'; | ||
|
|
||
| export function getContractAddress(params: { | ||
| from: string; | ||
| nonce: string; | ||
| }): string { | ||
| return getContractAddressImpl(params); | ||
| } |
This file contains hidden or 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,5 @@ | ||
| import { z } from 'zod'; | ||
|
|
||
| export const addressSchema = z.string().regex(/^0x[0-9a-fA-F]{40}$/, { | ||
| message: 'The value must be a valid address', | ||
| }); |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,14 +1,10 @@ | ||
| import { z } from 'zod'; | ||
|
|
||
| export const decimalSchema = z | ||
| .number() | ||
| .or( | ||
| z | ||
| .string() | ||
| .regex(/^\d*$/, { | ||
| message: | ||
| 'The value must be a hexadecimal number, 0x-prefix is required', | ||
| }) | ||
| .transform(Number), | ||
| ) | ||
| .refine((n) => n >= 0 && n <= 128); | ||
| export const decimalSchema = z.number().or( | ||
| z | ||
| .string() | ||
| .regex(/^\d*$/, { | ||
| message: 'The value must be a decimal number', | ||
| }) | ||
| .transform(Number), | ||
| ); |
This file contains hidden or 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,9 @@ | ||
| import { zodResultMessage } from '../../../../src/misc/zodResultMessage'; | ||
| import { addressSchema } from '../schemas/addressSchema'; | ||
| import { ValidatorResult } from './result'; | ||
|
|
||
| export function addressValidator(newValue: string): ValidatorResult { | ||
| const validated = addressSchema.safeParse(newValue); | ||
| if (validated.success) return { success: true }; | ||
| else return { success: false, error: zodResultMessage(validated) }; | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,9 +1,15 @@ | ||
| import { identity } from 'lodash'; | ||
| import { ZodTypeAny } from 'zod'; | ||
|
|
||
| import { zodResultMessage } from '../../../../src/misc/zodResultMessage'; | ||
| import { decimalSchema } from '../schemas/decimalSchema'; | ||
| import { ValidatorResult } from './result'; | ||
|
|
||
| export function numberValidator(newValue: string): ValidatorResult { | ||
| const validated = decimalSchema.safeParse(newValue); | ||
| export function numberValidator( | ||
| newValue: string, | ||
| enhanceSchema: (schema: ZodTypeAny) => ZodTypeAny = identity, | ||
| ): ValidatorResult { | ||
| const validated = enhanceSchema(decimalSchema).safeParse(newValue); | ||
| if (validated.success) return { success: true }; | ||
| else return { success: false, error: zodResultMessage(validated) }; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.