Skip to content

Commit

Permalink
fix setting collateral
Browse files Browse the repository at this point in the history
  • Loading branch information
vidvidvid committed Dec 5, 2024
1 parent fea2f56 commit b0b4f1e
Showing 1 changed file with 112 additions and 117 deletions.
229 changes: 112 additions & 117 deletions packages/ui/hooks/market/useCollateralToggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { toast } from 'react-hot-toast';

import { useTransactionSteps } from '@ui/app/_components/dialogs/manage/TransactionStepsHandler';
import { INFO_MESSAGES } from '@ui/constants';
import {
TransactionType,
useManageDialogContext
} from '@ui/context/ManageDialogContext';
import { useMultiIonic } from '@ui/context/MultiIonicContext';
import type { MarketData } from '@ui/types/TokensDataMap';
import { errorCodeToMessage } from '@ui/utils/errorCodeToMessage';
Expand All @@ -26,8 +30,8 @@ export const useCollateralToggle = ({
);

const { currentSdk } = useMultiIonic();
const { addStepsForAction, transactionSteps, upsertTransactionStep } =
useTransactionSteps();
const { addStepsForType, upsertStepForType } = useManageDialogContext();
const { transactionSteps } = useTransactionSteps();

const handleCollateralToggle = async () => {
if (!transactionSteps.length) {
Expand All @@ -37,136 +41,127 @@ export const useCollateralToggle = ({
try {
let tx;

switch (enableCollateral) {
case true: {
const comptrollerContract = currentSdk.createComptroller(
comptrollerAddress,
currentSdk.publicClient
);

const exitCode = (
await comptrollerContract.simulate.exitMarket(
[selectedMarketData.cToken],
{ account: currentSdk.walletClient!.account!.address }
)
).result;

if (exitCode !== 0n) {
toast.error(errorCodeToMessage(Number(exitCode)));
return;
}
if (enableCollateral) {
const comptrollerContract = currentSdk.createComptroller(
comptrollerAddress,
currentSdk.publicClient
);

addStepsForAction([
{
error: false,
message: INFO_MESSAGES.COLLATERAL.DISABLE,
success: false
}
]);

upsertTransactionStep({
index: currentTransactionStep,
transactionStep: {
error: false,
message: INFO_MESSAGES.COLLATERAL.DISABLE,
success: false
}
});

tx = await comptrollerContract.write.exitMarket(
const exitCode = (
await comptrollerContract.simulate.exitMarket(
[selectedMarketData.cToken],
{
account: currentSdk.walletClient!.account!.address,
chain: currentSdk.publicClient.chain
}
);

upsertTransactionStep({
index: currentTransactionStep,
transactionStep: {
...transactionSteps[currentTransactionStep],
txHash: tx
}
});

await currentSdk.publicClient.waitForTransactionReceipt({
hash: tx
});

setEnableCollateral(false);

upsertTransactionStep({
index: currentTransactionStep,
transactionStep: {
...transactionSteps[currentTransactionStep],
success: true
}
});

break;
}
{ account: currentSdk.walletClient!.account!.address }
)
).result;

case false: {
addStepsForAction([
{
error: false,
message: INFO_MESSAGES.COLLATERAL.ENABLE,
success: false
}
]);

upsertTransactionStep({
index: currentTransactionStep,
transactionStep: {
error: false,
message: INFO_MESSAGES.COLLATERAL.ENABLE,
success: false
}
});

tx = await currentSdk.enterMarkets(
selectedMarketData.cToken,
comptrollerAddress
);

upsertTransactionStep({
index: currentTransactionStep,
transactionStep: {
...transactionSteps[currentTransactionStep],
txHash: tx
}
});

await currentSdk.publicClient.waitForTransactionReceipt({
hash: tx
});

setEnableCollateral(true);

upsertTransactionStep({
index: currentTransactionStep,
transactionStep: {
...transactionSteps[currentTransactionStep],
success: true
}
});

break;
if (exitCode !== 0n) {
toast.error(errorCodeToMessage(Number(exitCode)));
return;
}

addStepsForType(TransactionType.COLLATERAL, [
{
error: false,
message: INFO_MESSAGES.COLLATERAL.DISABLE,
success: false
}
]);

tx = await comptrollerContract.write.exitMarket(
[selectedMarketData.cToken],
{
account: currentSdk.walletClient!.account!.address,
chain: currentSdk.publicClient.chain
}
);

upsertStepForType(TransactionType.COLLATERAL, {
index: currentTransactionStep,
transactionStep: {
error: false,
message: INFO_MESSAGES.COLLATERAL.DISABLE,
txHash: tx,
success: false
}
});

await currentSdk.publicClient.waitForTransactionReceipt({
hash: tx
});

setEnableCollateral(false);

upsertStepForType(TransactionType.COLLATERAL, {
index: currentTransactionStep,
transactionStep: {
error: false,
message: INFO_MESSAGES.COLLATERAL.DISABLE,
txHash: tx,
success: true
}
});
} else {
addStepsForType(TransactionType.COLLATERAL, [
{
error: false,
message: INFO_MESSAGES.COLLATERAL.ENABLE,
success: false
}
]);

tx = await currentSdk.enterMarkets(
selectedMarketData.cToken,
comptrollerAddress
);

upsertStepForType(TransactionType.COLLATERAL, {
index: currentTransactionStep,
transactionStep: {
error: false,
message: INFO_MESSAGES.COLLATERAL.ENABLE,
txHash: tx,
success: false
}
});

await currentSdk.publicClient.waitForTransactionReceipt({
hash: tx
});

setEnableCollateral(true);

upsertStepForType(TransactionType.COLLATERAL, {
index: currentTransactionStep,
transactionStep: {
error: false,
message: INFO_MESSAGES.COLLATERAL.ENABLE,
txHash: tx,
success: true
}
});
}

await onSuccess();
return;
} catch (error) {
console.error(error);

upsertTransactionStep({
upsertStepForType(TransactionType.COLLATERAL, {
index: currentTransactionStep,
transactionStep: {
...transactionSteps[currentTransactionStep],
error: true
error: true,
message: enableCollateral
? INFO_MESSAGES.COLLATERAL.DISABLE
: INFO_MESSAGES.COLLATERAL.ENABLE,
success: false
}
});

toast.error(
`Error while ${
enableCollateral ? 'disabling' : 'enabling'
} collateral!`
);
}
}

Expand Down

0 comments on commit b0b4f1e

Please sign in to comment.