symbol selection work

This commit is contained in:
Tim
2024-02-27 18:03:35 -04:00
parent f1f8bdf099
commit fc126955b6
4 changed files with 59 additions and 40 deletions

View File

@@ -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');