search by pool addr bugfix

This commit is contained in:
Tim
2024-02-16 22:26:51 -04:00
parent d01169b2fd
commit 26becc3d87

View File

@@ -35,7 +35,7 @@ const tokenMap = {}
const poolMap = {} const poolMap = {}
let symbols = null let symbols = null
const indexer = new FlexSearch.Document({ const indexer = new FlexSearch.Document({
document: {id: 'id', index: ['id', 'a', 'b', 'q', 'bs', 'qs', 'e', 'd']}, document: {id: 'id', index: ['id', 'as[]', 'b', 'q', 'bs', 'qs', 'e', 'd']},
charset: {split: /\W+/}, charset: {split: /\W+/},
tokenize: 'forward', tokenize: 'forward',
}) })
@@ -45,45 +45,52 @@ async function getAllSymbols() {
const built = {} // keyed by (base,quote) so we only list one pool per pair even if there are many fee tiers const built = {} // keyed by (base,quote) so we only list one pool per pair even if there are many fee tiers
if (symbols===null) { if (symbols===null) {
symbols = {} symbols = {}
for (const t of metadata['t']) for (const t of metadata.t)
tokenMap[t['a']] = t tokenMap[t.a] = t
metadata['p'].forEach((p)=>{ metadata.p.forEach((p)=>{
poolMap[p['a']] = p poolMap[p.a] = p
// todo inversion // todo inversion
const base = tokenMap[p['b']]; const base = tokenMap[p.b];
const quote = tokenMap[p['q']]; const quote = tokenMap[p.q];
if (!base) { if (!base) {
console.log(`No token ${p['b']} found`) console.log(`No token ${p.b} found`)
return return
} }
if (!quote) { if (!quote) {
console.log(`No token ${p['q']} found`) console.log(`No token ${p.q} found`)
return return
} }
const symbol = base['s'] + '/' + quote['s'] const symbol = base.s + '/' + quote.s
const exchange = ['UNIv2','UNIv3'][p['e']] const exchange = ['UNIv2','UNIv3'][p.e]
const full_name = exchange+':'+symbol const full_name = exchange+':'+symbol
if (full_name in built) 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 return
}
built[full_name] = true built[full_name] = true
const longExchange = ['Uniswap v2', 'Uniswap v3',][p['e']] const longExchange = ['Uniswap v2', 'Uniswap v3',][p.e]
const description = `${base['n']} / ${quote['n']}` const description = `${base.n} / ${quote.n}`
const type = 'swap' const type = 'swap'
symbols[full_name] = {symbol, full_name, description, exchange, type,} symbols[full_name] = {symbol, full_name, description, exchange, type,
indexer.add({ index: {
// key // key
id: full_name, id: full_name,
// addresses // addresses
a: p['a'], as: [p.a], // multiple pool addrs for each fee tier
b: p['b'], b: p.b,
q: p['q'], q: p.q,
// symbols // symbols
bs: base['s'], bs: base.s,
qs: quote['s'], qs: quote.s,
e: exchange, e: exchange,
d: description, d: description,
}) }
}
}) })
for (const symbol of Object.values(symbols)) {
indexer.add(symbol.index)
}
} }
return Object.values(symbols) return Object.values(symbols)
} }