Skip to content

Commit

Permalink
🐛 slippage: fix slippage calculations for fixes operations
Browse files Browse the repository at this point in the history
  • Loading branch information
franm91 committed Apr 25, 2024
1 parent 05eba35 commit 4d5fdd9
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 20 deletions.
16 changes: 10 additions & 6 deletions components/asset/FloatingPool/FloatingPoolInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ const FloatingPoolInfo: FC<FloatingPoolInfoProps> = ({ symbol }) => {
))}
</>
),
tooltipTitle: t(
'This APR assumes a constant price for the EXA token during the vesting period of the esEXA reward',
),
...(depositRewards.some((r) => r.assetSymbol === 'esEXA') && {
tooltipTitle: t(
'This APR assumes a constant price for the EXA token during the vesting period of the esEXA reward',
),
}),
},
]
: []),
Expand All @@ -122,9 +124,11 @@ const FloatingPoolInfo: FC<FloatingPoolInfoProps> = ({ symbol }) => {
))}
</>
),
tooltipTitle: t(
'This APR assumes a constant price for the EXA token during the vesting period of the esEXA reward',
),
...(borrowRewards.some((r) => r.assetSymbol === 'esEXA') && {
tooltipTitle: t(
'This APR assumes a constant price for the EXA token during the vesting period of the esEXA reward',
),
}),
},
]
: []),
Expand Down
2 changes: 1 addition & 1 deletion components/operations/BorrowAtMaturity/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const BorrowAtMaturity: FC<PropsWithChildren> = ({ children }) => {
<Grid item mt={{ xs: 2, sm: 3 }}>
<ModalSubmit
label={
borrowInInstallmentsNeedsApproval
borrowInInstallmentsNeedsApproval && installments > 1
? t('Approve')
: translateOperation('borrowAtMaturity', { capitalize: true })
}
Expand Down
29 changes: 22 additions & 7 deletions hooks/useBorrowAtMaturity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default (): BorrowAtMaturity => {
const { accountData, marketAccount } = useAccountData(symbol);

const [fixedRate, setFixedRate] = useState<bigint | undefined>();
const [fixedFee, setFixedFee] = useState<bigint | undefined>();

const healthFactor = useHealthFactor();
const minBorrowRate = useMemo<bigint | undefined>(() => {
Expand Down Expand Up @@ -88,15 +89,25 @@ export default (): BorrowAtMaturity => {

const previewGasCost = useCallback(
async (quantity: string): Promise<bigint | undefined> => {
if (!marketAccount || !walletAddress || !marketContract || !ETHRouterContract || !date || !quantity || !opts)
if (
!marketAccount ||
!walletAddress ||
!marketContract ||
!ETHRouterContract ||
!date ||
!quantity ||
!opts ||
!fixedFee
)
return;

if (await needsApproval(quantity)) {
return approveEstimateGas();
}

const amount = parseUnits(quantity, marketAccount.decimals);
const maxAmount = (amount * slippage) / WAD;
const maxAmount = ((amount + fixedFee) * slippage) / WAD;

if (marketAccount.assetSymbol === 'WETH') {
const sim = await ETHRouterContract.simulate.borrowAtMaturity([date, amount, maxAmount], opts);
return estimate(sim.request);
Expand All @@ -116,6 +127,7 @@ export default (): BorrowAtMaturity => {
date,
opts,
needsApproval,
fixedFee,
slippage,
estimate,
approveEstimateGas,
Expand Down Expand Up @@ -190,10 +202,10 @@ export default (): BorrowAtMaturity => {
});
}

if (!marketAccount || !date || !qty || !walletAddress || !opts) return;
if (!marketAccount || !date || !qty || !walletAddress || !opts || !fixedFee) return;

const amount = parseUnits(qty, marketAccount.decimals);
const maxAmount = (amount * slippage) / WAD;
const maxAmount = ((amount + fixedFee) * slippage) / WAD;

let hash;
try {
Expand Down Expand Up @@ -267,14 +279,15 @@ export default (): BorrowAtMaturity => {
qty,
walletAddress,
opts,
fixedFee,
setErrorData,
t,
setTx,
symbol,
ETHRouterContract,
marketContract,
receiver,
setErrorData,
setTx,
handleOperationError,
symbol,
]);

const updateAPR = useCallback(async () => {
Expand All @@ -296,8 +309,10 @@ export default (): BorrowAtMaturity => {
const fixedAPR = ((rate - WAD) * 31_536_000n) / (date - currentTimestamp);

setFixedRate(fixedAPR);
setFixedFee(finalAssets - initialAssets);
} catch (error) {
setFixedRate(undefined);
setFixedFee(undefined);
}
} else {
setFixedRate(minBorrowRate);
Expand Down
28 changes: 22 additions & 6 deletions hooks/useDepositAtMaturity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default (): DepositAtMaturity => {
const handleOperationError = useHandleOperationError();

const [fixedRate, setFixedRate] = useState<bigint | undefined>();
const [fixedFee, setFixedFee] = useState<bigint | undefined>();
const [gtMaxYield, setGtMaxYield] = useState<boolean>(false);

const walletBalance = useBalance(symbol, assetContract?.address);
Expand All @@ -82,7 +83,8 @@ export default (): DepositAtMaturity => {
!date ||
!quantity ||
(walletBalance && parseFloat(quantity) > parseFloat(walletBalance)) ||
!opts
!opts ||
!fixedFee
)
return;

Expand All @@ -91,7 +93,7 @@ export default (): DepositAtMaturity => {
}

const amount = parseUnits(quantity, marketAccount.decimals);
const minAmount = (amount * slippage) / WAD;
const minAmount = ((amount + fixedFee) * slippage) / WAD;

if (marketAccount.assetSymbol === 'WETH') {
const sim = await ETHRouterContract.simulate.depositAtMaturity([date, minAmount], {
Expand All @@ -109,14 +111,15 @@ export default (): DepositAtMaturity => {
return estimate(sim.request);
},
[
opts,
marketAccount,
walletAddress,
marketContract,
ETHRouterContract,
date,
walletBalance,
opts,
needsApproval,
fixedFee,
slippage,
estimate,
approveEstimateGas,
Expand Down Expand Up @@ -166,13 +169,23 @@ export default (): DepositAtMaturity => {
);

const deposit = useCallback(async () => {
if (!marketAccount || !date || !qty || !ETHRouterContract || !marketContract || !walletAddress || !opts) return;
if (
!marketAccount ||
!date ||
!qty ||
!ETHRouterContract ||
!marketContract ||
!walletAddress ||
!opts ||
!fixedFee
)
return;

let hash;
setIsLoadingOp(true);
try {
const amount = parseUnits(qty, marketAccount.decimals);
const minAmount = (amount * slippage) / WAD;
const minAmount = ((amount + fixedFee) * slippage) / WAD;
if (marketAccount.assetSymbol === 'WETH') {
const args = [date, minAmount] as const;
const gasEstimation = await ETHRouterContract.estimateGas.depositAtMaturity(args, {
Expand Down Expand Up @@ -235,12 +248,13 @@ export default (): DepositAtMaturity => {
marketContract,
walletAddress,
opts,
fixedFee,
setIsLoadingOp,
slippage,
setTx,
symbol,
setErrorData,
handleOperationError,
symbol,
]);

const handleSubmitAction = useCallback(async () => {
Expand Down Expand Up @@ -273,8 +287,10 @@ export default (): DepositAtMaturity => {
const fixedAPR = ((rate - WAD) * 31_536_000n) / (date - currentTimestamp);

setFixedRate(fixedAPR);
setFixedFee(finalAssets - initialAssets);
} catch (error) {
setFixedRate(undefined);
setFixedFee(undefined);
}
} else {
setFixedRate(depositRate);
Expand Down
1 change: 1 addition & 0 deletions public/img/exaTokens/exaUSDC.e.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4d5fdd9

Please sign in to comment.