Skip to content

Commit

Permalink
add optional swap limit logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberliem committed Nov 23, 2023
1 parent ef4b346 commit 5b8f064
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
5 changes: 3 additions & 2 deletions pkg/source/kyber-pmm/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (p *PoolSimulator) CalcAmountOut(
inventoryLimit = limit.GetLimit(p.baseToken.Address)
}

if result.TokenAmountOut.Amount.Cmp(inventoryLimit) > 0 {
if inventoryLimit != nil && result.TokenAmountOut.Amount.Cmp(inventoryLimit) > 0 {
return nil, errors.New("not enough inventory")
}
return result, nil
Expand Down Expand Up @@ -349,12 +349,13 @@ func NewInventory(balance map[string]*big.Int) *Inventory {
}

// GetLimit returns a copy of balance for the token in Inventory
// return nil if there is no limit
func (i *Inventory) GetLimit(tokenAddress string) *big.Int {
i.lock.RLock()
defer i.lock.RUnlock()
balance, avail := i.Balance[tokenAddress]
if !avail {
return big.NewInt(0)
return nil
}
return big.NewInt(0).Set(balance)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/source/pool/swap_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "math/big"
// i.e: SwapLimit must be something affect multiple pools once it is change.
type SwapLimit interface {
// GetLimit return a limit for a certain key. Normally each dex will have different limit values
// return nil in case there is no limit
// For example: PMM's key is token's string and its value is the inventory's balance of that token
GetLimit(key string) *big.Int
// UpdateLimit update both limits for a trade (assuming each trade is from 1 token to another token)
Expand Down
8 changes: 6 additions & 2 deletions pkg/source/synthetix/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (p *PoolSimulator) CalcAmountOut(
if synthetixTradeVolume != nil {
allowedVol := limit.GetLimit(strconv.FormatUint(p.poolState.BlockTimestamp, 10))

if allowedVol.Cmp(synthetixTradeVolume) < 0 {
if allowedVol != nil && allowedVol.Cmp(synthetixTradeVolume) < 0 {
return nil, ErrSurpassedVolumeLimit
}
}
Expand Down Expand Up @@ -230,6 +230,9 @@ func (p *PoolSimulator) GetPoolStateVersion() PoolStateVersion {
}

func (p *PoolSimulator) CalculateLimit() map[string]*big.Int {
if p.poolState.AtomicMaxVolumePerBlock == nil {
return nil
}
var (
s = p.poolState
maxVol = big.NewInt(0).Set(p.poolState.AtomicMaxVolumePerBlock)
Expand Down Expand Up @@ -259,12 +262,13 @@ func NewLimits(atomicMaxVolumePerBlocks map[string]*big.Int) pool.SwapLimit {
}

// GetLimit returns a copy of balance for the token in Inventory

func (i *AtomicLimits) GetLimit(blockTimeStamp string) *big.Int {
i.lock.RLock()
defer i.lock.RUnlock()
balance, avail := i.Limits[blockTimeStamp]
if !avail {
return big.NewInt(0)
return nil
}
return big.NewInt(0).Set(balance)
}
Expand Down

0 comments on commit 5b8f064

Please sign in to comment.