From ee021c9590764d494a042a89df6150c9034f8a4f Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 23 Jan 2024 01:32:44 -0400 Subject: [PATCH] ohlc's --- src/blockchain/ohlcs.js | 48 +++++++++++++++++++++++++++++++++++ src/blockchain/prices.js | 1 + src/components/RoutePrice.vue | 5 +++- src/components/Vault.vue | 2 ++ src/socket.js | 7 +++-- tradingview.md | 3 +++ 6 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 src/blockchain/ohlcs.js create mode 100644 tradingview.md diff --git a/src/blockchain/ohlcs.js b/src/blockchain/ohlcs.js new file mode 100644 index 0000000..61bb852 --- /dev/null +++ b/src/blockchain/ohlcs.js @@ -0,0 +1,48 @@ +import {socket} from "@/socket.js"; +import {useStore} from "@/store/store.js"; +import {Exchange} from "@/blockchain/orderlib.js"; +import {uniswapV3PoolAddress} from "@/blockchain/uniswap.js"; +import {ethers, FixedNumber} from "ethers"; +import {uniswapV3PoolAbi} from "@/blockchain/abi.js"; + +const ohlcSubCounts = {} // key is route and value is a subscription counter +export const WIDE_PRICE_FORMAT = {decimals:38, width:512, signed:false}; // 38 decimals is 127 bits + + +export function subOHLCs( chainId, poolPeriods ) { + if( !poolPeriods.length ) + return + const toSub = [] + for( const [pool,period] of poolPeriods ) { + const key = `${pool}|${period}` + if (!(key in ohlcSubCounts) || ohlcSubCounts[key] === 0) { + ohlcSubCounts[key] = 1 + toSub.push(key) + } else { + ohlcSubCounts[key]++ + } + } + if( toSub.length ) + socket.emit('subOHLCs', chainId, toSub) +} + +export function unsubPrices( chainId, poolPeriods ) { + const toUnsub = [] + for( const [pool,period] of poolPeriods ) { + const key = `${pool}|${period}` + if (!(key in ohlcSubCounts) || ohlcSubCounts[key] === 0) { + console.error('overdecremented',pool,period) + ohlcSubCounts[key] = 1 + toSub.push(key) + } else { + ohlcSubCounts[key]-- + if( ohlcSubCounts[key] == 0 ) + toUnsub.push(key) + } + } + if( toUnsub.length ) + socket.emit('unsubOHLCs', chainId, toUnsub ) +} + +// todo get history + diff --git a/src/blockchain/prices.js b/src/blockchain/prices.js index a3845e6..8273d07 100644 --- a/src/blockchain/prices.js +++ b/src/blockchain/prices.js @@ -4,6 +4,7 @@ import {Exchange} from "@/blockchain/orderlib.js"; import {uniswapV3PoolAddress} from "@/blockchain/uniswap.js"; import {ethers, FixedNumber} from "ethers"; import {uniswapV3PoolAbi} from "@/blockchain/abi.js"; +import {subOHLCs} from "@/blockchain/ohlcs.js"; const subscriptionCounts = {} // key is route and value is a subscription counter export const WIDE_PRICE_FORMAT = {decimals:38, width:512, signed:false}; // 38 decimals is 127 bits diff --git a/src/components/RoutePrice.vue b/src/components/RoutePrice.vue index 5209364..86e573b 100644 --- a/src/components/RoutePrice.vue +++ b/src/components/RoutePrice.vue @@ -8,6 +8,7 @@ import {subPrices, unsubPrices, WIDE_PRICE_FORMAT} from "@/blockchain/prices.js" import {computed, onBeforeUnmount} from "vue"; import {FixedNumber} from "ethers"; import {routeInverted} from "@/misc.js"; +import {subOHLCs} from "@/blockchain/ohlcs.js"; const s = useStore() const os = useOrderStore() @@ -37,8 +38,10 @@ const price = computed(()=>{ return p.toPrecision(props.precision) }) -if( route.value ) +if( route.value ) { subPrices([route.value]) + subOHLCs( s.chainId.value, [[route.value.pool,'1D']]) +} else console.log('route is empty: no price') diff --git a/src/components/Vault.vue b/src/components/Vault.vue index 9228da8..dc5c24f 100644 --- a/src/components/Vault.vue +++ b/src/components/Vault.vue @@ -48,12 +48,14 @@ Vault Assets {{s.vaults.length>1?'#'+(num+1):''}} {{addr}} +

There are no funds currently in your vault. Send tokens to the address above to fund your vault.

There are no funds currently in your vault. Use the faucet below to mint some testnet coins into your vault.

diff --git a/src/socket.js b/src/socket.js index 5371768..331a816 100644 --- a/src/socket.js +++ b/src/socket.js @@ -1,7 +1,6 @@ import {io} from "socket.io-client"; import {useStore} from "@/store/store.js"; -import {flushOrders, onChainChanged} from "@/blockchain/wallet.js"; -import {ethers} from "ethers"; +import {flushOrders} from "@/blockchain/wallet.js"; import {parseOrderStatus} from "@/blockchain/orderlib.js"; export const socket = io(import.meta.env.VITE_WS_URL || undefined, {transports: ["websocket"]}) @@ -22,6 +21,10 @@ socket.on('p', async (chainId, pool, price) => { s.poolPrices[[chainId,pool]] = price }) +socket.on('ohlcs', async (chainId, pool, ohlcs) => { + console.log('pool bars', pool, ohlcs) +}) + socket.on('vb', async (chainId, vault, balances) => { const s = useStore() if( s.chainId.value !== chainId ) diff --git a/tradingview.md b/tradingview.md new file mode 100644 index 0000000..0acba4c --- /dev/null +++ b/tradingview.md @@ -0,0 +1,3 @@ +https://github.com/tradingview/charting_library +https://www.tradingview.com/charting-library-docs/latest/getting_started/Best-Practices/ +