-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23f85ed
commit 0620755
Showing
7 changed files
with
200 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
packages/dev-frontend/src/components/Trove/ExpensiveTroveChangeWarning.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import React, { useEffect } from "react"; | ||
|
||
import { Decimal, TroveChange } from "@liquity/lib-base"; | ||
import { PopulatedEthersLiquityTransaction } from "@liquity/lib-ethers"; | ||
|
||
import { useLiquity } from "../../hooks/LiquityContext"; | ||
import { Warning } from "../Warning"; | ||
|
||
export type GasEstimationState = | ||
| { type: "idle" | "inProgress" } | ||
| { type: "complete"; populatedTx: PopulatedEthersLiquityTransaction }; | ||
|
||
type ExpensiveTroveChangeWarningParams = { | ||
troveChange?: Exclude<TroveChange<Decimal>, { type: "invalidCreation" }>; | ||
maxBorrowingRate: Decimal; | ||
borrowingFeeDecayToleranceMinutes: number; | ||
gasEstimationState: GasEstimationState; | ||
setGasEstimationState: (newState: GasEstimationState) => void; | ||
}; | ||
|
||
export const ExpensiveTroveChangeWarning: React.FC<ExpensiveTroveChangeWarningParams> = ({ | ||
troveChange, | ||
maxBorrowingRate, | ||
borrowingFeeDecayToleranceMinutes, | ||
gasEstimationState, | ||
setGasEstimationState | ||
}) => { | ||
const { liquity } = useLiquity(); | ||
|
||
useEffect(() => { | ||
if (troveChange && troveChange.type !== "closure") { | ||
setGasEstimationState({ type: "inProgress" }); | ||
|
||
let cancelled = false; | ||
|
||
const timeoutId = setTimeout(async () => { | ||
const populatedTx = await (troveChange.type === "creation" | ||
? liquity.populate.openTrove(troveChange.params, { | ||
maxBorrowingRate, | ||
borrowingFeeDecayToleranceMinutes | ||
}) | ||
: liquity.populate.adjustTrove(troveChange.params, { | ||
maxBorrowingRate, | ||
borrowingFeeDecayToleranceMinutes | ||
})); | ||
|
||
if (!cancelled) { | ||
setGasEstimationState({ type: "complete", populatedTx }); | ||
console.log( | ||
"Estimated TX cost: " + | ||
Decimal.from(`${populatedTx.rawPopulatedTransaction.gasLimit}`).prettify(0) | ||
); | ||
} | ||
}, 333); | ||
|
||
return () => { | ||
clearTimeout(timeoutId); | ||
cancelled = true; | ||
}; | ||
} else { | ||
setGasEstimationState({ type: "idle" }); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [troveChange]); | ||
|
||
if ( | ||
troveChange && | ||
gasEstimationState.type === "complete" && | ||
gasEstimationState.populatedTx.gasHeadroom !== undefined && | ||
gasEstimationState.populatedTx.gasHeadroom >= 200000 | ||
) { | ||
return troveChange.type === "creation" ? ( | ||
<Warning> | ||
The cost of opening a Trove in this collateral ratio range is high. It is recommended to | ||
choose a slightly different collateral ratio. | ||
</Warning> | ||
) : ( | ||
<Warning> | ||
The cost of adjusting a Trove into this collateral ratio range is high. It is recommended to | ||
choose a slightly different collateral ratio. | ||
</Warning> | ||
); | ||
} | ||
|
||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Box, Flex, Text } from "theme-ui"; | ||
|
||
import { Icon } from "./Icon"; | ||
|
||
export const Warning: React.FC = ({ children }) => ( | ||
<Box | ||
sx={{ | ||
display: "flex", | ||
flexDirection: "column", | ||
justifyContent: "space-around", | ||
|
||
mb: [2, 3], | ||
p: 3, | ||
|
||
border: 1, | ||
borderRadius: "8px", | ||
borderColor: "warning", | ||
boxShadow: 2 | ||
// bg: "rgba(46, 182, 234, 0.05)" | ||
}} | ||
> | ||
<Flex sx={{ alignItems: "center" }}> | ||
<Icon name="exclamation-triangle" size="lg" /> | ||
<Text sx={{ ml: 2 }}>{children}</Text> | ||
</Flex> | ||
</Box> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useEffect, useState } from "react"; | ||
import { Decimal, TroveChange } from "@liquity/lib-base"; | ||
|
||
type ValidTroveChange = Exclude<TroveChange<Decimal>, { type: "invalidCreation" }>; | ||
|
||
const paramsEq = (a?: Decimal, b?: Decimal) => (a && b ? a.eq(b) : !a && !b); | ||
|
||
const equals = (a: ValidTroveChange, b: ValidTroveChange): boolean => { | ||
return ( | ||
a.type === b.type && | ||
paramsEq(a.params.borrowLUSD, b.params.borrowLUSD) && | ||
paramsEq(a.params.repayLUSD, b.params.repayLUSD) && | ||
paramsEq(a.params.depositCollateral, b.params.depositCollateral) && | ||
paramsEq(a.params.withdrawCollateral, b.params.withdrawCollateral) | ||
); | ||
}; | ||
|
||
export const useStableTroveChange = ( | ||
troveChange: ValidTroveChange | undefined | ||
): ValidTroveChange | undefined => { | ||
const [stableTroveChange, setStableTroveChange] = useState(troveChange); | ||
|
||
useEffect(() => { | ||
if (!!stableTroveChange !== !!troveChange) { | ||
setStableTroveChange(troveChange); | ||
} else if (stableTroveChange && troveChange && !equals(stableTroveChange, troveChange)) { | ||
setStableTroveChange(troveChange); | ||
} | ||
}, [stableTroveChange, troveChange]); | ||
|
||
return stableTroveChange; | ||
}; |