Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional swap limit logic #242

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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