Implement rate limiting for address conversion API calls and enhanced UI
This commit is contained in:
parent
50d403f4b8
commit
bfbe6b29b5
2180
index.html
2180
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
|
* Process request queue 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) {
|
function processRequestQueue() {
|
||||||
return new Promise((resolve, reject) => {
|
if (isProcessingQueue || requestQueue.length === 0) return;
|
||||||
if (!rawAddr || !rawAddr.includes(":")) {
|
|
||||||
resolve(rawAddr);
|
isProcessingQueue = true;
|
||||||
|
|
||||||
|
const processNext = () => {
|
||||||
|
if (requestQueue.length === 0) {
|
||||||
|
isProcessingQueue = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { rawAddr, resolve } = requestQueue.shift();
|
||||||
|
|
||||||
|
// Check cache first
|
||||||
if (addrCache.has(rawAddr)) {
|
if (addrCache.has(rawAddr)) {
|
||||||
resolve(addrCache.get(rawAddr));
|
resolve(addrCache.get(rawAddr));
|
||||||
|
setTimeout(processNext, 50);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,23 +127,96 @@
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
.then((response) => {
|
.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}`);
|
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||||
|
}
|
||||||
return response.json();
|
return response.json();
|
||||||
})
|
})
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
|
if (data) {
|
||||||
const friendly =
|
const friendly =
|
||||||
data?.result?.bounceable?.b64url ||
|
|
||||||
data?.result?.non_bounceable?.b64url ||
|
data?.result?.non_bounceable?.b64url ||
|
||||||
rawAddr;
|
rawAddr.replace(":retry:", "");
|
||||||
addrCache.set(rawAddr, friendly);
|
const cleanAddr = rawAddr.replace(":retry:", "");
|
||||||
|
addrCache.set(cleanAddr, friendly);
|
||||||
resolve(friendly);
|
resolve(friendly);
|
||||||
|
} else {
|
||||||
|
resolve(rawAddr.replace(":retry:", ""));
|
||||||
|
}
|
||||||
|
setTimeout(processNext, REQUEST_DELAY);
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.warn("Address conversion failed:", error);
|
console.warn("Address conversion failed:", error);
|
||||||
|
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);
|
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user