|
4 | 4 | import { Asset, Assets, Chain, Chains } from '@chainflip/sdk/swap';
|
5 | 5 | import { COMMON_ASSETS, COMMON_CHAIN_SLUGS } from '@subwallet/chain-list';
|
6 | 6 | 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'; |
8 | 10 | import { CHAINFLIP_BROKER_API } from '@subwallet/extension-base/services/swap-service/handler/chainflip-handler';
|
9 | 11 | import { SwapPair, SwapProviderId } from '@subwallet/extension-base/types/swap';
|
10 | 12 | import BigN from 'bignumber.js';
|
@@ -117,3 +119,66 @@ export function getChainflipBroker (isTestnet: boolean) { // noted: currently no
|
117 | 119 | };
|
118 | 120 | }
|
119 | 121 | }
|
| 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