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 75b39f5 commit c7ac849
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 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,
undefined,
onSuccess
)
}

export default useApproveAndCallTStaking
10 changes: 6 additions & 4 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 All @@ -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)) {
Expand Down Expand Up @@ -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 }
Expand Down
28 changes: 28 additions & 0 deletions src/web3/hooks/userApproveAndCall.ts
Original file line number Diff line number Diff line change
@@ -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<void>
) => {
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

0 comments on commit c7ac849

Please sign in to comment.