From c7ac8495f287cc60b5a019df2e640fdc33047e13 Mon Sep 17 00:00:00 2001
From: Piotr Roslaniec
Date: Wed, 6 Dec 2023 11:34:13 +0100
Subject: [PATCH] wip
---
src/web3/hooks/useApproveAndCallTStaking.ts | 18 +++++++++++++
src/web3/hooks/useStakeTransaction.ts | 10 +++++---
src/web3/hooks/userApproveAndCall.ts | 28 +++++++++++++++++++++
3 files changed, 52 insertions(+), 4 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..8c2449bd6
--- /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,
+ undefined,
+ onSuccess
+ )
+}
+
+export default useApproveAndCallTStaking
diff --git a/src/web3/hooks/useStakeTransaction.ts b/src/web3/hooks/useStakeTransaction.ts
index f3c2c6261..fc72899a8 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
@@ -22,7 +23,7 @@ enum CommonStakingErrors {
export const useStakeTransaction = (onSuccess: OnSuccessCallback) => {
const stakingContract = useTStakingContract()
const { openModal } = useModal()
- const { approve } = useApproveTStaking()
+ const { approveAndCall } = useApproveAndCallTStaking()
const onError = (error: any) => {
if (doesErrorInclude(error, CommonStakingErrors.ProviderInUse)) {
@@ -56,11 +57,12 @@ export const useStakeTransaction = (onSuccess: OnSuccessCallback) => {
}: StakeRequest) => {
const isApprovedForAmount = BigNumber.from(amount).lte(allowance)
if (!isApprovedForAmount) {
- await approve(amount.toString())
+ await approveAndCall(amount.toString())
+ } else {
+ await sendTransaction(stakingProvider, beneficiary, authorizer, amount)
}
- await sendTransaction(stakingProvider, beneficiary, authorizer, amount)
},
- [sendTransaction, stakingContract?.address, allowance, approve]
+ [sendTransaction, stakingContract?.address, allowance, approveAndCall]
)
return { stake, status }
diff --git a/src/web3/hooks/userApproveAndCall.ts b/src/web3/hooks/userApproveAndCall.ts
new file mode 100644
index 000000000..ad0f4b8ca
--- /dev/null
+++ b/src/web3/hooks/userApproveAndCall.ts
@@ -0,0 +1,28 @@
+import { useSendTransaction } from "./useSendTransaction"
+import { MaxUint256 } from "@ethersproject/constants"
+import { Contract } from "@ethersproject/contracts"
+
+const useApproveAndCall = (
+ tokenContract?: Contract,
+ spender?: string,
+ extraCallData?: Uint8Array,
+ onSuccess?: () => void | Promise
+) => {
+ const { sendTransaction, status } = useSendTransaction(
+ tokenContract!,
+ "approveAndCall",
+ onSuccess
+ )
+
+ const approveAndCall = async (amountToApprove = MaxUint256.toString()) => {
+ await sendTransaction(
+ spender,
+ amountToApprove,
+ extraCallData ?? Uint8Array.from([])
+ )
+ }
+
+ return { approveAndCall, status }
+}
+
+export default useApproveAndCall