Skip to content

Commit

Permalink
Merge pull request #5 from miraclesu/fix/price-impact
Browse files Browse the repository at this point in the history
fix: best trade price impact issue
  • Loading branch information
miraclesu authored Jul 13, 2021
2 parents 0c048f9 + 3cace53 commit 90c9fdd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
18 changes: 9 additions & 9 deletions entities/price.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ func NewPriceFromRoute(route *Route) (*Price, error) {
}

price := prices[0]
var err error
for i := 1; i < length; i++ {
if err := price.Multiply(prices[i]); err != nil {
price, err = price.Multiply(prices[i])
if err != nil {
return nil, err
}
}
Expand All @@ -51,26 +53,24 @@ func NewPrice(baseCurrency, quoteCurrency *Currency, denominator, numerator *big
}

func (p *Price) Raw() *Fraction {
return p.Fraction
return NewFraction(p.Fraction.Numerator, p.Fraction.Denominator)
}

func (p *Price) Adjusted() *Fraction {
p.Fraction.Multiply(p.Scalar)
return p.Fraction
return p.Fraction.Multiply(p.Scalar)
}

func (p *Price) Invert() {
p.BaseCurrency, p.QuoteCurrency = p.QuoteCurrency, p.BaseCurrency
}

func (p *Price) Multiply(other *Price) error {
func (p *Price) Multiply(other *Price) (*Price, error) {
if !p.QuoteCurrency.Equals(other.BaseCurrency) {
return ErrInvalidCurrency
return nil, ErrInvalidCurrency
}

p.Fraction.Multiply(other.Fraction)
p.QuoteCurrency = other.QuoteCurrency
return nil
fraction := p.Fraction.Multiply(other.Fraction)
return NewPrice(p.BaseCurrency, other.QuoteCurrency, fraction.Denominator, fraction.Numerator), nil
}

// performs floor division on overflow
Expand Down
4 changes: 2 additions & 2 deletions entities/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ func NewTrade(route *Route, amount *TokenAmount, tradeType constants.TradeType)
}
}

route, err := NewRoute(nextPairs, route.Input, nil)
nextRoute, err := NewRoute(nextPairs, route.Input, nil)
if err != nil {
return nil, err
}
nextMidPrice, err := NewPriceFromRoute(route)
nextMidPrice, err := NewPriceFromRoute(nextRoute)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 90c9fdd

Please sign in to comment.