Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Dec 6, 2023
1 parent 69432ce commit 0ed85d1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
18 changes: 18 additions & 0 deletions src/web3/hooks/useApproveAndCallTStaking.ts
Original file line number Diff line number Diff line change
@@ -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> | void) => {
const tToken = useToken(Token.T)
const tStakingContract = useTStakingContract()

return useApproveAndCall(
tToken.contract!,
tStakingContract?.address,
[],
onSuccess
)
}

export default useApproveAndCallTStaking
24 changes: 14 additions & 10 deletions src/web3/hooks/useStakeTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,13 +39,6 @@ export const useStakeTransaction = (onSuccess: OnSuccessCallback) => {
}
}

const { sendTransaction, status } = useSendTransaction(
stakingContract!,
"stake",
onSuccess,
onError
)

const allowance = useTStakingAllowance()

const stake = useCallback(
Expand All @@ -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 }
}
24 changes: 24 additions & 0 deletions src/web3/hooks/userApproveAndCall.ts
Original file line number Diff line number Diff line change
@@ -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<void>
) => {
const { sendTransaction, status } = useSendTransaction(
tokenContract!,
"approveAndCall",
onSuccess
)

const approveAndCall = async (amountToApprove = MaxUint256.toString()) => {
await sendTransaction(spender, [amountToApprove, ...extraCallData])
}

return { approveAndCall, status }
}

export default useApproveAndCall

0 comments on commit 0ed85d1

Please sign in to comment.