7e76cb79d1
There seems to be a weird race condition on the `/account-assets` endpoint when handling two different requests for the same chainId. Reproduction: https://codesandbox.io/s/ethereum-api-race-condition-repro-f24x4t?file=/src/index.js
55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
import axios, { AxiosInstance } from "axios";
|
|
import { AssetData, GasPrices, ParsedTx } from "./types";
|
|
|
|
const ethereumApi: AxiosInstance = axios.create({
|
|
baseURL: "https://ethereum-api.xyz",
|
|
timeout: 30000, // 30 secs
|
|
headers: {
|
|
Accept: "application/json",
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
|
|
export async function apiGetAccountAssets(address: string, chainId: string): Promise<AssetData[]> {
|
|
const ethChainId = chainId.split(":")[1];
|
|
const response = await ethereumApi.get(
|
|
`/account-assets?address=${address}&chainId=${ethChainId}`,
|
|
);
|
|
const { result } = response.data;
|
|
return result;
|
|
}
|
|
|
|
export async function apiGetAccountBalance(address: string, chainId: string): Promise<AssetData> {
|
|
const ethChainId = chainId.split(":")[1];
|
|
const response = await ethereumApi.get(
|
|
`/account-balance?address=${address}&chainId=${ethChainId}`,
|
|
);
|
|
const { result } = response.data;
|
|
return result;
|
|
}
|
|
|
|
export async function apiGetAccountTransactions(
|
|
address: string,
|
|
chainId: string,
|
|
): Promise<ParsedTx[]> {
|
|
const ethChainId = chainId.split(":")[1];
|
|
const response = await ethereumApi.get(
|
|
`/account-transactions?address=${address}&chainId=${ethChainId}`,
|
|
);
|
|
const { result } = response.data;
|
|
return result;
|
|
}
|
|
|
|
export const apiGetAccountNonce = async (address: string, chainId: string): Promise<number> => {
|
|
const ethChainId = chainId.split(":")[1];
|
|
const response = await ethereumApi.get(`/account-nonce?address=${address}&chainId=${ethChainId}`);
|
|
const { result } = response.data;
|
|
return result;
|
|
};
|
|
|
|
export const apiGetGasPrices = async (): Promise<GasPrices> => {
|
|
const response = await ethereumApi.get(`/gas-prices`);
|
|
const { result } = response.data;
|
|
return result;
|
|
};
|