Skip to content

Commit

Permalink
Merge pull request #192 from the-standard/202403-round
Browse files Browse the repository at this point in the history
202403 round
  • Loading branch information
ZakMooney authored Apr 2, 2024
2 parents 4dc8533 + 5161079 commit fc3bd15
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 44 deletions.
21 changes: 9 additions & 12 deletions src/components/collateral/actions/Deposit.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Box, Modal, Typography } from "@mui/material";
import { useEffect, useRef, useState } from "react";
import QRCode from "react-qr-code";
import { parseEther, parseUnits } from "viem";
import { parseUnits, formatUnits } from "viem";
import { sendTransaction } from "@wagmi/core";
import {
useWriteContract,
Expand All @@ -11,7 +11,7 @@ import {
useWatchBlockNumber
} from "wagmi";
import { constants } from "ethers";
import { formatUnits } from "ethers/lib/utils";
// import { formatUnits } from "ethers/lib/utils";
import ContentCopyIcon from "@mui/icons-material/ContentCopy";

import {
Expand Down Expand Up @@ -42,7 +42,7 @@ const Deposit: React.FC<DepositProps> = ({
const [open, setOpen] = useState(false);
const handleOpen = () => setOpen(true);
const handleClose = () => setOpen(false);
const [amount, setAmount] = useState(0);
const [amount, setAmount] = useState<any>(0);
const [maxBal, setMaxBal] = useState(0);
///store
const { vaultAddress } = useVaultAddressStore();
Expand Down Expand Up @@ -71,7 +71,7 @@ const Deposit: React.FC<DepositProps> = ({
},
})

const walletBalance = balanceData?.value;
const walletBalance: any = balanceData?.value;

const inputRef: any = useRef<HTMLInputElement>(null);

Expand All @@ -81,9 +81,9 @@ const Deposit: React.FC<DepositProps> = ({

const handleAmount = (e: any) => {
if (Number(e.target.value) < 10n ** 21n) {
setAmount(Number(e.target.value));
setAmount(parseUnits(e.target.value.toString(), decimals))
getSymbolForGreyBar(symbol);
getGreyBarUserInput(Number(e.target.value));
getGreyBarUserInput(formatUnits(parseUnits(e.target.value.toString(), decimals), decimals));
}
};

Expand Down Expand Up @@ -119,10 +119,9 @@ const Deposit: React.FC<DepositProps> = ({
abi: erc20Abi,
address: tokenAddress as any,
functionName: "transfer",
args: [vaultAddress, parseUnits(amount.toString(), decimals)],
args: [vaultAddress, amount],
});

getSnackBar('SUCCESS', 'Success!');
} catch (error: any) {
let errorMessage: any = '';
if (error && error.shortMessage) {
Expand Down Expand Up @@ -156,13 +155,12 @@ const Deposit: React.FC<DepositProps> = ({
const hash = await sendTransaction(wagmiConfig, {
account: address,
to: toAddress,
value: parseEther(txAmount.toString()),
value: txAmount,
})

setTxdata(hash);

getCircularProgress(false); // Set getCircularProgress to false after the transaction is mined
getSnackBar('SUCCESS', 'Success!');
inputRef.current.value = "";
inputRef.current.focus();
getGreyBarUserInput(0);
Expand Down Expand Up @@ -215,6 +213,7 @@ const Deposit: React.FC<DepositProps> = ({
getProgressType(2);
getCircularProgress(true);
} else if (isSuccess) {
getSnackBar('SUCCESS', 'Success!');
getCircularProgress(false); // Set getCircularProgress to false after the transaction is mined
inputRef.current.value = "";
inputRef.current.focus();
Expand Down Expand Up @@ -305,7 +304,6 @@ const Deposit: React.FC<DepositProps> = ({
}}
clickFunction={handleOpen}
>
{" "}
<img
style={{
height: "23px",
Expand Down Expand Up @@ -514,7 +512,6 @@ const Deposit: React.FC<DepositProps> = ({
}}
onClick={handleCopyText}
>
{" "}
<ContentCopyIcon />
</Box>
</Box>
Expand Down
25 changes: 15 additions & 10 deletions src/components/collateral/actions/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ const Swap: React.FC<SwapProps> = ({
};

const handleAmount = (e: any) => {
setAmount(Number(e.target.value));
setAmount(parseUnits(e.target.value.toString(), decimals))
};

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

const getSwapConversion = async () => {
Expand All @@ -74,7 +74,8 @@ const Swap: React.FC<SwapProps> = ({
`https://smart-vault-api.thestandard.io/estimate_swap?in=${swapIn}&out=${swapOut}&amount=${swapAmount}`
);
const data = response.data;
setReceiveAmountFormatted(formatUnits(data.toString(), receiveDecimals));
setReceiveAmountFormatted(parseUnits(data.toString(), receiveDecimals));
inputReceiveRef.current.value = parseUnits(data.toString(), receiveDecimals);
setSwapLoading(false);
} catch (error) {
console.log(error);
Expand Down Expand Up @@ -164,10 +165,14 @@ const Swap: React.FC<SwapProps> = ({
]);

const handleMaxBalance = async () => {
inputRef.current.value = collateralValue.toString();
handleAmount({ target: { value: collateralValue } });
const formatted = formatUnits(parseUnits(collateralValue.toString(), decimals), decimals);
inputRef.current.value = formatted;
handleAmount({ target: { value: formatted } });
};

console.log(123123, amount)
console.log(345345, receiveAmountFormatted)

if (vaultStore.status.version !== 1 && vaultStore.status.version !== 2) {
return (
<Box>
Expand Down Expand Up @@ -331,11 +336,11 @@ const Swap: React.FC<SwapProps> = ({
MozBoxSizing: "border-box",
WebkitBoxSizing: "border-box",
}}
value={swapLoading ? (
''
) : (
receiveAmountFormatted
)}
// value={swapLoading ? (
// ''
// ) : (
// receiveAmountFormatted
// )}
ref={inputReceiveRef}
type="number"
onChange={handleMinReturn}
Expand Down
18 changes: 9 additions & 9 deletions src/components/collateral/actions/Withdraw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
useWaitForTransactionReceipt,
} from "wagmi";
import { ethers } from "ethers";
import { parseUnits } from "viem";
import { parseUnits, formatUnits } from "viem";

import {
useVaultAddressStore,
Expand Down Expand Up @@ -43,9 +43,9 @@ const Withdraw: React.FC<WithdrawProps> = ({
const [txdata, setTxdata] = useState<any>(null);

const handleAmount = (e: any) => {
setAmount(Number(e.target.value));
setAmount(parseUnits(e.target.value.toString(), decimals))
getSymbolForGreyBar(symbol);
getGreyBarUserInput(Number(e.target.value));
getGreyBarUserInput(formatUnits(parseUnits(e.target.value.toString(), decimals), decimals));
};

//snackbar config
Expand All @@ -62,12 +62,11 @@ const Withdraw: React.FC<WithdrawProps> = ({
address: vaultAddress as any,
functionName: "removeCollateralNative",
args: [
ethers.utils.parseUnits(amount.toString()),
amount,
address as any
],
});

getSnackBar('SUCCESS', 'Success!');
} catch (error: any) {
let errorMessage: any = '';
if (error && error.shortMessage) {
Expand All @@ -85,12 +84,11 @@ const Withdraw: React.FC<WithdrawProps> = ({
functionName: "removeCollateral",
args: [
ethers.utils.formatBytes32String(symbol),
parseUnits(amount.toString(), decimals),
amount,
address as any
],
});

getSnackBar('SUCCESS', 'Success!');
} catch (error: any) {
let errorMessage: any = '';
if (error && error.shortMessage) {
Expand All @@ -105,6 +103,7 @@ const Withdraw: React.FC<WithdrawProps> = ({
getProgressType(1);
getCircularProgress(true);
} else if (isSuccess) {
getSnackBar('SUCCESS', 'Success!');
getCircularProgress(false); // Set getCircularProgress to false after the transaction is mined
inputRef.current.value = "";
inputRef.current.focus();
Expand Down Expand Up @@ -139,8 +138,9 @@ const Withdraw: React.FC<WithdrawProps> = ({
});

const handleMaxBalance = async () => {
inputRef.current.value = collateralValue.toString();
handleAmount({ target: { value: collateralValue } });
const formatted = formatUnits(parseUnits(collateralValue.toString(), decimals), decimals);
inputRef.current.value = formatted;
handleAmount({ target: { value: formatted } });
};

return (
Expand Down
26 changes: 13 additions & 13 deletions src/components/staking/StakingStake.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ const StakingStake = () => {
const { getSnackBar } = useSnackBarStore();
const { getCircularProgress, getProgressType } = useCircularProgressStore();
const [learnMore, setLearnMore] = useState(true);
const [tstStakeAmount, setTstStakeAmount] = useState(0);
const [eurosStakeAmount, setEurosStakeAmount] = useState(0);
const [tstStakeAmount, setTstStakeAmount] = useState<any>(0);
const [eurosStakeAmount, setEurosStakeAmount] = useState<any>(0);
const [stage, setStage] = useState('');
// const [autoTrade, setAutoTrade] = useState(false);

Expand Down Expand Up @@ -120,8 +120,8 @@ const StakingStake = () => {
const existingEurosAllowance: any = eurosData && eurosData[0].result;
const eurosBalance: any = eurosData && eurosData[1].result;

const tstInWei = parseEther(tstStakeAmount.toString());
const eurosInWei = parseEther(eurosStakeAmount.toString());
// const tstInWei = parseEther(tstStakeAmount.toString());
// const eurosInWei = parseEther(eurosStakeAmount.toString());

const { writeContract, isError, isPending, isSuccess } = useWriteContract();

Expand All @@ -132,7 +132,7 @@ const StakingStake = () => {
abi: erc20Abi,
address: tstAddress as any,
functionName: "approve",
args: [liquidationPoolAddress as any, tstInWei],
args: [liquidationPoolAddress as any, tstStakeAmount],
});
} catch (error: any) {
let errorMessage: any = '';
Expand All @@ -151,7 +151,7 @@ const StakingStake = () => {
abi: erc20Abi,
address: eurosAddress as any,
functionName: "approve",
args: [liquidationPoolAddress as any, eurosInWei],
args: [liquidationPoolAddress as any, eurosStakeAmount],
});

} catch (error: any) {
Expand All @@ -173,8 +173,8 @@ const StakingStake = () => {
address: liquidationPoolAddress as any,
functionName: "increasePosition",
args: [
tstInWei,
eurosInWei
tstStakeAmount,
eurosStakeAmount
],
});
} catch (error: any) {
Expand All @@ -188,10 +188,10 @@ const StakingStake = () => {
};

const handleLetsStake = async () => {
if (existingTstAllowance < tstInWei) {
handleApproveTst();
if (existingTstAllowance < tstStakeAmount) {
handleApproveTst();
} else {
if (existingEurosAllowance < eurosInWei) {
if (existingEurosAllowance < eurosStakeAmount) {
handleApproveEuros();
} else {
handleDepositToken();
Expand Down Expand Up @@ -258,7 +258,7 @@ const StakingStake = () => {

const handleTstAmount = (e: any) => {
if (Number(e.target.value) < 10n ** 21n) {
setTstStakeAmount(Number(e.target.value));
setTstStakeAmount(parseEther(e.target.value.toString()));
}
};

Expand All @@ -270,7 +270,7 @@ const StakingStake = () => {

const handleEurosAmount = (e: any) => {
if (Number(e.target.value) < 10n ** 21n) {
setEurosStakeAmount(Number(e.target.value));
setEurosStakeAmount(parseEther(e.target.value.toString()));
}
};

Expand Down

0 comments on commit fc3bd15

Please sign in to comment.