From 0ed85d1a25dd6dbf7cf7e3cf567b12cf5334f967 Mon Sep 17 00:00:00 2001
From: Piotr Roslaniec
Date: Tue, 5 Dec 2023 16:49:18 +0100
Subject: [PATCH] wip
---
src/web3/hooks/useApproveAndCallTStaking.ts | 18 ++++++++++++++++
src/web3/hooks/useStakeTransaction.ts | 24 ++++++++++++---------
src/web3/hooks/userApproveAndCall.ts | 24 +++++++++++++++++++++
3 files changed, 56 insertions(+), 10 deletions(-)
create mode 100644 src/web3/hooks/useApproveAndCallTStaking.ts
create mode 100644 src/web3/hooks/userApproveAndCall.ts
diff --git a/src/web3/hooks/useApproveAndCallTStaking.ts b/src/web3/hooks/useApproveAndCallTStaking.ts
new file mode 100644
index 000000000..e2569a218
--- /dev/null
+++ b/src/web3/hooks/useApproveAndCallTStaking.ts
@@ -0,0 +1,18 @@
+import { useToken } from "../../hooks/useToken"
+import { Token } from "../../enums"
+import { useTStakingContract } from "./useTStakingContract"
+import useApproveAndCall from "./userApproveAndCall"
+
+const useApproveAndCallTStaking = (onSuccess?: () => Promise | void) => {
+ const tToken = useToken(Token.T)
+ const tStakingContract = useTStakingContract()
+
+ return useApproveAndCall(
+ tToken.contract!,
+ tStakingContract?.address,
+ [],
+ onSuccess
+ )
+}
+
+export default useApproveAndCallTStaking
diff --git a/src/web3/hooks/useStakeTransaction.ts b/src/web3/hooks/useStakeTransaction.ts
index f3c2c6261..ab37ea203 100644
--- a/src/web3/hooks/useStakeTransaction.ts
+++ b/src/web3/hooks/useStakeTransaction.ts
@@ -7,6 +7,7 @@ import { useApproveTStaking } from "./useApproveTStaking"
import { BigNumber } from "ethers"
import { useTStakingAllowance } from "./useTStakingAllowance"
import doesErrorInclude from "../utils/doesErrorInclude"
+import useApproveAndCallTStaking from "./useApproveAndCallTStaking"
interface StakeRequest {
amount: string | number
@@ -38,13 +39,6 @@ export const useStakeTransaction = (onSuccess: OnSuccessCallback) => {
}
}
- const { sendTransaction, status } = useSendTransaction(
- stakingContract!,
- "stake",
- onSuccess,
- onError
- )
-
const allowance = useTStakingAllowance()
const stake = useCallback(
@@ -55,13 +49,23 @@ export const useStakeTransaction = (onSuccess: OnSuccessCallback) => {
authorizer,
}: StakeRequest) => {
const isApprovedForAmount = BigNumber.from(amount).lte(allowance)
+
+ // Approve and call if not approved for amount
if (!isApprovedForAmount) {
- await approve(amount.toString())
+ const { approveAndCall, status } = useApproveAndCallTStaking()
+ await approveAndCall(amount.toString())
}
+
+ // Otherwise, just stake
+ const { sendTransaction, status } = useSendTransaction(
+ stakingContract!,
+ "stake",
+ onSuccess,
+ onError
+ )
await sendTransaction(stakingProvider, beneficiary, authorizer, amount)
},
- [sendTransaction, stakingContract?.address, allowance, approve]
+ [stakingContract?.address, allowance, approve]
)
-
return { stake, status }
}
diff --git a/src/web3/hooks/userApproveAndCall.ts b/src/web3/hooks/userApproveAndCall.ts
new file mode 100644
index 000000000..0a5278b11
--- /dev/null
+++ b/src/web3/hooks/userApproveAndCall.ts
@@ -0,0 +1,24 @@
+import { useSendTransaction } from "./useSendTransaction"
+import { MaxUint256 } from "@ethersproject/constants"
+import { Contract } from "@ethersproject/contracts"
+
+const useApproveAndCall = (
+ tokenContract?: Contract,
+ spender?: string,
+ extraCallData = [],
+ onSuccess?: () => void | Promise
+) => {
+ const { sendTransaction, status } = useSendTransaction(
+ tokenContract!,
+ "approveAndCall",
+ onSuccess
+ )
+
+ const approveAndCall = async (amountToApprove = MaxUint256.toString()) => {
+ await sendTransaction(spender, [amountToApprove, ...extraCallData])
+ }
+
+ return { approveAndCall, status }
+}
+
+export default useApproveAndCall