From 85bdb4b267a450e467cc7bab823ad42116989a60 Mon Sep 17 00:00:00 2001 From: Yusuf Seyrek Date: Wed, 9 Aug 2023 15:53:32 +0300 Subject: [PATCH] fix(margin trading): response parsing (#354) --- src/utils/getTokenOutFromSwapResponse.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/utils/getTokenOutFromSwapResponse.ts b/src/utils/getTokenOutFromSwapResponse.ts index 6feeb642..fa9f4eb2 100644 --- a/src/utils/getTokenOutFromSwapResponse.ts +++ b/src/utils/getTokenOutFromSwapResponse.ts @@ -2,16 +2,20 @@ export default function getTokenOutFromSwapResponse( response: BroadcastResult, denom: string, ): Coin { - if (response.result) { - const rawLogs = JSON.parse(response.result.rawLogs) - const events = rawLogs[0].events - const tokenSwappedEvent = events.find((e: { type: string }) => e.type === 'token_swapped') - const tokensOutValue = tokenSwappedEvent.attributes.find( - (a: { key: string }) => a.key === 'tokens_out', - ).value - const amount = tokensOutValue.split(denom)[0] + try { + if (response.result) { + const rawLogs = JSON.parse(response.result.rawLogs) + const events = rawLogs[0].events + const tokenSwappedEvent = events.find((e: { type: string }) => e.type === 'token_swapped') + const tokensOutValue = tokenSwappedEvent.attributes.find( + (a: { key: string }) => a.key === 'tokens_out', + ).value + const amount = tokensOutValue.split(denom)[0] - return { denom, amount } + return { denom, amount } + } + } catch (ex) { + console.error(ex) } return { denom: '', amount: '' }