From fc126955b61b7a7b1b6dc1a4db8cbe0ff23f43f1 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 27 Feb 2024 18:03:35 -0400 Subject: [PATCH] symbol selection work --- .gitignore | 3 +- public/metadata.json | 1 - src/charts/datafeed.js | 92 +++++++++++++++++++++++++----------------- src/version.js | 3 +- 4 files changed, 59 insertions(+), 40 deletions(-) delete mode 100644 public/metadata.json diff --git a/.gitignore b/.gitignore index 7c05d41..0e2c017 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /.env -version.json +/public/version.json +/public/metadata.json ### JetBrains template # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider diff --git a/public/metadata.json b/public/metadata.json deleted file mode 100644 index 39498f9..0000000 --- a/public/metadata.json +++ /dev/null @@ -1 +0,0 @@ -{"t":[{"a":"0x2653aB60a6fD19d3e1f4E729412C219257CA6e24","n":"USD Coin (Arb1)","s":"USDC","d":6},{"a":"0xEaD55ce1cC577C8020F5FcE39D4e5C643F591a0c","n":"USD Coin","s":"USDC","d":6},{"a":"0x87b60276434d5cB5CE46d2316B596c7Bc5f87cCA","n":"Wrapped Ether","s":"WETH","d":18}],"p":[{"a":"0x0CB9B6A3Bd11c5b7378712AC9a9D71419ADE06AA","b":"0x87b60276434d5cB5CE46d2316B596c7Bc5f87cCA","q":"0xEaD55ce1cC577C8020F5FcE39D4e5C643F591a0c","f":500,"e":1,"d":12,"x":{"data":{"uri":"https://alpha.dexorder.trade/42161/0xC6962004f452bE9203591991D15f6b388e09E8D0/","inverted":false}}},{"a":"0xDE0C15b466b9A3720c27221C7465737fa46C4e96","b":"0x2653aB60a6fD19d3e1f4E729412C219257CA6e24","q":"0x87b60276434d5cB5CE46d2316B596c7Bc5f87cCA","f":500,"e":1,"d":-12,"x":{"data":{"uri":"https://alpha.dexorder.trade/42161/0xC31E54c7a869B9FcBEcc14363CF510d1c41fa443/","inverted":true}}}]} \ No newline at end of file diff --git a/src/charts/datafeed.js b/src/charts/datafeed.js index a27d938..33c0375 100644 --- a/src/charts/datafeed.js +++ b/src/charts/datafeed.js @@ -33,18 +33,60 @@ const configurationData = { const tokenMap = {} const poolMap = {} -let symbols = null +let _symbols = null + const indexer = new FlexSearch.Document({ - document: {id: 'id', index: ['id', 'as[]', 'b', 'q', 'bs', 'qs', 'e', 'd']}, + document: {id: 'id', index: ['id', 'as[]', 'b', 'q', 'bs', 'qs', 'e', 'd']}, // this must match what is generated for the index object in addSymbol() charset: {split: /\W+/}, tokenize: 'forward', }) +const indexes = {} +const symbolsSeen = {} // keyed by (base,quote) so we only list one pool per pair even if there are many fee tiers + +function addSymbol(p, base, quote, inverted) { + const symbol = base.s + '/' + quote.s + const exchange = ['UNIv2', 'UNIv3'][p.e] + const full_name = exchange + ':' + symbol // + '%' + formatFee(fee) + if (full_name in symbolsSeen) { + // add this pool's address to the index but don't create a new symbol + indexes[full_name].as.push(p.a) + return + } + symbolsSeen[full_name] = true + const longExchange = ['Uniswap v2', 'Uniswap v3',][p.e] + const description = `${base.n} / ${quote.n}` + const type = 'swap' + _symbols[full_name] = {symbol, full_name, description, exchange, type, inverted,} + indexes[full_name] = { + // key + id: full_name, + + // addresses + as: [p.a], // multiple pool addrs for each fee tier + b: p.b, + q: p.q, + + // symbols + bs: base.s, + qs: quote.s, + e: exchange, + d: description, + } +} + + +// function formatFee(fee) { +// let str = (fee / 10000).toFixed(2); +// if (str.startsWith('0')) // start with the decimal point not a zero +// str = str.slice(1) +// return str +// } async function getAllSymbols() { - const built = {} // keyed by (base,quote) so we only list one pool per pair even if there are many fee tiers - if (symbols===null) { - symbols = {} + + if (_symbols===null) { + _symbols = {} for (const t of metadata.t) tokenMap[t.a] = t metadata.p.forEach((p)=>{ @@ -60,39 +102,14 @@ async function getAllSymbols() { console.log(`No token ${p.q} found`) return } - const symbol = base.s + '/' + quote.s - const exchange = ['UNIv2','UNIv3'][p.e] - const full_name = exchange+':'+symbol - if (full_name in built) { - // add this pool's address to the index but don't create a new symbol - symbols[full_name].index.as.push(p.a) - return - } - built[full_name] = true - const longExchange = ['Uniswap v2', 'Uniswap v3',][p.e] - const description = `${base.n} / ${quote.n}` - const type = 'swap' - symbols[full_name] = {symbol, full_name, description, exchange, type, - index: { - // key - id: full_name, - // addresses - as: [p.a], // multiple pool addrs for each fee tier - b: p.b, - q: p.q, - // symbols - bs: base.s, - qs: quote.s, - e: exchange, - d: description, - } - } + addSymbol(p, base, quote, false); + addSymbol(p, quote, base, true); }) - for (const symbol of Object.values(symbols)) { - indexer.add(symbol.index) - } + console.log('indexes', indexes) + Object.values(indexes).forEach(indexer.add.bind(indexer)) } - return Object.values(symbols) + console.log('symbols', _symbols) + return _symbols } export default { @@ -132,7 +149,7 @@ export default { const result = [] for (const f of found) for (const key of f.result) - result.push(symbols[key]) + result.push(_symbols[key]) onResultReadyCallback(result); }, @@ -145,6 +162,7 @@ export default { console.log('[resolveSymbol]: Method call', symbolName); const symbols = await getAllSymbols(); const symbolItem = symbols[symbolName] + console.log('symbol resolved?', symbolItem) if (!symbolItem) { console.log('[resolveSymbol]: Cannot resolve symbol', symbolName); onResolveErrorCallback('cannot resolve symbol'); diff --git a/src/version.js b/src/version.js index edce3ee..ed94910 100644 --- a/src/version.js +++ b/src/version.js @@ -2,5 +2,6 @@ const versionPromise = fetch('/version.json').then(async (response)=>await respo const metadataPromise = fetch('/metadata.json').then(async (response)=>await response.json()) export const version = await versionPromise -export const metadata = await metadataPromise console.log('version', version) +export const metadata = await metadataPromise +console.log('metadata', metadata)