Skip to content

Commit 3d7c0dc

Browse files
committed
add ObservableQueryIbcPfmTransferV2 to swap queries
1 parent 6acc2dd commit 3d7c0dc

File tree

10 files changed

+982
-21
lines changed

10 files changed

+982
-21
lines changed

apps/extension/src/pages/main/token-detail/modal.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const TokenDetailModal: FunctionComponent<{
8686
bitcoinQueriesStore,
8787
priceStore,
8888
price24HChangesStore,
89-
skipQueriesStore,
89+
swapQueriesStore,
9090
uiConfigStore,
9191
} = useStore();
9292

@@ -300,9 +300,9 @@ export const TokenDetailModal: FunctionComponent<{
300300
}&outCoinMinimalDenom=uusdc&entryPoint=token_detail`
301301
);
302302
},
303-
disabled: !skipQueriesStore.queryIBCSwap.isSwappableCurrency(
303+
disabled: !swapQueriesStore.querySwappable.isSwappableToken(
304304
chainId,
305-
currency
305+
currency.coinMinimalDenom
306306
),
307307
},
308308
{

apps/extension/src/stores/root.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ export class RootStore {
406406
this.swapQueriesStore = new SwapQueries(
407407
this.queriesStore.sharedContext,
408408
this.chainStore,
409-
"https://keplr-api-dev.keplr.app"
409+
"https://keplr-api-dev.keplr.app" // TODO: change to production URL
410410
);
411411
this.starknetQueriesStore = new StarknetQueriesStore(
412412
this.queriesStore.sharedContext,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class IBCSwapAmountConfig extends AmountConfig {
5454
[CosmosAccount, CosmwasmAccount]
5555
>,
5656
public readonly ethereumAccountStore: EthereumAccountStore,
57-
protected readonly skipQueries: SkipQueries,
57+
protected readonly skipQueries: SkipQueries, // TODO: replace with swap queries
5858
initialChainId: string,
5959
senderConfig: ISenderConfig,
6060
initialOutChainId: string,
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import {
2+
IChainInfoImpl,
3+
ObservableQuery,
4+
QuerySharedContext,
5+
} from "@keplr-wallet/stores";
6+
import { ChainsResponseV2 } from "./types";
7+
import { computed, makeObservable } from "mobx";
8+
import { ChainIdHelper } from "@keplr-wallet/cosmos";
9+
import { computedFn } from "mobx-utils";
10+
import Joi from "joi";
11+
import { InternalChainStore } from "../internal";
12+
import { simpleFetch } from "@keplr-wallet/simple-fetch";
13+
14+
const Schema = Joi.object<ChainsResponseV2>({
15+
chains: Joi.array().items(
16+
Joi.object({
17+
chain_id: Joi.string(),
18+
pfm_enabled: Joi.boolean(),
19+
supports_memo: Joi.boolean(),
20+
chain_type: Joi.string(),
21+
}).unknown(true)
22+
),
23+
}).unknown(true);
24+
25+
export class ObservableQueryChainsV2 extends ObservableQuery<ChainsResponseV2> {
26+
constructor(
27+
sharedContext: QuerySharedContext,
28+
protected readonly chainStore: InternalChainStore,
29+
protected readonly skipURL: string
30+
) {
31+
super(sharedContext, skipURL, "/v1/swap/chains");
32+
33+
makeObservable(this);
34+
}
35+
36+
protected override async fetchResponse(
37+
abortController: AbortController
38+
): Promise<{ headers: any; data: ChainsResponseV2 }> {
39+
const _result = await simpleFetch(this.baseURL, this.url, {
40+
signal: abortController.signal,
41+
headers: {
42+
...(() => {
43+
const res: { authorization?: string } = {};
44+
if (process.env["SKIP_API_KEY"]) {
45+
res.authorization = process.env["SKIP_API_KEY"];
46+
}
47+
48+
return res;
49+
})(),
50+
},
51+
});
52+
const result = {
53+
headers: _result.headers,
54+
data: _result.data,
55+
};
56+
57+
const validated = Schema.validate(result.data);
58+
if (validated.error) {
59+
console.log("Failed to validate chains response", validated.error);
60+
throw validated.error;
61+
}
62+
63+
return {
64+
headers: result.headers,
65+
data: validated.value,
66+
};
67+
}
68+
69+
@computed
70+
get chains(): {
71+
chainInfo: IChainInfoImpl;
72+
pfmEnabled: boolean;
73+
supportsMemo: boolean;
74+
chainType: string;
75+
}[] {
76+
if (!this.response) {
77+
return [];
78+
}
79+
80+
return this.response.data.chains
81+
.filter((chain) => {
82+
const isEVMChain = chain.chain_type === "evm";
83+
const chainId = isEVMChain
84+
? `eip155:${chain.chain_id}`
85+
: chain.chain_id;
86+
87+
return this.chainStore.hasChain(chainId);
88+
})
89+
.filter((chain) => {
90+
const isEVMChain = chain.chain_type === "evm";
91+
const chainId = isEVMChain
92+
? `eip155:${chain.chain_id}`
93+
: chain.chain_id;
94+
95+
return this.chainStore.isInChainInfosInListUI(chainId);
96+
})
97+
.map((chain) => {
98+
const isEVMChain = chain.chain_type === "evm";
99+
const chainId = isEVMChain
100+
? `eip155:${chain.chain_id}`
101+
: chain.chain_id;
102+
103+
return {
104+
chainInfo: this.chainStore.getChain(chainId),
105+
pfmEnabled: chain.pfm_enabled,
106+
supportsMemo: chain.supports_memo ?? false,
107+
chainType: chain.chain_type,
108+
};
109+
});
110+
}
111+
112+
isPFMEnabled = computedFn((chainId: string): boolean => {
113+
const chain = this.chains.find((chain) => {
114+
return (
115+
chain.chainInfo.chainIdentifier ===
116+
ChainIdHelper.parse(chainId).identifier
117+
);
118+
});
119+
if (!chain) {
120+
return false;
121+
}
122+
123+
return chain.pfmEnabled;
124+
});
125+
126+
isSupportsMemo = computedFn((chainId: string): boolean => {
127+
const chain = this.chains.find((chain) => {
128+
return (
129+
chain.chainInfo.chainIdentifier ===
130+
ChainIdHelper.parse(chainId).identifier
131+
);
132+
});
133+
if (!chain) {
134+
return false;
135+
}
136+
137+
return chain.supportsMemo;
138+
});
139+
140+
isChainTypeEVM = computedFn((chainId: string): boolean => {
141+
const chain = this.chains.find((chain) => {
142+
return (
143+
chain.chainInfo.chainIdentifier ===
144+
ChainIdHelper.parse(chainId).identifier
145+
);
146+
});
147+
if (!chain) {
148+
return false;
149+
}
150+
151+
return chain.chainType === "evm";
152+
});
153+
}

0 commit comments

Comments
 (0)