@ledgerhq/coin-hedera
Version:
Ledger Hedera Coin integration
72 lines • 2.96 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.thirdwebClient = void 0;
const live_env_1 = require("@ledgerhq/live-env");
const live_network_1 = __importDefault(require("@ledgerhq/live-network"));
const viem_1 = require("viem");
const constants_1 = require("../constants");
const utils_1 = require("../logic/utils");
const API_URL = (0, live_env_1.getEnv)("API_HEDERA_THIRDWEB_URL");
async function fetchERC20Transactions(contractAddress, options) {
const transactions = [];
const params = new URLSearchParams(options);
let page = 1;
let hasMorePages = true;
while (hasMorePages) {
params.set("page", page.toString());
const response = await (0, live_network_1.default)({
method: "GET",
url: `${API_URL}/v1/contracts/${constants_1.HEDERA_MAINNET_CHAIN_ID}/${contractAddress}/events?${params.toString()}`,
});
const newTransactions = response.data.result.events;
transactions.push(...newTransactions);
// stop if we received fewer items than the limit or no items
if (newTransactions.length < Number(options.limit) || newTransactions.length === 0) {
hasMorePages = false;
}
else {
page++;
}
}
return transactions;
}
async function getERC20TransactionsForAccount({ address, contractAddresses, transactionFetcher = fetchERC20Transactions, since, }) {
const allTransactions = [];
const evmAddress = await (0, utils_1.toEVMAddress)(address);
if (contractAddresses.length === 0) {
return allTransactions;
}
if (!evmAddress) {
return allTransactions;
}
const baseParams = {
limit: "1000",
filterTopic0: constants_1.ERC20_TRANSFER_EVENT_TOPIC,
...(since && { filterBlockTimestampGte: since }),
};
for (const contractAddress of contractAddresses) {
const outTransactionOptions = {
...baseParams,
filterTopic1: (0, viem_1.pad)(evmAddress).toString(),
};
const inTransactionOptions = {
...baseParams,
filterTopic2: (0, viem_1.pad)(evmAddress).toString(),
};
const outgoingTxs = await transactionFetcher(contractAddress, outTransactionOptions);
const incomingTxs = await transactionFetcher(contractAddress, inTransactionOptions);
allTransactions.push(...outgoingTxs, ...incomingTxs);
}
return allTransactions;
}
// Thirdweb API is used in addition to mirror node because:
// - mirror node has a 1-week range limitation for ERC20 events queries
// - mirror node has rate limits that we could exceed with ERC20 integration
exports.thirdwebClient = {
fetchERC20Transactions,
getERC20TransactionsForAccount,
};
//# sourceMappingURL=thirdweb.js.map