Implement rate limiting for address conversion API calls and enhanced UI
This commit is contained in:
parent
50d403f4b8
commit
bfbe6b29b5
2218
index.html
2218
index.html
File diff suppressed because it is too large
Load Diff
@ -89,19 +89,32 @@
|
||||
});
|
||||
};
|
||||
|
||||
// Rate limiting for API calls
|
||||
let requestQueue = [];
|
||||
let isProcessingQueue = false;
|
||||
const REQUEST_DELAY = 300;
|
||||
let conversionEnabled = true;
|
||||
|
||||
/**
|
||||
* Convert raw address to b64 format
|
||||
* @param {string} rawAddr - The raw address to convert
|
||||
* @returns {Promise} Promise object that resolves with user-friendly address
|
||||
* Process request queue with rate limiting
|
||||
*/
|
||||
tonBlockchainAPI.convertTob64 = function (rawAddr) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!rawAddr || !rawAddr.includes(":")) {
|
||||
resolve(rawAddr);
|
||||
function processRequestQueue() {
|
||||
if (isProcessingQueue || requestQueue.length === 0) return;
|
||||
|
||||
isProcessingQueue = true;
|
||||
|
||||
const processNext = () => {
|
||||
if (requestQueue.length === 0) {
|
||||
isProcessingQueue = false;
|
||||
return;
|
||||
}
|
||||
|
||||
const { rawAddr, resolve } = requestQueue.shift();
|
||||
|
||||
// Check cache first
|
||||
if (addrCache.has(rawAddr)) {
|
||||
resolve(addrCache.get(rawAddr));
|
||||
setTimeout(processNext, 50);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -114,25 +127,98 @@
|
||||
}
|
||||
)
|
||||
.then((response) => {
|
||||
if (!response.ok)
|
||||
if (!response.ok) {
|
||||
if (response.status === 429) {
|
||||
if (rawAddr.includes(":retry:")) {
|
||||
console.warn(
|
||||
"Rate limit exceeded, using original address:",
|
||||
rawAddr.replace(":retry:", "")
|
||||
);
|
||||
const originalAddr = rawAddr.replace(":retry:", "");
|
||||
resolve(originalAddr);
|
||||
setTimeout(processNext, REQUEST_DELAY);
|
||||
return;
|
||||
}
|
||||
|
||||
requestQueue.push({ rawAddr: rawAddr + ":retry:", resolve });
|
||||
setTimeout(processNext, 2000);
|
||||
return;
|
||||
}
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then((data) => {
|
||||
const friendly =
|
||||
data?.result?.bounceable?.b64url ||
|
||||
data?.result?.non_bounceable?.b64url ||
|
||||
rawAddr;
|
||||
addrCache.set(rawAddr, friendly);
|
||||
resolve(friendly);
|
||||
if (data) {
|
||||
const friendly =
|
||||
data?.result?.non_bounceable?.b64url ||
|
||||
rawAddr.replace(":retry:", "");
|
||||
const cleanAddr = rawAddr.replace(":retry:", "");
|
||||
addrCache.set(cleanAddr, friendly);
|
||||
resolve(friendly);
|
||||
} else {
|
||||
resolve(rawAddr.replace(":retry:", ""));
|
||||
}
|
||||
setTimeout(processNext, REQUEST_DELAY);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.warn("Address conversion failed:", error);
|
||||
resolve(rawAddr);
|
||||
resolve(rawAddr.replace(":retry:", "")); // Fallback to original address
|
||||
setTimeout(processNext, REQUEST_DELAY);
|
||||
});
|
||||
};
|
||||
|
||||
processNext();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert raw address to b64 format with rate limiting
|
||||
* @param {string} rawAddr - The raw address to convert
|
||||
* @returns {Promise} Promise object that resolves with user-friendly address
|
||||
*/
|
||||
tonBlockchainAPI.convertTob64 = function (rawAddr) {
|
||||
return new Promise((resolve) => {
|
||||
// if it doesn't look like a raw address, return as-is
|
||||
if (!rawAddr || typeof rawAddr !== "string" || rawAddr === "Unknown") {
|
||||
resolve(rawAddr);
|
||||
return;
|
||||
}
|
||||
|
||||
// If conversion is disabled, return original address
|
||||
if (!conversionEnabled) {
|
||||
resolve(rawAddr);
|
||||
return;
|
||||
}
|
||||
|
||||
const isRawAddress =
|
||||
rawAddr.includes(":") && rawAddr.match(/^-?\d+:[a-fA-F0-9]{64}$/);
|
||||
|
||||
if (!isRawAddress) {
|
||||
// Already user-friendly or not a TON address
|
||||
resolve(rawAddr);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check cache first
|
||||
if (addrCache.has(rawAddr)) {
|
||||
resolve(addrCache.get(rawAddr));
|
||||
return;
|
||||
}
|
||||
|
||||
// Add to queue for conversion
|
||||
requestQueue.push({ rawAddr, resolve });
|
||||
processRequestQueue();
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Enable or disable address conversion
|
||||
* @param {boolean} enabled - Whether to enable address conversion
|
||||
*/
|
||||
tonBlockchainAPI.setConversionEnabled = function (enabled) {
|
||||
conversionEnabled = enabled;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetch transaction history for an address
|
||||
* @param {string} address - The TON address to check
|
||||
|
||||
Loading…
Reference in New Issue
Block a user