Skip to content

Commit

Permalink
Fluid Dex: add limits handling (#594)
Browse files Browse the repository at this point in the history
* feat(fluid-dex): add logic for limits handling
* feat(fluid-dex): clean up console logs
* refactor(fluid-dex): cleanup
* feat(fluid-dex): improve bignumber constants, clone limits
* feat(fluid-dex): optimize constants
* refactor(fluid-dex): optimize new big ints
* feat(fluid-dex): update limits in UpdateBalance

---------

Signed-off-by: bergben <[email protected]>
Signed-off-by: SunSpirit <[email protected]>
Co-authored-by: SunSpirit <[email protected]>
Co-authored-by: Phu Ngo <[email protected]>
  • Loading branch information
3 people authored Nov 18, 2024
1 parent 1a27253 commit bb663b1
Show file tree
Hide file tree
Showing 9 changed files with 1,378 additions and 527 deletions.
1,149 changes: 746 additions & 403 deletions pkg/liquidity-source/fluid/dex-t1/abis/dexReservesResolver.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions pkg/liquidity-source/fluid/dex-t1/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ const (

const (
String1e18 = "1000000000000000000"
String1e27 = "1000000000000000000000000000"

DexAmountsDecimals = 12

FeePercentPrecision int64 = 1e4
Fee100PercentPrecision int64 = 1e6

MaxPriceDiff int64 = 5 // 5%
)

var bI1e18, _ = new(big.Int).SetString(String1e18, 10) // 1e18
var bI1e27, _ = new(big.Int).SetString(String1e27, 10) // 1e27
var bI10 = new(big.Int).SetInt64(10)
var bI100 = new(big.Int).SetInt64(100)
14 changes: 11 additions & 3 deletions pkg/liquidity-source/fluid/dex-t1/pool_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dexT1

import (
"context"
"math/big"
"strings"

"github.com/KyberNetwork/ethrpc"
Expand Down Expand Up @@ -85,6 +84,7 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
extra := PoolExtra{
CollateralReserves: curPool.CollateralReserves,
DebtReserves: curPool.DebtReserves,
DexLimits: curPool.Limits,
}

extraBytes, err := json.Marshal(extra)
Expand All @@ -98,8 +98,16 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
Exchange: string(valueobject.ExchangeFluidDexT1),
Type: DexType,
Reserves: entity.PoolReserves{
new(big.Int).Add(curPool.CollateralReserves.Token0RealReserves, curPool.DebtReserves.Token0RealReserves).String(),
new(big.Int).Add(curPool.CollateralReserves.Token1RealReserves, curPool.DebtReserves.Token1RealReserves).String(),
getMaxReserves(
curPool.Limits.WithdrawableToken0,
curPool.Limits.BorrowableToken0,
curPool.CollateralReserves.Token0RealReserves,
curPool.DebtReserves.Token0RealReserves).String(),
getMaxReserves(
curPool.Limits.WithdrawableToken1,
curPool.Limits.BorrowableToken1,
curPool.CollateralReserves.Token1RealReserves,
curPool.DebtReserves.Token1RealReserves).String(),
},
Tokens: []*entity.PoolToken{
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestPoolListUpdater(t *testing.T) {
err error

config = Config{
DexReservesResolver: "0xE8a07a32489BD9d5a00f01A55749Cf5cB854Fd13",
DexReservesResolver: "0x45f4ad57e300da55c33dea579a40fcee000d7b94",
}
)

Expand Down
490 changes: 412 additions & 78 deletions pkg/liquidity-source/fluid/dex-t1/pool_simulator.go

Large diffs are not rendered by default.

176 changes: 143 additions & 33 deletions pkg/liquidity-source/fluid/dex-t1/pool_simulator_test.go

Large diffs are not rendered by default.

41 changes: 39 additions & 2 deletions pkg/liquidity-source/fluid/dex-t1/pool_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dexT1

import (
"context"
"math/big"
"time"

"github.com/KyberNetwork/ethrpc"
Expand Down Expand Up @@ -59,6 +60,7 @@ func (t *PoolTracker) getNewPoolState(
CollateralReserves: collateralReserves,
DebtReserves: debtReserves,
IsSwapAndArbitragePaused: isSwapAndArbitragePaused,
DexLimits: poolReserves.Limits,
}

extraBytes, err := json.Marshal(extra)
Expand All @@ -71,8 +73,18 @@ func (t *PoolTracker) getNewPoolState(
p.Extra = string(extraBytes)
p.BlockNumber = blockNumber
p.Timestamp = time.Now().Unix()

p.Reserves = entity.PoolReserves{poolReserves.BalanceToken0.String(), poolReserves.BalanceToken1.String()}
p.Reserves = entity.PoolReserves{
getMaxReserves(
poolReserves.Limits.WithdrawableToken0,
poolReserves.Limits.BorrowableToken0,
poolReserves.CollateralReserves.Token0RealReserves,
poolReserves.DebtReserves.Token0RealReserves).String(),
getMaxReserves(
poolReserves.Limits.WithdrawableToken1,
poolReserves.Limits.BorrowableToken1,
poolReserves.CollateralReserves.Token1RealReserves,
poolReserves.DebtReserves.Token1RealReserves).String(),
}

return p, nil
}
Expand Down Expand Up @@ -119,3 +131,28 @@ func (t *PoolTracker) getPoolReserves(

return pool, isSwapAndArbitragePaused, resp.BlockNumber.Uint64(), nil
}

func getMaxReserves(
withdrawableLimit TokenLimit,
borrowableLimit TokenLimit,
realColReserves *big.Int,
realDebtReserves *big.Int,
) *big.Int {
// max available reserves: the smaller possible value between real reserves and the expandTo limits
// the expandTo limits include liquidity layer balances, utilization limits, withdrawable and borrowable limits

// if expandTo for borrowable and withdrawable match, that means they are a hard limit like liquidity layer balance
// or utilization limit. In that case expandTo can not be summed up. Otherwise it's the case of expanding withdrawal
// and borrow limits, for which we must sum up the max available reserve amount.
maxLimitReserves := new(big.Int).Add(borrowableLimit.ExpandsTo, withdrawableLimit.ExpandsTo)
if borrowableLimit.ExpandsTo.Cmp(withdrawableLimit.ExpandsTo) == 0 {
maxLimitReserves.Set(borrowableLimit.ExpandsTo)
}

maxRealReserves := new(big.Int).Add(realColReserves, realDebtReserves)

if maxRealReserves.Cmp(maxLimitReserves) < 0 {
return maxRealReserves
}
return maxLimitReserves
}
9 changes: 4 additions & 5 deletions pkg/liquidity-source/fluid/dex-t1/pool_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (
"testing"

"github.com/KyberNetwork/ethrpc"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/valueobject"
"github.com/KyberNetwork/logger"
"github.com/ethereum/go-ethereum/common"
"github.com/goccy/go-json"
"github.com/stretchr/testify/require"

"github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/valueobject"
)

func TestPoolTracker(t *testing.T) {
Expand All @@ -26,7 +25,7 @@ func TestPoolTracker(t *testing.T) {

var (
config = Config{
DexReservesResolver: "0x87807F35b81cFdF762cEF717059B992920081298",
DexReservesResolver: "0x45f4ad57e300da55c33dea579a40fcee000d7b94",
}
)

Expand Down
18 changes: 16 additions & 2 deletions pkg/liquidity-source/fluid/dex-t1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type PoolExtra struct {
CollateralReserves CollateralReserves
DebtReserves DebtReserves
IsSwapAndArbitragePaused bool
DexLimits DexLimits
}

type CollateralReserves struct {
Expand All @@ -32,15 +33,27 @@ type DebtReserves struct {
Token1ImaginaryReserves *big.Int `json:"token1ImaginaryReserves"`
}

type TokenLimit struct {
Available *big.Int `json:"available"` // maximum available swap amount
ExpandsTo *big.Int `json:"expandsTo"` // maximum amount the available swap amount expands to
ExpandDuration *big.Int `json:"expandDuration"` // duration for `available` to grow to `expandsTo`
}

type DexLimits struct {
WithdrawableToken0 TokenLimit `json:"withdrawableToken0"`
WithdrawableToken1 TokenLimit `json:"withdrawableToken1"`
BorrowableToken0 TokenLimit `json:"borrowableToken0"`
BorrowableToken1 TokenLimit `json:"borrowableToken1"`
}

type PoolWithReserves struct {
PoolAddress common.Address `json:"poolAddress"`
Token0Address common.Address `json:"token0Address"`
Token1Address common.Address `json:"token1Address"`
Fee *big.Int `json:"fee"`
CollateralReserves CollateralReserves `json:"collateralReserves"`
DebtReserves DebtReserves `json:"debtReserves"`
BalanceToken0 *big.Int `json:"balanceToken0"`
BalanceToken1 *big.Int `json:"balanceToken1"`
Limits DexLimits `json:"limits"`
}

type Gas struct {
Expand All @@ -56,4 +69,5 @@ type SwapInfo struct {
HasNative bool `json:"hasNative"`
NewCollateralReserves CollateralReserves `json:"-"`
NewDebtReserves DebtReserves `json:"-"`
NewDexLimits DexLimits `json:"-"`
}

0 comments on commit bb663b1

Please sign in to comment.