From a6b65bec0085f9da1a7e90556ffc74b4ec1b2c92 Mon Sep 17 00:00:00 2001 From: marcomariscal Date: Mon, 26 Jun 2023 12:51:46 -0700 Subject: [PATCH 1/8] feat: liquity adjust --- .../experimental_/MessageTranslator_.tsx | 10 ++ .../widgets/liquity/adjust/LiquityAdjust.tsx | 114 ++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx diff --git a/src/components/experimental_/MessageTranslator_.tsx b/src/components/experimental_/MessageTranslator_.tsx index ccc66a723..6486d1168 100644 --- a/src/components/experimental_/MessageTranslator_.tsx +++ b/src/components/experimental_/MessageTranslator_.tsx @@ -7,6 +7,7 @@ import Avatar from '../Avatar'; import { Widgetize } from '../MessageTranslator'; import { composeFromString } from '../cactiComponents/tools/compose'; import { FeedbackButton } from './FeedbackButton_'; +import LiquityAdjust from './widgets/liquity/adjust/LiquityAdjust'; import LiquityBorrow from './widgets/liquity/borrow/LiquityBorrow'; import Transfer from './widgets/transfer/Transfer'; import Uniswap from './widgets/uniswap/Uniswap'; @@ -127,6 +128,15 @@ const Widget = ({ widget }: { widget: Widget }) => { 'liquity-borrow', ); + widgets.set( + 'liquity-adjust', + + ); /* If available, return the widget in the widgets map */ if (widgets.has(fnName)) { diff --git a/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx b/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx new file mode 100644 index 000000000..6f84f15d1 --- /dev/null +++ b/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx @@ -0,0 +1,114 @@ +import { useEffect, useState } from 'react'; +import { EthersLiquity } from '@liquity/lib-ethers'; +import { UnsignedTransaction } from 'ethers/lib/utils.js'; +import { + ActionResponse, + DoubleLineResponse, + HeaderResponse, + ListResponse, +} from '@/components/cactiComponents'; +import useSigner from '@/hooks/useSigner'; +import { cleanValue } from '@/utils'; + +interface AdjustProps { + borrowAmount?: string; + repayAmount?: string; + depositCollateral?: string; + withdrawCollateral?: string; +} + +const LiquityAdjust = ({ + borrowAmount, + repayAmount, + depositCollateral, + withdrawCollateral, +}: AdjustProps) => { + const signer = useSigner(); + + const handleInput = (amount: string | undefined) => { + if (!amount) return 0; + const cleaned = cleanValue(amount, 18) || '0'; + return isNaN(+cleaned) ? 0 : +cleaned; + }; + + const borrowLUSD = handleInput(borrowAmount); + const repayLUSD = handleInput(repayAmount); + const _withdrawCollateral = handleInput(withdrawCollateral); + const _depositCollateral = handleInput(depositCollateral); + const isBorrow = !!borrowAmount || !!depositCollateral; + const [label, setLabel] = useState(); + + const [sendParams, setSendParams] = useState(); + + useEffect(() => { + (async () => { + if (!signer) return; + + const liquity = await EthersLiquity.connect(signer); + + // handle repay + const { rawPopulatedTransaction: repayParams } = await liquity.populate.adjustTrove({ + repayLUSD, + withdrawCollateral: _withdrawCollateral, + }); + + // handle borrow + const { rawPopulatedTransaction: borrowParams } = await liquity.populate.adjustTrove({ + borrowLUSD, + depositCollateral: _depositCollateral, + }); + + // assess if borrow or repay + const params = isBorrow ? borrowParams : repayParams; + + setLabel( + isBorrow + ? `Adjust Trove: Borrow ${borrowLUSD} ${'LUSD'} using ${_depositCollateral} ${'ETH'}` + : `Adjust Trove: Repay ${repayLUSD} ${'LUSD'} and withdraw ${_withdrawCollateral} ${'ETH'}` + ); + + setSendParams(params); + })(); + }, [_depositCollateral, _withdrawCollateral, borrowLUSD, isBorrow, repayLUSD, signer]); + + return ( + <> + + + + + + ); +}; + +export default LiquityAdjust; From f4a500e86bfa90b13ed2818411fdbac378e86310 Mon Sep 17 00:00:00 2001 From: marcomariscal Date: Mon, 26 Jun 2023 13:13:33 -0700 Subject: [PATCH 2/8] fix: labels --- .../widgets/liquity/adjust/LiquityAdjust.tsx | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx b/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx index 6f84f15d1..1c2129b36 100644 --- a/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx +++ b/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx @@ -87,17 +87,11 @@ const LiquityAdjust = ({ From 4b8b1850ee48f872a58f24ecb653d8538dee1d17 Mon Sep 17 00:00:00 2001 From: marcomariscal Date: Mon, 26 Jun 2023 13:16:32 -0700 Subject: [PATCH 3/8] fix: logic --- .../widgets/liquity/adjust/LiquityAdjust.tsx | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx b/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx index 1c2129b36..dc6be4a1c 100644 --- a/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx +++ b/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx @@ -46,20 +46,15 @@ const LiquityAdjust = ({ const liquity = await EthersLiquity.connect(signer); - // handle repay - const { rawPopulatedTransaction: repayParams } = await liquity.populate.adjustTrove({ - repayLUSD, - withdrawCollateral: _withdrawCollateral, - }); - - // handle borrow - const { rawPopulatedTransaction: borrowParams } = await liquity.populate.adjustTrove({ - borrowLUSD, - depositCollateral: _depositCollateral, - }); - - // assess if borrow or repay - const params = isBorrow ? borrowParams : repayParams; + const { rawPopulatedTransaction: params } = isBorrow + ? await liquity.populate.adjustTrove({ + borrowLUSD, + depositCollateral: _depositCollateral, + }) + : await liquity.populate.adjustTrove({ + repayLUSD, + withdrawCollateral: _withdrawCollateral, + }); setLabel( isBorrow From 66a131c791ff2ea4a6d166b89bd2c390819d6f57 Mon Sep 17 00:00:00 2001 From: marcomariscal Date: Mon, 26 Jun 2023 15:02:44 -0700 Subject: [PATCH 4/8] fix: logic --- .../widgets/liquity/adjust/LiquityAdjust.tsx | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx b/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx index dc6be4a1c..39f9f5643 100644 --- a/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx +++ b/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx @@ -1,4 +1,5 @@ import { useEffect, useState } from 'react'; +import { Decimalish, TroveAdjustmentParams } from '@liquity/lib-base'; import { EthersLiquity } from '@liquity/lib-ethers'; import { UnsignedTransaction } from 'ethers/lib/utils.js'; import { @@ -26,9 +27,11 @@ const LiquityAdjust = ({ const signer = useSigner(); const handleInput = (amount: string | undefined) => { - if (!amount) return 0; - const cleaned = cleanValue(amount, 18) || '0'; - return isNaN(+cleaned) ? 0 : +cleaned; + if (!amount) return undefined; + const cleaned = cleanValue(amount, 18); + if (!cleaned) return undefined; + const num = +cleaned; + return isNaN(num) || num === 0 ? undefined : +cleaned; }; const borrowLUSD = handleInput(borrowAmount); @@ -45,16 +48,15 @@ const LiquityAdjust = ({ if (!signer) return; const liquity = await EthersLiquity.connect(signer); + const validParams = {} as TroveAdjustmentParams; - const { rawPopulatedTransaction: params } = isBorrow - ? await liquity.populate.adjustTrove({ - borrowLUSD, - depositCollateral: _depositCollateral, - }) - : await liquity.populate.adjustTrove({ - repayLUSD, - withdrawCollateral: _withdrawCollateral, - }); + if (borrowLUSD) validParams.borrowLUSD = borrowLUSD; + if (repayLUSD) validParams.repayLUSD = repayLUSD; + if (_depositCollateral) validParams.depositCollateral = _depositCollateral; + if (_withdrawCollateral) validParams.withdrawCollateral = _withdrawCollateral; + + const { rawPopulatedTransaction: params } = await liquity.populate.adjustTrove(validParams); + setSendParams(params); setLabel( isBorrow @@ -73,12 +75,10 @@ const LiquityAdjust = ({ projectName="Liquity" altUrl={`https://www.liquity.org/`} /> - + {borrowAmount && } + {repayAmount && } + {depositCollateral && } + {withdrawCollateral && } Date: Mon, 26 Jun 2023 15:10:28 -0700 Subject: [PATCH 5/8] fix: labels --- .../widgets/liquity/adjust/LiquityAdjust.tsx | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx b/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx index 39f9f5643..bcd6782a9 100644 --- a/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx +++ b/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx @@ -38,7 +38,7 @@ const LiquityAdjust = ({ const repayLUSD = handleInput(repayAmount); const _withdrawCollateral = handleInput(withdrawCollateral); const _depositCollateral = handleInput(depositCollateral); - const isBorrow = !!borrowAmount || !!depositCollateral; + const isBorrow = !!borrowLUSD || !!_depositCollateral; const [label, setLabel] = useState(); const [sendParams, setSendParams] = useState(); @@ -55,15 +55,29 @@ const LiquityAdjust = ({ if (_depositCollateral) validParams.depositCollateral = _depositCollateral; if (_withdrawCollateral) validParams.withdrawCollateral = _withdrawCollateral; + // handle if for some reason there is a borrow and a repay input + if (borrowLUSD && repayLUSD) { + validParams.repayLUSD = 0; // TODO handle more gracefully to identify what user actually wants to do + } + + // handle if for some reason there is a deposit and a withdraw input + if (_depositCollateral && _withdrawCollateral) { + validParams.withdrawCollateral = 0; // TODO handle more gracefully to identify what user actually wants to do + } + const { rawPopulatedTransaction: params } = await liquity.populate.adjustTrove(validParams); setSendParams(params); - setLabel( - isBorrow - ? `Adjust Trove: Borrow ${borrowLUSD} ${'LUSD'} using ${_depositCollateral} ${'ETH'}` - : `Adjust Trove: Repay ${repayLUSD} ${'LUSD'} and withdraw ${_withdrawCollateral} ${'ETH'}` - ); + // handle repay label + const labels: { [action: string]: string } = {}; + if (repayLUSD) labels.repayLUSD = `Repay ${repayLUSD} ${'LUSD'}`; + if (_withdrawCollateral) + labels.withdrawCollateral = `Withdraw ${_withdrawCollateral} ${'ETH'}`; + if (borrowLUSD) labels.borrowLUSD = `Borrow ${borrowLUSD} ${'LUSD'}`; + if (_depositCollateral) labels.depositCollateral = `Deposit ${_depositCollateral} ${'ETH'}`; + const label = Object.values(labels).join(' and '); + setLabel(label); setSendParams(params); })(); }, [_depositCollateral, _withdrawCollateral, borrowLUSD, isBorrow, repayLUSD, signer]); From 40c8bb366194385be959ab84544f0b7bd9a780ca Mon Sep 17 00:00:00 2001 From: marcomariscal Date: Mon, 26 Jun 2023 15:19:50 -0700 Subject: [PATCH 6/8] fix: labels --- .../widgets/liquity/adjust/LiquityAdjust.tsx | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx b/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx index bcd6782a9..af5a55af6 100644 --- a/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx +++ b/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx @@ -1,6 +1,7 @@ import { useEffect, useState } from 'react'; import { Decimalish, TroveAdjustmentParams } from '@liquity/lib-base'; import { EthersLiquity } from '@liquity/lib-ethers'; +import { validateAndParseAddress } from '@uniswap/sdk-core'; import { UnsignedTransaction } from 'ethers/lib/utils.js'; import { ActionResponse, @@ -38,9 +39,9 @@ const LiquityAdjust = ({ const repayLUSD = handleInput(repayAmount); const _withdrawCollateral = handleInput(withdrawCollateral); const _depositCollateral = handleInput(depositCollateral); - const isBorrow = !!borrowLUSD || !!_depositCollateral; - const [label, setLabel] = useState(); + const [validLiquityParams, setValidLiquityParams] = useState>(); + const [label, setLabel] = useState(); const [sendParams, setSendParams] = useState(); useEffect(() => { @@ -65,22 +66,23 @@ const LiquityAdjust = ({ validParams.withdrawCollateral = 0; // TODO handle more gracefully to identify what user actually wants to do } + setValidLiquityParams(validParams); const { rawPopulatedTransaction: params } = await liquity.populate.adjustTrove(validParams); setSendParams(params); - // handle repay label + // handle labels for each param const labels: { [action: string]: string } = {}; + if (repayLUSD) labels.repayLUSD = `Repay ${repayLUSD} ${'LUSD'}`; if (_withdrawCollateral) labels.withdrawCollateral = `Withdraw ${_withdrawCollateral} ${'ETH'}`; if (borrowLUSD) labels.borrowLUSD = `Borrow ${borrowLUSD} ${'LUSD'}`; if (_depositCollateral) labels.depositCollateral = `Deposit ${_depositCollateral} ${'ETH'}`; - const label = Object.values(labels).join(' and '); + const label = Object.values(labels).join(' and '); setLabel(label); - setSendParams(params); })(); - }, [_depositCollateral, _withdrawCollateral, borrowLUSD, isBorrow, repayLUSD, signer]); + }, [_depositCollateral, _withdrawCollateral, borrowLUSD, repayLUSD, signer]); return ( <> @@ -89,18 +91,26 @@ const LiquityAdjust = ({ projectName="Liquity" altUrl={`https://www.liquity.org/`} /> - {borrowAmount && } - {repayAmount && } - {depositCollateral && } - {withdrawCollateral && } + {validLiquityParams?.borrowLUSD && ( + + )} + {validLiquityParams?.repayLUSD && ( + + )} + {validLiquityParams?.depositCollateral && ( + + )} + {validLiquityParams?.withdrawCollateral && ( + + )} From e4bcb3090945a541160170f962f9950ac5770352 Mon Sep 17 00:00:00 2001 From: marcomariscal Date: Tue, 27 Jun 2023 10:43:05 -0700 Subject: [PATCH 7/8] fix: labels --- .../widgets/liquity/adjust/LiquityAdjust.tsx | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx b/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx index af5a55af6..feabc00bc 100644 --- a/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx +++ b/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx @@ -107,11 +107,17 @@ const LiquityAdjust = ({ title="Breakdown" data={[ ['New Collateralization Ratio', 'something'], - ['Borrow LUSD', validLiquityParams?.borrowLUSD], - ['Repay LUSD', validLiquityParams?.repayLUSD], - ['Deposit ETH', validLiquityParams?.depositCollateral], - ['Withdraw ETH', validLiquityParams?.withdrawCollateral], - ]} + validLiquityParams?.borrowLUSD && ['Borrow LUSD', validLiquityParams?.borrowLUSD], + validLiquityParams?.repayLUSD && ['Repay LUSD', validLiquityParams?.repayLUSD], + validLiquityParams?.depositCollateral && [ + 'Deposit ETH', + validLiquityParams?.depositCollateral, + ], + validLiquityParams?.withdrawCollateral && [ + 'Withdraw ETH', + validLiquityParams?.withdrawCollateral, + ], + ].filter(Boolean)} collapsible /> Date: Tue, 27 Jun 2023 10:48:44 -0700 Subject: [PATCH 8/8] chore: remove unused --- .../experimental_/widgets/liquity/adjust/LiquityAdjust.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx b/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx index feabc00bc..3a276fc2e 100644 --- a/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx +++ b/src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx @@ -1,7 +1,6 @@ import { useEffect, useState } from 'react'; import { Decimalish, TroveAdjustmentParams } from '@liquity/lib-base'; import { EthersLiquity } from '@liquity/lib-ethers'; -import { validateAndParseAddress } from '@uniswap/sdk-core'; import { UnsignedTransaction } from 'ethers/lib/utils.js'; import { ActionResponse,