Skip to content

Commit 0e54494

Browse files
committed
code refactor
1 parent e80920a commit 0e54494

File tree

6 files changed

+73
-98
lines changed

6 files changed

+73
-98
lines changed

apps/extension/src/pages/ibc-swap/components/swap-fee-info/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ export const SwapFeeInfo: FunctionComponent<{
457457
</React.Fragment>
458458
) : null}
459459

460-
{amountConfig instanceof SwapAmountConfig && amountConfig.provider ? (
460+
{amountConfig.provider ? (
461461
<React.Fragment>
462462
<Gutter size="0.5rem" />
463463
<XAxis alignY="center">

apps/extension/src/pages/ibc-swap/index.tsx

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,7 @@ export const IBCSwapPage: FunctionComponent = observer(() => {
239239
// swap일 경우 (osmosis에서 실행될 경우) swap이 몇번 필요한지에 따라 영향을 미칠 것이다.
240240
let type = "default";
241241

242-
const queryRoute = swapConfigs.amountConfig
243-
.getQuerySwapHelper()
244-
?.getRoute(uiConfigStore.ibcSwapConfig.slippageNum);
245-
242+
const queryRoute = swapConfigs.amountConfig.getQueryRoute();
246243
if (queryRoute && queryRoute.response) {
247244
const routeData = queryRoute.response.data;
248245
const { steps, provider } = routeData;
@@ -424,10 +421,7 @@ export const IBCSwapPage: FunctionComponent = observer(() => {
424421
}, [swapConfigs.amountConfig, setSearchParams, tempSwitchAmount]);
425422

426423
// 10초마다 route query 자동 refresh
427-
const querySwapHelper = swapConfigs.amountConfig.getQuerySwapHelper();
428-
const queryRoute = querySwapHelper?.getRoute(
429-
uiConfigStore.ibcSwapConfig.slippageNum
430-
);
424+
const queryRoute = swapConfigs.amountConfig.getQueryRoute();
431425
useEffect(() => {
432426
if (queryRoute && !queryRoute.isFetching) {
433427
const timeoutId = setTimeout(() => {
@@ -564,9 +558,7 @@ export const IBCSwapPage: FunctionComponent = observer(() => {
564558

565559
let tx: MakeTxResponse | UnsignedEVMTransactionWithErc20Approvals;
566560

567-
const queryRoute = swapConfigs.amountConfig
568-
.getQuerySwapHelper()!
569-
.getRoute(uiConfigStore.ibcSwapConfig.slippageNum);
561+
const queryRoute = swapConfigs.amountConfig.getQueryRoute()!;
570562

571563
const channels: {
572564
portId: string;
@@ -2013,9 +2005,7 @@ const WarningGuideBox: FunctionComponent<{
20132005
return err.message || err.toString();
20142006
}
20152007

2016-
const queryError = amountConfig
2017-
.getQuerySwapHelper()
2018-
?.getRoute(slippageTolerancePercent)?.error;
2008+
const queryError = amountConfig.getQueryRoute()?.error;
20192009
if (queryError) {
20202010
return queryError.message || queryError.toString();
20212011
}

apps/hooks-internal/src/ibc-swap/amount.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,6 @@ export class IBCSwapAmountConfig extends AmountConfig {
619619
}
620620
}
621621

622-
// CHECK: 사용처가 없음
623622
// /route query의 결과와 /msgs_direct query의 결과를 비교하기 위한 키를 생성한다.
624623
createSwapRouteKeyFromRouteResponse(response: RouteResponse): string {
625624
let key = "";
@@ -643,7 +642,6 @@ export class IBCSwapAmountConfig extends AmountConfig {
643642
return key;
644643
}
645644

646-
// CHECK: 사용처가 없음
647645
// /route query의 결과와 /msgs_direct query의 결과를 비교하기 위한 키를 생성한다.
648646
createSwapRouteKeyFromMsgsDirectResponse(
649647
response: MsgsDirectResponse

apps/hooks-internal/src/swap/amount.ts

Lines changed: 47 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
CosmosTxData,
2828
EVMTxData,
2929
SwapProvider,
30+
ObservableQueryRouteInnerV2,
3031
} from "@keplr-wallet/stores-internal";
3132
import {
3233
EthereumAccountStore,
@@ -109,11 +110,7 @@ export class SwapAmountConfig extends AmountConfig {
109110
if (this.fraction > 0) {
110111
let result = this.maxAmount;
111112

112-
const slippageTolerancePercent = this._getSlippageTolerancePercent();
113-
114-
const queryRoute = this.getQuerySwapHelper(result)?.getRoute(
115-
slippageTolerancePercent
116-
);
113+
const queryRoute = this.getQueryRoute(result);
117114
if (queryRoute?.response != null) {
118115
const bridgeFee = queryRoute.bridgeFees.reduce(
119116
(acc: CoinPretty, fee: CoinPretty) => {
@@ -154,14 +151,9 @@ export class SwapAmountConfig extends AmountConfig {
154151
}
155152

156153
get outAmount(): CoinPretty {
157-
const querySwapHelper = this.getQuerySwapHelper();
158-
if (!querySwapHelper) {
159-
return new CoinPretty(this.outCurrency, "0");
160-
}
161-
162-
const slippageTolerancePercent = this._getSlippageTolerancePercent();
163-
164-
return querySwapHelper.getRoute(slippageTolerancePercent).outAmount;
154+
return (
155+
this.getQueryRoute()?.outAmount ?? new CoinPretty(this.outCurrency, "0")
156+
);
165157
}
166158

167159
get outChainId(): string {
@@ -181,25 +173,11 @@ export class SwapAmountConfig extends AmountConfig {
181173
}
182174

183175
get swapPriceImpact(): RatePretty | undefined {
184-
const querySwapHelper = this.getQuerySwapHelper();
185-
if (!querySwapHelper) {
186-
return undefined;
187-
}
188-
189-
const slippageTolerancePercent = this._getSlippageTolerancePercent();
190-
191-
return querySwapHelper.getRoute(slippageTolerancePercent).swapPriceImpact;
176+
return this.getQueryRoute()?.swapPriceImpact;
192177
}
193178

194179
get provider(): SwapProvider | undefined {
195-
const querySwapHelper = this.getQuerySwapHelper();
196-
if (!querySwapHelper) {
197-
return undefined;
198-
}
199-
200-
const slippageTolerancePercent = this._getSlippageTolerancePercent();
201-
202-
return querySwapHelper.getRoute(slippageTolerancePercent).provider;
180+
return this.getQueryRoute()?.provider;
203181
}
204182

205183
@action
@@ -218,35 +196,25 @@ export class SwapAmountConfig extends AmountConfig {
218196
}
219197

220198
get otherFees(): CoinPretty[] {
221-
const querySwapHelper = this.getQuerySwapHelper();
222-
if (!querySwapHelper) {
223-
return [new CoinPretty(this.outCurrency, "0")];
224-
}
225-
226-
const slippageTolerancePercent = this._getSlippageTolerancePercent();
227-
228-
return querySwapHelper.getRoute(slippageTolerancePercent).bridgeFees;
199+
return (
200+
this.getQueryRoute()?.bridgeFees ?? [
201+
new CoinPretty(this.outCurrency, "0"),
202+
]
203+
);
229204
}
230205

231206
async fetch(): Promise<void> {
232-
const querySwapHelper = this.getQuerySwapHelper();
233-
if (querySwapHelper) {
234-
const slippageTolerancePercent = this._getSlippageTolerancePercent();
235-
236-
await querySwapHelper.getRoute(slippageTolerancePercent).fetch();
207+
const queryRoute = this.getQueryRoute();
208+
if (queryRoute) {
209+
await queryRoute.fetch();
237210
}
238211
}
239212

240213
get isFetchingInAmount(): boolean {
241214
if (this.fraction === 1) {
242-
const slippageTolerancePercent = this._getSlippageTolerancePercent();
243-
244215
return (
245-
this.getQuerySwapHelper(this.maxAmount)?.getRoute(
246-
slippageTolerancePercent
247-
).isFetching ??
248-
this.getQuerySwapHelper()?.getRoute(slippageTolerancePercent)
249-
.isFetching ??
216+
this.getQueryRoute(this.maxAmount)?.isFetching ??
217+
this.getQueryRoute()?.isFetching ??
250218
false
251219
);
252220
}
@@ -255,27 +223,19 @@ export class SwapAmountConfig extends AmountConfig {
255223
}
256224

257225
get isFetchingOutAmount(): boolean {
258-
const slippageTolerancePercent = this._getSlippageTolerancePercent();
259-
260-
return (
261-
this.getQuerySwapHelper()?.getRoute(slippageTolerancePercent)
262-
.isFetching ?? false
263-
);
226+
return this.getQueryRoute()?.isFetching ?? false;
264227
}
265228

266229
get type(): "swap" | "transfer" | "not-ready" {
267-
const querySwapHelper = this.getQuerySwapHelper();
268-
if (!querySwapHelper) {
230+
const queryRoute = this.getQueryRoute();
231+
if (!queryRoute) {
269232
return "not-ready";
270233
}
271234

272-
const slippageTolerancePercent = this._getSlippageTolerancePercent();
273-
274-
const res = querySwapHelper.getRoute(slippageTolerancePercent).response;
235+
const res = queryRoute.response;
275236
if (!res) {
276237
return "not-ready";
277238
}
278-
279239
const containsSwap = res.data.steps.some(
280240
(step) => step.type === RouteStepType.SWAP
281241
);
@@ -933,6 +893,32 @@ export class SwapAmountConfig extends AmountConfig {
933893
toAddress
934894
);
935895
}
896+
897+
getQueryRoute(amount?: CoinPretty): ObservableQueryRouteInnerV2 | undefined {
898+
if (!amount && this.amount.length === 0) {
899+
return;
900+
}
901+
902+
const amountIn = amount ?? this.amount[0];
903+
const fromAddress = this.getAddressSync(this.chainId);
904+
const toAddress = this.getAddressSync(this.outChainId);
905+
if (!fromAddress || !toAddress) {
906+
return;
907+
}
908+
const slippageTolerancePercent = this._getSlippageTolerancePercent();
909+
910+
return this.swapQueries.querySwapHelper
911+
.getSwapHelper(
912+
this.chainId,
913+
amountIn.currency.coinMinimalDenom,
914+
amountIn.toCoin().amount,
915+
this.outChainId,
916+
this.outCurrency.coinMinimalDenom,
917+
fromAddress,
918+
toAddress
919+
)
920+
.getRoute(slippageTolerancePercent);
921+
}
936922
}
937923

938924
export const useSwapAmountConfig = (

packages/stores/src/account/cosmos.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import {
5050
ProtoMsgsOrWithAminoMsgs,
5151
} from "./types";
5252
import {
53+
decodeBase64,
5354
getEip712TypedDataBasedOnChainInfo,
5455
txEventsWithPreOnFulfill,
5556
} from "./utils";
@@ -2197,12 +2198,12 @@ export class CosmosAccountImpl {
21972198
destinationDomain: cctpMsg.value.destination_domain,
21982199
mintRecipient:
21992200
typeof cctpMsg.value.mint_recipient === "string"
2200-
? this.decodeBase64(cctpMsg.value.mint_recipient)
2201+
? decodeBase64(cctpMsg.value.mint_recipient)
22012202
: cctpMsg.value.mint_recipient,
22022203
burnToken: cctpMsg.value.burn_token,
22032204
destinationCaller:
22042205
typeof cctpMsg.value.destination_caller === "string"
2205-
? this.decodeBase64(cctpMsg.value.destination_caller)
2206+
? decodeBase64(cctpMsg.value.destination_caller)
22062207
: cctpMsg.value.destination_caller,
22072208
}).finish(),
22082209
},
@@ -2256,7 +2257,7 @@ export class CosmosAccountImpl {
22562257
from: cctpMsg.value.from,
22572258
amount: cctpMsg.value.amount,
22582259
destinationDomain: cctpMsg.value.destination_domain,
2259-
mintRecipient: this.decodeBase64(mintRecipient),
2260+
mintRecipient: decodeBase64(mintRecipient),
22602261
burnToken: cctpMsg.value.burn_token,
22612262
})
22622263
).finish(),
@@ -2276,21 +2277,4 @@ export class CosmosAccountImpl {
22762277
protected get queries(): DeepReadonly<QueriesSetBase & CosmosQueries> {
22772278
return this.queriesStore.get(this.chainId);
22782279
}
2279-
2280-
private decodeBase64(base64: string): Uint8Array {
2281-
try {
2282-
const bin = atob(base64);
2283-
const arr = new Uint8Array(bin.length);
2284-
for (let i = 0; i < bin.length; i++) {
2285-
arr[i] = bin.charCodeAt(i);
2286-
}
2287-
return arr;
2288-
} catch (e) {
2289-
throw new Error(
2290-
`Failed to decode Base64: ${
2291-
e instanceof Error ? e.message : "Unknown error"
2292-
}`
2293-
);
2294-
}
2295-
}
22962280
}

packages/stores/src/account/utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,20 @@ export const getEip712TypedDataBasedOnChainInfo = (
161161
// Return default types for Evmos
162162
return types;
163163
};
164+
165+
export function decodeBase64(base64: string): Uint8Array {
166+
try {
167+
const bin = atob(base64);
168+
const arr = new Uint8Array(bin.length);
169+
for (let i = 0; i < bin.length; i++) {
170+
arr[i] = bin.charCodeAt(i);
171+
}
172+
return arr;
173+
} catch (e) {
174+
throw new Error(
175+
`Failed to decode Base64: ${
176+
e instanceof Error ? e.message : "Unknown error"
177+
}`
178+
);
179+
}
180+
}

0 commit comments

Comments
 (0)