feat: create_order instruction (SOL canonical currency)
This commit is contained in:
@@ -1,19 +1,30 @@
|
||||
'use client'
|
||||
|
||||
import { use } from 'react'
|
||||
import { use, useState } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { useWallet, useKitTransactionSigner } from '@solana/connector/react'
|
||||
import { ListingDetail } from '@/components/ListingDetail'
|
||||
import { useTx } from '@/hooks/useTx'
|
||||
import { getCloseListingInstruction } from '@solisting/sdk'
|
||||
import { useListing } from '@/hooks/useListing'
|
||||
import {
|
||||
getCloseListingInstruction,
|
||||
getCreateOrderInstructionAsync,
|
||||
findOrderPda,
|
||||
deriveEscrowId,
|
||||
} from '@solisting/sdk'
|
||||
import { DESCRO_PROGRAM_ADDRESS } from '@descro/sdk'
|
||||
import type { Address } from '@solisting/sdk'
|
||||
|
||||
const SYSTEM_PROGRAM = '11111111111111111111111111111111' as Address<'11111111111111111111111111111111'>
|
||||
|
||||
export default function ListingPage({ params }: { params: Promise<{ pk: string }> }) {
|
||||
const { pk } = use(params)
|
||||
const router = useRouter()
|
||||
const { account } = useWallet()
|
||||
const { signer } = useKitTransactionSigner()
|
||||
const sendTx = useTx()
|
||||
const { data: listing } = useListing(pk as Address)
|
||||
const [orderLoading, setOrderLoading] = useState(false)
|
||||
|
||||
async function handleClose() {
|
||||
if (!signer) return
|
||||
@@ -22,13 +33,47 @@ export default function ListingPage({ params }: { params: Promise<{ pk: string }
|
||||
router.push('/listings')
|
||||
}
|
||||
|
||||
async function handlePlaceOrder() {
|
||||
if (!signer || !account || !listing) return
|
||||
setOrderLoading(true)
|
||||
try {
|
||||
const orderId = BigInt(Date.now())
|
||||
const [orderPda] = await findOrderPda(pk as Address, account as Address, orderId)
|
||||
const escrowId = deriveEscrowId(orderPda)
|
||||
|
||||
const ix = await getCreateOrderInstructionAsync({
|
||||
buyer: signer,
|
||||
seller: listing.data.seller,
|
||||
listingAccount: pk as Address,
|
||||
canonicalOracle: SYSTEM_PROGRAM,
|
||||
targetOracle: SYSTEM_PROGRAM,
|
||||
descroProgram: DESCRO_PROGRAM_ADDRESS,
|
||||
orderId,
|
||||
escrowId,
|
||||
resolver: account as Address,
|
||||
paymentCurrency: { __kind: 'Sol' },
|
||||
expectedAmount: listing.data.price,
|
||||
maxSlippageBps: 0,
|
||||
})
|
||||
await sendTx(
|
||||
[ix as never],
|
||||
[['listing', pk], ['orders', pk]],
|
||||
'create_order',
|
||||
)
|
||||
} catch (err) {
|
||||
console.error('create_order failed:', err)
|
||||
} finally {
|
||||
setOrderLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<ListingDetail
|
||||
pk={pk as Address}
|
||||
walletAddress={account ?? null}
|
||||
onUpdate={() => router.push(`/listing/create?edit=${pk}`)}
|
||||
onClose={handleClose}
|
||||
onPlaceOrder={() => {}}
|
||||
onPlaceOrder={orderLoading ? undefined : handlePlaceOrder}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user