From d7e60ad5079cd4b5a67c3e45d8e4a684b38f7f05 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 13 Mar 2024 17:11:48 -0400 Subject: [PATCH] symbol id fixes --- src/blockchain/ohlcs.js | 21 ++++++++------- src/charts/chart.js | 4 +-- src/charts/datafeed.js | 57 +++++++++++++++++++++++++---------------- 3 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/blockchain/ohlcs.js b/src/blockchain/ohlcs.js index 77ef4f9..a46b735 100644 --- a/src/blockchain/ohlcs.js +++ b/src/blockchain/ohlcs.js @@ -16,17 +16,16 @@ export function subOHLC( chainId, pool, period ) { export function unsubOHLC( chainId, pool, period ) { - for( const [pool,period] of poolPeriods ) { - const key = `${pool}|${period}` - const ckey = `${chainId}|${key}` - if (!(ckey in ohlcSubCounts) || ohlcSubCounts[ckey] === 0) { - console.error('overdecremented ohlc', pool, period) - ohlcSubCounts[ckey] = 1 - } else { - ohlcSubCounts[ckey]-- - if (ohlcSubCounts[key] === 0) - socket.emit('unsubOHLCs', chainId, [key]) - } + const key = `${pool}|${period}` + const ckey = `${chainId}|${key}` + if (!(ckey in ohlcSubCounts) || ohlcSubCounts[ckey] === 0) { + console.error('overdecremented ohlc', pool, period) + ohlcSubCounts[ckey] = 1 + } else { + ohlcSubCounts[ckey]-- + if (ohlcSubCounts[key] === 0) + // noinspection JSCheckFunctionSignatures + socket.emit('unsubOHLCs', chainId, [key]) } } diff --git a/src/charts/chart.js b/src/charts/chart.js index 6e66f17..f396078 100644 --- a/src/charts/chart.js +++ b/src/charts/chart.js @@ -42,11 +42,9 @@ export function initWidget(el) { library_path: "/charting_library/", // debug: true, autosize: true, - // symbol: 'AAPL', - symbol: 'UNIv3:WETH/USDC', // use this for ohlc + symbol: 'default', interval: '15', container: el, - // datafeed: new Datafeeds.UDFCompatibleDatafeed("https://demo-feed-data.tradingview.com"), datafeed: DataFeed, // use this for ohlc locale: "en", disabled_features: [], diff --git a/src/charts/datafeed.js b/src/charts/datafeed.js index 4e797dc..8438f48 100644 --- a/src/charts/datafeed.js +++ b/src/charts/datafeed.js @@ -1,14 +1,11 @@ -import { - subscribeOnStream, - unsubscribeFromStream, -} from './streaming.js'; +import {subscribeOnStream, unsubscribeFromStream,} from './streaming.js'; import {jBars, tvResolutionToPeriodString} from './jBars.js'; import {metadata} from "@/version.js"; import FlexSearch from "flexsearch"; import {useChartOrderStore} from "@/orderbuild.js"; import {useOrderStore, useStore} from "@/store/store.js"; -import {subOHLC, unsubAllOHLCs, unsubOHLC} from "@/blockchain/ohlcs.js"; +import {subOHLC, unsubOHLC} from "@/blockchain/ohlcs.js"; let feeDropdown = null let widget = null @@ -32,7 +29,7 @@ function updateFeeDropdown() { const feeOpts = { items: symbolItem.pools.map((p)=> { return { - title: (p[1]/10000).toFixed(3)+'%', + title: (p[1]/10000).toFixed(2)+'%', onSelect: ()=>selectPool(p), } }) @@ -83,7 +80,7 @@ const poolMap = {} let _symbols = null const indexer = new FlexSearch.Document({ - document: {id: 'id', index: ['id', 'as[]', 'b', 'q', 'bs', 'qs', 'e', 'd']}, // this must match what is generated for the index object in addSymbol() + document: {id: 'id', index: ['fn', 'as[]', 'b', 'q', 'bs', 'qs', 'e', 'd']}, // this must match what is generated for the index object in addSymbol() charset: {split: /\W+/}, tokenize: 'forward', }) @@ -92,29 +89,39 @@ 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) { - console.log('addSymbol', p) - if (inverted) - [base, quote] = [quote,base] + // console.log('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 existing index but don't create a new symbol - const symbolInfo = _symbols[full_name]; + let key = `${base.a}${quote.a}` + let found = symbolsSeen[key] + if (!found) { + key = `${quote.a}${base.a}` + found = symbolsSeen[key] + } + if (found) { + // add this pool's address to the existing symbol as an additional fee tier + const symbolInfo = _symbols[key]; symbolInfo.pools.push([p.a, p.f]) symbolInfo.pools.sort((a,b)=>a[1]-b[1]) - indexes[full_name].as.push(p.a) + indexes[key].as.push(p.a) return } - symbolsSeen[full_name] = true + symbolsSeen[key] = true const longExchange = ['Uniswap v2', 'Uniswap v3',][p.e] const description = `${base.n} / ${quote.n}` const type = 'swap' const pools = [[p.a, p.f]] - _symbols[full_name] = {symbol, full_name, description, exchange, type, inverted, base, quote, pools, x:p.x} - indexes[full_name] = { + _symbols[key] = { + full_name: key, symbol, ticker: full_name, description, + exchange, type, inverted, base, quote, pools, x:p.x + } + if (defaultSymbol===null) + defaultSymbol = _symbols[key] + console.log('added symbol', key, _symbols[key]) + indexes[key] = { // key - id: full_name, + id: key, // addresses as: [p.a], // multiple pool addrs for each fee tier @@ -122,6 +129,7 @@ function addSymbol(p, base, quote, inverted) { q: p.q, // symbols + fn: full_name, bs: base.s, qs: quote.s, e: exchange, @@ -164,8 +172,13 @@ async function getAllSymbols() { return _symbols } -export function lookupSymbol(fullName) { - return _symbols[fullName] +export function lookupSymbol(key) { + return _symbols[key] +} + +export function lookupBaseQuote(baseAddr, quoteAddr) { + + return _symbols[key] } export const DataFeed = { @@ -199,7 +212,7 @@ export const DataFeed = { ) => { console.log('[resolveSymbol]: Method call', symbolName); const symbols = await getAllSymbols(); - const symbolItem = symbols[symbolName] + const symbolItem = symbolName === 'default' ? defaultSymbol : symbols[symbolName] console.log('symbol resolved?', symbolItem) if (!symbolItem) { console.log('[resolveSymbol]: Cannot resolve symbol', symbolName); @@ -269,7 +282,6 @@ export const DataFeed = { const chainId = useStore().chainId; const poolAddr = useChartOrderStore().selectedPool[0]; const period = tvResolutionToPeriodString(resolution); - console.log('poolAddr', poolAddr) subscriptions[subscriberUID] = [chainId, poolAddr, period] subOHLC(chainId, poolAddr, period) return; // disable @@ -293,4 +305,5 @@ export const DataFeed = { }, }; +let defaultSymbol = null const subscriptions = {}