useSolanaClient
This commit is contained in:
@@ -3,8 +3,6 @@
|
|||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { YStack, XStack, Text, Button, Input, Spinner } from 'tamagui'
|
import { YStack, XStack, Text, Button, Input, Spinner } from 'tamagui'
|
||||||
import {
|
import {
|
||||||
createSolanaRpc,
|
|
||||||
createSolanaRpcSubscriptions,
|
|
||||||
pipe,
|
pipe,
|
||||||
createTransactionMessage,
|
createTransactionMessage,
|
||||||
setTransactionMessageFeePayerSigner,
|
setTransactionMessageFeePayerSigner,
|
||||||
@@ -16,7 +14,7 @@ import {
|
|||||||
getSignatureFromTransaction,
|
getSignatureFromTransaction,
|
||||||
address as kitAddress,
|
address as kitAddress,
|
||||||
} from '@solana/kit'
|
} from '@solana/kit'
|
||||||
import { useKitTransactionSigner, useWallet, useCluster } from '@solana/connector/react'
|
import { useKitTransactionSigner, useWallet, useCluster, useSolanaClient } from '@solana/connector/react'
|
||||||
import { getCreateEscrowInstructionAsync } from '@descro/sdk'
|
import { getCreateEscrowInstructionAsync } from '@descro/sdk'
|
||||||
import type { Address } from '@descro/sdk'
|
import type { Address } from '@descro/sdk'
|
||||||
|
|
||||||
@@ -36,8 +34,7 @@ interface CreateEscrowFormProps {
|
|||||||
export function CreateEscrowForm({ onCreated }: CreateEscrowFormProps) {
|
export function CreateEscrowForm({ onCreated }: CreateEscrowFormProps) {
|
||||||
const { signer } = useKitTransactionSigner()
|
const { signer } = useKitTransactionSigner()
|
||||||
const { account: address } = useWallet()
|
const { account: address } = useWallet()
|
||||||
const { cluster } = useCluster()
|
const { client } = useSolanaClient()
|
||||||
const rpcUrl = cluster?.url ?? null
|
|
||||||
|
|
||||||
const [buyer, setBuyer] = useState('')
|
const [buyer, setBuyer] = useState('')
|
||||||
const [amountSol, setAmountSol] = useState('')
|
const [amountSol, setAmountSol] = useState('')
|
||||||
@@ -51,7 +48,7 @@ export function CreateEscrowForm({ onCreated }: CreateEscrowFormProps) {
|
|||||||
setError(null)
|
setError(null)
|
||||||
setSuccess(null)
|
setSuccess(null)
|
||||||
|
|
||||||
if (!signer || !address || !rpcUrl) {
|
if (!signer || !address || !client) {
|
||||||
setError('Connect your wallet first.')
|
setError('Connect your wallet first.')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -83,8 +80,7 @@ export function CreateEscrowForm({ onCreated }: CreateEscrowFormProps) {
|
|||||||
|
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
try {
|
try {
|
||||||
const rpc = createSolanaRpc(rpcUrl)
|
const { rpc, rpcSubscriptions } = client
|
||||||
const rpcSubscriptions = createSolanaRpcSubscriptions(rpcUrl.replace('http', 'ws'))
|
|
||||||
const escrowId = getNextEscrowId(address)
|
const escrowId = getNextEscrowId(address)
|
||||||
|
|
||||||
const ix = await getCreateEscrowInstructionAsync({
|
const ix = await getCreateEscrowInstructionAsync({
|
||||||
|
|||||||
@@ -3,8 +3,6 @@
|
|||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { YStack, XStack, Text, Button, Spinner, Separator } from 'tamagui'
|
import { YStack, XStack, Text, Button, Spinner, Separator } from 'tamagui'
|
||||||
import {
|
import {
|
||||||
createSolanaRpc,
|
|
||||||
createSolanaRpcSubscriptions,
|
|
||||||
pipe,
|
pipe,
|
||||||
createTransactionMessage,
|
createTransactionMessage,
|
||||||
setTransactionMessageFeePayerSigner,
|
setTransactionMessageFeePayerSigner,
|
||||||
@@ -16,7 +14,7 @@ import {
|
|||||||
getSignatureFromTransaction,
|
getSignatureFromTransaction,
|
||||||
isSome,
|
isSome,
|
||||||
} from '@solana/kit'
|
} from '@solana/kit'
|
||||||
import { useKitTransactionSigner, useWallet, useCluster } from '@solana/connector/react'
|
import { useKitTransactionSigner, useWallet, useSolanaClient } from '@solana/connector/react'
|
||||||
import { StatusBadge } from './StatusBadge'
|
import { StatusBadge } from './StatusBadge'
|
||||||
import {
|
import {
|
||||||
isAwaitingDeposit,
|
isAwaitingDeposit,
|
||||||
@@ -46,8 +44,7 @@ export function EscrowDetail({ item, onAction }: EscrowDetailProps) {
|
|||||||
|
|
||||||
const { signer } = useKitTransactionSigner()
|
const { signer } = useKitTransactionSigner()
|
||||||
const { account: address } = useWallet()
|
const { account: address } = useWallet()
|
||||||
const { cluster } = useCluster()
|
const { client } = useSolanaClient()
|
||||||
const rpcUrl = cluster?.url ?? null
|
|
||||||
|
|
||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState(false)
|
||||||
const [error, setError] = useState<string | null>(null)
|
const [error, setError] = useState<string | null>(null)
|
||||||
@@ -60,7 +57,7 @@ export function EscrowDetail({ item, onAction }: EscrowDetailProps) {
|
|||||||
const isResolver = address != null && resolverAddr != null && address === resolverAddr
|
const isResolver = address != null && resolverAddr != null && address === resolverAddr
|
||||||
|
|
||||||
async function runTx(buildIx: () => Promise<unknown>) {
|
async function runTx(buildIx: () => Promise<unknown>) {
|
||||||
if (!signer || !address || !rpcUrl) {
|
if (!signer || !address || !client) {
|
||||||
setError('Connect your wallet first.')
|
setError('Connect your wallet first.')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -68,8 +65,7 @@ export function EscrowDetail({ item, onAction }: EscrowDetailProps) {
|
|||||||
setError(null)
|
setError(null)
|
||||||
setSuccess(null)
|
setSuccess(null)
|
||||||
try {
|
try {
|
||||||
const rpc = createSolanaRpc(rpcUrl)
|
const { rpc, rpcSubscriptions } = client
|
||||||
const rpcSubscriptions = createSolanaRpcSubscriptions(rpcUrl.replace('http', 'ws'))
|
|
||||||
const ix = await buildIx()
|
const ix = await buildIx()
|
||||||
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send()
|
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send()
|
||||||
const txMsg = pipe(
|
const txMsg = pipe(
|
||||||
|
|||||||
@@ -3,8 +3,6 @@
|
|||||||
import React, { useState, useEffect } from 'react'
|
import React, { useState, useEffect } from 'react'
|
||||||
import { YStack, XStack, Text, Button, Input, Spinner, Separator } from 'tamagui'
|
import { YStack, XStack, Text, Button, Input, Spinner, Separator } from 'tamagui'
|
||||||
import {
|
import {
|
||||||
createSolanaRpc,
|
|
||||||
createSolanaRpcSubscriptions,
|
|
||||||
pipe,
|
pipe,
|
||||||
createTransactionMessage,
|
createTransactionMessage,
|
||||||
setTransactionMessageFeePayerSigner,
|
setTransactionMessageFeePayerSigner,
|
||||||
@@ -16,7 +14,7 @@ import {
|
|||||||
getSignatureFromTransaction,
|
getSignatureFromTransaction,
|
||||||
address as kitAddress,
|
address as kitAddress,
|
||||||
} from '@solana/kit'
|
} from '@solana/kit'
|
||||||
import { useKitTransactionSigner, useWallet, useCluster } from '@solana/connector/react'
|
import { useKitTransactionSigner, useWallet, useSolanaClient } from '@solana/connector/react'
|
||||||
import {
|
import {
|
||||||
fetchAllResolvers,
|
fetchAllResolvers,
|
||||||
getRegisterResolverInstructionAsync,
|
getRegisterResolverInstructionAsync,
|
||||||
@@ -40,8 +38,7 @@ function truncAddr(addr: string): string {
|
|||||||
export function ResolverPanel() {
|
export function ResolverPanel() {
|
||||||
const { signer } = useKitTransactionSigner()
|
const { signer } = useKitTransactionSigner()
|
||||||
const { account: address } = useWallet()
|
const { account: address } = useWallet()
|
||||||
const { cluster } = useCluster()
|
const { client } = useSolanaClient()
|
||||||
const rpcUrl = cluster?.url ?? null
|
|
||||||
|
|
||||||
const [resolvers, setResolvers] = useState<ResolverEntryWithPda[]>([])
|
const [resolvers, setResolvers] = useState<ResolverEntryWithPda[]>([])
|
||||||
const [resolversLoading, setResolversLoading] = useState(false)
|
const [resolversLoading, setResolversLoading] = useState(false)
|
||||||
@@ -58,11 +55,10 @@ export function ResolverPanel() {
|
|||||||
const [success, setSuccess] = useState<string | null>(null)
|
const [success, setSuccess] = useState<string | null>(null)
|
||||||
|
|
||||||
async function loadResolvers() {
|
async function loadResolvers() {
|
||||||
if (!rpcUrl) return
|
if (!client) return
|
||||||
setResolversLoading(true)
|
setResolversLoading(true)
|
||||||
try {
|
try {
|
||||||
const rpc = createSolanaRpc(rpcUrl)
|
const all = await fetchAllResolvers(client.rpc)
|
||||||
const all = await fetchAllResolvers(rpc)
|
|
||||||
setResolvers(all)
|
setResolvers(all)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to load resolvers', err)
|
console.error('Failed to load resolvers', err)
|
||||||
@@ -74,14 +70,14 @@ export function ResolverPanel() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadResolvers()
|
loadResolvers()
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [rpcUrl])
|
}, [client])
|
||||||
|
|
||||||
async function handleRegister(e: React.FormEvent) {
|
async function handleRegister(e: React.FormEvent) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
setError(null)
|
setError(null)
|
||||||
setSuccess(null)
|
setSuccess(null)
|
||||||
|
|
||||||
if (!signer || !address || !rpcUrl) {
|
if (!signer || !address || !client) {
|
||||||
setError('Connect your wallet first.')
|
setError('Connect your wallet first.')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -108,8 +104,7 @@ export function ResolverPanel() {
|
|||||||
|
|
||||||
setFormLoading(true)
|
setFormLoading(true)
|
||||||
try {
|
try {
|
||||||
const rpc = createSolanaRpc(rpcUrl)
|
const { rpc, rpcSubscriptions } = client
|
||||||
const rpcSubscriptions = createSolanaRpcSubscriptions(rpcUrl.replace('http', 'ws'))
|
|
||||||
const resolverType = RESOLVER_TYPE_OPTIONS[resolverTypeIdx].value
|
const resolverType = RESOLVER_TYPE_OPTIONS[resolverTypeIdx].value
|
||||||
|
|
||||||
const ix = await getRegisterResolverInstructionAsync({
|
const ix = await getRegisterResolverInstructionAsync({
|
||||||
|
|||||||
@@ -1,28 +1,26 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { useState, useEffect } from 'react'
|
import { useState, useEffect } from 'react'
|
||||||
import { createSolanaRpc, createSolanaRpcSubscriptions } from '@solana/kit'
|
import { useSolanaClient } from '@solana/connector/react'
|
||||||
import { useCluster } from '@solana/connector/react'
|
|
||||||
import { fetchMaybeEscrowAccount, subscribeEscrow } from '@descro/sdk'
|
import { fetchMaybeEscrowAccount, subscribeEscrow } from '@descro/sdk'
|
||||||
import type { Address, EscrowAccount } from '@descro/sdk'
|
import type { Address, EscrowAccount } from '@descro/sdk'
|
||||||
|
|
||||||
export function useEscrowDetail(pda: Address | null) {
|
export function useEscrowDetail(pda: Address | null) {
|
||||||
const { cluster } = useCluster()
|
const { client } = useSolanaClient()
|
||||||
const rpcUrl = cluster?.url ?? null
|
|
||||||
|
|
||||||
const [account, setAccount] = useState<EscrowAccount | null>(null)
|
const [account, setAccount] = useState<EscrowAccount | null>(null)
|
||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState(false)
|
||||||
const [error, setError] = useState<string | null>(null)
|
const [error, setError] = useState<string | null>(null)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!pda || !rpcUrl) {
|
if (!pda || !client) {
|
||||||
setAccount(null)
|
setAccount(null)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let cancelled = false
|
let cancelled = false
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
const rpc = createSolanaRpc(rpcUrl)
|
const { rpc, rpcSubscriptions } = client
|
||||||
|
|
||||||
fetchMaybeEscrowAccount(rpc, pda)
|
fetchMaybeEscrowAccount(rpc, pda)
|
||||||
.then((maybeAcc) => {
|
.then((maybeAcc) => {
|
||||||
@@ -38,8 +36,6 @@ export function useEscrowDetail(pda: Address | null) {
|
|||||||
if (!cancelled) setLoading(false)
|
if (!cancelled) setLoading(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
const wsUrl = rpcUrl.replace('http', 'ws')
|
|
||||||
const rpcSubscriptions = createSolanaRpcSubscriptions(wsUrl)
|
|
||||||
const unsub = subscribeEscrow(rpcSubscriptions, pda, (updated) => {
|
const unsub = subscribeEscrow(rpcSubscriptions, pda, (updated) => {
|
||||||
if (!cancelled) setAccount(updated)
|
if (!cancelled) setAccount(updated)
|
||||||
})
|
})
|
||||||
@@ -48,7 +44,7 @@ export function useEscrowDetail(pda: Address | null) {
|
|||||||
cancelled = true
|
cancelled = true
|
||||||
unsub()
|
unsub()
|
||||||
}
|
}
|
||||||
}, [pda, rpcUrl])
|
}, [pda, client])
|
||||||
|
|
||||||
return { account, loading, error }
|
return { account, loading, error }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user