Skip to content

Commit

Permalink
Merge branch 'develop' into fix-arb-transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
GrassRootsCrypto committed Oct 12, 2024
2 parents b21e0fb + e32f74a commit ca705fa
Showing 1 changed file with 94 additions and 83 deletions.
177 changes: 94 additions & 83 deletions src/renderer/components/swap/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1331,78 +1331,63 @@ export const Swap = ({
const oSwapParams: O.Option<SwapTxParams> = useMemo(
() => {
const swapParamsThor = FP.pipe(
sequenceTOption(oPoolAddress, oSourceAssetWB, oQuote, oPriceSwapInFee),
O.map(
([
poolAddress,
{ walletType, walletAddress, walletAccount, walletIndex, hdMode },
txDetails,
oPriceSwapInFee
]) => {
let amountToSwap = convertBaseAmountDecimal(amountToSwapMax1e8, sourceAssetAmount.decimal)
if (!isTokenAsset(sourceAsset) && !isTradeAsset(sourceAsset) && !isSynthAsset(sourceAsset)) {
if (sourceChainAssetAmount.lte(amountToSwap.plus(oPriceSwapInFee.baseAmount))) {
amountToSwap = sourceChainAssetAmount.minus(oPriceSwapInFee.baseAmount)
}
}

return {
poolAddress,
asset: sourceAsset,
amount: amountToSwap,
memo: shortenMemo(txDetails.memo), // short asset
walletType,
sender: walletAddress,
walletAccount,
walletIndex,
hdMode,
dex
sequenceTOption(oPoolAddress, oSourceAssetWB, oQuote),
O.map(([poolAddress, { walletType, walletAddress, walletAccount, walletIndex, hdMode }, txDetails]) => {
let amountToSwap = convertBaseAmountDecimal(amountToSwapMax1e8, sourceAssetAmount.decimal)
if (!isTokenAsset(sourceAsset) && !isTradeAsset(sourceAsset) && !isSynthAsset(sourceAsset)) {
if (sourceChainAssetAmount.lt(amountToSwap.plus(swapFees.inFee.amount))) {
amountToSwap = sourceChainAssetAmount.minus(swapFees.inFee.amount)
}
}
)
return {
poolAddress,
asset: sourceAsset,
amount: amountToSwap,
memo: shortenMemo(txDetails.memo), // short asset
walletType,
sender: walletAddress,
walletAccount,
walletIndex,
hdMode,
dex
}
})
)
const swapParamsMaya = FP.pipe(
sequenceTOption(oPoolAddress, oSourceAssetWB, oQuoteMaya, oPriceSwapInFee),
O.map(
([
poolAddress,
{ walletType, walletAddress, walletAccount, walletIndex, hdMode },
quoteSwap,
oPriceSwapInFee
]) => {
let amountToSwap = convertBaseAmountDecimal(amountToSwapMax1e8, sourceAssetAmount.decimal)
if (!isTokenAsset(sourceAsset) && !isTradeAsset(sourceAsset) && !isSynthAsset(sourceAsset)) {
if (sourceChainAssetAmount.lte(amountToSwap.plus(oPriceSwapInFee.baseAmount))) {
amountToSwap = sourceChainAssetAmount.minus(oPriceSwapInFee.baseAmount)
}
}
return {
poolAddress,
asset: sourceAsset,
amount: amountToSwap,
memo: quoteSwap.memo, // The memo will be different based on the selected quote
walletType,
sender: walletAddress,
walletAccount,
walletIndex,
hdMode,
dex
sequenceTOption(oPoolAddress, oSourceAssetWB, oQuoteMaya),
O.map(([poolAddress, { walletType, walletAddress, walletAccount, walletIndex, hdMode }, quoteSwap]) => {
let amountToSwap = convertBaseAmountDecimal(amountToSwapMax1e8, sourceAssetAmount.decimal)
if (!isTokenAsset(sourceAsset) && !isTradeAsset(sourceAsset) && !isSynthAsset(sourceAsset)) {
if (sourceChainAssetAmount.lt(amountToSwap.plus(swapFees.inFee.amount))) {
amountToSwap = sourceChainAssetAmount.minus(swapFees.inFee.amount)
}
}
)
return {
poolAddress,
asset: sourceAsset,
amount: amountToSwap,
memo: quoteSwap.memo, // The memo will be different based on the selected quote
walletType,
sender: walletAddress,
walletAccount,
walletIndex,
hdMode,
dex
}
})
)
return dex.chain === THORChain ? swapParamsThor : swapParamsMaya
},
[
oPoolAddress,
oSourceAssetWB,
oQuote,
oPriceSwapInFee,
oQuoteMaya,
dex,
amountToSwapMax1e8,
sourceAssetAmount.decimal,
sourceAsset,
swapFees.inFee.amount,
sourceChainAssetAmount
] // Include both quote dependencies
)
Expand Down Expand Up @@ -1562,29 +1547,6 @@ export const Swap = ({
[isApprovedERC20Token$, subscribeIsApprovedState]
)

// whenever `oApproveParams` has been updated,
// `approveFeeParamsUpdated` needs to be called to update `approveFeesRD`
// + `checkApprovedStatus` needs to be called
useEffect(() => {
FP.pipe(
oApproveParams,
// Do nothing if prev. and current router are the same
O.filter((params) => !eqOApproveParams.equals(O.some(params), prevApproveParams.current)),
// update ref
O.map((params) => {
prevApproveParams.current = O.some(params) // Update reference to current params

// Using setTimeout to delay the execution of subsequent actions
setTimeout(() => {
approveFeeParamsUpdated(params)
checkApprovedStatus(params)
}, 100) // Delay of 100 milliseconds

return true
})
)
}, [approveFeeParamsUpdated, checkApprovedStatus, oApproveParams, oPoolAddress])

const reloadApproveFeesHandler = useCallback(() => {
FP.pipe(oApproveParams, O.map(reloadApproveFee))
}, [oApproveParams, reloadApproveFee])
Expand All @@ -1595,6 +1557,7 @@ export const Swap = ({
const setSourceAsset = useCallback(
async (asset: AnyAsset) => {
// delay to avoid render issues while switching
resetIsApprovedState()
await delay(100)
setAmountToSwapMax1e8(initialAmountToSwapMax1e8)
setQuote(O.none)
Expand All @@ -1607,27 +1570,75 @@ export const Swap = ({
targetWalletType: oTargetWalletType,
recipientAddress: oRecipientAddress
})
await delay(100) // Optional delay to ensure state updates properly
// Step 3: Check approval for the new asset
FP.pipe(
oApproveParams, // Use the new asset's approval parameters
O.map((params) => checkApprovedStatus(params))
)
},
[initialAmountToSwapMax1e8, oRecipientAddress, oTargetWalletType, onChangeAsset, setAmountToSwapMax1e8, targetAsset]
[
checkApprovedStatus,
initialAmountToSwapMax1e8,
oApproveParams,
oRecipientAddress,
oTargetWalletType,
onChangeAsset,
resetIsApprovedState,
setAmountToSwapMax1e8,
targetAsset
]
)

const setTargetAsset = useCallback(
async (asset: AnyAsset) => {
// delay to avoid render issues while switching
await delay(100)
// Step 1: Reset approval state before changing the asset
resetIsApprovedState()

// Step 2: Switch target asset
await delay(100) // Optional delay to ensure state updates properly

onChangeAsset({
source: sourceAsset,
sourceWalletType,
target: asset,
// back to default 'keystore' type
// Reset the wallet type for the new target asset
targetWalletType: O.some('keystore'),
// Set recipient address to 'none' will lead to use keystore address in `WalletView`
recipientAddress: O.none
})
await delay(100) // Optional delay to ensure state updates properly
// Step 3: Check approval for the new asset
FP.pipe(
oApproveParams, // Use the new asset's approval parameters
O.map((params) => checkApprovedStatus(params))
)
},
[onChangeAsset, sourceAsset, sourceWalletType]
[onChangeAsset, resetIsApprovedState, sourceAsset, sourceWalletType, checkApprovedStatus, oApproveParams]
)

// whenever `oApproveParams` has been updated,
// `approveFeeParamsUpdated` needs to be called to update `approveFeesRD`
// + `checkApprovedStatus` needs to be called
useEffect(() => {
FP.pipe(
oApproveParams,
// Do nothing if prev. and current router are the same
O.filter((params) => !eqOApproveParams.equals(O.some(params), prevApproveParams.current)),
// update ref
O.map((params) => {
prevApproveParams.current = O.some(params) // Update reference to current params

// Using setTimeout to delay the execution of subsequent actions
setTimeout(() => {
approveFeeParamsUpdated(params)
checkApprovedStatus(params)
}, 100) // Delay of 100 milliseconds

return true
})
)
}, [approveFeeParamsUpdated, checkApprovedStatus, oApproveParams, oPoolAddress])

const minAmountError = useMemo(() => {
if (isZeroAmountToSwap) return false
const minAmountIn = convertBaseAmountDecimal(reccommendedAmountIn.baseAmount, amountToSwapMax1e8.decimal)
Expand Down

0 comments on commit ca705fa

Please sign in to comment.