Skip to content

Commit

Permalink
fix: Swap event subscription on balancer (#22)
Browse files Browse the repository at this point in the history
* feat: Mellow LRTs on balancerf

* fix swap event subscribing

* fix price in swap
  • Loading branch information
rbajollari authored Jul 7, 2024
1 parent 025f75e commit b9a0dea
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
3 changes: 2 additions & 1 deletion client/spot_prices.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ func (c *Client) QueryBalancerSpotPrice(p pool.Pool, blockNum uint64) indexer.Sp
c.reportError(fmt.Errorf("error initializing %s pool caller: %w", p.ExchangePair(), err))
return indexer.SpotPrice{}
}
poolRate, err := poolCaller.GetTokenRate(nil, common.HexToAddress(p.TokenAddress))

poolRate, err := poolCaller.GetTokenRate(nil, common.HexToAddress(p.BaseAddress))
if err != nil {
c.reportError(fmt.Errorf("error getting %s token rate from pool: %w", p.ExchangePair(), err))
return indexer.SpotPrice{}
Expand Down
10 changes: 6 additions & 4 deletions client/swaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ func (c *Client) WatchBalancerSwapEvent(p pool.Pool) error {
poolIdParam := make([][32]byte, 1)
poolIdParam[0] = poolId
tokenInParam := make([]common.Address, 1)
tokenInParam[0] = common.HexToAddress(p.TokenAddress)
tokenOutParam := make([]common.Address, 1)
tokenOutParam[0] = common.HexToAddress(p.TokenAddress)
tokenInParam[0] = common.HexToAddress(p.BaseAddress)
tokenOutParam := []common.Address{}
for _, tokenAddress := range p.QuoteAddresses{
tokenOutParam = append(tokenOutParam, common.HexToAddress(tokenAddress))
}

vaultFilterer, err := vault.NewPoolFilterer(vaultAddress, c.ethClient)
if err != nil {
Expand All @@ -156,7 +158,7 @@ func (c *Client) WatchBalancerSwapEvent(p pool.Pool) error {
return err
case event := <-eventSink:
// query rate from pool contract
poolRate, err := poolCaller.GetRate(nil)
poolRate, err := poolCaller.GetTokenRate(nil, common.HexToAddress(p.BaseAddress))
if err != nil {
return err
}
Expand Down
17 changes: 9 additions & 8 deletions pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import (
)

type Pool struct {
Address string `yaml:"address"`
TokenAddress string `yaml:"token_address"`
Base string `yaml:"base"`
Quote string `yaml:"quote"`
BaseDecimal uint64 `yaml:"base_decimal"`
QuoteDecimal uint64 `yaml:"quote_decimal"`
InvertPrice bool `yaml:"invert_price"`
Address string `yaml:"address"`
Base string `yaml:"base"`
Quote string `yaml:"quote"`
BaseAddress string `yaml:"base_address"`
QuoteAddresses []string `yaml:"quote_addresses"`
BaseDecimal uint64 `yaml:"base_decimal"`
QuoteDecimal uint64 `yaml:"quote_decimal"`
InvertPrice bool `yaml:"invert_price"`
}

func (p *Pool) ExchangePair() string {
Expand Down Expand Up @@ -78,7 +79,7 @@ func (p *Pool) ConvertBalancerEventToSwap(event *vault.PoolSwap, price *big.Int)
BlockNum: indexer.BlockNum(event.Raw.BlockNumber),
Timestamp: utils.CurrentUnixTime(),
ExchangePair: p.ExchangePair(),
Price: p.SqrtPriceX96ToDec(price),
Price: sdkmath.LegacyNewDecFromBigIntWithPrec(price, 18),
Volume: p.swapBalancerVolume(event),
}
}
Expand Down
43 changes: 42 additions & 1 deletion sample-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,50 @@ exchanges:
- "wss://mainnet.infura.io/ws/v3/6849a09aeeb044b592d46bcdce07ccef"
pools:
- address: "0x4216d5900a6109bba48418b5e2AB6cc4e61Cf477"
token_address: "0x84631c0d0081FDe56DeB72F6DE77abBbF6A9f93a"
base: "AMPHRETH"
quote: "ETH"
base_address: "0x5fD13359Ba15A84B76f7F87568309040176167cd"
quote_addresses:
- "0x7a4EffD87C2f3C55CA251080b1343b605f327E3a"
- "0x84631c0d0081FDe56DeB72F6DE77abBbF6A9f93a"
- "0xBEEF69Ac7870777598A04B2bd4771c71212E6aBc"
- "0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0"
base_decimal: 18
quote_decimal: 18
invert_price: false
- address: "0x4216d5900a6109bba48418b5e2AB6cc4e61Cf477"
base: "RSTETH"
quote: "ETH"
base_address: "0x7a4EffD87C2f3C55CA251080b1343b605f327E3a"
quote_addresses:
- "0x5fD13359Ba15A84B76f7F87568309040176167cd"
- "0x84631c0d0081FDe56DeB72F6DE77abBbF6A9f93a"
- "0xBEEF69Ac7870777598A04B2bd4771c71212E6aBc"
- "0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0"
base_decimal: 18
quote_decimal: 18
invert_price: false
- address: "0x4216d5900a6109bba48418b5e2AB6cc4e61Cf477"
base: "RE7LRT"
quote: "ETH"
base_address: "0x84631c0d0081FDe56DeB72F6DE77abBbF6A9f93a"
quote_addresses:
- "0x5fD13359Ba15A84B76f7F87568309040176167cd"
- "0x7a4EffD87C2f3C55CA251080b1343b605f327E3a"
- "0xBEEF69Ac7870777598A04B2bd4771c71212E6aBc"
- "0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0"
base_decimal: 18
quote_decimal: 18
invert_price: false
- address: "0x4216d5900a6109bba48418b5e2AB6cc4e61Cf477"
base: "STEAKLRT"
quote: "ETH"
base_address: "0xBEEF69Ac7870777598A04B2bd4771c71212E6aBc"
quote_addresses:
- "0x5fD13359Ba15A84B76f7F87568309040176167cd"
- "0x7a4EffD87C2f3C55CA251080b1343b605f327E3a"
- "0x84631c0d0081FDe56DeB72F6DE77abBbF6A9f93a"
- "0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0"
base_decimal: 18
quote_decimal: 18
invert_price: false
Expand Down

0 comments on commit b9a0dea

Please sign in to comment.