feat: order write instructions (accept/reject/cancel/close-stale/complete/dispute)
This commit is contained in:
@@ -1,12 +1,118 @@
|
||||
'use client'
|
||||
|
||||
import { use } from 'react'
|
||||
import { useWallet } from '@solana/connector/react'
|
||||
import { useWallet, useKitTransactionSigner } from '@solana/connector/react'
|
||||
import { OrderDetail } from '@/components/OrderDetail'
|
||||
import { useTx } from '@/hooks/useTx'
|
||||
import { useOrder } from '@/hooks/useOrder'
|
||||
import {
|
||||
getAcceptOrderInstruction,
|
||||
getRejectOrderInstructionAsync,
|
||||
getCancelOrderInstructionAsync,
|
||||
getCloseStaleOrderInstruction,
|
||||
} from '@solisting/sdk'
|
||||
import {
|
||||
getCompleteInstructionAsync,
|
||||
getDisputeInstruction,
|
||||
DESCRO_PROGRAM_ADDRESS,
|
||||
findResolverEntryPda,
|
||||
} from '@descro/sdk'
|
||||
import type { Address } from '@solisting/sdk'
|
||||
|
||||
export default function OrderPage({ params }: { params: Promise<{ pk: string }> }) {
|
||||
const { pk } = use(params)
|
||||
const { account } = useWallet()
|
||||
return <OrderDetail pk={pk as Address} walletAddress={account ?? null} />
|
||||
const { signer } = useKitTransactionSigner()
|
||||
const sendTx = useTx()
|
||||
const { data } = useOrder(pk as Address)
|
||||
|
||||
const invalidate = [['order', pk], ['listings']]
|
||||
|
||||
async function handleAccept() {
|
||||
if (!signer || !data) return
|
||||
const od = data.order.data
|
||||
const [resolverEntry] = await findResolverEntryPda({ authority: od.resolver })
|
||||
const ix = getAcceptOrderInstruction({
|
||||
seller: signer,
|
||||
resolver: od.resolver,
|
||||
listingAccount: od.listing,
|
||||
orderAccount: pk as Address,
|
||||
escrowAccount: od.escrowAccount,
|
||||
resolverEntry,
|
||||
descroProgram: DESCRO_PROGRAM_ADDRESS,
|
||||
})
|
||||
await sendTx([ix as never], invalidate, 'accept_order')
|
||||
}
|
||||
|
||||
async function handleReject() {
|
||||
if (!signer || !data) return
|
||||
const od = data.order.data
|
||||
const ix = await getRejectOrderInstructionAsync({
|
||||
seller: signer,
|
||||
buyer: od.buyer,
|
||||
listingAccount: od.listing,
|
||||
orderAccount: pk as Address,
|
||||
escrowAccount: od.escrowAccount,
|
||||
descroProgram: DESCRO_PROGRAM_ADDRESS,
|
||||
})
|
||||
await sendTx([ix as never], invalidate, 'reject_order')
|
||||
}
|
||||
|
||||
async function handleCancel() {
|
||||
if (!signer || !data) return
|
||||
const od = data.order.data
|
||||
const ix = await getCancelOrderInstructionAsync({
|
||||
buyer: signer,
|
||||
listingAccount: od.listing,
|
||||
orderAccount: pk as Address,
|
||||
escrowAccount: od.escrowAccount,
|
||||
descroProgram: DESCRO_PROGRAM_ADDRESS,
|
||||
})
|
||||
await sendTx([ix as never], invalidate, 'cancel_order')
|
||||
}
|
||||
|
||||
async function handleCloseStale() {
|
||||
if (!signer || !data) return
|
||||
const od = data.order.data
|
||||
const ix = getCloseStaleOrderInstruction({
|
||||
caller: signer,
|
||||
orderAccount: pk as Address,
|
||||
escrowAccount: od.escrowAccount,
|
||||
})
|
||||
await sendTx([ix as never], invalidate, 'close_stale_order')
|
||||
}
|
||||
|
||||
async function handleComplete() {
|
||||
if (!signer || !data) return
|
||||
const od = data.order.data
|
||||
const ix = await getCompleteInstructionAsync({
|
||||
buyer: signer,
|
||||
seller: od.seller,
|
||||
escrowAccount: od.escrowAccount,
|
||||
})
|
||||
await sendTx([ix as never], [['order', pk]], 'complete')
|
||||
}
|
||||
|
||||
async function handleDispute() {
|
||||
if (!signer || !data) return
|
||||
const od = data.order.data
|
||||
const ix = getDisputeInstruction({
|
||||
initiator: signer,
|
||||
escrowAccount: od.escrowAccount,
|
||||
})
|
||||
await sendTx([ix as never], [['order', pk]], 'dispute')
|
||||
}
|
||||
|
||||
return (
|
||||
<OrderDetail
|
||||
pk={pk as Address}
|
||||
walletAddress={account ?? null}
|
||||
onAccept={handleAccept}
|
||||
onReject={handleReject}
|
||||
onCancel={handleCancel}
|
||||
onCloseStale={handleCloseStale}
|
||||
onComplete={handleComplete}
|
||||
onDispute={handleDispute}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user