forked from smlxl/evm.codes
-
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.
feat: Add dynamic gas fee calculations (smlxl#76)
- Add documentation for dynamic gas cost and gas refunds - Add a gas estimator to compute fees based on Inputs and Common params - Change "Static gas" column to "Minimum gas" to avoid misleading 0 cost - Use Input stack variable names more consistently in the doc - Extend opcodes.json with dynamicFee definition of user inputs - Add support for opcode specific dynamic docs with variables from Common gasPrices param - Show dynamic gas fee tooltip only when it's active in the selected fork UI: - Add focus state on the input fields Misc: - Upgrade to NextJS 12 with Webpack 5 - Get rid of browserify with global ethereumjs objects - Simplify lookup of table's opcodes object - Fix saving of selected fork setting value - Fix setting common chain and hardfork Closes smlxl#57 smlxl#67 smlxl#85
- Loading branch information
Tair Asim
authored
Dec 24, 2021
1 parent
a41de7f
commit d96b26d
Showing
215 changed files
with
3,639 additions
and
1,823 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
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
# misc | ||
.DS_Store | ||
*.pem | ||
tsconfig.tsbuildinfo | ||
|
||
# debug | ||
npm-debug.log* | ||
|
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
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 |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { useEffect, useState, useContext } from 'react' | ||
|
||
import debounce from 'lodash.debounce' | ||
import { IOpcode } from 'types' | ||
|
||
import { EthereumContext } from 'context/ethereumContext' | ||
|
||
import { calculateDynamicFee } from 'util/gas' | ||
|
||
import { Input, Radio, Icon } from 'components/ui' | ||
import { H2 } from 'components/ui/Doc' | ||
|
||
const debounceTimeout = 100 // ms | ||
|
||
type Props = { | ||
opcode: IOpcode | ||
fork: string | ||
} | ||
|
||
type InputValue = { | ||
[name: string]: string | undefined | ||
} | ||
|
||
const DynamicFee = ({ opcode, fork }: Props) => { | ||
const { dynamicFee } = opcode | ||
const forkInputs = dynamicFee ? dynamicFee[fork].inputs : null | ||
|
||
const { common } = useContext(EthereumContext) | ||
const [inputs, setInputs] = useState<InputValue | undefined>() | ||
const [result, setResult] = useState('0') | ||
|
||
const handleCompute = debounce((inputs) => { | ||
if (common) { | ||
try { | ||
setResult(calculateDynamicFee(opcode, common, inputs)) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
} | ||
}, debounceTimeout) | ||
|
||
// Initialize inputs with default keys & values | ||
useEffect(() => { | ||
const inputValues: InputValue = {} | ||
Object.keys(forkInputs || []).map((key: string) => { | ||
inputValues[key] = '0' // false for boolean, zero for numbers | ||
}) | ||
setInputs(inputValues) | ||
handleCompute(inputValues) | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [dynamicFee]) | ||
|
||
const handleChange = (key: string, value: string) => { | ||
const newInputs = { | ||
...inputs, | ||
[key]: value, | ||
} | ||
setInputs(newInputs) | ||
handleCompute(newInputs) | ||
} | ||
|
||
if (!forkInputs || !inputs) { | ||
return null | ||
} | ||
|
||
return ( | ||
<div className="md:w-96"> | ||
<div className="flex justify-between items-center"> | ||
<H2>Estimate your gas cost</H2> | ||
</div> | ||
|
||
<div className="bg-indigo-100 dark:bg-black-500 p-4 rounded shadow"> | ||
{Object.keys(forkInputs).map((key: string) => { | ||
const input = forkInputs[key] | ||
|
||
return ( | ||
<div key={key}> | ||
<label className="block text-sm text-gray-500 mb-1" htmlFor={key}> | ||
{input.label} | ||
</label> | ||
|
||
{input.type === 'number' && ( | ||
<Input | ||
type="number" | ||
min={0} | ||
max={1000000000000} | ||
name={key} | ||
value={inputs[key]} | ||
onChange={(e) => handleChange(key, e.target.value)} | ||
className="bg-white bg-opacity-75 dark:bg-black-400 mb-4 text-sm font-mono" | ||
/> | ||
)} | ||
|
||
{input.type === 'boolean' && ( | ||
<div className="mb-4"> | ||
<Radio | ||
text="Yes" | ||
value={'1'} | ||
isChecked={inputs[key] === '1'} | ||
onChange={() => handleChange(key, '1')} | ||
/> | ||
<Radio | ||
text="No" | ||
value={'0'} | ||
isChecked={inputs[key] === '0'} | ||
onChange={() => handleChange(key, '0')} | ||
/> | ||
</div> | ||
)} | ||
</div> | ||
) | ||
})} | ||
|
||
<div className="flex items-center pt-2"> | ||
<Icon name="gas-station-fill" className="text-indigo-500 mr-2" /> | ||
Static gas + dynamic gas = {result} | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default DynamicFee |
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
Oops, something went wrong.