symbol id fixes

This commit is contained in:
Tim
2024-03-13 17:11:48 -04:00
parent 90723ba4ed
commit d7e60ad507
3 changed files with 46 additions and 36 deletions

View File

@@ -16,7 +16,6 @@ export function subOHLC( chainId, pool, period ) {
export function unsubOHLC( chainId, pool, period ) { export function unsubOHLC( chainId, pool, period ) {
for( const [pool,period] of poolPeriods ) {
const key = `${pool}|${period}` const key = `${pool}|${period}`
const ckey = `${chainId}|${key}` const ckey = `${chainId}|${key}`
if (!(ckey in ohlcSubCounts) || ohlcSubCounts[ckey] === 0) { if (!(ckey in ohlcSubCounts) || ohlcSubCounts[ckey] === 0) {
@@ -25,10 +24,10 @@ export function unsubOHLC( chainId, pool, period ) {
} else { } else {
ohlcSubCounts[ckey]-- ohlcSubCounts[ckey]--
if (ohlcSubCounts[key] === 0) if (ohlcSubCounts[key] === 0)
// noinspection JSCheckFunctionSignatures
socket.emit('unsubOHLCs', chainId, [key]) socket.emit('unsubOHLCs', chainId, [key])
} }
} }
}
export function unsubAllOHLCs( chainId ) { export function unsubAllOHLCs( chainId ) {

View File

@@ -42,11 +42,9 @@ export function initWidget(el) {
library_path: "/charting_library/", library_path: "/charting_library/",
// debug: true, // debug: true,
autosize: true, autosize: true,
// symbol: 'AAPL', symbol: 'default',
symbol: 'UNIv3:WETH/USDC', // use this for ohlc
interval: '15', interval: '15',
container: el, container: el,
// datafeed: new Datafeeds.UDFCompatibleDatafeed("https://demo-feed-data.tradingview.com"),
datafeed: DataFeed, // use this for ohlc datafeed: DataFeed, // use this for ohlc
locale: "en", locale: "en",
disabled_features: [], disabled_features: [],

View File

@@ -1,14 +1,11 @@
import { import {subscribeOnStream, unsubscribeFromStream,} from './streaming.js';
subscribeOnStream,
unsubscribeFromStream,
} from './streaming.js';
import {jBars, tvResolutionToPeriodString} from './jBars.js'; import {jBars, tvResolutionToPeriodString} from './jBars.js';
import {metadata} from "@/version.js"; import {metadata} from "@/version.js";
import FlexSearch from "flexsearch"; import FlexSearch from "flexsearch";
import {useChartOrderStore} from "@/orderbuild.js"; import {useChartOrderStore} from "@/orderbuild.js";
import {useOrderStore, useStore} from "@/store/store.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 feeDropdown = null
let widget = null let widget = null
@@ -32,7 +29,7 @@ function updateFeeDropdown() {
const feeOpts = { const feeOpts = {
items: symbolItem.pools.map((p)=> { items: symbolItem.pools.map((p)=> {
return { return {
title: (p[1]/10000).toFixed(3)+'%', title: (p[1]/10000).toFixed(2)+'%',
onSelect: ()=>selectPool(p), onSelect: ()=>selectPool(p),
} }
}) })
@@ -83,7 +80,7 @@ const poolMap = {}
let _symbols = null let _symbols = null
const indexer = new FlexSearch.Document({ 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+/}, charset: {split: /\W+/},
tokenize: 'forward', 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 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) { function addSymbol(p, base, quote, inverted) {
console.log('addSymbol', p) // console.log('addSymbol', p, base, quote, inverted)
if (inverted)
[base, quote] = [quote,base]
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 // + '%' + formatFee(fee) const full_name = exchange + ':' + symbol // + '%' + formatFee(fee)
if (full_name in symbolsSeen) { let key = `${base.a}${quote.a}`
// add this pool's address to the existing index but don't create a new symbol let found = symbolsSeen[key]
const symbolInfo = _symbols[full_name]; 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.push([p.a, p.f])
symbolInfo.pools.sort((a,b)=>a[1]-b[1]) symbolInfo.pools.sort((a,b)=>a[1]-b[1])
indexes[full_name].as.push(p.a) indexes[key].as.push(p.a)
return return
} }
symbolsSeen[full_name] = true symbolsSeen[key] = 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'
const pools = [[p.a, p.f]] const pools = [[p.a, p.f]]
_symbols[full_name] = {symbol, full_name, description, exchange, type, inverted, base, quote, pools, x:p.x} _symbols[key] = {
indexes[full_name] = { 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 // key
id: full_name, id: key,
// addresses // addresses
as: [p.a], // multiple pool addrs for each fee tier as: [p.a], // multiple pool addrs for each fee tier
@@ -122,6 +129,7 @@ function addSymbol(p, base, quote, inverted) {
q: p.q, q: p.q,
// symbols // symbols
fn: full_name,
bs: base.s, bs: base.s,
qs: quote.s, qs: quote.s,
e: exchange, e: exchange,
@@ -164,8 +172,13 @@ async function getAllSymbols() {
return _symbols return _symbols
} }
export function lookupSymbol(fullName) { export function lookupSymbol(key) {
return _symbols[fullName] return _symbols[key]
}
export function lookupBaseQuote(baseAddr, quoteAddr) {
return _symbols[key]
} }
export const DataFeed = { export const DataFeed = {
@@ -199,7 +212,7 @@ export const DataFeed = {
) => { ) => {
console.log('[resolveSymbol]: Method call', symbolName); console.log('[resolveSymbol]: Method call', symbolName);
const symbols = await getAllSymbols(); const symbols = await getAllSymbols();
const symbolItem = symbols[symbolName] const symbolItem = symbolName === 'default' ? defaultSymbol : symbols[symbolName]
console.log('symbol resolved?', symbolItem) console.log('symbol resolved?', symbolItem)
if (!symbolItem) { if (!symbolItem) {
console.log('[resolveSymbol]: Cannot resolve symbol', symbolName); console.log('[resolveSymbol]: Cannot resolve symbol', symbolName);
@@ -269,7 +282,6 @@ export const DataFeed = {
const chainId = useStore().chainId; const chainId = useStore().chainId;
const poolAddr = useChartOrderStore().selectedPool[0]; const poolAddr = useChartOrderStore().selectedPool[0];
const period = tvResolutionToPeriodString(resolution); const period = tvResolutionToPeriodString(resolution);
console.log('poolAddr', poolAddr)
subscriptions[subscriberUID] = [chainId, poolAddr, period] subscriptions[subscriberUID] = [chainId, poolAddr, period]
subOHLC(chainId, poolAddr, period) subOHLC(chainId, poolAddr, period)
return; // disable return; // disable
@@ -293,4 +305,5 @@ export const DataFeed = {
}, },
}; };
let defaultSymbol = null
const subscriptions = {} const subscriptions = {}