Skip to content

Commit

Permalink
Update inputMethod to inputAmount anytime the asset changes (#5934)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinchung authored Jul 16, 2024
1 parent 6749e2f commit 8f7acd7
Showing 1 changed file with 71 additions and 69 deletions.
140 changes: 71 additions & 69 deletions src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,87 +790,89 @@ export function useSwapInputsController({
const didInputAssetChange = current.assetToSellId !== previous?.assetToSellId;
const didOutputAssetChange = current.assetToBuyId !== previous?.assetToBuyId;

if (didInputAssetChange || didOutputAssetChange) {
const balance = internalSelectedInputAsset.value?.maxSwappableAmount;

const areBothAssetsSet = internalSelectedInputAsset.value && internalSelectedOutputAsset.value;
const didFlipAssets =
didInputAssetChange && didOutputAssetChange && areBothAssetsSet && previous && current.assetToSellId === previous.assetToBuyId;

if (!didFlipAssets) {
// If either asset was changed but the assets were not flipped
if (!balance || equalWorklet(balance, 0)) {
isQuoteStale.value = 0;
isFetching.value = false;
inputValues.modify(values => {
return {
...values,
inputAmount: 0,
inputNativeValue: 0,
outputAmount: 0,
outputNativeValue: 0,
};
});
return;
}
if (!didInputAssetChange && !didOutputAssetChange) return;

if (didInputAssetChange) {
inputMethod.value = 'inputAmount';
sliderXPosition.value = withSpring(SLIDER_WIDTH / 2, snappySpringConfig);
}
const balance = internalSelectedInputAsset.value?.maxSwappableAmount;

const { inputAmount, inputNativeValue } = getInputValuesForSliderPositionWorklet({
selectedInputAsset: internalSelectedInputAsset.value,
percentageToSwap: didInputAssetChange ? 0.5 : percentageToSwap.value,
sliderXPosition: didInputAssetChange ? SLIDER_WIDTH / 2 : sliderXPosition.value,
});
const areBothAssetsSet = internalSelectedInputAsset.value && internalSelectedOutputAsset.value;
const didFlipAssets =
didInputAssetChange && didOutputAssetChange && areBothAssetsSet && previous && current.assetToSellId === previous.assetToBuyId;

inputValues.modify(values => {
return {
...values,
inputAmount,
inputNativeValue,
};
});
} else {
// If the assets were flipped
inputMethod.value = 'inputAmount';

const inputNativePrice = internalSelectedInputAsset.value?.nativePrice || internalSelectedInputAsset.value?.price?.value || 0;
const outputNativePrice = internalSelectedOutputAsset.value?.nativePrice || internalSelectedOutputAsset.value?.price?.value || 0;

const prevInputNativeValue = inputValues.value.inputNativeValue;
const prevOutputAmount = inputValues.value.outputAmount;
const newInputAmount = inputNativePrice > 0 ? divWorklet(prevInputNativeValue, inputNativePrice) : prevOutputAmount;

const inputAmount = Number(
valueBasedDecimalFormatter({
amount: newInputAmount,
nativePrice: inputNativePrice,
roundingMode: 'up',
isStablecoin: internalSelectedInputAsset.value?.type === 'stablecoin' ?? false,
stripSeparators: true,
})
);

const prevOutputNativeValue = inputValues.value.outputNativeValue;
const prevInputAmount = inputValues.value.inputAmount;
const newOutputAmount = outputNativePrice > 0 ? divWorklet(prevOutputNativeValue, outputNativePrice) : prevInputAmount;
if (!didFlipAssets) {
// If either asset was changed but the assets were not flipped
inputMethod.value = 'inputAmount';

// Handle when there is no balance for the input
if (!balance || equalWorklet(balance, 0)) {
isQuoteStale.value = 0;
isFetching.value = false;
inputValues.modify(values => {
return {
...values,
inputAmount,
inputNativeValue: mulWorklet(newInputAmount, inputNativePrice),
outputAmount: newOutputAmount,
outputNativeValue: mulWorklet(newOutputAmount, outputNativePrice),
inputAmount: 0,
inputNativeValue: 0,
outputAmount: 0,
outputNativeValue: 0,
};
});
return;
}

if (internalSelectedInputAsset.value && internalSelectedOutputAsset.value) {
fetchQuoteAndAssetPrices();
if (didInputAssetChange) {
sliderXPosition.value = withSpring(SLIDER_WIDTH / 2, snappySpringConfig);
}

const { inputAmount, inputNativeValue } = getInputValuesForSliderPositionWorklet({
selectedInputAsset: internalSelectedInputAsset.value,
percentageToSwap: didInputAssetChange ? 0.5 : percentageToSwap.value,
sliderXPosition: didInputAssetChange ? SLIDER_WIDTH / 2 : sliderXPosition.value,
});

inputValues.modify(values => {
return {
...values,
inputAmount,
inputNativeValue,
};
});
} else {
// If the assets were flipped
inputMethod.value = 'inputAmount';

const inputNativePrice = internalSelectedInputAsset.value?.nativePrice || internalSelectedInputAsset.value?.price?.value || 0;
const outputNativePrice = internalSelectedOutputAsset.value?.nativePrice || internalSelectedOutputAsset.value?.price?.value || 0;

const prevInputNativeValue = inputValues.value.inputNativeValue;
const prevOutputAmount = inputValues.value.outputAmount;
const newInputAmount = inputNativePrice > 0 ? divWorklet(prevInputNativeValue, inputNativePrice) : prevOutputAmount;

const inputAmount = Number(
valueBasedDecimalFormatter({
amount: newInputAmount,
nativePrice: inputNativePrice,
roundingMode: 'up',
isStablecoin: internalSelectedInputAsset.value?.type === 'stablecoin' ?? false,
stripSeparators: true,
})
);

const prevOutputNativeValue = inputValues.value.outputNativeValue;
const prevInputAmount = inputValues.value.inputAmount;
const newOutputAmount = outputNativePrice > 0 ? divWorklet(prevOutputNativeValue, outputNativePrice) : prevInputAmount;

inputValues.modify(values => {
return {
...values,
inputAmount,
inputNativeValue: mulWorklet(newInputAmount, inputNativePrice),
outputAmount: newOutputAmount,
outputNativeValue: mulWorklet(newOutputAmount, outputNativePrice),
};
});
}

if (areBothAssetsSet) {
fetchQuoteAndAssetPrices();
}
}
);
Expand Down

0 comments on commit 8f7acd7

Please sign in to comment.