Skip to content

Instantly share code, notes, and snippets.

@dcts
Created April 12, 2024 17:52
Show Gist options
  • Save dcts/e92ad3302be703707592761426854dec to your computer and use it in GitHub Desktop.
Save dcts/e92ad3302be703707592761426854dec to your computer and use it in GitHub Desktop.
getTokenInfos.ts for DeXter
// Helper function to download data
function downloadObjectAsJson(exportObj: any, exportName: string) {
var dataStr =
"data:text/json;charset=utf-8," +
encodeURIComponent(JSON.stringify(exportObj, null, 2));
var downloadAnchorNode = document.createElement("a");
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", exportName + ".json");
document.body.appendChild(downloadAnchorNode); // required for firefox
downloadAnchorNode.click();
downloadAnchorNode.remove();
}
useEffect(() => {
const newOptions = options.filter(
(option) => option["name"].toLowerCase().indexOf(query.toLowerCase()) > -1
);
const tokenDict: Record<
string,
{ address: string; iconUrl: string; name: string; symbol: string }
> = {};
newOptions.forEach((opt) => {
tokenDict[opt.token1.address] = {
address: opt.token1.address,
iconUrl: opt.token1.iconUrl,
name: opt.token1.name,
symbol: opt.token1.symbol,
};
tokenDict[opt.token2.address] = {
address: opt.token2.address,
iconUrl: opt.token2.iconUrl,
name: opt.token2.name,
symbol: opt.token2.symbol,
};
});
downloadObjectAsJson(tokenDict, "tokenDict");
}, [options, query]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment