Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integration: liquity adjust #296

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: liquity adjust
  • Loading branch information
marcomariscal committed Jun 26, 2023
commit a6b65bec0085f9da1a7e90556ffc74b4ec1b2c92
10 changes: 10 additions & 0 deletions src/components/experimental_/MessageTranslator_.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -127,6 +128,15 @@ const Widget = ({ widget }: { widget: Widget }) => {
'liquity-borrow',
<LiquityBorrow borrowAmount={parsedArgs[0]} collateralAmount={parsedArgs[1]} />
);
widgets.set(
'liquity-adjust',
<LiquityAdjust
borrowAmount={parsedArgs[0]}
repayAmount={parsedArgs[1]}
depositCollateral={parsedArgs[2]}
withdrawCollateral={parsedArgs[3]}
/>
);

/* If available, return the widget in the widgets map */
if (widgets.has(fnName)) {
Expand Down
114 changes: 114 additions & 0 deletions src/components/experimental_/widgets/liquity/adjust/LiquityAdjust.tsx
Original file line number Diff line number Diff line change
@@ -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<string>();

const [sendParams, setSendParams] = useState<UnsignedTransaction>();

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 (
<>
<HeaderResponse
text="Adjust Trove with Liquity"
projectName="Liquity"
altUrl={`https://www.liquity.org/`}
/>
<DoubleLineResponse
tokenSymbol={'LUSD'}
tokenValueInUsd={1}
amount={borrowLUSD}
amountValueInUsd={borrowLUSD}
/>
<ListResponse
title="Breakdown"
data={[
'New Collateralization Ratio',
'something',
...(isBorrow
? [
['Borrow Amount', borrowLUSD],
['Deposit Collateral', _depositCollateral],
]
: [
['Repay Amount', repayLUSD],
['Withdraw Collateral', _withdrawCollateral],
]),
]}
collapsible
/>
<ActionResponse
label={label}
txParams={undefined}
sendParams={sendParams}
approvalParams={undefined}
/>
</>
);
};

export default LiquityAdjust;