Skip to content

Commit

Permalink
feat(State2): Show correct withdrawable DEFI++ tokens and update with…
Browse files Browse the repository at this point in the history
…drawal flow
  • Loading branch information
gbonumore committed Jun 6, 2022
1 parent b6babb3 commit 37ec72c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
9 changes: 7 additions & 2 deletions components/balance.tsx
@@ -1,4 +1,4 @@
import { ethers } from "ethers";
import { BigNumber, ethers } from "ethers";
import { useStoreState } from "hooks/useStore";
import { MigratorOpenState } from "store/slice";
import { useConnect } from "wagmi";
Expand Down Expand Up @@ -87,7 +87,12 @@ const BalanceState: React.FC = () => {
Your can withdraw:{" "}
<span className="text-primary-dark font-bold">
{formatDisplayNumber(
ethers.utils.formatUnits(state.userDeposits, state.decimals)
ethers.utils.formatUnits(
BigNumber.from(state.userDeposits).mul(
BigNumber.from(state.rate)
),
state.decimals
)
)}{" "}
DEFI++
</span>
Expand Down
5 changes: 5 additions & 0 deletions components/notification.tsx
Expand Up @@ -26,6 +26,11 @@ export const approvalSuccessNotification = () => {
toast("Approval Successful!", { type: "success" });
};

export const withdrawalSuccessNotification = () => {
toast.dismiss();
toast("Withdrawal Successful!", { type: "success" });
};

export const errorNotification = (err: string) => {
toast.dismiss();
toast(`Deposit Failed... ${err}`, { type: "error" });
Expand Down
26 changes: 25 additions & 1 deletion store/slice.ts
@@ -1,12 +1,18 @@
import {
approvalSuccessNotification,
depositSuccessNotification,
withdrawalSuccessNotification,
errorNotification,
infoNotification,
} from "@components/notification";
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { BigNumber, ethers } from "ethers";
import { thunkApprove, thunkDeposit, thunkGetData } from "./thunks";
import {
thunkApprove,
thunkDeposit,
thunkGetData,
thunkWithdraw,
} from "./thunks";

export enum MigratorOpenState {
Open,
Expand All @@ -25,6 +31,7 @@ export type RootState = {
approvalLimit: BigNumberString;
userDeposits: BigNumberString;
loading: boolean;
rate: BigNumberString;
};

const appSlice = createSlice({
Expand All @@ -37,6 +44,7 @@ const appSlice = createSlice({
approvalLimit: "0",
userDeposits: "0",
loading: false,
rate: "0",
} as RootState,

extraReducers: (builder) => {
Expand Down Expand Up @@ -77,6 +85,21 @@ const appSlice = createSlice({
errorNotification(action.error.message ?? "Unknown Error");
});

builder.addCase(thunkWithdraw.pending, (state) => {
state.loading = true;
infoNotification("Withdrawal Request In Progress...");
});

builder.addCase(thunkWithdraw.rejected, (state, action) => {
state.loading = false;
errorNotification(action.error.message ?? "Unknown Error");
});

builder.addCase(thunkWithdraw.fulfilled, (state) => {
state.loading = false;
withdrawalSuccessNotification();
});

builder.addCase(thunkGetData.pending, (state) => {
state.loading = true;
});
Expand Down Expand Up @@ -110,6 +133,7 @@ const appSlice = createSlice({
state.migratorOpenState = action.payload.newState.migratorOpenState;
state.totalDeposits = action.payload.newState.totalDeposits;
state.userDeposits = action.payload.newState.userDeposits;
state.rate = action.payload.newState.rate;
},

setDeposit: (state, action: PayloadAction<{ depositAmount: string }>) => {
Expand Down
3 changes: 3 additions & 0 deletions store/thunks.ts
Expand Up @@ -90,6 +90,7 @@ export const thunkGetData = createAsyncThunk(
const approvalLimit = account
? bdi.allowance(account, addresses.contracts.BASKET_MIGRATOR)
: 0;
const rate = migrator.rate();

const results = await promiseObject({
decimals,
Expand All @@ -98,6 +99,7 @@ export const thunkGetData = createAsyncThunk(
balance,
approvalLimit,
userDeposits,
rate,
});

return {
Expand All @@ -107,6 +109,7 @@ export const thunkGetData = createAsyncThunk(
totalDeposits: results.totalDeposits.toString(),
migratorOpenState: results.migratorOpenState,
userDeposits: results.userDeposits.toString(),
rate: results.rate.toString(),
};
}
);

0 comments on commit 37ec72c

Please sign in to comment.