Skip to content

Commit

Permalink
Compare the responses from skip API's route and msgs_direct and throw…
Browse files Browse the repository at this point in the history
… an error if unmatched before sending tx
  • Loading branch information
Thunnini committed Jan 5, 2024
1 parent e918fbe commit 4977149
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
72 changes: 70 additions & 2 deletions packages/extension/src/hooks/ibc-swap/amount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import {
} from "@keplr-wallet/stores";
import { useState } from "react";
import { action, makeObservable, observable, override } from "mobx";
import { SkipQueries } from "../../stores/skip";
import {
MsgsDirectResponse,
RouteResponse,
SkipQueries,
} from "../../stores/skip";
import { ObservableQueryIBCSwapInner } from "../../stores/skip/ibc-swap";

export class IBCSwapAmountConfig extends AmountConfig {
Expand Down Expand Up @@ -132,7 +136,8 @@ export class IBCSwapAmountConfig extends AmountConfig {

async getTx(
slippageTolerancePercent: number,
affiliateFeeReceiver: string
affiliateFeeReceiver: string,
swapRouterKey?: string
): Promise<MakeTxResponse> {
const queryIBCSwap = this.getQueryIBCSwap();
if (!queryIBCSwap) {
Expand Down Expand Up @@ -208,6 +213,25 @@ export class IBCSwapAmountConfig extends AmountConfig {
throw new Error("Tx is not ready");
}

if (swapRouterKey) {
const queryMsgsDirect = queryIBCSwap.getQueryMsgsDirect(
chainIdsToAddresses,
slippageTolerancePercent,
affiliateFeeReceiver
);
if (!queryMsgsDirect.response) {
throw new Error("Can't happen: queryMsgsDirect is not ready");
}

const key = this.createSwapRouteKeyFromMsgsDirectResponse(
queryMsgsDirect.response.data
);

if (swapRouterKey !== key) {
throw new Error("Swap router key does not match");
}
}

return tx;
}

Expand Down Expand Up @@ -318,6 +342,50 @@ export class IBCSwapAmountConfig extends AmountConfig {
}
}

// /route query의 결과와 /msgs_direct query의 결과를 비교하기 위한 키를 생성한다.
createSwapRouteKeyFromRouteResponse(response: RouteResponse): string {
let key = "";

for (const operation of response.operations) {
if ("swap" in operation) {
for (const swapOperation of operation.swap.swap_in.swap_operations) {
key += `/${swapOperation.pool}/${swapOperation.denom_in}/${swapOperation.denom_out}`;
}
}
}

return key;
}

// /route query의 결과와 /msgs_direct query의 결과를 비교하기 위한 키를 생성한다.
createSwapRouteKeyFromMsgsDirectResponse(
response: MsgsDirectResponse
): string {
let key = "";

for (const msg of response.msgs) {
if (msg.msg_type_url === "/ibc.applications.transfer.v1.MsgTransfer") {
const memo = JSON.parse(msg.msg).memo;
if (memo) {
const obj = JSON.parse(memo);
for (const operation of obj.wasm.msg.swap_and_action.user_swap
.swap_exact_asset_in.operations) {
key = `/${operation.pool}/${operation.denom_in}/${operation.denom_out}`;
}
}
}
if (msg.msg_type_url === "/cosmwasm.wasm.v1.MsgExecuteContract") {
const obj = JSON.parse(msg.msg);
for (const operation of obj.msg.swap_and_action.user_swap
.swap_exact_asset_in.operations) {
key = `/${operation.pool}/${operation.denom_in}/${operation.denom_out}`;
}
}
}

return key;
}

@override
override get uiProperties(): UIProperties {
const prev = super.uiProperties;
Expand Down
11 changes: 10 additions & 1 deletion packages/extension/src/pages/ibc-swap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,19 @@ export const IBCSwapPage: FunctionComponent = observer(() => {
const swapReceiver: string[] = [];

try {
let swapRouteKey = "";
if (queryRoute.response) {
swapRouteKey =
ibcSwapConfigs.amountConfig.createSwapRouteKeyFromRouteResponse(
queryRoute.response.data
);
}

const [_tx] = await Promise.all([
ibcSwapConfigs.amountConfig.getTx(
uiConfigStore.ibcSwapConfig.slippageNum,
SwapFeeBps.receiver
SwapFeeBps.receiver,
swapRouteKey
),
// queryRoute는 ibc history를 추적하기 위한 채널 정보 등을 얻기 위해서 사용된다.
// /msgs_direct로도 얻을 순 있지만 따로 데이터를 해석해야되기 때문에 좀 힘들다...
Expand Down

0 comments on commit 4977149

Please sign in to comment.