Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 51 additions & 48 deletions apps/agentic-chat/src/components/tools/SendUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { toolStateToStepStatus } from '@/lib/executionState'
import { analytics } from '@/lib/mixpanel'
import { switchNetworkStep } from '@/lib/steps/switchNetworkStep'
import { firstFourLastFour } from '@/lib/utils'
import { withWalletLock } from '@/lib/walletMutex'
import type { SolanaWalletSigner } from '@/utils/chains/types'
import { executeSend } from '@/utils/sendExecutor'

Expand All @@ -30,55 +31,57 @@ export function SendUI({ toolPart }: ToolUIComponentProps<'sendTool'>) {
const ctx = useToolExecution(toolCallId, 'sendTool', {})

useExecuteOnce(ctx, sendData, async (data: SendOutput, ctx) => {
try {
const { tx } = data

if (!tx?.from) throw new Error('Invalid send output: missing tx.from')
if (!tx?.chainId) throw new Error('Invalid send output: missing tx.chainId')
if (!data.sendData?.chainId) throw new Error('Invalid send output: missing sendData.chainId')

const assetChainId = data.sendData.chainId
const { chainNamespace } = fromChainId(assetChainId)

const currentAddress =
chainNamespace === CHAIN_NAMESPACE.Evm ? ctx.refs.evmAddress.current : ctx.refs.solanaAddress.current
if (!currentAddress) throw new Error('Wallet disconnected. Please reconnect and try again.')
if (currentAddress.toLowerCase() !== tx.from.toLowerCase()) {
throw new Error('Wallet address changed. Please re-initiate the transaction.')
await withWalletLock(async () => {
try {
const { tx } = data

if (!tx?.from) throw new Error('Invalid send output: missing tx.from')
if (!tx?.chainId) throw new Error('Invalid send output: missing tx.chainId')
if (!data.sendData?.chainId) throw new Error('Invalid send output: missing sendData.chainId')

const assetChainId = data.sendData.chainId
const { chainNamespace } = fromChainId(assetChainId)

const currentAddress =
chainNamespace === CHAIN_NAMESPACE.Evm ? ctx.refs.evmAddress.current : ctx.refs.solanaAddress.current
if (!currentAddress) throw new Error('Wallet disconnected. Please reconnect and try again.')
if (currentAddress.toLowerCase() !== tx.from.toLowerCase()) {
throw new Error('Wallet address changed. Please re-initiate the transaction.')
}

let solanaSigner: SolanaWalletSigner | undefined
if (chainNamespace === CHAIN_NAMESPACE.Solana && ctx.refs.solanaWallet.current) {
solanaSigner = await ctx.refs.solanaWallet.current.getSigner()
}

ctx.setState(draft => {
draft.toolOutput = data
draft.meta.networkName = data.sendData.asset.network
})
ctx.advanceStep()

await switchNetworkStep(ctx, assetChainId)

ctx.setSubstatus('Requesting signature...')
const sendTxHash = await executeSend(tx, { solanaSigner })
ctx.setMeta({ txHash: sendTxHash })
ctx.advanceStep()
ctx.markTerminal()
ctx.persist()

analytics.trackSend({
asset: data.sendData.asset.symbol,
amount: data.sendData.amount,
network: data.sendData.asset.network,
})

toast.success(`Send of ${data.sendData.amount} ${data.sendData.asset.symbol.toUpperCase()} is complete`)
} catch (error) {
ctx.failAndPersist(error)

toast.error(`Send of ${data.sendData.amount} ${data.sendData.asset.symbol.toUpperCase()} failed`)
}

let solanaSigner: SolanaWalletSigner | undefined
if (chainNamespace === CHAIN_NAMESPACE.Solana && ctx.refs.solanaWallet.current) {
solanaSigner = await ctx.refs.solanaWallet.current.getSigner()
}

ctx.setState(draft => {
draft.toolOutput = data
draft.meta.networkName = data.sendData.asset.network
})
ctx.advanceStep()

await switchNetworkStep(ctx, assetChainId)

ctx.setSubstatus('Requesting signature...')
const sendTxHash = await executeSend(tx, { solanaSigner })
ctx.setMeta({ txHash: sendTxHash })
ctx.advanceStep()
ctx.markTerminal()
ctx.persist()

analytics.trackSend({
asset: data.sendData.asset.symbol,
amount: data.sendData.amount,
network: data.sendData.asset.network,
})

toast.success(`Send of ${data.sendData.amount} ${data.sendData.asset.symbol.toUpperCase()} is complete`)
} catch (error) {
ctx.failAndPersist(error)

toast.error(`Send of ${data.sendData.amount} ${data.sendData.asset.symbol.toUpperCase()} failed`)
}
})
})

const prepareStepStatus = toolStateToStepStatus(toolState)
Expand Down
109 changes: 56 additions & 53 deletions apps/agentic-chat/src/components/tools/SwitchNetworkUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ArrowRightLeft } from 'lucide-react'
import { useExecuteOnce } from '@/hooks/useExecuteOnce'
import { useToolExecution } from '@/hooks/useToolExecution'
import { networkNameToChainId } from '@/lib/chains'
import { withWalletLock } from '@/lib/walletMutex'
import { useChatStore } from '@/stores/chatStore'

import { CollapsableDetails } from '../ui/CollapsableDetails'
Expand Down Expand Up @@ -34,67 +35,69 @@ export function SwitchNetworkUI({ toolPart }: ToolUIComponentProps<'switchNetwor
})

useExecuteOnce(ctx, networkData, async (data: SwitchNetworkOutput, ctx) => {
const { refs } = ctx

const targetChainId = networkNameToChainId[data.network]
await withWalletLock(async () => {
const { refs } = ctx

const targetChainId = networkNameToChainId[data.network]

if (!targetChainId) {
ctx.setState(draft => {
draft.error = `Network "${data.network}" not found`
draft.meta.phase = 'error'
draft.meta.network = data.network
})
ctx.markTerminal()
ctx.persist()
return
}

if (!targetChainId) {
ctx.setState(draft => {
draft.error = `Network "${data.network}" not found`
draft.meta.phase = 'error'
draft.meta.network = data.network
})
ctx.markTerminal()
ctx.persist()
return
}

if (data.network === 'solana') {
if (refs.solanaWallet.current && refs.primaryWallet.current && !isSolanaWallet(refs.primaryWallet.current)) {
await refs.changePrimaryWallet.current(refs.solanaWallet.current.id)
if (data.network === 'solana') {
if (refs.solanaWallet.current && refs.primaryWallet.current && !isSolanaWallet(refs.primaryWallet.current)) {
await refs.changePrimaryWallet.current(refs.solanaWallet.current.id)
}

ctx.setState(draft => {
draft.meta.phase = 'success'
draft.meta.network = data.network
})
ctx.advanceStep()
ctx.markTerminal()
ctx.persist()
return
}

ctx.setState(draft => {
draft.meta.phase = 'success'
draft.meta.phase = 'switching'
draft.meta.network = data.network
draft.error = undefined
})
ctx.advanceStep()
ctx.markTerminal()
ctx.persist()
return
}

ctx.setState(draft => {
draft.meta.phase = 'switching'
draft.meta.network = data.network
draft.error = undefined
})

try {
if (refs.evmWallet.current && refs.primaryWallet.current && !isEthereumWallet(refs.primaryWallet.current)) {
await refs.changePrimaryWallet.current(refs.evmWallet.current.id)
}

if (!refs.evmWallet.current) {
throw new Error('EVM wallet not connected')
try {
if (refs.evmWallet.current && refs.primaryWallet.current && !isEthereumWallet(refs.primaryWallet.current)) {
await refs.changePrimaryWallet.current(refs.evmWallet.current.id)
}

if (!refs.evmWallet.current) {
throw new Error('EVM wallet not connected')
}

await refs.evmWallet.current.connector.switchNetwork({ networkChainId: targetChainId })
ctx.setState(draft => {
draft.meta.phase = 'success'
})
ctx.advanceStep()
ctx.markTerminal()
ctx.persist()
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error)
ctx.setState(draft => {
draft.meta.phase = 'error'
draft.error = errorMessage
})
ctx.markTerminal()
ctx.persist()
}

await refs.evmWallet.current.connector.switchNetwork({ networkChainId: targetChainId })
ctx.setState(draft => {
draft.meta.phase = 'success'
})
ctx.advanceStep()
ctx.markTerminal()
ctx.persist()
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error)
ctx.setState(draft => {
draft.meta.phase = 'error'
draft.error = errorMessage
})
ctx.markTerminal()
ctx.persist()
}
})
})

const phase = ctx.state.meta.phase
Expand Down
75 changes: 39 additions & 36 deletions apps/agentic-chat/src/components/tools/VaultDepositUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useToolExecution } from '@/hooks/useToolExecution'
import { toolStateToStepStatus } from '@/lib/executionState'
import { switchNetworkStepByChainIdNumber } from '@/lib/steps/switchNetworkStep'
import { firstFourLastFour } from '@/lib/utils'
import { withWalletLock } from '@/lib/walletMutex'
import { sendTransaction } from '@/utils/sendTransaction'

import { Amount } from '../ui/Amount'
Expand All @@ -27,43 +28,45 @@ export function VaultDepositUI({ toolPart }: ToolUIComponentProps<'vaultDepositT
const ctx = useToolExecution(toolCallId, 'vaultDepositTool', {})

useExecuteOnce(ctx, depositData, async (data: VaultDepositOutput, ctx) => {
try {
const { depositTx } = data

if (!ctx.refs.evmAddress.current) {
throw new Error('Wallet disconnected. Please reconnect and try again.')
await withWalletLock(async () => {
try {
const { depositTx } = data

if (!ctx.refs.evmAddress.current) {
throw new Error('Wallet disconnected. Please reconnect and try again.')
}

ctx.setState(draft => {
draft.toolOutput = data
draft.meta.networkName = data.summary.network
})
ctx.advanceStep()

const { chainReference } = fromChainId(depositTx.chainId)
await switchNetworkStepByChainIdNumber(ctx, Number(chainReference))

ctx.setSubstatus('Requesting signature...')
const depositTxHash = await sendTransaction({
chainId: depositTx.chainId,
data: depositTx.data,
from: depositTx.from,
to: depositTx.to,
value: depositTx.value,
})
ctx.setMeta({ txHash: depositTxHash })
ctx.advanceStep()
ctx.markTerminal()
ctx.persist()

toast.success(
`Vault deposit of ${data.summary.asset.amount} ${data.summary.asset.symbol.toUpperCase()} is complete`
)
} catch (error) {
ctx.failAndPersist(error)

toast.error(`Vault deposit failed`)
}

ctx.setState(draft => {
draft.toolOutput = data
draft.meta.networkName = data.summary.network
})
ctx.advanceStep()

const { chainReference } = fromChainId(depositTx.chainId)
await switchNetworkStepByChainIdNumber(ctx, Number(chainReference))

ctx.setSubstatus('Requesting signature...')
const depositTxHash = await sendTransaction({
chainId: depositTx.chainId,
data: depositTx.data,
from: depositTx.from,
to: depositTx.to,
value: depositTx.value,
})
ctx.setMeta({ txHash: depositTxHash })
ctx.advanceStep()
ctx.markTerminal()
ctx.persist()

toast.success(
`Vault deposit of ${data.summary.asset.amount} ${data.summary.asset.symbol.toUpperCase()} is complete`
)
} catch (error) {
ctx.failAndPersist(error)

toast.error(`Vault deposit failed`)
}
})
})

const prepareStepStatus = toolStateToStepStatus(toolState)
Expand Down
Loading
Loading