Skip to content

Commit

Permalink
feat: ✨ bebop: add inventory (#543)
Browse files Browse the repository at this point in the history
* feat: ✨ bebop: add limit

Signed-off-by: thanhpp <thanhphanphu18@gmail.com>

---------

Signed-off-by: thanhpp <thanhphanphu18@gmail.com>
  • Loading branch information
thanhpp authored Oct 15, 2024
1 parent 18107a7 commit 1563b3c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
34 changes: 34 additions & 0 deletions pkg/liquidity-source/bebop/limit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package bebop

import (
"math/big"
"sync/atomic"
)

// Limit: limit 1 bebop swap per route
type Limit struct {
hasSwap *atomic.Bool
}

func NewLimit(_ map[string]*big.Int) *Limit {
b := new(atomic.Bool)
return &Limit{
hasSwap: b,
}
}

func (l *Limit) GetLimit(key string) *big.Int {
if l.hasSwap.Load() {
return big.NewInt(0)
}

return nil
}

func (l *Limit) UpdateLimit(
_, _ string, _, _ *big.Int,
) (*big.Int, *big.Int, error) {
l.hasSwap.Store(true)

return nil, nil, nil
}
33 changes: 33 additions & 0 deletions pkg/liquidity-source/bebop/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,17 @@ func NewPoolSimulator(entityPool entity.Pool) (*PoolSimulator, error) {
}

func (p *PoolSimulator) CalcAmountOut(params pool.CalcAmountOutParams) (*pool.CalcAmountOutResult, error) {
if params.Limit.GetLimit("") != nil {
return nil, pool.ErrNotEnoughInventory
}

if params.TokenAmountIn.Token == p.Info.Tokens[0] {
return p.swap(params.TokenAmountIn.Amount, p.Token0, p.Token1, p.ZeroToOnePriceLevels)
} else {
return p.swap(params.TokenAmountIn.Amount, p.Token1, p.Token0, p.OneToZeroPriceLevels)
}
}

func (p *PoolSimulator) UpdateBalance(params pool.UpdateBalanceParams) {
var amountInAfterDecimals, decimalsPow, amountInBF big.Float
amountInBF.SetInt(params.TokenAmountIn.Amount)
Expand All @@ -122,12 +127,40 @@ func (p *PoolSimulator) UpdateBalance(params pool.UpdateBalanceParams) {

p.OneToZeroPriceLevels = getNewPriceLevelsState(&amountInAfterDecimals, p.OneToZeroPriceLevels)
}

// to handle the "top levels of orderbook" issue
// the swapLimit will be updated to 0, to limit using bebopRFQ once each route
// ref:https://team-kyber.slack.com/archives/C061UNZDUVC/p1728974288547259
_, _, _ = params.SwapLimit.UpdateLimit(
"", "",
nil, nil,
)
}

func (p *PoolSimulator) GetMetaInfo(_ string, _ string) interface{} {
return nil
}

func (p *PoolSimulator) CalculateLimit() map[string]*big.Int {
var pmmInventory = make(map[string]*big.Int, len(p.GetTokens()))
tokens := p.GetTokens()
rsv := p.GetReserves()
if len(tokens) != len(rsv) {
return pmmInventory
}

for i, tok := range tokens {
// rsv of a token can be set to 1 wei to bypass the aggregator check
if rsv[i].Int64() == 1 {
continue
}

pmmInventory[tok] = big.NewInt(0).Set(rsv[i]) //clone here.
}

return pmmInventory
}

func (p *PoolSimulator) swap(amountIn *big.Int, baseToken, quoteToken entity.PoolToken,
priceLevel []PriceLevel) (*pool.CalcAmountOutResult, error) {

Expand Down
14 changes: 14 additions & 0 deletions pkg/liquidity-source/bebop/pool_simulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func TestPoolSimulator_GetAmountOut(t *testing.T) {
Amount: tc.amountIn,
},
TokenOut: "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270",
Limit: NewLimit(nil),
}

result, err := poolSimulator.CalcAmountOut(params)
Expand All @@ -109,15 +110,24 @@ func TestPoolSimulator_GetAmountOut2(t *testing.T) {

tests := []struct {
name string
updateLimit bool
amountIn *big.Int
expectedAmountOut *big.Int
expectedErr error
}{
{
name: "it should return correct amountOut when swap in levels",
updateLimit: false,
amountIn: bigIntFromString("100000000000000000000"),
expectedAmountOut: bigIntFromString("7595710"),
},
{
name: "it should return not enough inventory",
updateLimit: true,
amountIn: bigIntFromString("100000000000000000000"),
expectedAmountOut: bigIntFromString("7595710"),
expectedErr: pool.ErrNotEnoughInventory,
},
}

for _, tc := range tests {
Expand All @@ -129,6 +139,10 @@ func TestPoolSimulator_GetAmountOut2(t *testing.T) {
Amount: tc.amountIn,
},
TokenOut: "0xdac17f958d2ee523a2206206994597c13d831ec7",
Limit: NewLimit(nil),
}
if tc.updateLimit {
_, _, _ = params.Limit.UpdateLimit("", "", nil, nil)
}

result, err := poolSimulator.CalcAmountOut(params)
Expand Down

0 comments on commit 1563b3c

Please sign in to comment.