Skip to content

Commit

Permalink
Teleporter wip on local
Browse files Browse the repository at this point in the history
  • Loading branch information
lumos42 committed Apr 26, 2024
1 parent 1847852 commit 7729379
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 6 deletions.
101 changes: 101 additions & 0 deletions plugin/evm/orderbook/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,107 @@ func aggregateOrdersByPrice(orders []Order) map[string]string {
return aggregatedOrders
}

// {"jsonrpc":"2.0","id":1,"method":"orderbook_subscribe","params":["streamDepthUpdateForMarketAndType", 0, "1s"]}
func (api *OrderBookAPI) StreamDepthUpdateForMarketAndType(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 = &MarketDepthByType{}

go executeFuncAndRecoverPanic(func() {
for {
select {
case <-ticker.C:
newMarketDepth := getDepthForMarketByType(api.db, Market(market))
depthUpdate := getUpdateInDepthByType(newMarketDepth, oldMarketDepth)
notifier.Notify(rpcSub.ID, depthUpdate)
oldMarketDepth = newMarketDepth
case <-rpcSub.Err():
ticker.Stop()
return
case <-notifier.Closed():
ticker.Stop()
return
}
}
}, "panic in StreamDepthUpdateForMarketAndType", RPCPanicsCounter)

return rpcSub, nil
}

func getUpdateInDepthByType(newMarketDepth *MarketDepthByType, oldMarketDepth *MarketDepthByType) *MarketDepthByType {
var diff = &MarketDepthByType{
Market: newMarketDepth.Market,
Orders: map[string]map[string]string{},
}
for price, types := range newMarketDepth.Orders {
for orderType, depth := range types {
oldDepth, exists := oldMarketDepth.Orders[price][orderType]
if !exists || oldDepth != depth {
if diff.Orders[price] == nil {
diff.Orders[price] = make(map[string]string)
}
diff.Orders[price][orderType] = depth
}
}
}
// Check for any prices or order types that no longer exist and set them to zero
for price, types := range oldMarketDepth.Orders {
for orderType := range types {
if newMarketDepth.Orders[price] == nil || newMarketDepth.Orders[price][orderType] == "" {
if diff.Orders[price] == nil {
diff.Orders[price] = make(map[string]string)
}
diff.Orders[price][orderType] = big.NewInt(0).String()
}
}
}
return diff
}

func getDepthForMarketByType(db LimitOrderDatabase, market Market) *MarketDepthByType {
longOrders := db.GetLongOrders(market, nil /* lowerbound */, nil /* currentBlock */)
shortOrders := db.GetShortOrders(market, nil /* upperbound */, nil /* currentBlock */)
return &MarketDepthByType{
Market: market,
Orders: aggregateOrdersByPriceAndOrderType(longOrders, shortOrders),
}
}

func aggregateOrdersByPriceAndOrderType(longOrders []Order, shortOrders []Order) map[string]map[string]string {
aggregatedOrders := map[string]map[string]string{}
for _, order := range append(longOrders, shortOrders...) {
orderType := order.OrderType.String()
price := order.Price.String()

if aggregatedOrders[price] == nil {
aggregatedOrders[price] = make(map[string]string)
}

quantityStr := aggregatedOrders[price][orderType]
if quantityStr == "" {
quantityStr = "0"
}
quantity, _ := big.NewInt(0).SetString(quantityStr, 10)
aggregatedOrders[price][orderType] = quantity.Add(quantity, order.GetUnFilledBaseAssetQuantity()).String()
}
return aggregatedOrders
}

type MarketDepthByType struct {
Market Market `json:"market"`
Orders map[string]map[string]string `json:"orders"` // price -> orderType -> quantity
}

type MarketDepth struct {
Market Market `json:"market"`
Longs map[string]string `json:"longs"`
Expand Down
14 changes: 8 additions & 6 deletions scripts/run_local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ if ! [[ "$0" =~ scripts/run_local.sh ]]; then
exit 255
fi

avalanche network clean
avalanche network clean --skip-update-check
# (avalanche subnet delete localnet) || true --skip-update-check

./scripts/build.sh custom_evm.bin

Expand All @@ -19,15 +20,16 @@ then
echo "31b571bf6894a248831ff937bb49f7754509fe93bbd2517c9c73c4144c0e97dc" > $FILE
fi

avalanche subnet create localnet --force --custom --genesis genesis.json --vm custom_evm.bin --config .avalanche-cli.json --teleporter=false
# avalanche subnet create localnet --teleporter --force --custom --genesis genesis.json --vm custom_evm.bin --config .avalanche-cli.json --skip-update-check
avalanche subnet create localnet --teleporter --force --custom --genesis genesis.json --custom-vm-path custom_evm.bin --config .avalanche-cli.json --skip-update-check

# configure and add chain.json
avalanche subnet configure localnet --chain-config chain.json --config .avalanche-cli.json
avalanche subnet configure localnet --subnet-config subnet.json --config .avalanche-cli.json
# avalanche subnet configure localnet --per-node-chain-config node_config.json --config .avalanche-cli.json
avalanche subnet configure localnet --chain-config chain.json --config .avalanche-cli.json --skip-update-check
avalanche subnet configure localnet --subnet-config subnet.json --config .avalanche-cli.json --skip-update-check
# avalanche subnet configure localnet --per-node-chain-config node_config.json --config .avalanche-cli.json --skip-update-check

# use the same avalanchego version as the one used in subnet-evm
# use tee to keep showing outut while storing in a var
OUTPUT=$(avalanche subnet deploy localnet -l --avalanchego-version v1.11.2 --config .avalanche-cli.json | tee /dev/fd/2)
OUTPUT=$(avalanche subnet deploy localnet -l --avalanchego-version v1.11.2 --config .avalanche-cli.json --skip-update-check| tee /dev/fd/2)

setStatus

0 comments on commit 7729379

Please sign in to comment.