Skip to content

Commit

Permalink
retry zaps up to three times
Browse files Browse the repository at this point in the history
  • Loading branch information
jankjr committed Oct 5, 2023
1 parent 70df1d4 commit a959ecd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 36 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@popperjs/core": "^2.11.5",
"@rainbow-me/rainbowkit": "^1.0.4",
"@react-spring/web": "^9.7.1",
"@reserve-protocol/token-zapper": "2.4.0",
"@reserve-protocol/token-zapper": "2.5.0",
"@uiw/react-md-editor": "^3.20.5",
"@uniswap/permit2-sdk": "^1.2.0",
"@viem/anvil": "^0.0.6",
Expand Down
83 changes: 52 additions & 31 deletions src/views/issuance/components/zap/state/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
resolvedZapState,
zappableTokens,
} from './zapper'
import { set } from 'react-ga'

/**
* I've tried to keep react effects to a minimum so most async code is triggered via some signal
Expand Down Expand Up @@ -59,7 +60,7 @@ export const permitSignature = atom(null as null | string)

// The amount of slippage we allow when zap involves a trade:
// This is not exposed in the UI yet, but probably should be.
const tradeSlippage = atom(0.01)
const tradeSlippage = atom(0.1)

// We sent the zap transaction,
// and are waiting for the user to sign off on it and for it to commit
Expand Down Expand Up @@ -131,8 +132,10 @@ const debouncedUserInputGenerator = atomWithDebounce(
).debouncedValueAtom

let firstTime = true
export const redoQuote = atom(0)
export const zapQuotePromise = loadable(
onlyNonNullAtom(async (get) => {
get(redoQuote)
const input = get(debouncedUserInputGenerator)
if (input.inputQuantity.amount === 0n) {
return null
Expand Down Expand Up @@ -290,39 +293,57 @@ export const approvalTxFee = loadable(

export const resolvedApprovalTxFee = simplifyLoadable(approvalTxFee)

export const zapTransaction = loadable(
atom(async (get) => {
const result = get(zapQuote)
const approvalNeeded = get(resolvedApprovalNeeded)
if (!(approvalNeeded && result)) {
return null
}

let permit2 = undefined
if (approvalNeeded.usingPermit2 === true) {
const permit = get(permit2ToSignAtom)
const signature = get(permitSignature)
permit2 =
signature != null && permit != null
? {
permit: permit.permit,
signature,
}
: undefined
}
const tx = await result.toTransaction({
permit2,
returnDust: get(collectDust),
})
console.log("=== abstract zap transaction ===")
console.log(result.describe().join("\n"))
return {
result,
transaction: tx,
permit2,
}
const zapTxAtom = atom(async (get) => {
const result = get(zapQuote)
const approvalNeeded = get(resolvedApprovalNeeded)
if (!(approvalNeeded && result)) {
return null
}

let permit2 = undefined
if (approvalNeeded.usingPermit2 === true) {
const permit = get(permit2ToSignAtom)
const signature = get(permitSignature)
permit2 =
signature != null && permit != null
? {
permit: permit.permit,
signature,
}
: undefined
}
const tx = await result.toTransaction({
permit2,
returnDust: get(collectDust),
})
console.log("=== abstract zap transaction ===")
console.log(result.describe().join("\n"))
return {
result,
transaction: tx,
permit2,
}
})

export const zapTransaction = loadable(
zapTxAtom
)

let errorCount = 0
const zapTxAtomStore = atom(() => null, (get, set, update) => {
const a = get(zapTransaction)
if (a.state === 'hasData') {
errorCount = 0
}
if (a.state === 'hasError') {
if (errorCount > 5) {
return
}
set(redoQuote, Math.random())
errorCount += 1;
}
})
export const resolvedZapTransaction = simplifyLoadable(zapTransaction)

export const zapTransactionGasEstimateUnits = loadable(
Expand Down

0 comments on commit a959ecd

Please sign in to comment.