Skip to content

Commit 4760130

Browse files
authored
Merge pull request #1615 from mars-protocol/develop
v2.11.2
2 parents 846c924 + c248e23 commit 4760130

File tree

1 file changed

+80
-20
lines changed

1 file changed

+80
-20
lines changed

src/utils/broadcast.ts

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,73 @@ function getTransactionCoinsGrouped(
183183
.filter((event: TransactionEvent) => eventTypes.includes(event.type))
184184
.flat()
185185

186+
// First, collect all swap events to detect multi-hop swaps
187+
const swapEvents = filteredEvents.filter((event: TransactionEvent) => {
188+
if (!Array.isArray(event.attributes)) return false
189+
const action = event.attributes.find(
190+
(a: TransactionEventAttribute) => a.key === 'action',
191+
)?.value
192+
return action === 'swap'
193+
})
194+
195+
// Process swap events to show complete multi-hop route
196+
if (swapEvents.length > 1) {
197+
// For multi-hop swaps, show the complete route: input -> output -> input -> output -> etc.
198+
swapEvents.forEach((swapEvent: TransactionEvent, index: number) => {
199+
const coinInDenom = swapEvent.attributes.find(
200+
(a: TransactionEventAttribute) => a.key === 'offer_asset',
201+
)?.value
202+
const coinInAmount = swapEvent.attributes.find(
203+
(a: TransactionEventAttribute) => a.key === 'offer_amount',
204+
)?.value
205+
const coinOutDenom = swapEvent.attributes.find(
206+
(a: TransactionEventAttribute) => a.key === 'ask_asset',
207+
)?.value
208+
const coinOutAmount = swapEvent.attributes.find(
209+
(a: TransactionEventAttribute) => a.key === 'return_amount',
210+
)?.value
211+
212+
if (coinInDenom && coinInAmount && coinOutDenom && coinOutAmount) {
213+
// Add input for each swap
214+
transactionCoins.push({
215+
type: 'swap',
216+
coin: BNCoin.fromDenomAndBigNumber(coinInDenom, BN(coinInAmount)),
217+
})
218+
// Add output for each swap
219+
transactionCoins.push({
220+
type: 'swap',
221+
coin: BNCoin.fromDenomAndBigNumber(coinOutDenom, BN(coinOutAmount)),
222+
})
223+
}
224+
})
225+
} else if (swapEvents.length === 1) {
226+
// Single swap, process normally
227+
const swapEvent = swapEvents[0]
228+
const coinInDenom = swapEvent.attributes.find(
229+
(a: TransactionEventAttribute) => a.key === 'offer_asset',
230+
)?.value
231+
const coinInAmount = swapEvent.attributes.find(
232+
(a: TransactionEventAttribute) => a.key === 'offer_amount',
233+
)?.value
234+
const coinOutDenom = swapEvent.attributes.find(
235+
(a: TransactionEventAttribute) => a.key === 'ask_asset',
236+
)?.value
237+
const coinOutAmount = swapEvent.attributes.find(
238+
(a: TransactionEventAttribute) => a.key === 'return_amount',
239+
)?.value
240+
241+
if (coinInDenom && coinInAmount && coinOutDenom && coinOutAmount) {
242+
transactionCoins.push({
243+
type: 'swap',
244+
coin: BNCoin.fromDenomAndBigNumber(coinInDenom, BN(coinInAmount)),
245+
})
246+
transactionCoins.push({
247+
type: 'swap',
248+
coin: BNCoin.fromDenomAndBigNumber(coinOutDenom, BN(coinOutAmount)),
249+
})
250+
}
251+
}
252+
186253
filteredEvents.forEach((event: TransactionEvent) => {
187254
if (!Array.isArray(event.attributes)) return
188255

@@ -192,6 +259,11 @@ function getTransactionCoinsGrouped(
192259
return
193260
}
194261

262+
// Skip swap events as they are now handled above
263+
if (action === 'swap') {
264+
return
265+
}
266+
195267
// Check if the event type is a token_swapped and get coins from the event
196268
// This is needed for the "old" swap event, while the new swap event has an action attribute
197269
if (event.type === 'token_swapped') {
@@ -218,7 +290,14 @@ function getTransactionCoinsGrouped(
218290
const tokenIn = event.attributes.find((a) => a.key === 'TokenIn')?.value
219291
const tokenOut = event.attributes.find((a) => a.key === 'TokenOut')?.value
220292

221-
if (requestAmountIn && swapAmountOut && tokenIn && tokenOut) {
293+
// Only process Duality swaps that actually resulted in a swap (SwapAmountOut > 0)
294+
if (
295+
requestAmountIn &&
296+
swapAmountOut &&
297+
tokenIn &&
298+
tokenOut &&
299+
BN(swapAmountOut).isGreaterThan(0)
300+
) {
222301
transactionCoins.push({
223302
type: 'swap',
224303
coin: BNCoin.fromDenomAndBigNumber(tokenIn, BN(requestAmountIn)),
@@ -384,25 +463,6 @@ function getCoinsFromEvent(event: TransactionEvent) {
384463
if (depositTokens) depositTokens.map((coin) => coins.push({ coin: coin }))
385464
}
386465

387-
// Check if the event is a swap event and then return the coins
388-
const isSwap = event.attributes.find((a) => a.key === 'action')?.value === 'swap'
389-
if (isSwap) {
390-
const coinInDenom = event.attributes.find((a) => a.key === 'offer_asset')?.value
391-
const coinInAmount = event.attributes.find((a) => a.key === 'offer_amount')?.value
392-
const coinOutDenom = event.attributes.find((a) => a.key === 'ask_asset')?.value
393-
const coinOutAmount = event.attributes.find((a) => a.key === 'return_amount')?.value
394-
if (coinInDenom && coinInAmount && coinOutDenom && coinOutAmount) {
395-
coins.push(
396-
{
397-
coin: BNCoin.fromDenomAndBigNumber(coinInDenom, BN(coinInAmount)),
398-
},
399-
{
400-
coin: BNCoin.fromDenomAndBigNumber(coinOutDenom, BN(coinOutAmount)),
401-
},
402-
)
403-
}
404-
}
405-
406466
return coins
407467
}
408468

0 commit comments

Comments
 (0)