Skip to content

Commit

Permalink
Added useSendCreatePromotionTransaction()
Browse files Browse the repository at this point in the history
  • Loading branch information
Ncookiez committed Nov 20, 2023
1 parent 5f3703c commit cb377ab
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/hyperstructure-react-hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ yarn add @generationsoftware/hyperstructure-react-hooks
- `useSendApproveTransaction`
- `useSendClaimRewardsTransaction`
- `useSendClaimVaultFeesTransaction`
- `useSendCreatePromotionTransaction`
- `useSendDeployLiquidationPairTransaction`
- `useSendDeployVaultTransaction`
- `useSendDepositTransaction`
Expand Down
2 changes: 1 addition & 1 deletion packages/hyperstructure-react-hooks/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@generationsoftware/hyperstructure-react-hooks",
"version": "1.9.2",
"version": "1.9.3",
"license": "MIT",
"main": "./dist/index.mjs",
"types": "./dist/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions packages/hyperstructure-react-hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export * from './tokens/useTokens'
export * from './transactions/useSendApproveTransaction'
export * from './transactions/useSendClaimRewardsTransaction'
export * from './transactions/useSendClaimVaultFeesTransaction'
export * from './transactions/useSendCreatePromotionTransaction'
export * from './transactions/useSendDeployLiquidationPairTransaction'
export * from './transactions/useSendDeployVaultTransaction'
export * from './transactions/useSendDepositTransaction'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const useSendClaimRewardsTransaction = (
abi: twabRewardsABI,
functionName: 'claimRewards',
args: claimRewardsArgs,
enabled: enabled
enabled
})

const {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import { Vault } from '@generationsoftware/hyperstructure-client-js'
import {
getSecondsSinceEpoch,
SECONDS_PER_DAY,
TWAB_REWARDS_ADDRESSES,
twabControllerABI,
twabRewardsABI
} from '@shared/utilities'
import { useEffect, useMemo } from 'react'
import { Address, isAddress, TransactionReceipt } from 'viem'
import {
useContractRead,
useContractReads,
useContractWrite,
useNetwork,
usePrepareContractWrite,
useWaitForTransaction
} from 'wagmi'

/**
* Prepares and submits a `createPromotion` transaction to a TWAB rewards contract
*
* Default settings:
* - Promotion starts as soon as possible.
* - Epochs last 1 day.
* @param vault the vault the promotion should be created for
* @param tokenAddress the address of the token to reward users with
* @param numberOfEpochs the number of epochs the promotion should last for (1 to 255)
* @param tokensPerEpoch the number of tokens to distribute per epoch
* @param options optional settings or callbacks
* @returns
*/
export const useSendCreatePromotionTransaction = (
vault: Vault,
tokenAddress: Address,
numberOfEpochs: number,
tokensPerEpoch: bigint,
options?: {
startTimestamp?: bigint
epochDuration?: number
onSend?: (txHash: `0x${string}`) => void
onSuccess?: (txReceipt: TransactionReceipt) => void
onError?: () => void
}
): {
isWaiting: boolean
isConfirming: boolean
isSuccess: boolean
isError: boolean
txHash?: Address
txReceipt?: TransactionReceipt
sendCreatePromotionTransaction?: () => void
} => {
const { chain } = useNetwork()

const twabRewardsAddress = !!vault ? TWAB_REWARDS_ADDRESSES[vault.chainId] : undefined

const { data: twabControllerAddress } = useContractRead({
chainId: vault?.chainId,
address: twabRewardsAddress,
abi: twabRewardsABI,
functionName: 'twabController',
enabled: !!vault && !!twabRewardsAddress
})

const { data: twabControllerInfo } = useContractReads({
contracts: [
{
chainId: vault?.chainId,
address: twabControllerAddress,
abi: twabControllerABI,
functionName: 'PERIOD_OFFSET'
},
{
chainId: vault?.chainId,
address: twabControllerAddress,
abi: twabControllerABI,
functionName: 'PERIOD_LENGTH'
}
]
})

const defaultStartTimestamp = useMemo(() => {
const periodOffset = twabControllerInfo?.[0]?.result as number | undefined
const periodLength = twabControllerInfo?.[1]?.result as number | undefined

if (
!!periodOffset &&
typeof periodOffset === 'number' &&
!!periodLength &&
typeof periodLength === 'number'
) {
const currentTimestamp = getSecondsSinceEpoch()

let defaultStartTimestamp = periodOffset
while (defaultStartTimestamp <= currentTimestamp) {
defaultStartTimestamp += periodLength
}

return BigInt(defaultStartTimestamp)
}
}, [twabControllerInfo])

const startTimestamp = options?.startTimestamp ?? defaultStartTimestamp
const epochDuration = options?.epochDuration ?? SECONDS_PER_DAY

const enabled =
!!vault &&
chain?.id === vault.chainId &&
!!tokenAddress &&
isAddress(tokenAddress) &&
!!numberOfEpochs &&
!!tokensPerEpoch &&
!!twabRewardsAddress &&
!!startTimestamp &&
!!epochDuration

const { config } = usePrepareContractWrite({
chainId: vault?.chainId,
address: twabRewardsAddress,
abi: twabRewardsABI,
functionName: 'createPromotion',
args: [
vault?.address,
tokenAddress,
startTimestamp as bigint,
tokensPerEpoch,
epochDuration,
numberOfEpochs
],
enabled
})

const {
data: txSendData,
isLoading: isWaiting,
isError: isSendingError,
isSuccess: isSendingSuccess,
write: sendCreatePromotionTransaction
} = useContractWrite(config)

const txHash = txSendData?.hash

useEffect(() => {
if (!!txHash && isSendingSuccess) {
options?.onSend?.(txHash)
}
}, [isSendingSuccess])

const {
data: txReceipt,
isLoading: isConfirming,
isSuccess,
isError: isConfirmingError
} = useWaitForTransaction({ chainId: vault?.chainId, hash: txHash })

useEffect(() => {
if (!!txReceipt && isSuccess) {
options?.onSuccess?.(txReceipt)
}
}, [isSuccess])

const isError = isSendingError || isConfirmingError

useEffect(() => {
if (isError) {
options?.onError?.()
}
}, [isError])

return {
isWaiting,
isConfirming,
isSuccess,
isError,
txHash,
txReceipt,
sendCreatePromotionTransaction
}
}

0 comments on commit cb377ab

Please sign in to comment.