Skip to content

Commit

Permalink
실제 swap이 이루어지고 있는 토큰만 destination에 표시하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Thunnini committed May 20, 2024
1 parent 46d5d3a commit 34cf51c
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 10 deletions.
7 changes: 7 additions & 0 deletions apps/extension/src/stores/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import { TokenContractsQueries } from "./token-contracts";
import {
SkipQueries,
Price24HChangesStore,
SwapUsageQueries,
} from "@keplr-wallet/stores-internal";

export class RootStore {
Expand Down Expand Up @@ -102,6 +103,7 @@ export class RootStore {
EthereumQueries
]
>;
public readonly swapUsageQueries: SwapUsageQueries;
public readonly skipQueriesStore: SkipQueries;
public readonly accountStore: AccountStore<
[CosmosAccount, CosmwasmAccount, SecretAccount]
Expand Down Expand Up @@ -197,9 +199,14 @@ export class RootStore {
}),
EthereumQueries.use()
);
this.swapUsageQueries = new SwapUsageQueries(
this.queriesStore.sharedContext,
process.env["KEPLR_EXT_TX_HISTORY_BASE_URL"]
);
this.skipQueriesStore = new SkipQueries(
this.queriesStore.sharedContext,
this.chainStore,
this.swapUsageQueries,
SwapVenue
);

Expand Down
17 changes: 9 additions & 8 deletions apps/mobile/src/navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ import {
DecryptedKeyRingDatasResponse,
} from './screen/register/import-from-extension';
import {SwapIcon} from './components/icon/swap.tsx';
import {IBCSwapScreen} from './screen/ibc-swap';
import {IBCSwapDestinationSelectAssetScreen} from './screen/ibc-swap/select-asset';
import {EditFavoriteUrlScreen} from './screen/web/edit-favorite';
import {SearchUrlScreen} from './screen/web/search';
Expand Down Expand Up @@ -641,14 +642,14 @@ export const MainTabNavigation: FunctionComponent = () => {
}}
component={HomeScreen}
/>
{/*<Tab.Screen*/}
{/* name="Swap"*/}
{/* options={{*/}
{/* headerTitle: HomeScreenHeaderTitleFunc,*/}
{/* ...homeHeaderOptions,*/}
{/* }}*/}
{/* component={IBCSwapScreen}*/}
{/*/>*/}
<Tab.Screen
name="Swap"
options={{
headerTitle: HomeScreenHeaderTitleFunc,
...homeHeaderOptions,
}}
component={IBCSwapScreen}
/>
<Tab.Screen
name="WebTab"
options={{headerShown: false}}
Expand Down
2 changes: 2 additions & 0 deletions apps/mobile/src/screen/ibc-swap/select-asset/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ export const IBCSwapDestinationSelectAssetScreen: FunctionComponent = observer(
<BoundaryScrollViewBoundary
itemHeight={74}
gap={8}
initialNumItemsToRender={99999}
floodNumItemsToRender={99999}
data={(
filteredTokens.map(viewToken => ({
type: 'filtered',
Expand Down
8 changes: 7 additions & 1 deletion apps/mobile/src/stores/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import {
GravityBridgeCurrencyRegistrar,
KeplrETCQueries,
} from '@keplr-wallet/stores-etc';
import {SkipQueries} from '@keplr-wallet/stores-internal';
import {SkipQueries, SwapUsageQueries} from '@keplr-wallet/stores-internal';
import {DeepLinkStore} from './deep-link';
import {EthereumQueries, EthereumAccountStore} from '@keplr-wallet/stores-eth';
import {WebpageStore} from './webpage';
Expand Down Expand Up @@ -98,6 +98,7 @@ export class RootStore {
EthereumQueries,
]
>;
public readonly swapUsageQueries: SwapUsageQueries;
public readonly skipQueriesStore: SkipQueries;
public readonly accountStore: AccountStore<
[CosmosAccount, CosmwasmAccount, SecretAccount]
Expand Down Expand Up @@ -194,9 +195,14 @@ export class RootStore {
EthereumQueries.use(),
);

this.swapUsageQueries = new SwapUsageQueries(
this.queriesStore.sharedContext,
'https://satellite-develop.keplr.app',
);
this.skipQueriesStore = new SkipQueries(
this.queriesStore.sharedContext,
this.chainStore,
this.swapUsageQueries,
SwapVenue,
);

Expand Down
1 change: 1 addition & 0 deletions apps/stores-internal/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./skip";
export * from "./price-changes";
export * from "./swap-usage";
77 changes: 77 additions & 0 deletions apps/stores-internal/src/skip/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AssetsResponse } from "./types";
import { computed, makeObservable } from "mobx";
import Joi from "joi";
import { InternalChainStore } from "../internal";
import { SwapUsageQueries } from "../swap-usage";

const Schema = Joi.object<AssetsResponse>({
chain_to_assets_map: Joi.object().pattern(
Expand All @@ -28,6 +29,7 @@ export class ObservableQueryAssetsInner extends ObservableQuery<AssetsResponse>
constructor(
sharedContext: QuerySharedContext,
protected readonly chainStore: InternalChainStore,
protected readonly swapUsageQueries: SwapUsageQueries,
skipURL: string,
public readonly chainId: string
) {
Expand Down Expand Up @@ -105,6 +107,79 @@ export class ObservableQueryAssetsInner extends ObservableQuery<AssetsResponse>
return [];
}

@computed
get assetsOnlySwapUsages(): {
denom: string;
chainId: string;
originDenom: string;
originChainId: string;
}[] {
if (
!this.response ||
!this.response.data ||
!this.response.data.chain_to_assets_map
) {
return [];
}

if (!this.chainStore.hasChain(this.chainId)) {
return [];
}

const chainInfo = this.chainStore.getChain(this.chainId);
if (!this.chainStore.isInChainInfosInListUI(chainInfo.chainId)) {
return [];
}

const assetsInResponse =
this.response.data.chain_to_assets_map[chainInfo.chainId];
if (assetsInResponse) {
const res: {
denom: string;
chainId: string;
originDenom: string;
originChainId: string;
}[] = [];

for (const asset of assetsInResponse.assets) {
if (
this.chainStore.hasChain(asset.chain_id) &&
this.chainStore.hasChain(asset.origin_chain_id)
) {
if (
!this.swapUsageQueries.querySwapUsage
.getSwapUsage(this.chainId)
.isSwappable(asset.denom)
) {
continue;
}

// IBC asset일 경우 그냥 넣는다.
if (asset.denom.startsWith("ibc/")) {
res.push({
denom: asset.denom,
chainId: asset.chain_id,
originDenom: asset.origin_denom,
originChainId: asset.origin_chain_id,
});
// IBC asset이 아니라면 알고있는 currency만 넣는다.
} else if (chainInfo.findCurrencyWithoutReaction(asset.denom)) {
res.push({
denom: asset.denom,
chainId: asset.chain_id,
originDenom: asset.origin_denom,
originChainId: asset.origin_chain_id,
});
}
}
}

return res;
}

return [];
}

protected override async fetchResponse(
abortController: AbortController
): Promise<{ headers: any; data: AssetsResponse }> {
Expand All @@ -130,12 +205,14 @@ export class ObservableQueryAssets extends HasMapStore<ObservableQueryAssetsInne
constructor(
protected readonly sharedContext: QuerySharedContext,
protected readonly chainStore: InternalChainStore,
protected readonly swapUsageQueries: SwapUsageQueries,
protected readonly skipURL: string
) {
super((chainId) => {
return new ObservableQueryAssetsInner(
this.sharedContext,
this.chainStore,
this.swapUsageQueries,
this.skipURL,
chainId
);
Expand Down
2 changes: 1 addition & 1 deletion apps/stores-internal/src/skip/ibc-swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ export class ObservableQueryIbcSwap extends HasMapStore<ObservableQueryIBCSwapIn
const swapChainInfo = this.chainStore.getChain(this.swapVenue.chainId);

const queryAssets = this.queryAssets.getAssets(swapChainInfo.chainId);
const assets = queryAssets.assets;
const assets = queryAssets.assetsOnlySwapUsages;

// Key is chain identifier
const res = new Map<
Expand Down
3 changes: 3 additions & 0 deletions apps/stores-internal/src/skip/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ObservableQueryAssets } from "./assets";
import { ObservableQueryIbcSwap } from "./ibc-swap";
import { ObservableQueryMsgsDirect } from "./msgs-direct";
import { InternalChainStore } from "../internal";
import { SwapUsageQueries } from "../swap-usage";

export class SkipQueries {
public readonly queryChains: DeepReadonly<ObservableQueryChains>;
Expand All @@ -23,6 +24,7 @@ export class SkipQueries {
constructor(
sharedContext: QuerySharedContext,
chainStore: InternalChainStore,
swapUsageQueries: SwapUsageQueries,
swapVenue: {
name: string;
chainId: string;
Expand All @@ -36,6 +38,7 @@ export class SkipQueries {
this.queryAssets = new ObservableQueryAssets(
sharedContext,
chainStore,
swapUsageQueries,
"https://api.skip.money"
);
this.queryAssetsFromSource = new ObservableQueryAssetsFromSource(
Expand Down
2 changes: 2 additions & 0 deletions apps/stores-internal/src/swap-usage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./queries";
export * from "./swap-usage";
11 changes: 11 additions & 0 deletions apps/stores-internal/src/swap-usage/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { QuerySharedContext } from "@keplr-wallet/stores";
import { DeepReadonly } from "utility-types";
import { ObservableQuerySwapUsage } from "./swap-usage";

export class SwapUsageQueries {
public readonly querySwapUsage: DeepReadonly<ObservableQuerySwapUsage>;

constructor(sharedContext: QuerySharedContext, baseURL: string) {
this.querySwapUsage = new ObservableQuerySwapUsage(sharedContext, baseURL);
}
}
62 changes: 62 additions & 0 deletions apps/stores-internal/src/swap-usage/swap-usage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
HasMapStore,
ObservableQuery,
QuerySharedContext,
} from "@keplr-wallet/stores";
import { computed, makeObservable } from "mobx";
import { ChainIdHelper } from "@keplr-wallet/cosmos";

export type SwapUsageResponse = string[];

export class ObservableQuerySwapUsageInner extends ObservableQuery<SwapUsageResponse> {
constructor(
sharedContext: QuerySharedContext,
baseURL: string,
chainIdentifier: string
) {
super(sharedContext, baseURL, `/swap-usage/denoms/${chainIdentifier}`);

makeObservable(this);
}

get denoms(): string[] {
if (!this.response) {
return [];
}

return this.response.data;
}

@computed
get denomMap(): Map<string, boolean> {
const map = new Map<string, boolean>();
for (const denom of this.denoms) {
map.set(denom, true);
}

return map;
}

isSwappable(denom: string): boolean {
return this.denomMap.has(denom);
}
}

export class ObservableQuerySwapUsage extends HasMapStore<ObservableQuerySwapUsageInner> {
constructor(
protected readonly sharedContext: QuerySharedContext,
protected readonly baseURL: string
) {
super((str) => {
return new ObservableQuerySwapUsageInner(
this.sharedContext,
baseURL,
str
);
});
}

getSwapUsage(chainIdentifier: string): ObservableQuerySwapUsageInner {
return this.get(ChainIdHelper.parse(chainIdentifier).identifier);
}
}

0 comments on commit 34cf51c

Please sign in to comment.