Skip to content

Commit

Permalink
Merge pull request #195 from the-standard/202403-round
Browse files Browse the repository at this point in the history
feat: fix swap rounding
  • Loading branch information
ZakMooney authored Apr 3, 2024
2 parents 456b99f + 72ba332 commit bd6a88c
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/components/collateral/actions/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ const Swap: React.FC<SwapProps> = ({
const [swapLoading, setSwapLoading] = useState<any>(false);
const [swapAssets, setSwapAssets] = useState<any>();
const [amount, setAmount] = useState<any>(0);
// const [receiveAmount, setReceiveAmount] = useState<any>(0);
const [receiveAmountFormatted, setReceiveAmountFormatted] = useState<any>(0);
const [receiveAmount, setReceiveAmount] = useState<any>(0);
const [receiveAsset, setReceiveAsset] = useState<any>('');
const [receiveDecimals, setReceiveDecimals] = useState<any>();
const { vaultStore } = useVaultStore();
Expand All @@ -61,21 +60,21 @@ const Swap: React.FC<SwapProps> = ({
};

const handleMinReturn = (e: any) => {
setReceiveAmountFormatted(parseUnits(e.target.value.toString(), receiveDecimals));
setReceiveAmount(parseUnits(e.target.value.toString(), receiveDecimals));
};

const getSwapConversion = async () => {
try {
setSwapLoading(true);
const swapIn = symbol;
const swapOut = receiveAsset;
const swapAmount = parseUnits(amount.toString(), decimals).toString();
const swapAmount = amount.toString();
const response = await axios.get(
`https://smart-vault-api.thestandard.io/estimate_swap?in=${swapIn}&out=${swapOut}&amount=${swapAmount}`
);
const data = response.data;
setReceiveAmountFormatted(parseUnits(data.toString(), receiveDecimals));
inputReceiveRef.current.value = parseUnits(data.toString(), receiveDecimals);
setReceiveAmount(BigInt(data));
inputReceiveRef.current.value = formatUnits(data.toString(), receiveDecimals);
setSwapLoading(false);
} catch (error) {
console.log(error);
Expand Down Expand Up @@ -121,12 +120,9 @@ const Swap: React.FC<SwapProps> = ({
ethers.utils.formatBytes32String(symbol),
ethers.utils.formatBytes32String(receiveAsset),
amount,
receiveAmountFormatted,
// parseUnits(amount.toString(), decimals),
// parseUnits(receiveAmountFormatted.toString(), receiveDecimals),
receiveAmount,
],
});
// getSnackBar('SUCCESS', 'Success!');
} catch (error: any) {
let errorMessage: any = '';
if (error && error.shortMessage) {
Expand All @@ -147,15 +143,15 @@ const Swap: React.FC<SwapProps> = ({
setSwapLoading(false);
inputRef.current.value = "";
setAmount(0);
setReceiveAmountFormatted(0);
setReceiveAmount(0);
setReceiveAsset('');
} else if (isError) {
getSnackBar('ERROR', 'There was an error');
getCircularProgress(false);
setSwapLoading(false);
inputRef.current.value = "";
setAmount(0);
setReceiveAmountFormatted(0);
setReceiveAmount(0);
setReceiveAsset('');
}
}, [
Expand Down Expand Up @@ -337,7 +333,7 @@ const Swap: React.FC<SwapProps> = ({
// value={swapLoading ? (
// ''
// ) : (
// receiveAmountFormatted
// receiveAmount
// )}
ref={inputReceiveRef}
type="number"
Expand Down Expand Up @@ -402,7 +398,7 @@ const Swap: React.FC<SwapProps> = ({
isDisabled={
!amount||
!receiveAsset ||
!(receiveAmountFormatted > 0) ||
!(receiveAmount > 0) ||
swapLoading
}
isSuccess={!swapLoading}
Expand Down

0 comments on commit bd6a88c

Please sign in to comment.