Skip to content

Commit ad8ffde

Browse files
authored
Merge pull request #8 from brainbot-com/feature/max-button
Add a Max amount button to dump all
2 parents 58908ff + 01f83e5 commit ad8ffde

File tree

4 files changed

+101
-21
lines changed

4 files changed

+101
-21
lines changed

packages/website/components/dump-box/dump-box-layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const DumpBoxLayout = ({message, children, style}: Props) => {
1515

1616
{message}
1717
<div
18-
className={`max-w-md mx-auto flex flex-col items-center mt-10 rounded-lg
18+
className={`max-w-md mx-auto flex flex-col mt-10 rounded-lg
1919
p-4 shadow-md gap-y-4 ${style === 'in-progress' ? "bg-orange border border-4 border-black text-black" : "bg-rich-black text-white"} break-words`}
2020

2121
>

packages/website/components/dump-box/dump-form.tsx

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@ import { randomBytes } from '@ethersproject/random'
1111
import { keccak256 } from '@ethersproject/keccak256'
1212
import { ZERO_ADDRESS } from '../../config'
1313
import { commit } from '../../utils/eth-swap'
14-
import { InputRow } from '../input-row'
14+
import { CustomDecimalInput } from '../input-row'
1515
import { CurrencyBadge } from './currency-badge'
1616
// @ts-ignore
1717
import PowDumpSmallLogo from '../../public/assets/images/powdump_dapp_small_pow.png'
1818
// @ts-ignore
1919
import EthereumLogo from '../../public/assets/images/ethereum-logo.png'
2020
import { Button } from '../button'
2121
import dynamic from 'next/dynamic'
22-
import {isSwapSupportedOnChain} from "../../utils/helpers";
22+
import { isSwapSupportedOnChain } from '../../utils/helpers'
23+
import { useEthBalance } from '../../hooks/useEthBalance'
24+
import { CurrencyAmount } from '@uniswap/sdk-core'
25+
import { ExtendedEther } from '../../utils/ether'
26+
import { maxAmountSpend } from '../../utils/maxAmountSpend'
27+
import {SupportedChainId} from "../../constants/chains";
2328

2429
const PriceRow = dynamic(() => import('./price-row'), {
2530
ssr: false,
@@ -37,7 +42,7 @@ const errorsTranslations = {
3742
[NO_POS_AMOUNT_ENTERED as string]: 'Enter expected PoS amount',
3843
[TERMS_NOT_ACCEPTED as string]: 'Accept terms and conditions',
3944
[PRICE_CANT_BE_ZERO as string]: "Price can't be 0",
40-
[SWAP_NOT_SUPPORTED_ON_CHAIN as string]: "Chain not supported for starting a swap",
45+
[SWAP_NOT_SUPPORTED_ON_CHAIN as string]: 'Chain not supported for starting a swap',
4146
}
4247

4348
type Props = {
@@ -65,7 +70,7 @@ const validateForm = ({
6570
throw new Error(NO_WALLET_CONNECTED)
6671
}
6772

68-
if(!chainId || !isSwapSupportedOnChain(chainId)) {
73+
if (!chainId || !isSwapSupportedOnChain(chainId)) {
6974
throw new Error('SWAP_NOT_SUPPORTED_ON_CHAIN')
7075
}
7176

@@ -127,6 +132,7 @@ export function DumpForm() {
127132
const deleteTxSecrets = useStore(state => state.deleteTxSecrets)
128133
const setNotification = useStore(state => state.setNotification)
129134
const { account, provider, chainId } = useWeb3React<Web3Provider>()
135+
const { balance } = useEthBalance()
130136

131137
const [error, setError] = useState('')
132138

@@ -243,25 +249,66 @@ export function DumpForm() {
243249
}
244250
}
245251

252+
const currencyAmount = CurrencyAmount.fromRawAmount(
253+
// ChainId doesn't really matter here as the currency is ETH (no matter the chainID)
254+
ExtendedEther.onCreate(chainId ? chainId : SupportedChainId.MAINNET),
255+
// @ts-ignore
256+
balance
257+
)
258+
259+
const maxAmount = maxAmountSpend(currencyAmount)
260+
const maxAmountFormatted = maxAmount ? maxAmount.toExact() : "0"
261+
246262
return (
247263
<>
248-
<InputRow
249-
id="pow-amount"
250-
value={ethPoWAmount}
251-
onChangeInputValue={value => {
252-
setPoWAmount(value)
253-
updateFormValue('ethPoWAmount', value)
254-
}}
255-
disabled={!isSwapEnabled}
256-
type="text"
257-
placeholder="0.0"
258-
pattern={'^[0-9]*[.,]?[0-9]*$'}
259-
append={<CurrencyBadge icon={PowDumpSmallLogo} name={'PoW ETH'} />}
260-
/>
264+
<div>
265+
<div className={"relative"}>
266+
<div className="flex flex-col rounded-md bg-brown-orange pt-3 border border-1 border-transparent hover:border-gray focus-within:border-gray">
267+
268+
<div className={"flex"}>
269+
<div className={" flex-1 w-72"}>
270+
<CustomDecimalInput
271+
className={"appearance-none outline-none bg-brown-orange text-2xl text-white pl-4 group-hover:text-white"}
272+
id="pow-amount"
273+
value={ethPoWAmount}
274+
onChangeInputValue={value => {
275+
setPoWAmount(value)
276+
updateFormValue('ethPoWAmount', value)
277+
}}
278+
disabled={!isSwapEnabled}
279+
type="text"
280+
placeholder="0.0"
281+
pattern={'^[0-9]*[.,]?[0-9]*$'}
282+
/>
283+
</div>
284+
<div className={"mr-5 bg-brown-orange pl-2"}>
285+
<CurrencyBadge icon={PowDumpSmallLogo} name={'PoW ETH'} />
286+
</div>
287+
</div>
288+
289+
<div className={'flex flex-row justify-end items-center mr-5 text-gray text-sm mb-2'}>
290+
Balance: {currencyAmount.toFixed(5)}
291+
{maxAmountFormatted === ethPoWAmount ? null : (
292+
<button
293+
disabled={!isSwapEnabled}
294+
className={'bg-gray-500 border border-0 border-transparent rounded-sm px-2 text-gray hover:cursor-pointer hover:text-white hover:border-white hover:bg-rich-black-lighter ml-1'}
295+
onClick={() => {
296+
setPoWAmount(maxAmountFormatted)
297+
updateFormValue('ethPoWAmount', maxAmountFormatted)
298+
}}
299+
>
300+
Max
301+
</button>
302+
)}
303+
</div>
304+
</div>
305+
</div>
306+
</div>
307+
261308
<div className="w-full">
262309
<div className="flex flex-col rounded-md dark:bg-rich-black-lighter pt-2 border border-1 border-rich-black-lightest">
263310
<div className="flex flex-row my-2">
264-
<div className={`flex-1 mx-5 ${!isSwapEnabled ? "text-gray" : ""}`}>
311+
<div className={`flex-1 mx-5 ${!isSwapEnabled ? 'text-gray' : ''}`}>
265312
<PriceRow />
266313
<div className={'h-px bg-rich-black-lightest my-2'}></div>
267314

@@ -300,7 +347,7 @@ export function DumpForm() {
300347
updateFormValue('termsAccepted', !termsAccepted)
301348
}}
302349
/>{' '}
303-
<span className={!isSwapEnabled ? "text-gray" : ""}>I understand and accept the Terms & Conditions</span>
350+
<span className={!isSwapEnabled ? 'text-gray' : ''}>I understand and accept the Terms & Conditions</span>
304351
</label>
305352
</div>
306353

packages/website/components/layout.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type {ReactNode} from "react";
2-
import styles from "../styles/Home.module.css";
32
import Head from "next/head";
43
import {NavBar} from "./nav-bar";
54
import Notification from "./notifications/notifications";
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Code copied from uniswap/interface
3+
* https://github.com/Uniswap/interface/blob/main/src/utils/maxAmountSpend.ts
4+
*
5+
* Modified to keep 3 times .01 ETH - .01 ETH for tx fee and .02 ETH if the tx fails and the user needs to refund
6+
*/
7+
8+
import { Currency, CurrencyAmount } from '@uniswap/sdk-core'
9+
import JSBI from 'jsbi'
10+
11+
// keep .01 ETH for gas for the transaction itself, and keep .01 ETH for eventual gas for a refund and .01 ETH for the actual refund tx cost
12+
const MIN_NATIVE_CURRENCY_FOR_GAS: JSBI = JSBI.multiply(
13+
JSBI.exponentiate(JSBI.BigInt(10), JSBI.BigInt(16)),
14+
JSBI.BigInt(3)
15+
) // .03 ETH
16+
17+
/**
18+
* Given some token amount, return the max that can be spent of it
19+
* @param currencyAmount to return max of
20+
*/
21+
export function maxAmountSpend(currencyAmount?: CurrencyAmount<Currency>): CurrencyAmount<Currency> | undefined {
22+
if (!currencyAmount) return undefined
23+
if (currencyAmount.currency.isNative) {
24+
if (JSBI.greaterThan(currencyAmount.quotient, MIN_NATIVE_CURRENCY_FOR_GAS)) {
25+
return CurrencyAmount.fromRawAmount(
26+
currencyAmount.currency,
27+
JSBI.subtract(currencyAmount.quotient, MIN_NATIVE_CURRENCY_FOR_GAS)
28+
)
29+
} else {
30+
return CurrencyAmount.fromRawAmount(currencyAmount.currency, JSBI.BigInt(0))
31+
}
32+
}
33+
return currencyAmount
34+
}

0 commit comments

Comments
 (0)