Skip to content

Commit

Permalink
[Feat] chain fee custom header (#372)
Browse files Browse the repository at this point in the history
* chore: remove token in CI and update readme

* feat: send custom header in every chain-fee request

* refactor: HttpClient

* refactor: get and post methods -> request

* refactor: adapt code to the new method

* fix: add default cache explicit

---------

Co-authored-by: Julián Ariel Martínez <[email protected]>
  • Loading branch information
sreblock and Julián Ariel Martínez authored Mar 14, 2023
1 parent 57859cd commit 910a90e
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,11 @@ export class ExchangeRatesController extends BaseController<ExchangeRatesControl

const query = `${BaseApiEndpoint}token_price/${this.networkNativeCurrency.coingeckoPlatformId}`;

return httpClient.get(query, {
contract_addresses: tokenContracts,
vs_currencies: this._preferencesController.nativeCurrency,
return httpClient.request(query, {
params: {
contract_addresses: tokenContracts,
vs_currencies: this._preferencesController.nativeCurrency,
},
});
};

Expand Down
8 changes: 6 additions & 2 deletions packages/background/src/controllers/GasPricesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { retryHandling } from '../utils/retryHandling';

const CHAIN_FEE_DATA_SERVICE_URL = 'https://chain-fee.blockwallet.io/v1';
const BLOCKS_TO_WAIT_BEFORE_CHECKING_FOR_CHAIN_SUPPORT = 100;
const CHAIN_FEE_CUSTOM_HEADER = { wallet: 'BlockWallet' };
const API_CALLS_DELAY = 100 * MILISECOND;
const API_CALLS_RETRIES = 5;

Expand Down Expand Up @@ -456,10 +457,13 @@ export class GasPricesController extends BaseController<GasPricesControllerState
// If the chain has support request the service
const feeDataResponse = await retryHandling(
() =>
httpClient.get<FeeDataResponse>(
httpClient.request<FeeDataResponse>(
`${CHAIN_FEE_DATA_SERVICE_URL}/fee_data`,
{
c: chainId,
params: {
c: chainId,
},
headers: CHAIN_FEE_CUSTOM_HEADER,
}
),
API_CALLS_DELAY,
Expand Down
49 changes: 25 additions & 24 deletions packages/background/src/controllers/SwapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export default class SwapController extends BaseController<
if (exchangeType === ExchangeType.SWAP_1INCH) {
// 1Inch router contract address
const res = await retryHandling<OneInchSpenderRes>(() =>
httpClient.get<OneInchSpenderRes>(
httpClient.request<OneInchSpenderRes>(
`${ONEINCH_SWAPS_ENDPOINT}${chainId}/approve/spender`
)
);
Expand All @@ -238,22 +238,21 @@ export default class SwapController extends BaseController<
}: OneInchSwapQuoteParams): Promise<SwapQuote> => {
try {
const res = await retryHandling<OneInchSwapQuoteResponse>(() =>
httpClient.get<
OneInchSwapQuoteResponse,
OneInchSwapQuoteParams
>(
httpClient.request<OneInchSwapQuoteResponse>(
`${ONEINCH_SWAPS_ENDPOINT}${this._networkController.network.chainId}/quote`,
{
fromTokenAddress:
fromTokenAddress === '0x0'
? ONEINCH_NATIVE_ADDRESS
: fromTokenAddress,
toTokenAddress:
toTokenAddress === '0x0'
? ONEINCH_NATIVE_ADDRESS
: toTokenAddress,
amount,
fee: BASE_SWAP_FEE,
params: {
fromTokenAddress:
fromTokenAddress === '0x0'
? ONEINCH_NATIVE_ADDRESS
: fromTokenAddress,
toTokenAddress:
toTokenAddress === '0x0'
? ONEINCH_NATIVE_ADDRESS
: toTokenAddress,
amount,
fee: BASE_SWAP_FEE,
},
}
)
);
Expand Down Expand Up @@ -288,15 +287,17 @@ export default class SwapController extends BaseController<

try {
const res = await retryHandling<OneInchSwapRequestResponse>(() =>
httpClient.get<
OneInchSwapRequestResponse,
OneInchSwapRequestParams
>(`${ONEINCH_SWAPS_ENDPOINT}${chainId}/swap`, {
...swapParams,
fee: BASE_SWAP_FEE,
referrerAddress: REFERRER_ADDRESS,
allowPartialFill: false,
})
httpClient.request<OneInchSwapRequestResponse>(
`${ONEINCH_SWAPS_ENDPOINT}${chainId}/swap`,
{
params: {
...swapParams,
fee: BASE_SWAP_FEE,
referrerAddress: REFERRER_ADDRESS,
allowPartialFill: false,
},
}
)
);

const methodSignature =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { MILISECOND } from '../../utils/constants/time';
export const BLOCKS_TO_WAIT_BEFORE_CHECHKING_FOR_CHAIN_SUPPORT = 100;
const OFF_CHAIN_BLOCK_FETCH_SERVICE_URL = 'https://chain-fee.blockwallet.io/v1';
const OFF_CHAIN_BLOCK_FETCH_SERVICE_MAX_REPEATED_BLOCKS_TOLERANCE = 100;
const OFF_CHAIN_BLOCK_CUSTOM_HEADER = { wallet: 'BlockWallet' };
const API_CALLS_DELAY = 100 * MILISECOND;
const API_CALLS_RETRIES = 5;

Expand Down Expand Up @@ -370,10 +371,13 @@ export class OffChainBlockFetchService {
try {
const blockDataResponse = await retryHandling(
() =>
httpClient.get<{
httpClient.request<{
bn: string;
}>(`${OFF_CHAIN_BLOCK_FETCH_SERVICE_URL}/bn`, {
c: chainId,
params: {
c: chainId,
},
headers: OFF_CHAIN_BLOCK_CUSTOM_HEADER,
}),
API_CALLS_DELAY,
API_CALLS_RETRIES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,19 +236,18 @@ export class ContractSignatureParser {
// this retry is for network/http errors
const result = await retryHandling(
() =>
httpClient.get<{
httpClient.request<{
status: string;
result: string;
message?: string;
}>(
`${etherscanAPI}/api`,
{
}>(`${etherscanAPI}/api`, {
params: {
module: 'contract',
action: 'getabi',
address,
},
30000
),
timeout: 30000,
}),
API_CALLS_DELAY
);

Expand Down Expand Up @@ -342,10 +341,12 @@ export class ContractSignatureParser {
try {
fourByteResponse = await retryHandling(
() =>
httpClient.get<FourByteResponse>(
httpClient.request<FourByteResponse>(
`https://www.4byte.directory/api/v1/signatures/`,
{
hex_signature: bytes,
params: {
hex_signature: bytes,
},
}
),
API_CALLS_DELAY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2327,7 +2327,7 @@ export class TransactionController extends BaseController<
): Promise<[TransactionMeta, boolean]> {
const baseUrl = 'https://protect.flashbots.net/tx/';

const response = await httpClient.get<FlashbotsStatusResponse>(
const response = await httpClient.request<FlashbotsStatusResponse>(
baseUrl + meta.transactionParams.hash
);

Expand Down
54 changes: 28 additions & 26 deletions packages/background/src/utils/bridgeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ const LiFiBridge: IBridge = {
getSupportedTokensForChain: async function (
chainId: number
): Promise<IToken[]> {
const response = await httpClient.get<GetLifiTokensResponse>(
const response = await httpClient.request<GetLifiTokensResponse>(
`${LIFI_BRIDGE_ENDPOINT}/tokens`
);
const chainTokens = response.tokens[chainId] || [];
return chainTokens.map(lifiTokenToIToken);
},
getSupportedChains: async function (): Promise<IChain[]> {
const response = await httpClient.get<GetLifiChainsResponse>(
const response = await httpClient.request<GetLifiChainsResponse>(
`${LIFI_BRIDGE_ENDPOINT}/chains`
);
const chains = response.chains || [];
Expand All @@ -195,14 +195,16 @@ const LiFiBridge: IBridge = {
getRoutes: async function (
request: getBridgeRoutesRequest
): Promise<IBridgeRoute[]> {
const response = await httpClient.get<GetLifiConnectionsResponse>(
const response = await httpClient.request<GetLifiConnectionsResponse>(
`${LIFI_BRIDGE_ENDPOINT}/connections`,
{
allowExchanges: '[]',
fromChain: request.fromChainId,
toChain: request.toChainId,
fromToken: request.fromTokenAddress,
toToken: request.toTokenAddress,
params: {
allowExchanges: '[]',
fromChain: request.fromChainId,
toChain: request.toChainId,
fromToken: request.fromTokenAddress,
toToken: request.toTokenAddress,
},
}
);
const result = response.connections;
Expand All @@ -215,19 +217,21 @@ const LiFiBridge: IBridge = {
},
getQuote: async function (r: getBridgeQuoteRequest): Promise<IBridgeQuote> {
try {
const response = await httpClient.get<
const response = await httpClient.request<
GetLiFiQuoteResponse | LiFiErrorResponse
>(`${LIFI_BRIDGE_ENDPOINT}/quote`, {
fromToken: r.fromTokenAddress,
toToken: r.toTokenAddress,
fromChain: r.fromChainId,
toChain: r.toChainId,
fromAmount: r.fromAmount,
fromAddress: r.fromAddress,
referrer: r.referrer,
integrator: 'blockwallet.io',
slippage: r.slippage,
fee: BASE_BRIDGE_FEE,
params: {
fromToken: r.fromTokenAddress,
toToken: r.toTokenAddress,
fromChain: r.fromChainId,
toChain: r.toChainId,
fromAmount: r.fromAmount,
fromAddress: r.fromAddress,
referrer: r.referrer,
integrator: 'blockwallet.io',
slippage: r.slippage,
fee: BASE_BRIDGE_FEE,
},
});
const responseData = response as GetLiFiQuoteResponse;
return {
Expand Down Expand Up @@ -263,19 +267,17 @@ const LiFiBridge: IBridge = {
}
},
getStatus: async function (r: getStatusRequest): Promise<IBridgeStatus> {
const response = await httpClient.get<
const response = await httpClient.request<
GetLiFiStatusResponse | LiFiErrorResponse
>(
`${LIFI_BRIDGE_ENDPOINT}/status`,
{
>(`${LIFI_BRIDGE_ENDPOINT}/status`, {
params: {
bridge: r.tool,
fromChain: r.fromChainId,
toChain: r.toChainId,
txHash: r.sendTxHash,
},
undefined,
'no-cache'
);
cache: 'no-cache',
});
const responseData = response as GetLiFiStatusResponse;

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/background/src/utils/contractsInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function fetchContractDetails(
try {
const responseText = await retryHandling<string>(
() =>
http.get(
http.request(
`${CONTRACTS_URL}/${chainId}/${toChecksumAddress(
address
)}.json`
Expand Down
Loading

0 comments on commit 910a90e

Please sign in to comment.