Skip to content

Commit

Permalink
Fix/covalent api url (#2683)
Browse files Browse the repository at this point in the history
* fix: covalent api url

* fix: remove key

* fix: lint
  • Loading branch information
sidmorizon committed Mar 9, 2023
1 parent b13e87a commit fe538f6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
38 changes: 27 additions & 11 deletions packages/engine/src/endpoint.ts
@@ -1,18 +1,34 @@
const DEFAULT_ONLINE_ENDPOINT = 'https://fiat.onekeycn.com';
const TEST_ENDPOINT = 'https://fiat.onekeytest.com';

const DEFAULT_SOCKET_ENDPOINT = 'wss://fiat.onekeycn.com';
const TEST_SOCKET_ENDPOINT = 'wss://fiat.onekeytest.com';
const endpointsMap: Record<
'fiat' | 'wss' | 'covalent',
{ prd: string; test: string }
> = {
fiat: {
prd: 'https://fiat.onekeycn.com',
test: 'https://fiat.onekeytest.com',
},
wss: {
prd: 'wss://fiat.onekeycn.com',
test: 'wss://fiat.onekeytest.com',
},
covalent: {
prd: 'https://node.onekey.so/covalent/client1-HghTg3a33',
test: 'https://node.onekeytest.com/covalent/client1-HghTg3a33',
},
};

let endpoint = DEFAULT_ONLINE_ENDPOINT;
let websocketEndpoint = DEFAULT_SOCKET_ENDPOINT;
let endpoint = '';
let websocketEndpoint = '';
let covalentApiEndpoint = '';

export const switchTestEndpoint = (isTestEnable?: boolean) => {
endpoint = isTestEnable ? TEST_ENDPOINT : DEFAULT_ONLINE_ENDPOINT;
websocketEndpoint = isTestEnable
? TEST_SOCKET_ENDPOINT
: DEFAULT_SOCKET_ENDPOINT;
const key = isTestEnable ? 'test' : 'prd';
endpoint = endpointsMap.fiat[key];
websocketEndpoint = endpointsMap.wss[key];
covalentApiEndpoint = endpointsMap.covalent[key];
};

switchTestEndpoint(false);

export const getFiatEndpoint = () => endpoint;
export const getSocketEndpoint = () => websocketEndpoint;
export const getCovalentApiEndpoint = () => covalentApiEndpoint;
20 changes: 10 additions & 10 deletions packages/engine/src/managers/covalent.ts
Expand Up @@ -2,7 +2,6 @@ import axios from 'axios';
import BigNumber from 'bignumber.js';
import camelcase from 'camelcase-keys';

import { COVALENT_API_KEY } from '@onekeyhq/shared/src/config/appConfig';
import debugCodes from '@onekeyhq/shared/src/debug/debugCodes';
import { HISTORY_CONSTS } from '@onekeyhq/shared/src/engine/engineConsts';

Expand Down Expand Up @@ -32,6 +31,7 @@ import type { Token } from '../types/token';
import type { IEncodedTxEvm } from '../vaults/impl/evm/Vault';
import type { IDecodedTx, IDecodedTxAction } from '../vaults/types';
import type { VaultBase } from '../vaults/VaultBase';
import { getCovalentApiEndpoint } from '../endpoint';

const NOBODY = '0x0000000000000000000000000000000000000000';

Expand Down Expand Up @@ -149,12 +149,12 @@ function getNftDetail(
contractAddress: string,
tokenId: string,
): Promise<NftMetadata | null> {
const request = `https://api.covalenthq.com/v1/${chainId}/tokens/${contractAddress}/nft_metadata/${tokenId}/`;
const request = `${getCovalentApiEndpoint()}/v1/${chainId}/tokens/${contractAddress}/nft_metadata/${tokenId}/`;

return axios
.get<NftDetail>(request, {
params: {
'key': COVALENT_API_KEY,
// 'key': COVALENT_API_KEY,
},
})
.then((response) => {
Expand Down Expand Up @@ -400,13 +400,13 @@ function getTxHistories(
pageNumber = 0,
pageSize = 50,
): Promise<HistoryDetailList | null> {
const request = `https://api.covalenthq.com/v1/${chainId}/address/${address}/transactions_v2/`;
const request = `${getCovalentApiEndpoint()}/v1/${chainId}/address/${address}/transactions_v2/`;
return axios
.get<HistoryDetailList>(request, {
params: {
'page-number': pageNumber,
'page-size': pageSize,
'key': COVALENT_API_KEY,
// 'key': COVALENT_API_KEY,
},
})
.then(async (response) => {
Expand Down Expand Up @@ -867,17 +867,17 @@ async function fetchCovalentHistoryRaw({
pageSize = pageSize ?? HISTORY_CONSTS.FETCH_ON_CHAIN_LIMIT;

// https://www.covalenthq.com/docs/api/#/0/Get%20ERC20%20token%20transfers%20for%20address/USD/1
const tokenRequestUrl = `https://api.covalenthq.com/v1/${chainId}/address/${address}/transfers_v2/`;
const tokenRequestUrl = `${getCovalentApiEndpoint()}/v1/${chainId}/address/${address}/transfers_v2/`;
// https://www.covalenthq.com/docs/api/#/0/Get%20transactions%20for%20address/USD/1
const url = `https://api.covalenthq.com/v1/${chainId}/address/${address}/transactions_v2/`;
const url = `${getCovalentApiEndpoint()}/v1/${chainId}/address/${address}/transactions_v2/`;

const response = await axios.get<ICovalentHistoryList>(
contract ? tokenRequestUrl : url,
{
params: {
'page-number': pageNumber,
'page-size': pageSize,
'key': COVALENT_API_KEY,
// 'key': COVALENT_API_KEY,
'contract-address': contract,
},
},
Expand All @@ -892,13 +892,13 @@ function getErc20TransferHistories(
pageNumber: number,
pageSize: number,
): Promise<HistoryDetailList | null> {
const request = `https://api.covalenthq.com/v1/${chainId}/address/${address}/transfers_v2/`;
const request = `${getCovalentApiEndpoint()}/v1/${chainId}/address/${address}/transfers_v2/`;
return axios
.get<HistoryDetailList>(request, {
params: {
'page-number': pageNumber,
'page-size': pageSize,
'key': COVALENT_API_KEY,
// 'key': COVALENT_API_KEY,
'contract-address': contract,
},
})
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/views/Me/DevSetting/index.tsx
Expand Up @@ -18,6 +18,7 @@ import {
import { shortenAddress } from '@onekeyhq/components/src/utils';
import { copyToClipboard } from '@onekeyhq/components/src/utils/ClipboardUtils';
import {
getCovalentApiEndpoint,
getFiatEndpoint,
getSocketEndpoint,
} from '@onekeyhq/engine/src/endpoint';
Expand Down Expand Up @@ -215,7 +216,7 @@ export const DevSettingSection = () => {
</Container.Item>
<Container.Item
title={intl.formatMessage({ id: 'action__test_onekey_service' })}
subDescribe={`范围: \n[token、价格、余额、推送] \n ${fiatEndpoint}\n ${getSocketEndpoint()}`}
subDescribe={`范围: \n[token、价格、余额、推送、历史记录] \n ${fiatEndpoint}\n ${getSocketEndpoint()} \n ${getCovalentApiEndpoint()}`}
titleColor="text-critical"
>
<Switch
Expand Down

0 comments on commit fe538f6

Please sign in to comment.