Skip to content

Commit

Permalink
fix(claim-dapp): add allowance check before approve (UMAprotocol#24)
Browse files Browse the repository at this point in the history
* fix(claim-dapp): add allowance check before approve

Signed-off-by: Gamaranto <[email protected]>

* fix(claim-dapp): correct INIFITE_APPROVAL_AMOUNT

Signed-off-by: Gamaranto <[email protected]>

* fix(claim-dapp): use ethers constant

Signed-off-by: Gamaranto <[email protected]>
  • Loading branch information
Francesco authored Jul 7, 2021
1 parent 2ed3a73 commit 4f23957
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/hooks/useRedeem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { useTransactions } from "./useTransactions";
import { ethers } from "ethers";
import { getKpiOptionsEMP, getKpiTokenContract } from "../utils";

// max uint value is 2^256 - 1
const MAX_UINT_VAL = ethers.constants.MaxUint256;
const INFINITE_APPROVAL_AMOUNT = MAX_UINT_VAL;

export function useRedeem() {
const { account, signer, chainId } = useConnection();
const [error, setError] = React.useState<Error>();
Expand All @@ -21,12 +25,18 @@ export function useRedeem() {
const kpiOptionsToken = getKpiTokenContract(signer, chainId);
const emp = getKpiOptionsEMP(signer, chainId);

const approveTx = await kpiOptionsToken.approve(
emp.address,
ethers.utils.parseEther("20000000000000000000")
);
addTransaction({ ...approveTx, label: "approve" });
await approveTx.wait();
const allowance = await kpiOptionsToken.allowance(account, emp.address);
const balance = await kpiOptionsToken.balanceOf(account);
const hasToApprove = allowance.lt(balance);
if (hasToApprove) {
const approveTx = await kpiOptionsToken.approve(
emp.address,
INFINITE_APPROVAL_AMOUNT
);

addTransaction({ ...approveTx, label: "approve" });
await approveTx.wait();
}
const onComplete = () => {
// This will refetch all queries that have a key starting with "balance"
queryClient.invalidateQueries("balance");
Expand Down

0 comments on commit 4f23957

Please sign in to comment.