@subwallet/extension-base 1.3.40-0 → 1.3.41-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/cjs/constants/environment.js +4 -2
  2. package/cjs/koni/background/handlers/Extension.js +37 -38
  3. package/cjs/packageInfo.js +1 -1
  4. package/cjs/services/balance-service/helpers/subscribe/evm.js +6 -1
  5. package/cjs/services/balance-service/transfer/xcm/index.js +15 -9
  6. package/cjs/services/balance-service/transfer/xcm/utils.js +12 -14
  7. package/cjs/services/chain-service/constants.js +4 -3
  8. package/cjs/services/chain-service/handler/CardanoApi.js +25 -35
  9. package/cjs/services/earning-service/handlers/native-staking/tao.js +4 -38
  10. package/cjs/services/swap-service/handler/chainflip-handler.js +29 -18
  11. package/cjs/services/swap-service/handler/kyber-handler.js +5 -9
  12. package/cjs/services/swap-service/handler/simpleswap-handler.js +4 -7
  13. package/cjs/services/swap-service/handler/uniswap-handler.js +5 -12
  14. package/cjs/services/swap-service/utils.js +46 -37
  15. package/cjs/types/environment.js +19 -0
  16. package/cjs/utils/environment.js +30 -2
  17. package/constants/environment.d.ts +1 -0
  18. package/constants/environment.js +2 -1
  19. package/koni/background/handlers/Extension.js +37 -38
  20. package/package.json +11 -6
  21. package/packageInfo.js +1 -1
  22. package/services/balance-service/helpers/subscribe/evm.js +6 -1
  23. package/services/balance-service/transfer/xcm/index.js +15 -9
  24. package/services/balance-service/transfer/xcm/utils.d.ts +2 -0
  25. package/services/balance-service/transfer/xcm/utils.js +12 -14
  26. package/services/chain-service/constants.d.ts +1 -0
  27. package/services/chain-service/constants.js +4 -3
  28. package/services/chain-service/handler/CardanoApi.d.ts +1 -5
  29. package/services/chain-service/handler/CardanoApi.js +26 -34
  30. package/services/earning-service/handlers/native-staking/tao.d.ts +0 -11
  31. package/services/earning-service/handlers/native-staking/tao.js +4 -24
  32. package/services/swap-service/handler/chainflip-handler.d.ts +0 -2
  33. package/services/swap-service/handler/chainflip-handler.js +25 -13
  34. package/services/swap-service/handler/kyber-handler.d.ts +0 -1
  35. package/services/swap-service/handler/kyber-handler.js +5 -8
  36. package/services/swap-service/handler/simpleswap-handler.d.ts +0 -1
  37. package/services/swap-service/handler/simpleswap-handler.js +4 -6
  38. package/services/swap-service/handler/uniswap-handler.js +6 -13
  39. package/services/swap-service/utils.d.ts +0 -13
  40. package/services/swap-service/utils.js +46 -34
  41. package/types/environment.d.ts +9 -0
  42. package/types/environment.js +13 -0
  43. package/utils/environment.d.ts +2 -0
  44. package/utils/environment.js +27 -1
@@ -3,13 +3,16 @@
3
3
 
4
4
  import { cborToBytes, retryCardanoTxStatus } from '@subwallet/extension-base/services/balance-service/helpers/subscribe/cardano/utils';
5
5
  import { _ChainConnectionStatus } from '@subwallet/extension-base/services/chain-service/types';
6
- import { createPromiseHandler } from '@subwallet/extension-base/utils';
6
+ import { ProxyServiceRoute } from '@subwallet/extension-base/types/environment';
7
+ import { createPromiseHandler, fetchFromProxyService } from '@subwallet/extension-base/utils';
7
8
  import { BehaviorSubject } from 'rxjs';
8
9
  import { hexAddPrefix, isHex } from '@polkadot/util';
9
- export const API_KEY = {
10
- mainnet: process.env.BLOCKFROST_API_KEY_MAIN || '',
11
- testnet: process.env.BLOCKFROST_API_KEY_PREP || ''
12
- };
10
+
11
+ // export const API_KEY = {
12
+ // mainnet: process.env.BLOCKFROST_API_KEY_MAIN || '',
13
+ // testnet: process.env.BLOCKFROST_API_KEY_PREP || ''
14
+ // };
15
+
13
16
  export class CardanoApi {
14
17
  // private api: BlockFrostAPI;
15
18
 
@@ -25,7 +28,6 @@ export class CardanoApi {
25
28
  this.chainSlug = chainSlug;
26
29
  this.apiUrl = apiUrl;
27
30
  this.isTestnet = isTestnet !== null && isTestnet !== void 0 ? isTestnet : true;
28
- this.projectId = isTestnet ? API_KEY.testnet : API_KEY.mainnet;
29
31
  this.providerName = providerName || 'unknown';
30
32
  // this.api = this.createProvider(isTestnet);
31
33
  this.isReadyHandler = createPromiseHandler();
@@ -37,6 +39,9 @@ export class CardanoApi {
37
39
  get connectionStatus() {
38
40
  return this.connectionStatusSubject.getValue();
39
41
  }
42
+ fetchCardano(path, options) {
43
+ return fetchFromProxyService(ProxyServiceRoute.CARDANO, path, options, this.isTestnet);
44
+ }
40
45
  updateConnectionStatus(status) {
41
46
  const isConnected = status === _ChainConnectionStatus.CONNECTED;
42
47
  if (isConnected !== this.isApiConnectedSubject.value) {
@@ -107,12 +112,9 @@ export class CardanoApi {
107
112
  }
108
113
  async getBalanceMap(address) {
109
114
  try {
110
- const url = this.isTestnet ? `https://cardano-preprod.blockfrost.io/api/v0/addresses/${address}` : `https://cardano-mainnet.blockfrost.io/api/v0/addresses/${address}`;
111
- const response = await fetch(url, {
112
- method: 'GET',
113
- headers: {
114
- Project_id: this.projectId
115
- }
115
+ const path = `/addresses/${address}`;
116
+ const response = await this.fetchCardano(path, {
117
+ method: 'GET'
116
118
  });
117
119
  const addressBalance = await response.json();
118
120
  return addressBalance.amount;
@@ -123,13 +125,10 @@ export class CardanoApi {
123
125
  }
124
126
  async getUtxos(address, page, limit) {
125
127
  try {
126
- let url = this.isTestnet ? `https://cardano-preprod.blockfrost.io/api/v0/addresses/${address}/utxos` : `https://cardano-mainnet.blockfrost.io/api/v0/addresses/${address}/utxos`;
127
- url += `?page=${page}&count=${limit}`;
128
- const response = await fetch(url, {
129
- method: 'GET',
130
- headers: {
131
- Project_id: this.projectId
132
- }
128
+ let path = `/addresses/${address}/utxos`;
129
+ path += `?page=${page}&count=${limit}`;
130
+ const response = await this.fetchCardano(path, {
131
+ method: 'GET'
133
132
  });
134
133
  const utxos = await response.json();
135
134
  return utxos;
@@ -140,12 +139,9 @@ export class CardanoApi {
140
139
  }
141
140
  async getSpecificUtxo(txHash) {
142
141
  try {
143
- const url = this.isTestnet ? `https://cardano-preprod.blockfrost.io/api/v0/txs/${txHash}/utxos` : `https://cardano-mainnet.blockfrost.io/api/v0/txs/${txHash}/utxos`;
144
- const response = await fetch(url, {
145
- method: 'GET',
146
- headers: {
147
- Project_id: this.projectId
148
- }
142
+ const path = `/txs/${txHash}/utxos`;
143
+ const response = await this.fetchCardano(path, {
144
+ method: 'GET'
149
145
  });
150
146
  const utxo = await response.json();
151
147
  return utxo;
@@ -156,11 +152,10 @@ export class CardanoApi {
156
152
  }
157
153
  async sendCardanoTxReturnHash(tx) {
158
154
  try {
159
- const url = this.isTestnet ? 'https://cardano-preprod.blockfrost.io/api/v0/tx/submit' : 'https://cardano-mainnet.blockfrost.io/api/v0/tx/submit';
160
- const response = await fetch(url, {
155
+ const path = '/tx/submit';
156
+ const response = await this.fetchCardano(path, {
161
157
  method: 'POST',
162
158
  headers: {
163
- Project_id: this.projectId,
164
159
  'Content-Type': 'application/cbor'
165
160
  },
166
161
  body: cborToBytes(tx)
@@ -180,12 +175,9 @@ export class CardanoApi {
180
175
  async getStatusByTxHash(txHash, ttl) {
181
176
  const cronTime = 30000;
182
177
  return retryCardanoTxStatus(async () => {
183
- const url = this.isTestnet ? `https://cardano-preprod.blockfrost.io/api/v0/txs/${txHash}` : `https://cardano-mainnet.blockfrost.io/api/v0/txs/${txHash}`;
184
- const response = await fetch(url, {
185
- method: 'GET',
186
- headers: {
187
- Project_id: this.projectId
188
- }
178
+ const path = `/txs/${txHash}`;
179
+ const response = await this.fetchCardano(path, {
180
+ method: 'GET'
189
181
  });
190
182
  const txInfo = await response.json();
191
183
  if (txInfo.block && txInfo.hash && txInfo.index >= 0) {
@@ -37,17 +37,6 @@ interface Validator {
37
37
  take: string;
38
38
  apr: string;
39
39
  }
40
- export declare const BITTENSOR_API_KEY_1: string;
41
- export declare const BITTENSOR_API_KEY_2: string;
42
- export declare const BITTENSOR_API_KEY_3: string;
43
- export declare const BITTENSOR_API_KEY_4: string;
44
- export declare const BITTENSOR_API_KEY_5: string;
45
- export declare const BITTENSOR_API_KEY_6: string;
46
- export declare const BITTENSOR_API_KEY_7: string;
47
- export declare const BITTENSOR_API_KEY_8: string;
48
- export declare const BITTENSOR_API_KEY_9: string;
49
- export declare const BITTENSOR_API_KEY_10: string;
50
- export declare const bittensorApiKey: () => string;
51
40
  export declare class BittensorCache {
52
41
  private static instance;
53
42
  private cache;
@@ -8,31 +8,13 @@ import { getEarningStatusByNominations } from '@subwallet/extension-base/koni/ap
8
8
  import { _getAssetDecimals, _getAssetSymbol } from '@subwallet/extension-base/services/chain-service/utils';
9
9
  import BaseParaStakingPoolHandler from '@subwallet/extension-base/services/earning-service/handlers/native-staking/base-para';
10
10
  import { BasicTxErrorType, EarningStatus } from '@subwallet/extension-base/types';
11
- import { formatNumber, reformatAddress } from '@subwallet/extension-base/utils';
11
+ import { ProxyServiceRoute } from '@subwallet/extension-base/types/environment';
12
+ import { fetchFromProxyService, formatNumber, reformatAddress } from '@subwallet/extension-base/utils';
12
13
  import BigN from 'bignumber.js';
13
14
  import { t } from 'i18next';
14
15
  import { BN, BN_ZERO } from '@polkadot/util';
15
16
  import { calculateReward } from "../../utils/index.js";
16
17
  import { DEFAULT_DTAO_MINBOND } from "./dtao.js";
17
- export const BITTENSOR_API_KEY_1 = process.env.BITTENSOR_API_KEY_1 || '';
18
- export const BITTENSOR_API_KEY_2 = process.env.BITTENSOR_API_KEY_2 || '';
19
- export const BITTENSOR_API_KEY_3 = process.env.BITTENSOR_API_KEY_3 || '';
20
- export const BITTENSOR_API_KEY_4 = process.env.BITTENSOR_API_KEY_4 || '';
21
- export const BITTENSOR_API_KEY_5 = process.env.BITTENSOR_API_KEY_5 || '';
22
- export const BITTENSOR_API_KEY_6 = process.env.BITTENSOR_API_KEY_6 || '';
23
- export const BITTENSOR_API_KEY_7 = process.env.BITTENSOR_API_KEY_7 || '';
24
- export const BITTENSOR_API_KEY_8 = process.env.BITTENSOR_API_KEY_8 || '';
25
- export const BITTENSOR_API_KEY_9 = process.env.BITTENSOR_API_KEY_9 || '';
26
- export const BITTENSOR_API_KEY_10 = process.env.BITTENSOR_API_KEY_10 || '';
27
- function random(...keys) {
28
- const validKeys = keys.filter(key => key);
29
- const randomIndex = Math.floor(Math.random() * validKeys.length);
30
- return validKeys[randomIndex];
31
- }
32
- export const bittensorApiKey = () => {
33
- return random(BITTENSOR_API_KEY_1, BITTENSOR_API_KEY_2, BITTENSOR_API_KEY_3, BITTENSOR_API_KEY_4, BITTENSOR_API_KEY_5, BITTENSOR_API_KEY_6, BITTENSOR_API_KEY_7, BITTENSOR_API_KEY_8, BITTENSOR_API_KEY_9, BITTENSOR_API_KEY_10);
34
- };
35
-
36
18
  /* Fetch data */
37
19
  export class BittensorCache {
38
20
  static instance = null;
@@ -59,13 +41,11 @@ export class BittensorCache {
59
41
  return this.promise;
60
42
  }
61
43
  async fetchData() {
62
- const apiKey = bittensorApiKey();
63
44
  try {
64
- const resp = await fetch('https://api.taostats.io/api/validator/latest/v1?limit=50', {
45
+ const resp = await fetchFromProxyService(ProxyServiceRoute.BITTENSOR, '/validator/latest/v1?limit=50', {
65
46
  method: 'GET',
66
47
  headers: {
67
- 'Content-Type': 'application/json',
68
- Authorization: `${apiKey}`
48
+ 'Content-Type': 'application/json'
69
49
  }
70
50
  });
71
51
  if (!resp.ok) {
@@ -5,12 +5,10 @@ import { ChainService } from '@subwallet/extension-base/services/chain-service';
5
5
  import FeeService from '@subwallet/extension-base/services/fee-service/service';
6
6
  import { SwapBaseInterface } from '@subwallet/extension-base/services/swap-service/handler/base-handler';
7
7
  import { BaseStepDetail, CommonOptimalSwapPath, CommonStepFeeInfo, OptimalSwapPathParamsV2, SwapProviderId, SwapSubmitParams, SwapSubmitStepData, ValidateSwapProcessParams } from '@subwallet/extension-base/types';
8
- export declare const CHAINFLIP_BROKER_API: string;
9
8
  export declare class ChainflipSwapHandler implements SwapBaseInterface {
10
9
  private readonly isTestnet;
11
10
  private swapBaseHandler;
12
11
  providerSlug: SwapProviderId;
13
- private baseUrl;
14
12
  constructor(chainService: ChainService, balanceService: BalanceService, feeService: FeeService, isTestnet?: boolean);
15
13
  get chainService(): ChainService;
16
14
  get balanceService(): BalanceService;
@@ -8,15 +8,17 @@ import { getERC20TransactionObject, getEVMTransactionObject } from '@subwallet/e
8
8
  import { createSubstrateExtrinsic } from '@subwallet/extension-base/services/balance-service/transfer/token';
9
9
  import { _getAssetSymbol, _getContractAddressOfToken, _isChainSubstrateCompatible, _isNativeToken } from '@subwallet/extension-base/services/chain-service/utils';
10
10
  import { SwapBaseHandler } from '@subwallet/extension-base/services/swap-service/handler/base-handler';
11
- import { getChainflipSwap } from '@subwallet/extension-base/services/swap-service/utils';
12
11
  import { BasicTxErrorType, CommonStepType, DynamicSwapType, SwapProviderId, SwapStepType } from '@subwallet/extension-base/types';
13
- import { _reformatAddressWithChain } from '@subwallet/extension-base/utils';
12
+ import { ProxyServiceRoute } from '@subwallet/extension-base/types/environment';
13
+ import { _reformatAddressWithChain, fetchFromProxyService } from '@subwallet/extension-base/utils';
14
14
  import { getId } from '@subwallet/extension-base/utils/getId';
15
15
  import BigNumber from 'bignumber.js';
16
16
  const INTERMEDIARY_MAINNET_ASSET_SLUG = COMMON_ASSETS.USDC_ETHEREUM;
17
17
  const INTERMEDIARY_TESTNET_ASSET_SLUG = COMMON_ASSETS.USDC_SEPOLIA;
18
- export const CHAINFLIP_BROKER_API = process.env.CHAINFLIP_BROKER_API || '';
19
18
  export class ChainflipSwapHandler {
19
+ // private baseUrl: string;
20
+ // private assetsUrl: string;
21
+
20
22
  constructor(chainService, balanceService, feeService, isTestnet = true) {
21
23
  this.swapBaseHandler = new SwapBaseHandler({
22
24
  chainService,
@@ -27,8 +29,10 @@ export class ChainflipSwapHandler {
27
29
  });
28
30
  this.isTestnet = isTestnet;
29
31
  this.providerSlug = isTestnet ? SwapProviderId.CHAIN_FLIP_TESTNET : SwapProviderId.CHAIN_FLIP_MAINNET;
30
- this.baseUrl = getChainflipSwap(isTestnet);
32
+ // this.baseUrl = getChainflipSwap(isTestnet);
33
+ // this.assetsUrl = getAssetsUrl(isTestnet);
31
34
  }
35
+
32
36
  get chainService() {
33
37
  return this.swapBaseHandler.chainService;
34
38
  }
@@ -76,23 +80,31 @@ export class ChainflipSwapHandler {
76
80
  if (processMetadata.destChain !== quoteMetadata.destChain || processMetadata.srcChain !== quoteMetadata.srcChain) {
77
81
  throw new Error('Metadata for Chainflip not found');
78
82
  }
83
+ const assetsResponse = await fetchFromProxyService(ProxyServiceRoute.CHAINFLIP, '/assets', {
84
+ method: 'GET'
85
+ }, this.isTestnet);
86
+ const _allAssets = await assetsResponse.json();
87
+ const allAssets = _allAssets.assets;
88
+ const sourceAsset = allAssets.find(asset => asset.network === processMetadata.srcChain && asset.ticker === fromAssetId);
89
+ const destinationAsset = allAssets.find(asset => asset.network === processMetadata.destChain && asset.ticker === toAssetId);
90
+ if (!sourceAsset || !destinationAsset) {
91
+ throw new Error('Error get Chainflip data');
92
+ }
79
93
  const depositParams = {
80
- sourceChain: processMetadata.srcChain,
81
94
  destinationAddress: receiver,
82
- destinationAsset: toAssetId,
83
- destinationChain: processMetadata.destChain,
95
+ destinationAsset: destinationAsset.id,
96
+ sourceAsset: sourceAsset.id,
84
97
  minimumPrice: minReceive,
85
98
  // minimum accepted price for swaps through the channel
86
99
  refundAddress: address,
87
100
  // address to which assets are refunded
88
- retryDurationInBlocks: '100',
89
- // 100 blocks * 6 seconds = 10 minutes before deposits are refunded
90
- sourceAsset: fromAssetId
101
+ retryDurationInBlocks: '100' // 100 blocks * 6 seconds = 10 minutes before deposits are refunded
91
102
  };
92
- const url = `${this.baseUrl}&${new URLSearchParams(depositParams).toString()}`;
93
- const response = await fetch(url, {
103
+
104
+ const path = `/swap?${new URLSearchParams(depositParams).toString()}`;
105
+ const response = await fetchFromProxyService(ProxyServiceRoute.CHAINFLIP, path, {
94
106
  method: 'GET'
95
- });
107
+ }, this.isTestnet);
96
108
  const data = await response.json();
97
109
  if (!data.id || !data.address || data.address === '' || !data.issuedBlock || !data.network || !data.channelId) {
98
110
  throw new Error('Error get Chainflip data');
@@ -8,7 +8,6 @@ import { SwapBaseInterface } from './base-handler';
8
8
  export interface KyberSwapQuoteMetadata {
9
9
  priceImpact?: string;
10
10
  }
11
- export declare const KYBER_CLIENT_ID: string;
12
11
  export declare class KyberHandler implements SwapBaseInterface {
13
12
  private swapBaseHandler;
14
13
  transactionService: TransactionService;
@@ -6,14 +6,13 @@ import { TransactionError } from '@subwallet/extension-base/background/errors/Tr
6
6
  import { ChainType, ExtrinsicType } from '@subwallet/extension-base/background/KoniTypes';
7
7
  import { estimateTxFee, getERC20Allowance, getERC20SpendingApprovalTx } from '@subwallet/extension-base/koni/api/contract-handler/evm/web3';
8
8
  import { BasicTxErrorType, CommonStepType, DynamicSwapType, SwapErrorType, SwapFeeType, SwapProviderId, SwapStepType } from '@subwallet/extension-base/types';
9
- import { _reformatAddressWithChain, combineEthFee } from '@subwallet/extension-base/utils';
9
+ import { ProxyServiceRoute } from '@subwallet/extension-base/types/environment';
10
+ import { _reformatAddressWithChain, combineEthFee, fetchFromProxyService } from '@subwallet/extension-base/utils';
10
11
  import { getId } from '@subwallet/extension-base/utils/getId';
11
12
  import BigNumber from 'bignumber.js';
12
13
  import { _getChainNativeTokenSlug, _getContractAddressOfToken, _isNativeToken } from "../../chain-service/utils/index.js";
13
14
  import { calculateGasFeeParams } from "../../fee-service/utils/index.js";
14
15
  import { SwapBaseHandler } from "./base-handler.js";
15
- export const KYBER_CLIENT_ID = process.env.KYBER_CLIENT_ID || '';
16
- const kyberUrl = 'https://aggregator-api.kyberswap.com';
17
16
  async function buildTxForKyberSwap(params, chain) {
18
17
  const {
19
18
  recipient,
@@ -38,14 +37,13 @@ async function buildTxForKyberSwap(params, chain) {
38
37
  amountIn,
39
38
  gasInclude: 'true'
40
39
  });
41
- const url = `${kyberUrl}/${chain}/api/v1/routes?${queryParams.toString()}`;
40
+ const path = `/${chain}/api/v1/routes?${queryParams.toString()}`;
42
41
  try {
43
42
  var _routeData$data;
44
- const res = await fetch(url, {
43
+ const res = await fetchFromProxyService(ProxyServiceRoute.KYBER, path, {
45
44
  method: 'GET',
46
45
  headers: {
47
46
  'Content-Type': 'application/json',
48
- 'x-client-id': KYBER_CLIENT_ID,
49
47
  accept: 'application/json'
50
48
  }
51
49
  });
@@ -78,11 +76,10 @@ async function buildTxForKyberSwap(params, chain) {
78
76
  };
79
77
  console.log('routeSummary2', routeSummary);
80
78
  try {
81
- const res = await fetch(`${kyberUrl}/${chain}/api/v1/route/build`, {
79
+ const res = await fetchFromProxyService(ProxyServiceRoute.KYBER, `/${chain}/api/v1/route/build`, {
82
80
  method: 'POST',
83
81
  headers: {
84
82
  'Content-Type': 'application/json',
85
- 'x-client-id': KYBER_CLIENT_ID,
86
83
  accept: 'application/json'
87
84
  },
88
85
  body: JSON.stringify(body)
@@ -4,7 +4,6 @@ import { BaseStepDetail, CommonOptimalSwapPath, CommonStepFeeInfo, OptimalSwapPa
4
4
  import { BalanceService } from '../../balance-service';
5
5
  import { ChainService } from '../../chain-service';
6
6
  import { SwapBaseInterface } from './base-handler';
7
- export declare const simpleSwapApiKey: string;
8
7
  export declare class SimpleSwapHandler implements SwapBaseInterface {
9
8
  private swapBaseHandler;
10
9
  providerSlug: SwapProviderId;
@@ -6,14 +6,13 @@ import { TransactionError } from '@subwallet/extension-base/background/errors/Tr
6
6
  import { ChainType, ExtrinsicType } from '@subwallet/extension-base/background/KoniTypes';
7
7
  import { _getAssetDecimals, _getAssetSymbol, _getContractAddressOfToken, _isChainSubstrateCompatible, _isNativeToken } from '@subwallet/extension-base/services/chain-service/utils';
8
8
  import { BasicTxErrorType, CommonStepType, DynamicSwapType, SwapErrorType, SwapProviderId, SwapStepType } from '@subwallet/extension-base/types';
9
- import { _reformatAddressWithChain, formatNumber } from '@subwallet/extension-base/utils';
9
+ import { ProxyServiceRoute } from '@subwallet/extension-base/types/environment';
10
+ import { _reformatAddressWithChain, fetchFromProxyService, formatNumber } from '@subwallet/extension-base/utils';
10
11
  import { getId } from '@subwallet/extension-base/utils/getId';
11
12
  import BigN, { BigNumber } from 'bignumber.js';
12
13
  import { getERC20TransactionObject, getEVMTransactionObject } from "../../balance-service/transfer/smart-contract.js";
13
14
  import { createSubstrateExtrinsic } from "../../balance-service/transfer/token.js";
14
15
  import { SwapBaseHandler } from "./base-handler.js";
15
- const apiUrl = 'https://api.simpleswap.io/v3';
16
- export const simpleSwapApiKey = process.env.SIMPLE_SWAP_API_KEY || '';
17
16
  const toBNString = (input, decimal) => {
18
17
  const raw = new BigNumber(input);
19
18
  return raw.shiftedBy(decimal).integerValue(BigNumber.ROUND_CEIL).toFixed();
@@ -45,12 +44,11 @@ const buildTxForSimpleSwap = async params => {
45
44
  userRefundAddress: sender,
46
45
  userRefundExtraId: ''
47
46
  };
48
- const response = await fetch(`${apiUrl}/exchanges`, {
47
+ const response = await fetchFromProxyService(ProxyServiceRoute.SIMPLESWAP, '/exchanges', {
49
48
  method: 'POST',
50
49
  headers: {
51
- accept: 'application/json',
52
50
  'Content-Type': 'application/json',
53
- 'x-api-key': simpleSwapApiKey
51
+ accept: 'application/json'
54
52
  },
55
53
  body: JSON.stringify(requestBody)
56
54
  });
@@ -9,16 +9,13 @@ import { createAcrossBridgeExtrinsic } from '@subwallet/extension-base/services/
9
9
  import { getAcrossQuote } from '@subwallet/extension-base/services/balance-service/transfer/xcm/acrossBridge';
10
10
  import { DEFAULT_EXCESS_AMOUNT_WEIGHT, FEE_RATE_MULTIPLIER } from '@subwallet/extension-base/services/swap-service/utils';
11
11
  import { BasicTxErrorType, CommonStepType, DynamicSwapType, FeeOptionKey, SwapFeeType, SwapProviderId, SwapStepType } from '@subwallet/extension-base/types';
12
- import { _reformatAddressWithChain } from '@subwallet/extension-base/utils';
12
+ import { ProxyServiceRoute } from '@subwallet/extension-base/types/environment';
13
+ import { _reformatAddressWithChain, fetchFromProxyService } from '@subwallet/extension-base/utils';
13
14
  import { getId } from '@subwallet/extension-base/utils/getId';
14
15
  import BigNumber from 'bignumber.js';
15
16
  import { _getAssetOriginChain, _getChainNativeTokenSlug, _getContractAddressOfToken, _getEvmChainId, _isNativeToken } from "../../chain-service/utils/index.js";
16
17
  import { calculateGasFeeParams } from "../../fee-service/utils/index.js";
17
18
  import { SwapBaseHandler } from "./base-handler.js";
18
- const API_URL = 'https://trade-api.gateway.uniswap.org/v1';
19
- const headers = {
20
- 'x-api-key': process.env.UNISWAP_API_KEY || ''
21
- };
22
19
  async function fetchCheckApproval(request) {
23
20
  const {
24
21
  address,
@@ -49,10 +46,9 @@ async function fetchCheckApproval(request) {
49
46
  } else {
50
47
  return undefined;
51
48
  }
52
- const response = await fetch(`${API_URL}/check_approval`, {
49
+ const response = await fetchFromProxyService(ProxyServiceRoute.UNISWAP, '/check_approval', {
53
50
  method: 'POST',
54
51
  headers: {
55
- ...headers,
56
52
  'Content-Type': 'application/json'
57
53
  },
58
54
  body: JSON.stringify({
@@ -617,10 +613,9 @@ export class UniswapHandler {
617
613
  if (permitData) {
618
614
  body.permitData = permitData;
619
615
  }
620
- postTransactionResponse = await fetch(`${API_URL}/swap`, {
616
+ postTransactionResponse = await fetchFromProxyService(ProxyServiceRoute.UNISWAP, '/swap', {
621
617
  method: 'POST',
622
618
  headers: {
623
- ...headers,
624
619
  'Content-Type': 'application/json'
625
620
  },
626
621
  body: JSON.stringify(body)
@@ -631,10 +626,9 @@ export class UniswapHandler {
631
626
  const dutchQuote = quote;
632
627
  const submitSwapOrder = async () => {
633
628
  try {
634
- const res = await fetch(`${API_URL}/order`, {
629
+ const res = await fetchFromProxyService(ProxyServiceRoute.UNISWAP, '/order', {
635
630
  method: 'POST',
636
631
  headers: {
637
- ...headers,
638
632
  'Content-Type': 'application/json'
639
633
  },
640
634
  body: JSON.stringify({
@@ -675,10 +669,9 @@ export class UniswapHandler {
675
669
  const swapper = dutchQuote.orderInfo.swapper;
676
670
  return retryGetUniswapTx(async () => {
677
671
  try {
678
- const response = await fetch(`${API_URL}/orders?orderId=${orderId}&swapper=${swapper}`, {
672
+ const response = await fetchFromProxyService(ProxyServiceRoute.UNISWAP, `/orders?orderId=${orderId}&swapper=${swapper}`, {
679
673
  method: 'GET',
680
674
  headers: {
681
- ...headers,
682
675
  'Content-Type': 'application/json'
683
676
  }
684
677
  });
@@ -13,19 +13,6 @@ export declare function getSwapAlternativeAsset(swapPair: SwapPair): string | un
13
13
  export declare function getSwapAltToken(chainAsset: _ChainAsset): string | undefined;
14
14
  export declare function calculateSwapRate(fromAmount: string, toAmount: string, fromAsset: _ChainAsset, toAsset: _ChainAsset): number;
15
15
  export declare function convertSwapRate(rate: string, fromAsset: _ChainAsset, toAsset: _ChainAsset): number;
16
- export declare function getChainflipOptions(isTestnet: boolean): {
17
- network: string;
18
- broker?: undefined;
19
- } | {
20
- network: string;
21
- broker: {
22
- url: string;
23
- };
24
- };
25
- export declare function getChainflipBroker(isTestnet: boolean): {
26
- url: string;
27
- };
28
- export declare function getChainflipSwap(isTestnet: boolean): string;
29
16
  export declare function getBridgeStep(from: string, to: string): DynamicSwapAction;
30
17
  export declare function getSwapStep(from: string, to: string): DynamicSwapAction;
31
18
  export declare function findBridgeTransitDestination(assetRefMap: Record<string, _AssetRef>, fromToken: _ChainAsset, toToken: _ChainAsset): string | undefined;
@@ -4,7 +4,6 @@
4
4
  import { COMMON_ASSETS, COMMON_CHAIN_SLUGS } from '@subwallet/chain-list';
5
5
  import { _AssetRefPath } from '@subwallet/chain-list/types';
6
6
  import { _getAssetDecimals, _getAssetOriginChain, _getOriginChainOfAsset, _parseAssetRefKey } from '@subwallet/extension-base/services/chain-service/utils';
7
- import { CHAINFLIP_BROKER_API } from '@subwallet/extension-base/services/swap-service/handler/chainflip-handler';
8
7
  import { CommonStepType, DynamicSwapType, SwapStepType } from '@subwallet/extension-base/types';
9
8
  import { SwapProviderId } from '@subwallet/extension-base/types/swap';
10
9
  import BigN from 'bignumber.js';
@@ -70,39 +69,52 @@ export function convertSwapRate(rate, fromAsset, toAsset) {
70
69
  const bnRate = BigN(rate);
71
70
  return bnRate.times(10 ** decimalDiff).pow(-1).toNumber();
72
71
  }
73
- export function getChainflipOptions(isTestnet) {
74
- if (isTestnet) {
75
- return {
76
- network: getChainflipNetwork(isTestnet)
77
- };
78
- }
79
- return {
80
- network: getChainflipNetwork(isTestnet),
81
- broker: getChainflipBroker(isTestnet)
82
- };
83
- }
84
- function getChainflipNetwork(isTestnet) {
85
- return isTestnet ? 'perseverance' : 'mainnet';
86
- }
87
- export function getChainflipBroker(isTestnet) {
88
- // noted: currently not use testnet broker
89
- if (isTestnet) {
90
- return {
91
- url: `https://perseverance.chainflip-broker.io/rpc/${CHAINFLIP_BROKER_API}`
92
- };
93
- } else {
94
- return {
95
- url: `https://chainflip-broker.io/rpc/${CHAINFLIP_BROKER_API}`
96
- };
97
- }
98
- }
99
- export function getChainflipSwap(isTestnet) {
100
- if (isTestnet) {
101
- return `https://perseverance.chainflip-broker.io/swap?apikey=${CHAINFLIP_BROKER_API}`;
102
- } else {
103
- return `https://chainflip-broker.io/swap?apikey=${CHAINFLIP_BROKER_API}`;
104
- }
105
- }
72
+
73
+ // export function getChainflipOptions (isTestnet: boolean) {
74
+ // if (isTestnet) {
75
+ // return {
76
+ // network: getChainflipNetwork(isTestnet)
77
+ // };
78
+ // }
79
+
80
+ // return {
81
+ // network: getChainflipNetwork(isTestnet),
82
+ // broker: getChainflipBroker(isTestnet)
83
+ // };
84
+ // }
85
+
86
+ // function getChainflipNetwork (isTestnet: boolean) {
87
+ // return isTestnet ? 'perseverance' : 'mainnet';
88
+ // }
89
+
90
+ // export function getChainflipBroker (isTestnet: boolean) { // noted: currently not use testnet broker
91
+ // if (isTestnet) {
92
+ // return {
93
+ // url: `https://perseverance.chainflip-broker.io/rpc/${CHAINFLIP_BROKER_API}`
94
+ // };
95
+ // } else {
96
+ // return {
97
+ // url: `https://chainflip-broker.io/rpc/${CHAINFLIP_BROKER_API}`
98
+ // };
99
+ // }
100
+ // }
101
+
102
+ // export function getChainflipSwap (isTestnet: boolean) {
103
+ // if (isTestnet) {
104
+ // return `https://perseverance.chainflip-broker.io/swap?apikey=${CHAINFLIP_BROKER_API}`;
105
+ // } else {
106
+ // return `https://chainflip-broker.io/swap?apikey=${CHAINFLIP_BROKER_API}`;
107
+ // }
108
+ // }
109
+
110
+ // export function getAssetsUrl (isTestnet: boolean) {
111
+ // if (isTestnet) {
112
+ // return 'https://perseverance.chainflip-broker.io/assets';
113
+ // } else {
114
+ // return 'https://chainflip-broker.io/assets';
115
+ // }
116
+ // }
117
+
106
118
  export function getBridgeStep(from, to) {
107
119
  return {
108
120
  action: DynamicSwapType.BRIDGE,
@@ -0,0 +1,9 @@
1
+ export declare enum ProxyServiceRoute {
2
+ BITTENSOR = "/bittensor",
3
+ CHAINFLIP = "/chainflip",
4
+ KYBER = "/kyber",
5
+ SIMPLESWAP = "/simpleswap",
6
+ UNISWAP = "/uniswap",
7
+ CARDANO = "/cardano",
8
+ PARASPELL = "/paraspell"
9
+ }
@@ -0,0 +1,13 @@
1
+ // Copyright 2019-2022 @subwallet/extension-base authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export let ProxyServiceRoute;
5
+ (function (ProxyServiceRoute) {
6
+ ProxyServiceRoute["BITTENSOR"] = "/bittensor";
7
+ ProxyServiceRoute["CHAINFLIP"] = "/chainflip";
8
+ ProxyServiceRoute["KYBER"] = "/kyber";
9
+ ProxyServiceRoute["SIMPLESWAP"] = "/simpleswap";
10
+ ProxyServiceRoute["UNISWAP"] = "/uniswap";
11
+ ProxyServiceRoute["CARDANO"] = "/cardano";
12
+ ProxyServiceRoute["PARASPELL"] = "/paraspell";
13
+ })(ProxyServiceRoute || (ProxyServiceRoute = {}));
@@ -1,5 +1,6 @@
1
1
  import Bowser from 'bowser';
2
2
  import { EnvironmentSupport, RuntimeEnvironmentInfo, TargetEnvironment } from '../background/KoniTypes';
3
+ import { ProxyServiceRoute } from '../types/environment';
3
4
  export declare const RuntimeInfo: RuntimeEnvironmentInfo;
4
5
  export declare const BowserParser: Bowser.Parser.Parser;
5
6
  export declare const isBrave: boolean;
@@ -16,3 +17,4 @@ export declare const targetIsExtension: boolean;
16
17
  export declare const targetIsWeb: boolean;
17
18
  export declare const targetIsMobile: boolean;
18
19
  export declare const MODULE_SUPPORT: EnvironmentSupport;
20
+ export declare function fetchFromProxyService(service: ProxyServiceRoute, path: string, options: RequestInit, isTestnet?: boolean): Promise<Response>;
@@ -5,6 +5,7 @@ var _navigator3;
5
5
  import { isSupportWindow } from '@subwallet/extension-base/utils/mv3';
6
6
  import Bowser from 'bowser';
7
7
  import { RuntimeEnvironment } from "../background/KoniTypes.js";
8
+ import { SW_EXTERNAL_SERVICES_API } from "../constants/index.js";
8
9
  function detectRuntimeEnvironment() {
9
10
  if (isSupportWindow && typeof document !== 'undefined') {
10
11
  var _navigator, _window$location, _window$location2;
@@ -84,4 +85,29 @@ export const targetIsWeb = TARGET_ENV === 'webapp';
84
85
  export const targetIsMobile = TARGET_ENV === 'mobile';
85
86
  export const MODULE_SUPPORT = {
86
87
  MANTA_ZK: false
87
- };
88
+ };
89
+ var HEADERS;
90
+ (function (HEADERS) {
91
+ HEADERS["PLATFORM"] = "Platform";
92
+ })(HEADERS || (HEADERS = {}));
93
+ function formatExternalServiceApi(url, isTestnet) {
94
+ if (isTestnet === true) {
95
+ return `${url}/testnet`;
96
+ }
97
+ if (isTestnet === false) {
98
+ return `${url}/mainnet`;
99
+ }
100
+ return url;
101
+ }
102
+ export async function fetchFromProxyService(service, path, options, isTestnet) {
103
+ const baseUrl = formatExternalServiceApi(`${SW_EXTERNAL_SERVICES_API}${service}`, isTestnet);
104
+ const url = `${baseUrl}${path}`;
105
+ const headers = {
106
+ [HEADERS.PLATFORM]: TARGET_ENV,
107
+ ...(options.headers || {})
108
+ };
109
+ return fetch(url, {
110
+ ...options,
111
+ headers
112
+ });
113
+ }