Skip to content

Commit

Permalink
feat(txs-tracer-core): ✨ add interchain swap trace
Browse files Browse the repository at this point in the history
added tracer between non-native tokens
  • Loading branch information
DavideSegullo committed Apr 20, 2023
1 parent 262393d commit 913b64c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ibcTraceMachine } from '../ibc-trace-machine';
import { choose } from 'xstate/lib/actions';
import { txTraceMachine } from '../txs-trace-machine';
import {
getCrossSwapPacketSequence,
getCrossSwapAckPacketResponse,
getFungibleTokenPacketResponses,
} from '../../utils';

Expand All @@ -22,17 +22,14 @@ const initialContext: CrossSwapTraceContext = {
connectionTimeout: 10_000,
websocketUrl: '',
dstWebsocketUrl: '',
executorWebsocketUrl: '',
loading: false,
currentStep: 0,
totalSteps: 4,
errorCode: 0,
errorMessage: '',
srcChannel: '',
dstChannel: '',
startSrcChannel: undefined,
startDstChannel: undefined,
endSrcChannel: undefined,
endDstChannel: undefined,
query: '',
txHash: '',
};
Expand All @@ -57,24 +54,15 @@ export const crossSwapTraceMachine = createMachine(
dstChannel: (_, event) => {
return event.data.dstChannel;
},
startSrcChannel: (_, event) => {
return event.data.startSrcChannel;
},
startDstChannel: (_, event) => {
return event.data.startDstChannel;
},
endSrcChannel: (_, event) => {
return event.data.endSrcChannel;
},
endDstChannel: (_, event) => {
return event.data.endDstChannel;
},
websocketUrl: (_, event) => {
return event.data.websocketUrl;
},
dstWebsocketUrl: (_, event) => {
return event.data.dstWebsocketUrl;
},
executorWebsocketUrl: (_, event) => {
return event.data.executorWebsocketUrl;
},
query: (_, event) => {
return event.data.query;
},
Expand Down Expand Up @@ -106,7 +94,7 @@ export const crossSwapTraceMachine = createMachine(
return (
event.data.state === IBCTraceFinalState.Complete &&
event.data.ackTx !== undefined &&
getCrossSwapPacketSequence(event.data.ackTx).packetSequence !==
getCrossSwapAckPacketResponse(event.data.ackTx).packetSequence !==
undefined
);
},
Expand All @@ -126,14 +114,14 @@ export const crossSwapTraceMachine = createMachine(
return (
event.data.state === IBCTraceFinalState.Complete &&
event.data.ackTx !== undefined &&
getCrossSwapPacketSequence(event.data.ackTx).error !== undefined
getCrossSwapAckPacketResponse(event.data.ackTx).error !== undefined
);
},
actions: raise((_, event) => {
let errorMessage: string | undefined = '';

if (event.data.ackTx) {
const data = getCrossSwapPacketSequence(event.data.ackTx);
const data = getCrossSwapAckPacketResponse(event.data.ackTx);

errorMessage = data.error;
}
Expand Down Expand Up @@ -267,26 +255,20 @@ export const crossSwapTraceMachine = createMachine(
const tx = ctx.M1Tx;

if (tx) {
const data = getCrossSwapPacketSequence(tx);
const data = getCrossSwapAckPacketResponse(tx);

if (data.packetSequence) {
if (data.response) {
/**
* If endSrcChannel and endDstChannel are configured, it means
* we need to trace the transaction from Osmosis to
* the forwarder chain (For example Cosmos Hub)
* The channel between Osmosis and the destination chain
*/
const srcChannel = ctx.endSrcChannel
? ctx.endSrcChannel
: ctx.dstChannel;
const dstChannel = ctx.endDstChannel
? ctx.endDstChannel
: ctx.srcChannel;
const srcChannel = data.response.contract_result.channel_id;
const packetSequence = data.response.contract_result.packet_sequence;

return {
type: 'TRACE',
data: {
query: `acknowledge_packet.packet_src_channel='${srcChannel}' and acknowledge_packet.packet_dst_channel='${dstChannel}' and acknowledge_packet.packet_sequence=${data.packetSequence}`,
websocketUrl: ctx.dstWebsocketUrl,
query: `acknowledge_packet.packet_src_channel='${srcChannel}' and acknowledge_packet.packet_sequence=${packetSequence}`,
websocketUrl: ctx.executorWebsocketUrl,
},
};
}
Expand Down
29 changes: 6 additions & 23 deletions packages/txs-tracer-core/src/lib/types/cross-swap-trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,9 @@ export type CrossSwapTraceContext = Omit<IBCTraceContext, 'ackTx' | 'txs'> & {
errorMessage?: string;
totalSteps: number;
/**
* Data definitions for interchain swaps
* The url to the chain websocket that executes the swap contract.
*/
/**
* Channel related to token transfer using forward middleware on start.
* for example if I swap atom to osmo (And the swap is performed on Juno Chain),
* I'll set it to the channel between the Osmosis chain and the Cosmos Hub chain.
* endSrcChannel: channel-1
* endDstChannel: channel-207
*/
startSrcChannel?: string;
startDstChannel?: string;
/**
* Channel related to token transfer using forward middleware
* for example if I swap osmo to atom (And the swap is performed on Juno Chain),
* I'll set it to the channel between the Osmosis chain and the Cosmos Hub chain.
* endSrcChannel: channel-0
* endDstChannel: channel-141
*/
endSrcChannel?: string;
endDstChannel?: string;
executorWebsocketUrl: string;
};

export type CrossSwapMachineResultErrorPayload =
Expand All @@ -42,11 +25,11 @@ export type CrossSwapMachineResultErrorPayload =
};

export type CrossSwapTraceEventPayload = IBCTraceEventPayload & {
/**
* The url to the chain websocket that executes the swap contract.
*/
executorWebsocketUrl: string;
txHash: string;
startSrcChannel?: string;
startDstChannel?: string;
endSrcChannel?: string;
endDstChannel?: string;
};

export const CrossSwapTraceFinalState = {
Expand Down
4 changes: 3 additions & 1 deletion packages/txs-tracer-core/src/lib/utils/cross-swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const getFungibleTokenPacketResponses = (tx: IndexedTx) => {
};
};

export const getCrossSwapPacketSequence = (tx: IndexedTx) => {
export const getCrossSwapAckPacketResponse = (tx: IndexedTx) => {
const { success, error } = getFungibleTokenPacketResponses(tx);

try {
Expand All @@ -45,12 +45,14 @@ export const getCrossSwapPacketSequence = (tx: IndexedTx) => {

return {
packetSequence: response.contract_result.packet_sequence,
response,
error: undefined,
};
}
} catch {
return {
packetSequence: undefined,
response: undefined,
error: 'Unknown error',
};
}
Expand Down

0 comments on commit 913b64c

Please sign in to comment.