@thorwallet/xchain-ethereum
Version:
Ethereum client for XChainJS
130 lines • 5.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTokenTransactionHistory = exports.getETHTransactionHistory = exports.getTokenBalance = exports.getGasOracle = void 0;
const tslib_1 = require("tslib");
const axios_1 = tslib_1.__importDefault(require("axios"));
const utils_1 = require("./utils");
const lib_1 = require("@thorwallet/xchain-util/lib");
const getApiKeyQueryParameter = (apiKey) => (!!apiKey ? `&apiKey=${apiKey}` : '');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getAxiosWithRateLimitHandling = (url) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const response = yield axios_1.default.get(url);
if (JSON.stringify(response.data).includes('Max rate limit reached')) {
console.log('reached rate limit for', url, 'waiting 2s then trying again...');
yield new Promise((resolve) => setTimeout(resolve, 2000));
return getAxiosWithRateLimitHandling(url);
}
return response;
});
/**
* SafeGasPrice, ProposeGasPrice And FastGasPrice returned in string-Gwei
*
* @see https://etherscan.io/apis#gastracker
*
* @param {string} baseUrl The etherscan node url.
* @param {string} apiKey The etherscan API key. (optional)
* @returns {GasOracleResponse} LastBlock, SafeGasPrice, ProposeGasPrice, FastGasPrice
*/
const getGasOracle = (baseUrl, apiKey) => {
const url = baseUrl + '/api?module=gastracker&action=gasoracle';
return getAxiosWithRateLimitHandling(url + getApiKeyQueryParameter(apiKey)).then((response) => response.data.result);
};
exports.getGasOracle = getGasOracle;
/**
* Get token balance
*
* @see https://etherscan.io/apis#tokens
*
* @param {string} baseUrl The etherscan node url.
* @param {string} address The address.
* @param {string} assetAddress The token contract address.
* @param {string} apiKey The etherscan API key. (optional)
* @returns {BigNumberish} The token balance
*/
const getTokenBalance = ({ baseUrl, address, assetAddress, apiKey, }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const url = baseUrl + `/api?module=account&action=tokenbalance&contractaddress=${assetAddress}&address=${address}`;
const response = yield getAxiosWithRateLimitHandling(url + getApiKeyQueryParameter(apiKey));
return response.data.result;
});
exports.getTokenBalance = getTokenBalance;
/**
* Get ETH transaction history
*
* @see https://etherscan.io/apis#accounts
*
* @param {string} baseUrl The etherscan node url.
* @param {string} address The address.
* @param {TransactionHistoryParam} params The search options.
* @param {string} apiKey The etherscan API key. (optional)
* @returns {Array<ETHTransactionInfo>} The ETH transaction history
*/
const getETHTransactionHistory = ({ baseUrl, address, page, offset, startblock, endblock, apiKey, }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
let url = baseUrl + `/api?module=account&action=txlist&sort=desc` + getApiKeyQueryParameter(apiKey);
if (address)
url += `&address=${address}`;
if (offset)
url += `&offset=${offset}`;
if (page)
url += `&page=${page}`;
if (startblock)
url += `&startblock=${startblock}`;
if (endblock)
url += `&endblock=${endblock}`;
try {
const result = yield getAxiosWithRateLimitHandling(url).then((response) => response.data.result);
if (JSON.stringify(result).includes('Invalid API Key')) {
return Promise.reject(new Error('Invalid API Key'));
}
if (typeof result !== typeof []) {
throw new Error(JSON.stringify(result));
}
return result.filter((tx) => !lib_1.bnOrZero(tx.value).isZero()).map(utils_1.getTxFromEthTransaction);
}
catch (error) {
return Promise.reject(error);
}
});
exports.getETHTransactionHistory = getETHTransactionHistory;
/**
* Get token transaction history
*
* @see https://etherscan.io/apis#accounts
*
* @param {string} baseUrl The etherscan node url.
* @param {string} address The address.
* @param {TransactionHistoryParam} params The search options.
* @param {string} apiKey The etherscan API key. (optional)
* @returns {Array<Tx>} The token transaction history
*/
const getTokenTransactionHistory = ({ baseUrl, address, assetAddress, page, offset, startblock, endblock, apiKey, }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
let url = baseUrl + `/api?module=account&action=tokentx&sort=desc` + getApiKeyQueryParameter(apiKey);
if (address)
url += `&address=${address}`;
if (assetAddress)
url += `&contractaddress=${assetAddress}`;
if (offset)
url += `&offset=${offset}`;
if (page)
url += `&page=${page}`;
if (startblock)
url += `&startblock=${startblock}`;
if (endblock)
url += `&endblock=${endblock}`;
try {
const result = yield getAxiosWithRateLimitHandling(url).then((response) => response.data.result);
if (JSON.stringify(result).includes('Invalid API Key')) {
return Promise.reject(new Error('Invalid API Key'));
}
return result
.filter((tx) => !lib_1.bnOrZero(tx.value).isZero())
.reduce((acc, cur) => {
const tx = utils_1.getTxFromTokenTransaction(cur);
return tx ? [...acc, tx] : acc;
}, []);
}
catch (error) {
return Promise.reject(error);
}
});
exports.getTokenTransactionHistory = getTokenTransactionHistory;
//# sourceMappingURL=etherscan-api.js.map