Skip to content

Commit 1aba9de

Browse files
committed
Refactor error handling in AtomicChat components to improve user feedback and streamline API error messages.
1 parent a836b0e commit 1aba9de

12 files changed

Lines changed: 34 additions & 20 deletions

File tree

apps/app/components/Swap/AtomicChat/Actions/UserActions.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const UserLockAction: FC<UserCommitActionProps> = ({ quote, type }) => {
4242

4343
if (!sourceClient) throw new Error("No source client")
4444

45-
const result = await sourceClient.userLock({
45+
const result = await sourceClient.userLock({
4646
...resolveQuote(quote),
4747
sourceAddress: sourceWallet.address,
4848
destinationAddress: address,
@@ -54,7 +54,6 @@ export const UserLockAction: FC<UserCommitActionProps> = ({ quote, type }) => {
5454
destLpAddress,
5555
srcLpAddress,
5656
tokenContractAddress: source_asset.contractAddress,
57-
decimals: source_asset.decimals,
5857
atomicContract,
5958
chainId: source_network.chainId,
6059
hashlock,

apps/app/components/Swap/AtomicChat/Actions/index.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,6 @@ const TransactionMessage: FC<{ error: string | undefined, disableButton?: boolea
208208
/>
209209
)
210210
}
211-
if (error === 'TrainApiError') {
212-
return <WalletMessage status="error" header="API error" details="Something went wrong while communicating with the server. Please try again." />
213-
}
214211
if (error) {
215212
return <TransactionMessages.UexpectedErrorMessage message={error} />
216213
}

apps/app/components/Swap/AtomicChat/AtomicContent/useSwapProgress.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,27 @@ export function useSwapProgress(): SwapProgress {
171171

172172
return useMemo(() => {
173173
const sourceTxLink = buildExplorerLink(source_network?.caip2Id, lockTxId);
174-
const lpLockTx = htlcFromApi?.transactions?.find(t => t.type === HTLCTransaction.HTLCLock as string);
175-
const destTxLink = buildExplorerLink(destination_network?.caip2Id, lpLockTx?.hash);
174+
const solverLockTx = htlcFromApi?.transactions?.find(t => t.type === HTLCTransaction.HTLCLock as string);
175+
const destTxLink = buildExplorerLink(destination_network?.caip2Id, solverLockTx?.hash);
176176
const redeemTxLink = buildExplorerLink(destination_network?.caip2Id, destRedeemTx);
177177
const refundTxLink = buildExplorerLink(source_network?.caip2Id, refundTxId);
178178

179179
const isRefunded = sourceDetails?.status === LockStatus.Refunded;
180180

181+
// API error — overlay on current progress
182+
if (htlcFromApi?.error?.message) {
183+
const currentIndex = solverLockTx ? 2 : 1
184+
return {
185+
gaugeValue: 50, gaugeIcon: "x" as GaugeIcon,
186+
title: "Something went wrong",
187+
subtitle: htlcFromApi.error.message,
188+
steps: buildSteps(HAPPY_STEPS, currentIndex, { source: sourceTxLink, dest: destTxLink }, {
189+
0: { timelock: sourceDetails?.timelock },
190+
1: { description: solverLockTx ? <VerificationStatus /> : null, status: solverLockTx ? StepStatus.Complete : StepStatus.Failed },
191+
}),
192+
};
193+
}
194+
181195
// Initial (no tx yet)
182196
if (htlcStatus === HTLCStatus.Initial && !lockTxId) {
183197
return { gaugeValue: 0, gaugeIcon: null, title: "Ready to swap", subtitle: null, steps: [] };

apps/app/context/atomicContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export function AtomicProvider({ children }) {
163163
if (hashlock) updateHTLCState(hashlock, { htlcFromApi: order })
164164
},
165165
onFailed: () => {
166-
setError({ message: 'Please wait for the timelock to expire, then refund to receive your assets back.', disableButton: true })
166+
if (hashlock) updateHTLCState(hashlock, { htlcFromApi: { error: { message: 'Please wait for the timelock to expire, then refund to receive your assets back.' } } })
167167
},
168168
})
169169

packages/blockchains/aztec/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class AztecHTLCClient extends HTLCClient {
6868
await signer.wallet.registerContract(tokenInstance, TokenContract.artifact)
6969
const token = TokenContract.at(tokenAddress, signer.wallet)
7070

71-
const amount = parseUnits(params.amount.toString(), params.decimals)
71+
const amount = parseUnits(params.amount.toString(), params.sourceAsset.decimals)
7272

7373
// Authorize public token transfer
7474
const transferNonce = Fr.random()

packages/blockchains/evm/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class EvmHTLCClient extends HTLCClient {
3737
sourceAddress
3838
} = params
3939

40-
const parsedAmount = parseUnits(params.amount.toString(), params.decimals)
40+
const parsedAmount = parseUnits(params.amount.toString(), params.sourceAsset.decimals)
4141
const tokenAddress = sourceAsset.contractAddress || ZERO_ADDRESS
4242
const isNativeToken = !sourceAsset.contractAddress || sourceAsset.contractAddress === ZERO_ADDRESS
4343

packages/blockchains/fuel/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class FuelHTLCClient extends HTLCClient {
5050
sourceChain,
5151
} = params
5252

53-
const parsedAmount = parseUnits(params.amount.toString(), params.decimals)
53+
const parsedAmount = parseUnits(params.amount.toString(), params.sourceAsset.decimals)
5454
const isNativeToken = !sourceAsset.contractAddress
5555

5656
try {

packages/blockchains/solana/src/transactionBuilder.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,21 @@ export const userLockTransactionBuilder = async (params: UserLockParams): Promis
3535

3636
if (!walletPublicKey) throw new Error("Wallet not connected")
3737
if (!params.srcLpAddress) throw new Error("No LP address")
38+
if (!params.nonce) throw new Error("No nonce")
39+
if (!params.solverData) throw new Error("No solver data")
3840

3941
const hashlock = Buffer.from(params.hashlock.replace('0x', ''), 'hex')
40-
const bnAmount = toBaseUnits(params.amount, params.decimals)
42+
const bnAmount = toBaseUnits(params.amount, params.sourceAsset.decimals)
4143
const bnDstAmount = new BN(params.destinationAmount)
4244
const bnRewardAmount = new BN(params.rewardAmount || '0')
4345
const bnTimelockDelta = new BN(params.timelockDelta || 0)
4446
const bnRewardTimelockDelta = new BN(params.rewardTimelockDelta || 0)
4547
const bnQuoteExpiry = new BN(params.quoteExpiry)
4648
const lpPublicKey = new PublicKey(params.srcLpAddress)
4749
const hashlockArray = Array.from(hashlock)
48-
const userData = params.nonce != null ? Buffer.from(params.nonce.toString(), 'utf8') : Buffer.from([])
49-
const solverDataBytes = params.solverData ? Buffer.from(params.solverData, 'utf8') : Buffer.from([])
50-
50+
const userData = Buffer.from(params.nonce.toString(), 'utf8')
51+
const solverDataBytes = Buffer.from(params.solverData, 'utf8')
52+
5153
const [userLockPda] = PublicKey.findProgramAddressSync(
5254
[Buffer.from("user_lock"), hashlock],
5355
program.programId

packages/blockchains/starknet/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class StarknetHTLCClient extends HTLCClient {
3232
async userLock(params: UserLockParams): Promise<AtomicResult> {
3333
const signer = this.requireSigner()
3434

35-
const parsedAmount = parseUnits(params.amount.toString(), params.decimals)
35+
const parsedAmount = parseUnits(params.amount.toString(), params.sourceAsset.decimals)
3636
const tokenAddress = params.tokenContractAddress || params.sourceAsset.contractAddress || ZERO_ADDRESS
3737

3838
// ERC20 approval

packages/blockchains/ton/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class TonHTLCClient extends HTLCClient {
4848
async userLock(params: UserLockParams): Promise<AtomicResult> {
4949
const signer = this.requireSigner()
5050

51-
const parsedAmount = parseUnits(params.amount.toString(), params.decimals)
51+
const parsedAmount = parseUnits(params.amount.toString(), params.sourceAsset.decimals)
5252
const isNativeToken = !params.sourceAsset.contractAddress
5353

5454
try {

0 commit comments

Comments
 (0)