Skip to content

Commit 687b413

Browse files
VeleniralfetopitoW3stside
authored
Confirmmodal for txes (gnosis#1252)
* create wrapper for SubmitTx * create message for Trade tx * wrap Sumit Limit Order * reevalidate on price fill from PriceEstimation * don't mess with disabled SubmitButton * don't show modal for invalid form * rerrange prices * don't show 'All tokens disabled' while still fetching tokens * Low volume warning (gnosis#1268) * add usePriceEstimationInOwl hook * add low volume warning * pass networkId to TxMessage * WalletApi.getGasPrice * useGasPrice hook * dynamic gasPrice in lowVolume warning * move minFee calculation to utils * add useWethPriceInOwl hook * use ethPriceInOwl in calcMinTradableAmountInOwl * useWETHPriceInOwl in TxMessage * allow to specify gasPriceLevel for gas-station * use gasPriceLevel: 'fast' like the solver * Update src/components/TradeWidget/TxMessage.tsx Co-authored-by: Leandro Boscariol <[email protected]> * no CONST * console.log -> logDebug * fix typo * braces Co-authored-by: Leandro Boscariol <[email protected]> * Styling tx conf modal (gnosis#1262) * prelim styling save * styling * message text * pr review comments 1. added how order works 2. tooltips * CSS * fixed tooltip/anchor style Co-authored-by: Velenir <[email protected]> * fix Alert align style * add link to docs (provisional) * Style low volume warning - TX Confirmation modal (gnosis#1310) * styles * image fix * wording Co-authored-by: Leandro Boscariol <[email protected]> Co-authored-by: David <[email protected]>
1 parent 659a618 commit 687b413

File tree

11 files changed

+543
-17
lines changed

11 files changed

+543
-17
lines changed

src/api/gasStation.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ const GAS_STATIONS = {
55
4: 'https://safe-relay.staging.gnosisdev.com/api/v1/gas-station/',
66
}
77

8-
const GAS_PRICE_LEVEL: Exclude<keyof GasStationResponse, 'lastUpdate'> = 'standard'
8+
export type GasPriceLevel = Exclude<keyof GasStationResponse, 'lastUpdate'>
9+
10+
const GAS_PRICE_LEVEL: GasPriceLevel = 'standard'
911

1012
let cacheKey = ''
1113
let cachedGasPrice: string
@@ -26,7 +28,9 @@ interface GasStationResponse {
2628
fastest: string
2729
}
2830

29-
const fetchGasPriceFactory = (walletApi: WalletApi) => async (): Promise<string | undefined> => {
31+
const fetchGasPriceFactory = (walletApi: WalletApi) => async (
32+
gasPriceLevel: GasPriceLevel = GAS_PRICE_LEVEL,
33+
): Promise<string | undefined> => {
3034
const { blockchainState } = walletApi
3135

3236
if (!blockchainState) return undefined
@@ -47,7 +51,7 @@ const fetchGasPriceFactory = (walletApi: WalletApi) => async (): Promise<string
4751
const response = await fetch(gasStationURL)
4852
const json: GasStationResponse = await response.json()
4953

50-
const gasPrice = json[GAS_PRICE_LEVEL]
54+
const gasPrice = json[gasPriceLevel]
5155
if (gasPrice) {
5256
cacheKey = key
5357
cachedGasPrice = gasPrice

src/api/wallet/WalletApi.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { logDebug, toBN, txDataEncoder, generateWCOptions } from 'utils'
1313
import { subscribeToWeb3Event } from './subscriptionHelpers'
1414
import { getMatchingScreenSize, subscribeToScreenSizeChange } from 'utils/mediaQueries'
1515
import { composeProvider } from './composeProvider'
16-
import fetchGasPriceFactory from 'api/gasStation'
16+
import fetchGasPriceFactory, { GasPriceLevel } from 'api/gasStation'
1717
import { earmarkTxData } from 'api/earmark'
1818
import { Provider, isMetamaskProvider, isWalletConnectProvider, ProviderRpcError } from './providerUtils'
1919

@@ -53,6 +53,7 @@ export interface WalletApi {
5353
getProviderInfo(): ProviderInfo
5454
blockchainState: BlockchainUpdatePrompt
5555
userPrintAsync: Promise<string>
56+
getGasPrice(gasPriceLevel?: GasPriceLevel): Promise<number | null>
5657
}
5758

5859
export interface WalletInfo {
@@ -249,6 +250,7 @@ export class WalletApiImpl implements WalletApi {
249250
public blockchainState: BlockchainUpdatePrompt
250251

251252
private _unsubscribe: Command
253+
private _fetchGasPrice: ReturnType<typeof fetchGasPriceFactory> = async () => undefined
252254

253255
public constructor(web3: Web3) {
254256
this._listeners = []
@@ -349,6 +351,9 @@ export class WalletApiImpl implements WalletApi {
349351
closeOpenWebSocketConnection(this._web3)
350352

351353
const fetchGasPrice = fetchGasPriceFactory(this)
354+
355+
this._fetchGasPrice = fetchGasPrice
356+
352357
const earmarkingFunction = async (data?: string): Promise<string> => earmarkTxData(data, await this.userPrintAsync)
353358

354359
const composedProvider = composeProvider(provider, { fetchGasPrice, earmarkTxData: earmarkingFunction })
@@ -457,6 +462,26 @@ export class WalletApiImpl implements WalletApi {
457462
await this._notifyListeners()
458463
}
459464

465+
public async getGasPrice(gasPriceLevel?: GasPriceLevel): Promise<number | null> {
466+
// this never errors
467+
// returns undefined if unable to fetch
468+
let gasPrice = await this._fetchGasPrice(gasPriceLevel)
469+
470+
if (gasPrice) return +gasPrice
471+
try {
472+
// fallback to gasPrice from provider
473+
// {"jsonrpc":"2.0","method":"eth_gasPrice"} call
474+
gasPrice = await this._web3.eth.getGasPrice()
475+
476+
if (gasPrice) return +gasPrice
477+
} catch (error) {
478+
console.error('Error fetching gas price', error)
479+
}
480+
481+
// unable to fetch
482+
return null
483+
}
484+
460485
public async getAddress(): Promise<string> {
461486
assert(await this._connected, 'The wallet is not connected')
462487

src/api/wallet/WalletApiMock.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ export class WalletApiMock implements WalletApi {
5858
return this.connect()
5959
}
6060

61+
public async getGasPrice(): Promise<null> {
62+
return null
63+
}
64+
6165
public async getAddress(): Promise<string> {
6266
assert(this._connected, 'The wallet is not connected')
6367

src/components/DepositWidget/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ interface BalanceDisplayProps extends TokenLocalState {
181181
claimable: boolean,
182182
onTxHash: (hash: string) => void,
183183
): Promise<void>
184+
hasTokensToShow?: boolean
184185
}
185186

186187
const customFilterFnFactory = (searchTxt: string) => (token: TokenBalanceDetails): boolean => {
@@ -211,6 +212,7 @@ const BalancesDisplay: React.FC<BalanceDisplayProps> = ({
211212
enabling,
212213
enabled,
213214
requestWithdrawConfirmation,
215+
hasTokensToShow = false,
214216
}) => {
215217
const windowSpecs = useWindowSizes()
216218

@@ -315,7 +317,7 @@ const BalancesDisplay: React.FC<BalanceDisplayProps> = ({
315317
{...windowSpecs}
316318
/>
317319
))
318-
) : balances.length === 0 ? (
320+
) : balances.length === 0 && hasTokensToShow ? (
319321
<NoTokensMessage>
320322
<td>
321323
All tokens disabled. Enable some in <a onClick={toggleModal}>Manage Tokens</a>
@@ -401,6 +403,7 @@ const DepositWidget: React.FC = () => {
401403
<BalancesDisplayMemoed
402404
ethBalance={ethBalance}
403405
balances={balances}
406+
hasTokensToShow={allBalances.length > 0}
404407
error={error}
405408
{...restActions}
406409
requestWithdrawConfirmation={requestWithdrawConfirmation}

src/components/TradeWidget/PriceEstimations.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ interface PriceEstimationsProps {
7474
export const PriceEstimations: React.FC<PriceEstimationsProps> = props => {
7575
const { amount, isPriceInverted, priceInputId, priceInverseInputId } = props
7676

77-
const { setValue } = useFormContext<TradeFormData>()
77+
const { setValue, triggerValidation } = useFormContext<TradeFormData>()
7878

7979
const updatePrices = useCallback(
8080
(price: string, invertedPrice) => (): void => {
@@ -85,8 +85,9 @@ export const PriceEstimations: React.FC<PriceEstimationsProps> = props => {
8585
setValue(priceInverseInputId, price)
8686
setValue(priceInputId, invertedPrice)
8787
}
88+
triggerValidation()
8889
},
89-
[isPriceInverted, setValue, priceInputId, priceInverseInputId],
90+
[isPriceInverted, triggerValidation, setValue, priceInputId, priceInverseInputId],
9091
)
9192

9293
return (

0 commit comments

Comments
 (0)