Created
September 15, 2023 07:37
-
-
Save elmariachi111/0ba6362d8bc74df143cbc330a5b9f7f7 to your computer and use it in GitHub Desktop.
rewrite useInitiateAuction for wagmi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use client' | |
import { getUnixTime, sub } from 'date-fns' | |
import { useCallback, useState } from 'react' | |
import { Address, decodeErrorResult, toHex, zeroAddress } from 'viem' | |
import { abi } from '@/lib/abi/EasyAuction' | |
import { useEasyAuctionInitiateAuction } from '@/lib/generated/wagmi/contracts' | |
import { Transaction } from '@/types/chain' | |
import { BID_CANCEL_DEADLINE_INTERVAL_HOURS } from '@/utils/auction' | |
import { currentChain, safeDecodeLogs } from '@/utils/chain' | |
import { waitForTransaction } from '@wagmi/core' | |
import { useAccount } from 'wagmi' | |
export const useInitiateAuction = () => { | |
const { address: userAddress } = useAccount() | |
const [transaction, setTransaction] = useState<Transaction>() | |
const { writeAsync, isLoading, isSuccess } = useEasyAuctionInitiateAuction({ | |
chainId: currentChain.id | |
}) | |
const initiateAuction = useCallback( | |
async ( | |
auctionToken: string, | |
biddingToken: string, | |
auctionEndDate: Date, | |
auctionedSellAmount: bigint, | |
minFundingThreshold: bigint, | |
minimumBiddingAmountPerOrder: bigint | |
) => { | |
if (!userAddress) { | |
console.error('not connected') | |
return | |
} | |
try { | |
// Set orderCancellationEndDate | |
const orderCancellationEndDate = sub(auctionEndDate, { | |
hours: BID_CANCEL_DEADLINE_INTERVAL_HOURS | |
}) | |
const result = await writeAsync({ | |
args: [ | |
auctionToken as Address, | |
biddingToken as Address, | |
BigInt(getUnixTime(orderCancellationEndDate)), | |
BigInt(getUnixTime(auctionEndDate)), | |
auctionedSellAmount, | |
minFundingThreshold, // minBuyAmount set as minFundingThreshold | |
minimumBiddingAmountPerOrder, | |
minFundingThreshold, | |
false, | |
zeroAddress, | |
toHex(0) | |
] | |
}) | |
setTransaction(result) | |
const receipt = await waitForTransaction(result) | |
setTransaction({ ...result, receipt }) | |
const decodedLogs = safeDecodeLogs(receipt, abi) | |
console.log({ decodedLogs }) | |
setTransaction({ ...result, receipt, decodedLogs }) | |
} catch (e: any) { | |
console.error('something went wrong', e) | |
const dError = decodeErrorResult({ abi, data: e }) | |
console.error({ dError }) | |
setTransaction({ | |
error: dError || e.reason || e.message || e.toString() | |
}) | |
throw e | |
} | |
}, | |
[userAddress] | |
) | |
return { initiateAuction, isInitiating: isLoading, transaction, isSuccess } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment