forked from terra-money/wormhole
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
approve unlimited functionality in eth
Change-Id: Id268205f540cad6b3936570c650e209fe0220339
- Loading branch information
Showing
6 changed files
with
242 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { | ||
approveEth, | ||
ChainId, | ||
CHAIN_ID_ETH, | ||
getAllowanceEth, | ||
} from "@certusone/wormhole-sdk"; | ||
import { BigNumber } from "ethers"; | ||
import { useEffect, useMemo, useState } from "react"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
import { useEthereumProvider } from "../contexts/EthereumProviderContext"; | ||
import { selectTransferIsApproving } from "../store/selectors"; | ||
import { setIsApproving } from "../store/transferSlice"; | ||
import { ETH_TOKEN_BRIDGE_ADDRESS } from "../utils/consts"; | ||
|
||
export default function useAllowance( | ||
chainId: ChainId, | ||
tokenAddress?: string, | ||
transferAmount?: BigInt | ||
) { | ||
const dispatch = useDispatch(); | ||
const [allowance, setAllowance] = useState<BigInt | null>(null); | ||
const [isAllowanceFetching, setIsAllowanceFetching] = useState(false); | ||
const isApproveProcessing = useSelector(selectTransferIsApproving); | ||
const { signer } = useEthereumProvider(); | ||
const sufficientAllowance = | ||
chainId !== CHAIN_ID_ETH || | ||
(allowance && transferAmount && allowance >= transferAmount); | ||
|
||
useEffect(() => { | ||
let cancelled = false; | ||
if ( | ||
chainId === CHAIN_ID_ETH && | ||
tokenAddress && | ||
signer && | ||
!isApproveProcessing | ||
) { | ||
setIsAllowanceFetching(true); | ||
getAllowanceEth(ETH_TOKEN_BRIDGE_ADDRESS, tokenAddress, signer).then( | ||
(result) => { | ||
if (!cancelled) { | ||
setIsAllowanceFetching(false); | ||
setAllowance(result.toBigInt()); | ||
} | ||
}, | ||
(error) => { | ||
if (!cancelled) { | ||
setIsAllowanceFetching(false); | ||
//setError("Unable to retrieve allowance"); //TODO set an error | ||
} | ||
} | ||
); | ||
} | ||
|
||
return () => { | ||
cancelled = true; | ||
}; | ||
}, [chainId, tokenAddress, signer, isApproveProcessing]); | ||
|
||
const approveAmount: (amount: BigInt) => Promise<any> = useMemo(() => { | ||
return chainId !== CHAIN_ID_ETH || !tokenAddress || !signer | ||
? (amount: BigInt) => { | ||
return Promise.resolve(); | ||
} | ||
: (amount: BigInt) => { | ||
dispatch(setIsApproving(true)); | ||
return approveEth( | ||
ETH_TOKEN_BRIDGE_ADDRESS, | ||
tokenAddress, | ||
signer, | ||
BigNumber.from(amount) | ||
).then( | ||
() => { | ||
dispatch(setIsApproving(false)); | ||
return Promise.resolve(); | ||
}, | ||
() => { | ||
dispatch(setIsApproving(false)); | ||
return Promise.reject(); | ||
} | ||
); | ||
}; | ||
}, [chainId, tokenAddress, signer, dispatch]); | ||
|
||
return useMemo( | ||
() => ({ | ||
sufficientAllowance, | ||
approveAmount, | ||
isAllowanceFetching, | ||
isApproveProcessing, | ||
}), | ||
[ | ||
sufficientAllowance, | ||
approveAmount, | ||
isAllowanceFetching, | ||
isApproveProcessing, | ||
] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters