Skip to content

Commit 509abe7

Browse files
committed
[Issue-4079] feat: handle 1-length path
1 parent ff5b6b5 commit 509abe7

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

packages/extension-base/src/koni/background/handlers/Extension.ts

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import { EXTENSION_REQUEST_URL } from '@subwallet/extension-base/services/reques
4646
import { AuthUrls } from '@subwallet/extension-base/services/request-service/types';
4747
import { DEFAULT_AUTO_LOCK_TIME } from '@subwallet/extension-base/services/setting-service/constants';
4848
import { checkLiquidityForPool, estimateTokensForPool, getReserveForPool } from '@subwallet/extension-base/services/swap-service/handler/asset-hub/utils';
49+
import { generateSwapPairs } from '@subwallet/extension-base/services/swap-service/utils';
4950
import { SWTransaction, SWTransactionInput, SWTransactionResponse, SWTransactionResult, TransactionEmitter, TransactionEventResponse, ValidateTransactionResponseInput } from '@subwallet/extension-base/services/transaction-service/types';
5051
import { isProposalExpired, isSupportWalletConnectChain, isSupportWalletConnectNamespace } from '@subwallet/extension-base/services/wallet-connect-service/helpers';
5152
import { ResultApproveWalletConnectSession, WalletConnectNotSupportRequest, WalletConnectSessionRequest } from '@subwallet/extension-base/services/wallet-connect-service/types';
@@ -1325,6 +1326,10 @@ export default class KoniExtension {
13251326
const warnings: TransactionWarning[] = [];
13261327
const chainInfo = this.#koniState.getChainInfo(chain);
13271328

1329+
const test = generateSwapPairs(this.#koniState.getSubstrateApi(chain), this.#koniState.chainService, transferTokenInfo);
1330+
1331+
console.log('test', test);
1332+
13281333
const nativeTokenInfo = this.#koniState.getNativeTokenInfo(chain);
13291334
const nativeTokenSlug: string = nativeTokenInfo.slug;
13301335
const isTransferNativeToken = nativeTokenSlug === tokenSlug;

packages/extension-base/src/services/chain-service/utils/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,10 @@ export function _isAssetFungibleToken (chainAsset: _ChainAsset): boolean {
486486
return ![_AssetType.ERC721, _AssetType.PSP34, _AssetType.UNKNOWN].includes(chainAsset.assetType);
487487
}
488488

489+
export function _getFungibleAssetType () {
490+
return [_AssetType.NATIVE, _AssetType.LOCAL, _AssetType.ERC20, _AssetType.GRC20, _AssetType.TEP74, _AssetType.PSP22, _AssetType.VFT];
491+
}
492+
489493
export const _isAssetAutoEnable = (chainAsset: _ChainAsset): boolean => {
490494
return chainAsset.metadata ? !!chainAsset.metadata.autoEnable : false;
491495
};

packages/extension-base/src/services/swap-service/utils.ts

+66-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import { Asset, Assets, Chain, Chains } from '@chainflip/sdk/swap';
55
import { COMMON_ASSETS, COMMON_CHAIN_SLUGS } from '@subwallet/chain-list';
66
import { _ChainAsset } from '@subwallet/chain-list/types';
7-
import { _getAssetDecimals } from '@subwallet/extension-base/services/chain-service/utils';
7+
import { ChainService } from '@subwallet/extension-base/services/chain-service';
8+
import { _SubstrateApi } from '@subwallet/extension-base/services/chain-service/types';
9+
import { _getAssetDecimals, _getFungibleAssetType, _getMultiChainAsset } from '@subwallet/extension-base/services/chain-service/utils';
810
import { CHAINFLIP_BROKER_API } from '@subwallet/extension-base/services/swap-service/handler/chainflip-handler';
911
import { SwapPair, SwapProviderId } from '@subwallet/extension-base/types/swap';
1012
import BigN from 'bignumber.js';
@@ -117,3 +119,66 @@ export function getChainflipBroker (isTestnet: boolean) { // noted: currently no
117119
};
118120
}
119121
}
122+
123+
export function generateSwapPairs (substrateApi: _SubstrateApi, chainService: ChainService, fromAsset: _ChainAsset, maxPathLength = 1) {
124+
let currentTargets: _ChainAsset[] = [];
125+
let newTargets: _ChainAsset[] = [];
126+
127+
if (maxPathLength < 1) {
128+
return currentTargets;
129+
}
130+
131+
const xcmTargets = findXcmDestinations(chainService, fromAsset);
132+
const swapTargets = findSwapDestinations(chainService, fromAsset);
133+
134+
newTargets = Array.from(new Set([...xcmTargets, ...swapTargets]));
135+
currentTargets = Array.from(new Set([...currentTargets, ...newTargets]));
136+
137+
return currentTargets;
138+
}
139+
140+
export function findXcmDestinations (chainService: ChainService, chainAsset: _ChainAsset) {
141+
const xcmTargets: _ChainAsset[] = [];
142+
const multichainAssetSlug = _getMultiChainAsset(chainAsset);
143+
144+
if (!multichainAssetSlug) {
145+
return xcmTargets;
146+
}
147+
148+
const assetRegistry = chainService.getAssetRegistry();
149+
150+
for (const asset of Object.values(assetRegistry)) {
151+
if (multichainAssetSlug === _getMultiChainAsset(asset)) {
152+
xcmTargets.push(asset);
153+
}
154+
}
155+
156+
return xcmTargets;
157+
}
158+
159+
export function findSwapDestinations (chainService: ChainService, chainAsset: _ChainAsset) {
160+
const swapTargets: _ChainAsset[] = [];
161+
const chain = chainAsset.originChain;
162+
163+
const availableChains = Object.values(_PROVIDER_TO_SUPPORTED_PAIR_MAP).reduce((remainChains, currentChains) => {
164+
if (currentChains.includes(chain)) {
165+
currentChains.forEach((candidate) => {
166+
remainChains.add(candidate);
167+
});
168+
}
169+
170+
return remainChains;
171+
}, new Set<string>());
172+
173+
availableChains.forEach((candidate) => {
174+
const assets = chainService.getAssetByChainAndType(candidate, _getFungibleAssetType()); // todo: recheck assetType
175+
176+
swapTargets.push(...Object.values(assets));
177+
});
178+
179+
return swapTargets;
180+
}
181+
182+
export function isHasChannel (subtrateApi: _SubstrateApi, fromChain: _ChainAsset, toChain: _ChainAsset) {
183+
return true;
184+
}

0 commit comments

Comments
 (0)