Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 8 additions & 16 deletions src/hooks/useWrite.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useCallback, useMemo, useState } from "react";
import { useContractWrite, usePrepareContractWrite, useNetwork, useSigner } from "wagmi";

import { Status } from "constants";
import praseAppLog from "utils/praseAppLog";
import createParseToast from "utils/parseToast";
import useContract from "hooks/useContract";
import parseAppLog from "utils/parseAppLog";
import { Status } from "constants";
import { useToasts } from "providers/Toasts";
import { useAppLogs } from "providers/AppLogs";
import { useVersion } from "providers/Version";
import { useSettings } from "providers/Settings";
import { useToken } from "hooks/useToken";

export default function useWrite({
disabled: isDisabled,
Expand All @@ -19,16 +19,15 @@ export default function useWrite({
onComplete,
overrides = undefined,
}) {
const [loading, setLoading] = useState(false);

const { data: signer } = useSigner();
const { version } = useVersion();
const { chain } = useNetwork();
const { addLog } = useAppLogs();
const { addToast, closeToast } = useToasts();
const { token } = useToken();
const contractConfig = useContract(contract);
const [loading, setLoading] = useState(false);
const {
settings: { useToken },
} = useSettings();

const memoisedArgs = useMemo(() => args, [JSON.stringify(args)]);

Expand All @@ -53,14 +52,7 @@ export default function useWrite({
const onClick = useCallback(async () => {
setLoading(true);

const parseToast = createParseToast(
method,
memoisedArgs,
useToken,
chain.id,
version,
useToken
);
const parseToast = createParseToast(method, memoisedArgs, token, chain.id, version, useToken);

let toastId = addToast(parseToast(Status.PENDING, null), false);

Expand All @@ -78,7 +70,7 @@ export default function useWrite({

onComplete && (await onComplete());

addLog(praseAppLog(Status.SUCCESS, method, memoisedArgs, tx, useToken));
addLog(parseAppLog(Status.SUCCESS, method, memoisedArgs, tx, useToken));
addToast(parseToast(response.status ? Status.SUCCESS : Status.FAILED, tx));

return true;
Expand Down
86 changes: 42 additions & 44 deletions src/utils/parseToast.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Versions } from "providers/Version";
export default function createParseToast(
method,
args,
useToken,
token,
chainId = mainnet.id,
version,
contract
Expand All @@ -23,26 +23,26 @@ export default function createParseToast(

if (status === Status.SUCCESS) {
/*--------------------------------------------------------------
Successfull transactions
Successful transactions
--------------------------------------------------------------*/

switch (method) {
case "stake":
return {
...sharedProps,
content: `Staked ${format(args[0], useToken)} ${useToken} successfully`,
title: "Staked DAI",
content: `Staked ${format(args[0], token)} ${token} successfully`,
title: `Stake ${token}`,
};
case "unstake":
return {
...sharedProps,
content: `Unstaked ${format(args[0], useToken)} ${useToken} successfully`,
title: "Unstaked DAI",
content: `Unstaked ${format(args[0], token)} ${token} successfully`,
title: `Unstake ${token}`,
};
case "updateTrust":
return {
...sharedProps,
content: `Vouched ${format(args[1], useToken)} for ${truncateAddress(
content: `Vouched ${format(args[1], token)} for ${truncateAddress(
args[0]
)} successfully`,
title: "Vouched",
Expand All @@ -51,32 +51,32 @@ export default function createParseToast(
return {
...sharedProps,
content: `Approved ${
ethers.constants.MaxUint256.eq(args[1]) ? "max" : format(args[1], useToken)
ethers.constants.MaxUint256.eq(args[1]) ? "max" : format(args[1], token)
} successfully`,
title: "Approved",
title: `Approve ${token}`,
};
case "borrow":
const borrowAmount = version === Versions.V2 ? args[1] : args[0];
return {
...sharedProps,
content: `Borrowed ${format(borrowAmount, useToken)} ${useToken} successfully`,
title: "Borrowed",
content: `Borrowed ${format(borrowAmount, token)} ${token} successfully`,
title: `Borrow ${token}`,
};
case "repayBorrow":
const repayAmount = version === Versions.V2 ? args[1] : args[0];
const amount = ethers.constants.MaxUint256.eq(repayAmount)
? "maximum"
: format(repayAmount, useToken);
: format(repayAmount, token);
return {
...sharedProps,
content: `Repaid ${amount} ${useToken} successfully`,
title: "Repaid borrow",
content: `Repaid ${amount} ${token} successfully`,
title: `Repay ${token}`,
};
case "delegate":
return {
...sharedProps,
content: `Delegated to ${truncateAddress(args[0])} successfully`,
title: "Delegated",
title: "Delegate",
};
default:
return {
Expand All @@ -94,51 +94,51 @@ export default function createParseToast(
case "stake":
return {
...sharedProps,
content: `Staking ${format(args[0], useToken)} ${useToken} failed`,
title: "Staking DAI",
content: `Staking ${format(args[0], token)} ${token} failed`,
title: `Stake ${token}`,
};
case "unstake":
return {
...sharedProps,
content: `Unstaking ${format(args[0], useToken)} ${useToken} failed`,
title: "Unstake DAI",
content: `Unstaking ${format(args[0], token)} ${token} failed`,
title: `Unstake ${token}`,
};
case "updateTrust":
return {
...sharedProps,
content: `Vouching ${format(args[1], useToken)} ${useToken} for ${truncateAddress(
content: `Vouching ${format(args[1], token)} ${token} for ${truncateAddress(
args[0]
)} failed`,
title: "Vouching",
title: "Vouch",
};
case "approve":
return {
...sharedProps,
content: "Approving failed",
title: "Approving",
title: `Approve ${token}`,
};
case "borrow":
const borrowAmount = version === Versions.V2 ? args[1] : args[0];
return {
...sharedProps,
content: `Borrowing ${format(borrowAmount, useToken)} failed`,
title: "Borrowing",
content: `Borrowing ${format(borrowAmount, token)} failed`,
title: `Borrow ${token}`,
};
case "repayBorrow":
const repayAmount = version === Versions.V2 ? args[1] : args[0];
const amount = ethers.constants.MaxUint256.eq(repayAmount)
? "maximum"
: format(repayAmount, useToken);
: format(repayAmount, token);
return {
...sharedProps,
content: `Repaying ${amount} ${useToken} failed`,
title: "Repaying",
content: `Repaying ${amount} ${token} failed`,
title: `Repay ${token}`,
};
case "delegate":
return {
...sharedProps,
content: `Delegating to ${truncateAddress(args[0])} failed`,
title: "Delegating",
title: "Delegate",
};
default:
return {
Expand All @@ -156,51 +156,49 @@ export default function createParseToast(
case "stake":
return {
...sharedProps,
content: `Staking ${format(args[0], useToken)} ${useToken}`,
title: "Staking DAI",
content: `Staking ${format(args[0], token)} ${token}`,
title: `Stake ${token}`,
};
case "unstake":
return {
...sharedProps,
content: `Unstaking ${format(args[0], useToken)} ${useToken}`,
title: "Unstake DAI",
content: `Unstaking ${format(args[0], token)} ${token}`,
title: `Unstake ${token}`,
};
case "updateTrust":
return {
...sharedProps,
content: `Vouching ${format(args[1], useToken)} ${useToken} for ${truncateAddress(
args[0]
)}`,
title: "Vouching",
content: `Vouching ${format(args[1], token)} ${token} for ${truncateAddress(args[0])}`,
title: "Vouch",
};
case "approve":
return {
...sharedProps,
content: `Approving DAI for ${contract === "userManager" ? "Staking" : "Repay"}`,
title: "Approving",
content: `Approving ${token} for ${contract === "userManager" ? "Staking" : "Repay"}`,
title: `Approve ${token}`,
};
case "borrow":
const borrowAmount = version === Versions.V2 ? args[1] : args[0];
return {
...sharedProps,
content: `Borrowing ${format(borrowAmount, useToken)}`,
title: "Borrowing",
content: `Borrowing ${format(borrowAmount, token)} ${token}`,
title: `Borrow ${token}`,
};
case "repayBorrow":
const repayAmount = version === Versions.V2 ? args[1] : args[0];
const amount = ethers.constants.MaxUint256.eq(repayAmount)
? "maximum"
: format(repayAmount, useToken);
: format(repayAmount, token);
return {
...sharedProps,
content: `Repaying ${amount} ${useToken}`,
title: "Repaying",
content: `Repaying ${amount} ${token}`,
title: `Repay ${token}`,
};
case "delegate":
return {
...sharedProps,
content: `Delegating to ${truncateAddress(args[0])}`,
title: "Delegating",
title: "Delegate",
};
default:
return {
Expand Down
12 changes: 0 additions & 12 deletions src/utils/praseAppLog.js

This file was deleted.