Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Dec 5, 2023
1 parent 61ce4cf commit 21cf4b1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 14 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
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3729,10 +3729,10 @@
mkdirp "^1.0.4"
rimraf "^3.0.2"

"@nucypher/nucypher-contracts@7.0.0-rc.1":
version "7.0.0-rc.1"
resolved "https://registry.yarnpkg.com/@nucypher/nucypher-contracts/-/nucypher-contracts-7.0.0-rc.1.tgz#e958ebda099c0642ab2d9eca24bcf451d01ff107"
integrity sha512-4fZZfwQIqyhNmPbrIAUX8WB6rW9d2t2fCX3eFfcMOIwjCl144jab6fTAGjJx3ea1vXysq1bk9Ydvp93R82EtVg==
"@nucypher/nucypher-contracts@0.13.0":
version "0.13.0"
resolved "https://registry.yarnpkg.com/@nucypher/nucypher-contracts/-/nucypher-contracts-0.13.0.tgz#43ead85d4cdf7eb6b459ba77a6ff90a8bb796300"
integrity sha512-QB9vCVq2mLR2SeA/gSX8Px2QW3wcc9keQuTwgtDOA5J7PLoxDHEAko4ow6ZlsfjsbK572Xz6YReH09MBr71ujA==

"@openzeppelin/contracts-ethereum-package@^2.4.0":
version "2.5.0"
Expand Down

0 comments on commit 21cf4b1

Please sign in to comment.