Skip to content

Commit

Permalink
fix: increase slippage, gas buffer, add max gas
Browse files Browse the repository at this point in the history
  • Loading branch information
paulvaden authored Mar 14, 2023
1 parent 83f42ce commit fc8a448
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
8 changes: 2 additions & 6 deletions app/src/constants/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Option, Quote, Strike } from '@lyrafinance/lyra-js'
import { BigNumber } from 'ethers'

import { ONE_BN } from './bn'

Expand All @@ -11,11 +10,8 @@ export const ERROR_DIST_TO_LIQUIDATION_PRICE = 0.015
export const WARNING_DIST_TO_LIQUIDATION_PRICE = 0.025
export const MAX_UTILIZATION = 0.975

export const MIN_GAS_LIMIT = BigNumber.from(22000)
export const GAS_BUFFER = 1.1 // 10%

export const ITERATIONS = 1
export const SLIPPAGE = 0.5 / 100 // 0.5%
export const ITERATIONS = 3
export const SLIPPAGE = 2 / 100 // 2%

export type StrikeQuotesNullable = {
callBid: Quote | null
Expand Down
22 changes: 22 additions & 0 deletions app/src/constants/networks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { Network as LyraNetwork } from '@lyrafinance/lyra-js'
import { Network as LyraNetwork } from '@lyrafinance/lyra-js'
import { BigNumber } from 'ethers'
import nullthrows from 'nullthrows'

import filterNulls from '../utils/filterNulls'
Expand Down Expand Up @@ -30,6 +31,9 @@ export type NetworkConfig = {
readRpcUrls: string[]
blockExplorerUrl: string
iconUrls: string[]
gasBuffer: number
maxGas: BigNumber
minGas: BigNumber
faucetUrl?: string
nativeBridgeUrl?: string
fastBridgeUrl?: string
Expand Down Expand Up @@ -58,6 +62,9 @@ export const NETWORK_CONFIGS: Record<AppChain, NetworkConfig> = {
iconUrls: ['https://optimism.io/images/metamask_icon.svg', 'https://optimism.io/images/metamask_icon.png'],
nativeBridgeUrl: 'https://app.optimism.io/bridge/withdraw',
fastBridgeUrl: 'https://cbridge.celer.network/10/1/LYRA',
gasBuffer: 1.5,
minGas: BigNumber.from(22000),
maxGas: BigNumber.from(15000000),
},
[AppChain.OptimismGoerli]: {
name: 'Optimistic Ethereum (Goerli)',
Expand All @@ -71,6 +78,9 @@ export const NETWORK_CONFIGS: Record<AppChain, NetworkConfig> = {
faucetUrl: 'https://faucet.paradigm.xyz/',
nativeBridgeUrl: 'https://app.optimism.io/bridge/withdraw',
fastBridgeUrl: 'https://cbridge.celer.network/10/1/LYRA',
gasBuffer: 1.5,
minGas: BigNumber.from(22000),
maxGas: BigNumber.from(15000000),
},
[AppChain.Arbitrum]: {
name: 'Arbitrum One',
Expand All @@ -86,6 +96,9 @@ export const NETWORK_CONFIGS: Record<AppChain, NetworkConfig> = {
iconUrls: ['https://optimism.io/images/metamask_icon.svg', 'https://optimism.io/images/metamask_icon.png'],
nativeBridgeUrl: 'https://bridge.arbitrum.io/?l2ChainId=42161',
fastBridgeUrl: 'https://cbridge.celer.network/42161/1/LYRA',
gasBuffer: 1.5,
minGas: BigNumber.from(22000),
maxGas: BigNumber.from(30000000),
},
[AppChain.ArbitrumGoerli]: {
name: 'Arbitrum Goerli',
Expand All @@ -99,6 +112,9 @@ export const NETWORK_CONFIGS: Record<AppChain, NetworkConfig> = {
faucetUrl: 'https://faucet.quicknode.com/arbitrum/goerli',
nativeBridgeUrl: 'https://bridge.arbitrum.io/?l2ChainId=42161',
fastBridgeUrl: 'https://cbridge.celer.network/42161/1/LYRA',
gasBuffer: 1.5,
minGas: BigNumber.from(22000),
maxGas: BigNumber.from(30000000),
},
[AppChain.Ethereum]: {
name: 'Ethereum Mainnet',
Expand All @@ -112,6 +128,9 @@ export const NETWORK_CONFIGS: Record<AppChain, NetworkConfig> = {
]),
blockExplorerUrl: 'https://etherscan.io/',
iconUrls: [],
gasBuffer: 1.2,
minGas: BigNumber.from(22000),
maxGas: BigNumber.from(15000000),
},
[AppChain.EthereumGoerli]: {
name: 'Ethereum Goerli',
Expand All @@ -122,5 +141,8 @@ export const NETWORK_CONFIGS: Record<AppChain, NetworkConfig> = {
readRpcUrls: [`https://goerli.infura.io/v3/${INFURA_PROJECT_ID}`],
blockExplorerUrl: 'https://goerli.etherscan.io/',
iconUrls: [],
gasBuffer: 1.2,
minGas: BigNumber.from(22000),
maxGas: BigNumber.from(15000000),
},
}
21 changes: 12 additions & 9 deletions app/src/hooks/account/useTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { closeToast, createPendingToast, updatePendingToast, updateToast } from
import { ContractReceipt, PopulatedTransaction } from 'ethers'
import { useCallback } from 'react'

import { GAS_BUFFER, MIN_GAS_LIMIT } from '@/app/constants/contracts'
import { AppNetwork, Network } from '@/app/constants/networks'
import { TransactionType } from '@/app/constants/screen'
import { getChainIdForNetwork } from '@/app/utils/getChainIdForNetwork'
import getExplorerUrl from '@/app/utils/getExplorerUrl'
import getNetworkConfig from '@/app/utils/getNetworkConfig'
import getProvider from '@/app/utils/getProvider'
import logError from '@/app/utils/logError'
import postTransactionError from '@/app/utils/postTransactionError'
Expand Down Expand Up @@ -138,12 +138,17 @@ const getTimeout = (network: Network): number => {
}
}

async function getGasLimit(provider: JsonRpcProvider, tx: PopulatedTransaction) {
let gasLimit = await provider.estimateGas(tx)
if (gasLimit.lt(MIN_GAS_LIMIT)) {
gasLimit = MIN_GAS_LIMIT
async function getGasLimit(network: Network, provider: JsonRpcProvider, tx: PopulatedTransaction) {
const config = getNetworkConfig(network)
// add buffer to est. gas limit if not hardcoded
const gasLimit = tx.gasLimit ?? (await provider.estimateGas(tx)).mul(10000 * Math.max(1, config.gasBuffer)).div(10000)
if (gasLimit.lt(config.minGas)) {
return config.minGas
}
return gasLimit.mul(10000 * GAS_BUFFER).div(10000) // Add % buffer
if (gasLimit.gt(config.maxGas)) {
return config.maxGas
}
return gasLimit
}

export default function useTransaction(
Expand Down Expand Up @@ -197,9 +202,7 @@ export default function useTransaction(
}

try {
if (tx.gasLimit) {
tx.gasLimit = await getGasLimit(provider, tx)
}
tx.gasLimit = await getGasLimit(network, provider, tx)
} catch (error) {
reportError({ ...successOrErrorOptions, error, stage: TransactionFailureStage.App })
return null
Expand Down
1 change: 0 additions & 1 deletion app/src/utils/getIsQuoteHidden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const getIsQuoteHidden = (disabledReason: QuoteDisabledReason): boolean => {
switch (disabledReason) {
case QuoteDisabledReason.InsufficientLiquidity:
case QuoteDisabledReason.UnableToHedgeDelta:
case QuoteDisabledReason.DeltaOutOfRange:
return false
default:
return true
Expand Down

0 comments on commit fc8a448

Please sign in to comment.