Skip to content

Commit

Permalink
Add StreamDepthUpdateForMarketAndFreq (#140)
Browse files Browse the repository at this point in the history
* Add StreamDepthUpdateForMarketAndFreq

* Fix run_local

* Add OrderType in deepCopyOrder
  • Loading branch information
lumos42 authored Dec 15, 2023
1 parent 4c9bda2 commit 036cc2e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions plugin/evm/orderbook/memory_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,7 @@ func deepCopyOrder(order *Order) Order {
LifecycleList: *lifecycleList,
BlockNumber: big.NewInt(0).Set(order.BlockNumber),
RawOrder: order.RawOrder,
OrderType: order.OrderType,
}
}

Expand Down
37 changes: 37 additions & 0 deletions plugin/evm/orderbook/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ func (api *OrderBookAPI) GetDepthForMarket(ctx context.Context, market int) *Mar
return getDepthForMarket(api.db, Market(market))
}

// used by UI
func (api *OrderBookAPI) StreamDepthUpdateForMarket(ctx context.Context, market int) (*rpc.Subscription, error) {
notifier, _ := rpc.NotifierFromContext(ctx)
rpcSub := notifier.CreateSubscription()
Expand All @@ -270,6 +271,42 @@ func (api *OrderBookAPI) StreamDepthUpdateForMarket(ctx context.Context, market
return rpcSub, nil
}

// used by UI
// @todo: this is a duplicate of StreamDepthUpdateForMarket with a param for update frequency. Need to remove the original function later and keep this one.
func (api *OrderBookAPI) StreamDepthUpdateForMarketAndFreq(ctx context.Context, market int, updateFreq string) (*rpc.Subscription, error) {
notifier, _ := rpc.NotifierFromContext(ctx)
rpcSub := notifier.CreateSubscription()

if updateFreq == "" {
updateFreq = "1s"
}

duration, err := time.ParseDuration(updateFreq)
if err != nil {
return nil, fmt.Errorf("invalid update frequency %s", updateFreq)
}
ticker := time.NewTicker(duration)

var oldMarketDepth = &MarketDepth{}

go func() {
for {
select {
case <-ticker.C:
newMarketDepth := getDepthForMarket(api.db, Market(market))
depthUpdate := getUpdateInDepth(newMarketDepth, oldMarketDepth)
notifier.Notify(rpcSub.ID, depthUpdate)
oldMarketDepth = newMarketDepth
case <-notifier.Closed():
ticker.Stop()
return
}
}
}()

return rpcSub, nil
}

func getUpdateInDepth(newMarketDepth *MarketDepth, oldMarketDepth *MarketDepth) *MarketDepth {
var diff = &MarketDepth{
Market: newMarketDepth.Market,
Expand Down
1 change: 1 addition & 0 deletions plugin/evm/orderbook/trading_apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func (api *TradingAPI) GetMarginAndPositions(ctx context.Context, trader string)
return response, nil
}

// used by the sdk
func (api *TradingAPI) StreamDepthUpdateForMarket(ctx context.Context, market int) (*rpc.Subscription, error) {
notifier, _ := rpc.NotifierFromContext(ctx)
rpcSub := notifier.CreateSubscription()
Expand Down
4 changes: 2 additions & 2 deletions scripts/run_local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ avalanche network clean
FILE=/tmp/validator.pk
if [ ! -f "$FILE" ]
then
echo "$FILE does not exists."
echo "$FILE does not exist; creating"
echo "31b571bf6894a248831ff937bb49f7754509fe93bbd2517c9c73c4144c0e97dc" > $FILE
fi

avalanche subnet create hubblenet --force --custom --genesis genesis.json --vm custom_evm.bin --config .avalanche-cli.json
avalanche subnet create hubblenet --force --custom --genesis genesis.json --custom-vm-path custom_evm.bin --custom-vm-branch main --custom-vm-build-script scripts/build.sh --custom-vm-repo-url https://github.com/hubble-exchange/hubblenet --config .avalanche-cli.json

# configure and add chain.json
avalanche subnet configure hubblenet --chain-config chain.json --config .avalanche-cli.json
Expand Down
2 changes: 1 addition & 1 deletion scripts/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -e
function setStatus() {
cat <<EOF >local_status.sh
export CHAIN_ID=$(echo "$OUTPUT" | awk -F'|' '/node1/{print $4}' | awk -F'/' '{print $6}')
export LOGS_PATH="$(echo "$OUTPUT" | awk -F': ' '/Node log path: /{print $2}')"
export LOGS_PATH="$(echo "$OUTPUT" | awk -F': ' '/Node logs directory: /{print $2}')"
EOF

cat <<EOF >~/.hubblenet.json
Expand Down

0 comments on commit 036cc2e

Please sign in to comment.