Skip to content

Commit

Permalink
added fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-orbs committed Mar 25, 2024
1 parent 322bc02 commit e963c0f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
8 changes: 7 additions & 1 deletion src/app/swap/SwapBest.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,16 @@ export default function SwapBest({
dexOutAmount,
isDexTrade,
})
if (isDexTrade) {

const dexSwap = () => {
onOdosSwap(fromAsset, toAsset, fromAmount, toAmount, bestTrade, () => {
setFromAmount('')
mutateAssets()
})
}

if (isDexTrade) {
dexSwap()
} else {
onLHSwap({
fromAsset,
Expand All @@ -169,6 +174,7 @@ export default function SwapBest({
setFromAddress,
outAmount,
quote: lhQuote,
fallback: dexSwap,
callback: () => {
setFromAmount('')
mutateAssets()
Expand Down
27 changes: 14 additions & 13 deletions src/modules/LiquidityHub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const TOKEN_LIST = 'https://lhthena.s3.us-east-2.amazonaws.com/token-list-lh.jso
const zero = BN(0)

// analytics
const ANALYTICS_VERSION = 0.1
const ANALYTICS_VERSION = 0.3
const TX_UPDATER_KEYS = {
key: uuidv4(),
swapuuid: uuidv4(),
Expand All @@ -43,15 +43,6 @@ const initialAnalyticsData = {
partner: PARTNER,
chainId: CHAIN_ID,
isClobTrade: false,
isNotClobTradeReason: 'null',
firstFailureSessionId: 'null',
clobDexPriceDiffPercent: 'null',
approvalState: 'null',
signatureState: 'null',
swapState: 'null',
wrapState: 'null',
onChainClobSwapState: 'null',
userWasApprovedBeforeTheTrade: 'null',
isDexTrade: false,
version: ANALYTICS_VERSION,
}
Expand Down Expand Up @@ -291,7 +282,11 @@ const LhQuote = async args => {
inToken: isNative(args.inToken) ? zeroAddress : args.inToken,
outToken: isNative(args.outToken) ? zeroAddress : args.outToken,
inAmount: args.inAmount,
outAmount: !dexOutAmount ? '-1' : new BN(dexOutAmount).gt(0) ? dexOutAmount : '0',
outAmount: !dexOutAmount
? '-1'
: new BN(dexOutAmount).gt(0)
? BN(dexOutAmount).decimalPlaces(0).toString()
: '0',
user: args.account,
slippage: args.slippage,
qs: encodeURIComponent(window.location.hash),
Expand Down Expand Up @@ -501,7 +496,7 @@ const useSwap = () => {
const count = counter()
const submitTx = useSubmitTransaction()
const { incrementFailures } = useStore()
const { startTxn, writeTxn, updateTxn, endTxn } = useTxn()
const { startTxn, writeTxn, updateTxn, endTxn, closeTxn } = useTxn()

return useMutation({
mutationFn: async ({ fromAsset, toAsset, fromAmount, setFromAddress, outAmount, quote, callback }) => {
Expand Down Expand Up @@ -643,9 +638,15 @@ const useSwap = () => {
callback()
return tx
},
onError: () => {
onError: (error, args) => {
incrementFailures()
analytics.onClobFailure()
// fallback to dex swap if LH fails, only if both assets are not extended
const isRejected = error?.message.toLowerCase().includes('user rejected')
if (!args.fromAsset.extended && !args.toAsset.extended && !isRejected) {
closeTxn()
args.fallback()
}
},
})
}
Expand Down

0 comments on commit e963c0f

Please sign in to comment.