pool fee selection; ohlc subs

This commit is contained in:
Tim
2024-03-13 15:48:19 -04:00
parent 82a150df2b
commit 35856a4edb
7 changed files with 148 additions and 44 deletions

View File

@@ -3,10 +3,50 @@ import {
unsubscribeFromStream,
} from './streaming.js';
import {jBars} from './jBars.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";
let feeDropdown = null
let widget = null
export function initFeeDropdown(w) {
widget = w
widget.createDropdown(
{
title: 'Fees',
tooltip: 'Choose Fee Tier',
items: [/*{title: 'Automatic Fee Selection', onSelect: () => {console.log('autofees')}}*/],
icon: `<svg xmlns="http://www.w3.org/2000/svg" width="28" height="28"><g fill="none" stroke="currentColor"><circle cx="10" cy="10" r="2.5"/><circle cx="18" cy="18" r="2.5"/><path stroke-linecap="square" d="M17.5 7.5l-7 13"/></g></svg>`,
}
).then(dropdown => {feeDropdown = dropdown; updateFeeDropdown()})
}
function updateFeeDropdown() {
if (feeDropdown===null) return
const symbolItem = useChartOrderStore().selectedSymbol
const feeOpts = {
items: symbolItem.pools.map((p)=> {
return {
title: (p[1]/10000).toFixed(3)+'%',
onSelect: ()=>selectPool(p),
}
})
}
feeDropdown.applyOptions(feeOpts)
}
function selectPool(p) {
const co = useChartOrderStore();
if ( co.selectedPool === null || co.selectedPool[0] !== p[0] || co.selectedPool[1] !== p[0]) {
co.selectedPool = p
}
}
const lastBarsCache = new Map();
@@ -52,11 +92,17 @@ 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]
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];
symbolInfo.pools.push([p.a, p.f])
symbolInfo.pools.sort((a,b)=>a[1]-b[1])
indexes[full_name].as.push(p.a)
return
}
@@ -64,7 +110,8 @@ function addSymbol(p, base, quote, inverted) {
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, base, quote, x:p.x}
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] = {
// key
id: full_name,
@@ -97,7 +144,6 @@ async function getAllSymbols() {
tokenMap[t.a] = t
metadata.p.forEach((p)=>{
poolMap[p.a] = p
// todo inversion
const base = tokenMap[p.b];
const quote = tokenMap[p.q];
if (!base) {
@@ -122,7 +168,7 @@ export function lookupSymbol(fullName) {
return _symbols[fullName]
}
export default {
export const DataFeed = {
onReady: (callback) => {
console.log('[onReady]: Method call');
setTimeout(() => callback(configurationData));
@@ -160,12 +206,18 @@ export default {
onResolveErrorCallback('cannot resolve symbol');
return;
}
useChartOrderStore().selectedSymbol = symbolItem
const co = useChartOrderStore();
const os = useOrderStore()
co.selectedSymbol = symbolItem
const pool = symbolItem.pools[Math.trunc(symbolItem.pools.length/2)];
// noinspection JSValidateTypes
co.selectedPool = [pool]
updateFeeDropdown()
// Symbol information object
const symbolInfo = {
ticker: symbolItem.full_name,
name: symbolItem.symbol,
description: symbolItem.description,
description: symbolItem.description + ` ${(pool[1]/10000).toFixed(2)}%`,
type: symbolItem.type,
session: '24x7',
timezone: 'Etc/UTC',
@@ -180,7 +232,6 @@ export default {
volume_precision: 2,
data_status: 'streaming',
};
console.log('[resolveSymbol]: Symbol resolved', symbolName);
onSymbolResolvedCallback(symbolInfo);
},
@@ -188,11 +239,11 @@ export default {
getBars: async (symbolInfo, resolution, periodParams, onHistoryCallback, onErrorCallback) => {
const { from, to, firstDataRequest } = periodParams;
console.log('[getBars]: Method call', symbolInfo, resolution, from, to);
try {
// todo need to consider the selected fee tier
let bars, metadata;
[bars, metadata] = await jBars(from, to, resolution); // This is the one that does all the work
const pool = useChartOrderStore().selectedPool;
[bars, metadata] = await jBars(pool[0], from, to, resolution); // This is the one that does all the work
if (firstDataRequest) {
lastBarsCache.set(symbolInfo.full_name, {
...bars[bars.length - 1],
@@ -215,6 +266,11 @@ export default {
onResetCacheNeededCallback,
) => {
console.log('[subscribeBars]: Method call with subscriberUID:', subscriberUID);
const chainId = useStore().chainId;
const poolAddr = useChartOrderStore().selectedPool[0];
const period = tvResolutionToPeriodString(resolution);
subscriptions[subscriberUID] = [chainId, poolAddr, period]
subOHLC(chainId, poolAddr, period)
return; // disable
subscribeOnStream(
symbolInfo,
@@ -228,7 +284,12 @@ export default {
unsubscribeBars: (subscriberUID) => {
console.log('[unsubscribeBars]: Method call with subscriberUID:', subscriberUID);
const [chainId, poolAddr, period] = subscriptions[subscriberUID]
delete subscriptions[subscriberUID]
unsubOHLC(chainId, poolAddr, period)
return; // disable
unsubscribeFromStream(subscriberUID);
},
};
const subscriptions = {}