Skip to content

Commit

Permalink
fix(Integral): Improve logic in UpdateBalance function (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunspirit99 authored Oct 4, 2024
1 parent d47ade0 commit 9792140
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 63 deletions.
4 changes: 0 additions & 4 deletions pkg/liquidity-source/integral/abis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

var (
oracleABI abi.ABI
reserveABI abi.ABI
pairABI abi.ABI
factoryABI abi.ABI
relayerABI abi.ABI
Expand All @@ -19,9 +18,6 @@ func init() {
ABI *abi.ABI
data []byte
}{
{
&reserveABI, twapReservesJSON,
},
{
&pairABI, twapPairJSON,
},
Expand Down
38 changes: 0 additions & 38 deletions pkg/liquidity-source/integral/abis/Reserves.json

This file was deleted.

12 changes: 5 additions & 7 deletions pkg/liquidity-source/integral/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ var (
factoryAllPairsLengthMethod = "allPairsLength"

// relayer methods
relayerFactoryMethod = "factory"
relayerIsPairEnabledMethod = "isPairEnabled"
relayerGetPoolStateMethod = "getPoolState"
relayerGetPairByAddressMethod = "getPriceByPairAddress"

// reserves methods
libraryGetReservesMethod = "getReserves"
relayerFactoryMethod = "factory"
relayerIsPairEnabledMethod = "isPairEnabled"
relayerGetPoolStateMethod = "getPoolState"
relayerGetPairByAddressMethod = "getPriceByPairAddress"
relayerGetTokenLimitMaxMultiplierMethod = "getTokenLimitMaxMultiplier"
)
3 changes: 0 additions & 3 deletions pkg/liquidity-source/integral/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package integral

import _ "embed"

//go:embed abis/Reserves.json
var twapReservesJSON []byte

//go:embed abis/TwapFactory.json
var twapFactoryJSON []byte

Expand Down
33 changes: 25 additions & 8 deletions pkg/liquidity-source/integral/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/bignumber"
"github.com/KyberNetwork/logger"
)

func NewPoolSimulator(entityPool entity.Pool) (*PoolSimulator, error) {
Expand Down Expand Up @@ -107,14 +106,32 @@ func (t *PoolSimulator) GetMetaInfo(_ string, _ string) interface{} {
return nil
}

func (p *PoolSimulator) UpdateBalance(params pool.UpdateBalanceParams) {
si, ok := params.SwapInfo.(SwapInfo)
if !ok {
logger.Warnf("failed to UpdateBalance for Integral %v %v pool, wrong swapInfo type", p.Info.Address, p.Info.Exchange)
return
func (t *PoolSimulator) UpdateBalance(params pool.UpdateBalanceParams) {
if params.SwapInfo != nil {
if s, ok := params.SwapInfo.(SwapInfo); ok {
newToken0LimitMax := new(big.Int).Div(
new(big.Int).Mul(
t.IntegralPair.Token0LimitMax.ToBig(),
s.NewReserve0,
),
t.Info.Reserves[0],
)

newToken1LimitMax := new(big.Int).Div(
new(big.Int).Mul(
t.IntegralPair.Token1LimitMax.ToBig(),
s.NewReserve1,
),
t.Info.Reserves[1],
)

t.Info.Reserves[0] = s.NewReserve0
t.Info.Reserves[1] = s.NewReserve1

t.IntegralPair.Token0LimitMax = uint256.MustFromBig(newToken0LimitMax)
t.IntegralPair.Token1LimitMax = uint256.MustFromBig(newToken1LimitMax)
}
}

p.Info.Reserves = []*big.Int{si.NewReserve0, si.NewReserve1}
}

// https://github.com/IntegralHQ/Integral-SIZE-Smart-Contracts/blob/main/contracts/TwapRelayer.sol#L275
Expand Down
39 changes: 36 additions & 3 deletions pkg/liquidity-source/integral/pool_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient/gethclient"
"github.com/goccy/go-json"
"github.com/holiman/uint256"
)

type PoolTracker struct {
Expand Down Expand Up @@ -55,7 +56,8 @@ func (u *PoolTracker) getNewPoolState(
logger.Infof("%s: Start getting new state of pool (address: %s)", u.config.DexID, p.Address)

var (
reserves = [2]*big.Int{ZERO, ZERO}
token0LimitMaxMultiplier = ZERO
token1LimitMaxMultiplier = ZERO

poolState = [6]*big.Int{ZERO, ZERO, ZERO, ZERO, ZERO, ZERO}
// uint256 price,
Expand Down Expand Up @@ -96,7 +98,17 @@ func (u *PoolTracker) getNewPoolState(
Params: []interface{}{common.HexToAddress(p.Address)},
}, []interface{}{&isPairEnabled})

rpcRequest.AddCall(&ethrpc.Call{ABI: reserveABI, Target: p.Address, Method: libraryGetReservesMethod}, []interface{}{&reserves})
rpcRequest.AddCall(&ethrpc.Call{ABI: relayerABI,
Target: u.config.RelayerAddress,
Method: relayerGetTokenLimitMaxMultiplierMethod,
Params: []interface{}{token0},
}, []interface{}{&token0LimitMaxMultiplier})

rpcRequest.AddCall(&ethrpc.Call{ABI: relayerABI,
Target: u.config.RelayerAddress,
Method: relayerGetTokenLimitMaxMultiplierMethod,
Params: []interface{}{token1},
}, []interface{}{&token1LimitMaxMultiplier})

if _, err := rpcRequest.TryAggregate(); err != nil {
logger.Errorf("%s: failed to fetch basic pool data (address: %s, error: %v)", u.config.DexID, p.Address, err)
Expand Down Expand Up @@ -152,6 +164,27 @@ func (u *PoolTracker) getNewPoolState(
extraData.Y_Decimals = uint64(yDecimals)
extraData.IsEnabled = isPairEnabled

var reserve0, reserve1 string
if token0LimitMaxMultiplier.Cmp(ZERO) != 0 {
reserve0 = new(uint256.Int).Div(
new(uint256.Int).Mul(
extraData.Token0LimitMax,
precision,
),
uint256.MustFromBig(token0LimitMaxMultiplier),
).String()
}

if token1LimitMaxMultiplier.Cmp(ZERO) != 0 {
reserve1 = new(uint256.Int).Div(
new(uint256.Int).Mul(
extraData.Token1LimitMax,
precision,
),
uint256.MustFromBig(token1LimitMaxMultiplier),
).String()
}

extraBytes, err := json.Marshal(extraData)
if err != nil {
logger.Errorf("%s: failed to marshal extra data (address: %s, error: %v)", u.config.DexID, p.Address, err)
Expand All @@ -160,7 +193,7 @@ func (u *PoolTracker) getNewPoolState(

p.Timestamp = time.Now().Unix()
p.Extra = string(extraBytes)
p.Reserves = entity.PoolReserves([]string{reserves[0].String(), reserves[1].String()})
p.Reserves = entity.PoolReserves([]string{reserve0, reserve1})
p.SwapFee = float64(poolState[1].Uint64()) / precision.Float64()

logger.Infof("%s: Pool state updated successfully (address: %s)", u.config.DexID, p.Address)
Expand Down

0 comments on commit 9792140

Please sign in to comment.