Skip to content

Commit

Permalink
spot price
Browse files Browse the repository at this point in the history
  • Loading branch information
rbajollari committed Jun 21, 2024
1 parent f82c138 commit 2363757
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
25 changes: 25 additions & 0 deletions client/spot_prices.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/ethereum/go-ethereum/common"
balancerpool "github.com/ojo-network/ethereum-api/abi/balancer/pool"
"github.com/ojo-network/ethereum-api/abi/camelot"
"github.com/ojo-network/ethereum-api/abi/uniswap"
"github.com/ojo-network/ethereum-api/pool"
Expand All @@ -30,6 +31,10 @@ func (c *Client) PollSpotPrices(pools []pool.Pool) {
spotPrice = c.QueryAlgebraSpotPrice(p, blockNum)
c.logger.Info().Interface("spotPrice", spotPrice).Msg("spot price received")
c.indexer.AddPrice(spotPrice)
case pool.PoolBalancer:
spotPrice = c.QueryAlgebraSpotPrice(p, blockNum)
c.logger.Info().Interface("spotPrice", spotPrice).Msg("spot price received")
c.indexer.AddPrice(spotPrice)
}
}
}
Expand Down Expand Up @@ -76,3 +81,23 @@ func (c *Client) QueryAlgebraSpotPrice(p pool.Pool, blockNum uint64) indexer.Spo
Price: p.SqrtPriceX96ToDec(globalState.Price),
}
}

// QueryBalancerSpotPrice queries the spot price of a balancer pool
func (c *Client) QueryBalancerSpotPrice(p pool.Pool, blockNum uint64) indexer.SpotPrice {
poolCaller, err := balancerpool.NewPoolCaller(common.HexToAddress(p.Address), c.ethClient)
if err != nil {
c.reportError(fmt.Errorf("error initializing %s pool caller: %w", p.ExchangePair(), err))
return indexer.SpotPrice{}
}
poolRate, err := poolCaller.GetRate(nil)
if err != nil {
c.reportError(fmt.Errorf("error getting %s pool balance: %w", p.ExchangePair(), err))
return indexer.SpotPrice{}
}
return indexer.SpotPrice{
BlockNum: indexer.BlockNum(blockNum),
Timestamp: utils.CurrentUnixTime(),
ExchangePair: p.ExchangePair(),
Price: p.SqrtPriceX96ToDec(poolRate),
}
}
13 changes: 6 additions & 7 deletions client/swaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,11 @@ func (c *Client) WatchBalancerSwapEvent(p pool.Pool) error {
return err
}

callOpts := &bind.CallOpts{Context: c.ctx}
vaultAddress, err := poolCaller.GetVault(callOpts)
vaultAddress, err := poolCaller.GetVault(nil)
if err != nil {
return err
}
poolId, err := poolCaller.GetPoolId(callOpts)
poolId, err := poolCaller.GetPoolId(nil)
if err != nil {
return err
}
Expand All @@ -131,7 +130,7 @@ func (c *Client) WatchBalancerSwapEvent(p pool.Pool) error {
if err != nil {
return err
}
poolTokens, err := vaultCaller.GetPoolTokens(callOpts, poolId)
poolTokens, err := vaultCaller.GetPoolTokens(nil, poolId)
if err != nil {
return err
}
Expand All @@ -150,9 +149,9 @@ func (c *Client) WatchBalancerSwapEvent(p pool.Pool) error {
}

eventSink := make(chan *vault.PoolSwap)
watchOpts := &bind.WatchOpts{Start: nil, Context: c.ctx}
opts := &bind.WatchOpts{Start: nil, Context: c.ctx}
c.logger.Info().Msgf("subscribing to %s swap events", p.ExchangePair())
subscription, err := vaultFilterer.WatchSwap(watchOpts, eventSink, poolIdParam, tokenInParam, tokenOutParam)
subscription, err := vaultFilterer.WatchSwap(opts, eventSink, poolIdParam, tokenInParam, tokenOutParam)
if err != nil {
return err
}
Expand All @@ -167,7 +166,7 @@ func (c *Client) WatchBalancerSwapEvent(p pool.Pool) error {
return err
case event := <-eventSink:
// query rate from pool contract
poolRate, err := poolCaller.GetRate(callOpts)
poolRate, err := poolCaller.GetRate(nil)
if err != nil {
return err
}
Expand Down

0 comments on commit 2363757

Please sign in to comment.