Skip to content

Commit

Permalink
Move some logic of CalcAmountIn into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisngyn committed Dec 17, 2024
1 parent 098c7b2 commit d8b0f52
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
28 changes: 0 additions & 28 deletions pkg/source/limitorder/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ func (p *PoolSimulator) CalcAmountOut(param pool.CalcAmountOutParams) (*pool.Cal
return p.calcAmountOut(param.TokenAmountIn, param.TokenOut, param.Limit)
}

func (p *PoolSimulator) CalcAmountIn(param pool.CalcAmountInParams) (*pool.CalcAmountInResult, error) {
return p.calcAmountIn(param.TokenAmountOut, param.TokenIn, param.Limit)
}

func (p *PoolSimulator) UpdateBalance(params pool.UpdateBalanceParams) {
swapInfo, ok := params.SwapInfo.(SwapInfo)
if !ok {
Expand Down Expand Up @@ -169,30 +165,6 @@ func (p *PoolSimulator) calcAmountOut(
}, nil
}

func (p *PoolSimulator) calcAmountIn(
tokenAmountOut pool.TokenAmount,
tokenIn string,
limit pool.SwapLimit,
) (*pool.CalcAmountInResult, error) {
swapSide := p.getSwapSide(tokenIn, tokenAmountOut.Token)
amountIn, swapInfo, feeAmount, err := p.calcAmountInWithSwapInfo(swapSide, tokenAmountOut, limit)
if err != nil {
return nil, err
}
return &pool.CalcAmountInResult{
TokenAmountIn: &pool.TokenAmount{
Token: tokenIn,
Amount: amountIn,
},
Fee: &pool.TokenAmount{
Token: tokenIn,
Amount: feeAmount,
},
Gas: p.estimateGas(len(swapInfo.FilledOrders)),
SwapInfo: swapInfo,
}, nil
}

func addFilledMakingAmount(
filledMakingAmountByMaker map[string]*big.Int,
maker string,
Expand Down
28 changes: 28 additions & 0 deletions pkg/source/limitorder/pool_simulator_calc_amount_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,34 @@ import (
constant "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/bignumber"
)

func (p *PoolSimulator) CalcAmountIn(param pool.CalcAmountInParams) (*pool.CalcAmountInResult, error) {
return p.calcAmountIn(param.TokenAmountOut, param.TokenIn, param.Limit)
}

func (p *PoolSimulator) calcAmountIn(
tokenAmountOut pool.TokenAmount,
tokenIn string,
limit pool.SwapLimit,
) (*pool.CalcAmountInResult, error) {
swapSide := p.getSwapSide(tokenIn, tokenAmountOut.Token)
amountIn, swapInfo, feeAmount, err := p.calcAmountInWithSwapInfo(swapSide, tokenAmountOut, limit)
if err != nil {
return nil, err
}
return &pool.CalcAmountInResult{
TokenAmountIn: &pool.TokenAmount{
Token: tokenIn,
Amount: amountIn,
},
Fee: &pool.TokenAmount{
Token: tokenIn,
Amount: feeAmount,
},
Gas: p.estimateGas(len(swapInfo.FilledOrders)),
SwapInfo: swapInfo,
}, nil
}

func (p *PoolSimulator) calcAmountInWithSwapInfo(swapSide SwapSide, tokenAmountOut pool.TokenAmount, limit pool.SwapLimit) (*big.Int, SwapInfo, *big.Int, error) {
orderIDs := p.getOrderIDsBySwapSide(swapSide)
if len(orderIDs) == 0 {
Expand Down

0 comments on commit d8b0f52

Please sign in to comment.